'use strict';
var regex = require('regex-not');
var Cache = require('fragment-cache');
var utils = module.exports;
var cache = utils.cache = new Cache();
utils.arrayify = function(val) {
if (!Array.isArray(val)) {
return [val];
}
return val;
};
utils.memoize = function(type, pattern, options, fn) {
var key = utils.createKey(type + pattern, options);
if (cache.has(type, key)) {
return cache.get(type, key);
}
var val = fn(pattern, options);
if (options && options.cache === false) {
return val;
}
cache.set(type, key, val);
return val;
};
utils.createKey = function(pattern, options) {
var key = pattern;
if (typeof options === 'undefined') {
return key;
}
for (var prop in options) {
key += ';' + prop + '=' + String(options[prop]);
}
return key;
};
utils.createRegex = function(str) {
var opts = {contains: true, strictClose: false};
return regex(str, opts);
};