Sha256: 5b78a48c97386c2ae446d77db6967dd8c31ff056cd730baff499ff6a579316d6

Contents?: true

Size: 1.26 KB

Versions: 23

Compression:

Stored size: 1.26 KB

Contents

/*!
 * regex-cache <https://github.com/jonschlinkert/regex-cache>
 *
 * Copyright (c) 2015-2017, Jon Schlinkert.
 * Released under the MIT License.
 */

'use strict';

var equal = require('is-equal-shallow');
var basic = {};
var cache = {};

/**
 * Expose `regexCache`
 */

module.exports = regexCache;

/**
 * Memoize the results of a call to the new RegExp constructor.
 *
 * @param  {Function} fn [description]
 * @param  {String} str [description]
 * @param  {Options} options [description]
 * @param  {Boolean} nocompare [description]
 * @return {RegExp}
 */

function regexCache(fn, str, opts) {
  var key = '_default_', regex, cached;

  if (!str && !opts) {
    if (typeof fn !== 'function') {
      return fn;
    }
    return basic[key] || (basic[key] = fn(str));
  }

  var isString = typeof str === 'string';
  if (isString) {
    if (!opts) {
      return basic[str] || (basic[str] = fn(str));
    }
    key = str;
  } else {
    opts = str;
  }

  cached = cache[key];
  if (cached && equal(cached.opts, opts)) {
    return cached.regex;
  }

  memo(key, opts, (regex = fn(str, opts)));
  return regex;
}

function memo(key, opts, regex) {
  cache[key] = {regex: regex, opts: opts};
}

/**
 * Expose `cache`
 */

module.exports.cache = cache;
module.exports.basic = basic;

Version data entries

23 entries across 23 versions & 10 rubygems

Version Path
jass-0.9.5 vendor/node_modules/regex-cache/index.js
ilog-0.4.1 node_modules/regex-cache/index.js
ilog-0.4.0 node_modules/regex-cache/index.js
ilog-0.3.3 node_modules/regex-cache/index.js
jass-0.9.4 vendor/node_modules/regex-cache/index.js
blsk-sevcore-0.0.2 blsk_old/bluesky-doc/bluesky-doc/node_modules/regex-cache/index.js
optimacms-0.4.3 spec/dummy/node_modules/regex-cache/index.js
optimacms-0.4.2 spec/dummy/node_modules/regex-cache/index.js
locomotivecms-4.0.0.alpha1 app/javascript/node_modules/regex-cache/index.js
lux_assets-0.2.11 ./node_modules/regex-cache/index.js
lux_assets-0.2.9 ./node_modules/regex-cache/index.js
lux_assets-0.2.4 ./node_modules/regex-cache/index.js
lux_assets-0.2.2 ./node_modules/regex-cache/index.js
lux_assets-0.2.1 ./node_modules/regex-cache/index.js
locomotivecms-3.4.0 app/javascript/node_modules/regex-cache/index.js
jass-vue-0.4.0 vendor/node_modules/regex-cache/index.js
jass-vue-0.3.0 vendor/node_modules/regex-cache/index.js
jass-0.9.3 vendor/node_modules/regex-cache/index.js
cortex-0.1.3 spec/dummy/node_modules/regex-cache/index.js
condenser-0.0.4 lib/condenser/processors/node_modules/regex-cache/index.js