2 * regex-cache <https://github.com/jonschlinkert/regex-cache>
4 * Copyright (c) 2015-2017, Jon Schlinkert.
5 * Released under the MIT License.
10 var equal = require('is-equal-shallow');
18 module.exports = regexCache;
21 * Memoize the results of a call to the new RegExp constructor.
23 * @param {Function} fn [description]
24 * @param {String} str [description]
25 * @param {Options} options [description]
26 * @param {Boolean} nocompare [description]
30 function regexCache(fn, str, opts) {
31 var key = '_default_', regex, cached;
34 if (typeof fn !== 'function') {
37 return basic[key] || (basic[key] = fn(str));
40 var isString = typeof str === 'string';
43 return basic[str] || (basic[str] = fn(str));
51 if (cached && equal(cached.opts, opts)) {
55 memo(key, opts, (regex = fn(str, opts)));
59 function memo(key, opts, regex) {
60 cache[key] = {regex: regex, opts: opts};
67 module.exports.cache = cache;
68 module.exports.basic = basic;