3 var regex = require('regex-not');
4 var Cache = require('fragment-cache');
10 var utils = module.exports;
11 var cache = utils.cache = new Cache();
14 * Cast `val` to an array
18 utils.arrayify = function(val) {
19 if (!Array.isArray(val)) {
26 * Memoize a generated regex or function
29 utils.memoize = function(type, pattern, options, fn) {
30 var key = utils.createKey(type + pattern, options);
32 if (cache.has(type, key)) {
33 return cache.get(type, key);
36 var val = fn(pattern, options);
37 if (options && options.cache === false) {
41 cache.set(type, key, val);
46 * Create the key to use for memoization. The key is generated
47 * by iterating over the options and concatenating key-value pairs
48 * to the pattern string.
51 utils.createKey = function(pattern, options) {
53 if (typeof options === 'undefined') {
56 for (var prop in options) {
57 key += ';' + prop + '=' + String(options[prop]);
63 * Create the regex to use for matching text
66 utils.createRegex = function(str) {
67 var opts = {contains: true, strictClose: false};
68 return regex(str, opts);