2 * regex-cache <https://github.com/jonschlinkert/regex-cache>
4 * Copyright (c) 2015 Jon Schlinkert.
5 * Licensed under the MIT license.
10 var isPrimitive = require('is-primitive');
11 var equal = require('is-equal-shallow');
19 module.exports = regexCache;
22 * Memoize the results of a call to the new RegExp constructor.
24 * @param {Function} fn [description]
25 * @param {String} str [description]
26 * @param {Options} options [description]
27 * @param {Boolean} nocompare [description]
31 function regexCache(fn, str, opts) {
32 var key = '_default_', regex, cached;
35 if (typeof fn !== 'function') {
38 return basic[key] || (basic[key] = fn(str));
41 var isString = typeof str === 'string';
44 return basic[str] || (basic[str] = fn(str));
52 if (cached && equal(cached.opts, opts)) {
56 memo(key, opts, (regex = fn(str, opts)));
60 function memo(key, opts, regex) {
61 cache[key] = {regex: regex, opts: opts};
68 module.exports.cache = cache;
69 module.exports.basic = basic;