index.js
1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*!
* 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;