Sha256: 53dad30a9f427f75a93e968332fb8257c3b659a13501811ee9058e821ff54161

Contents?: true

Size: 1.29 KB

Versions: 9

Compression:

Stored size: 1.29 KB

Contents

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

'use strict';

var isPrimitive = require('is-primitive');
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

9 entries across 9 versions & 4 rubygems

Version Path
affiliator-0.2.1 node_modules/regex-cache/index.js
lanes-0.8.0 node_modules/regex-cache/index.js
brwy_rails-0.0.6 test/dummy/node_modules/regex-cache/index.js
brwy_rails-0.0.5 test/dummy/node_modules/regex-cache/index.js
blackboard-3.1.9 lib/site_template/node_modules/regex-cache/index.js
brwy_rails-0.0.4 test/dummy/node_modules/regex-cache/index.js
brwy_rails-0.0.3 test/dummy/node_modules/regex-cache/index.js
brwy_rails-0.0.2 test/dummy/node_modules/regex-cache/index.js
brwy_rails-0.0.1 test/dummy/node_modules/regex-cache/index.js