1 | 'use strict';
|
2 |
|
3 | var _util = require('util');
|
4 |
|
5 | var _lruCache = require('lru-cache');
|
6 |
|
7 | var _lruCache2 = _interopRequireDefault(_lruCache);
|
8 |
|
9 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
10 |
|
11 | var WHITESPACE_X = /\s+/g;
|
12 |
|
13 | function vaccuum(string) {
|
14 | return string.replace(WHITESPACE_X, '');
|
15 | }
|
16 |
|
17 | function hash(object) {
|
18 | return vaccuum((0, _util.inspect)(object, {
|
19 | depth: Infinity,
|
20 | showHidden: true
|
21 | }));
|
22 | }
|
23 |
|
24 | var cache = (0, _lruCache2.default)(100);
|
25 |
|
26 | function validate(anArrayOfKeys) {
|
27 | var moduleOutputPrefix = '[keykey]';
|
28 |
|
29 | if (!Array.isArray(anArrayOfKeys)) {
|
30 | throw new TypeError(moduleOutputPrefix + ' Must call with an array.');
|
31 | }
|
32 | }
|
33 |
|
34 | function reduce(anArrayOfKeys, each) {
|
35 | var subObject = {};
|
36 | anArrayOfKeys.forEach(each.bind(each, subObject, anArrayOfKeys));
|
37 | return subObject;
|
38 | }
|
39 |
|
40 | function set(anArrayOfKeys) {
|
41 | var key = hash(anArrayOfKeys);
|
42 | return cache.set(key, anArrayOfKeys);
|
43 | }
|
44 |
|
45 | function create(anArrayOfKeys, bypassCache) {
|
46 | validate(anArrayOfKeys);
|
47 |
|
48 | var creation = reduce(anArrayOfKeys, function (result, _, key) {
|
49 | result[key] = key;
|
50 | });
|
51 |
|
52 | !bypassCache && set(creation);
|
53 | return creation;
|
54 | }
|
55 |
|
56 | function get(anArrayOfKeys) {
|
57 | return cache.get(hash(anArrayOfKeys)) || create(anArrayOfKeys);
|
58 | }
|
59 |
|
60 | function isUndef(o) {
|
61 | return typeof o === 'undefined';
|
62 | }
|
63 |
|
64 | function isBool(o) {
|
65 | return typeof o === 'boolean';
|
66 | }
|
67 |
|
68 | function last(a) {
|
69 | return a[a.length - 1];
|
70 | }
|
71 |
|
72 | function notUndefOrBool(o) {
|
73 | return !isUndef(o) && !isBool(o);
|
74 | }
|
75 |
|
76 | function KeyKey(anArrayOfKeys, bypassCache) {
|
77 |
|
78 | if (notUndefOrBool(bypassCache)) {
|
79 | anArrayOfKeys = [].slice.call(arguments);
|
80 | var lastKey = last(anArrayOfKeys);
|
81 | if (isBool(lastKey)) {
|
82 | bypassCache = lastKey;
|
83 | anArrayOfKeys.pop();
|
84 | } else {
|
85 | bypassCache = false;
|
86 | }
|
87 | }
|
88 |
|
89 | return bypassCache ? create(anArrayOfKeys, bypassCache) : get(anArrayOfKeys);
|
90 | }
|
91 |
|
92 | module.exports = KeyKey;
|
93 |
|
94 | module.exports.reset = function () {
|
95 | return cache.reset();
|
96 | };
|
97 |
|
98 | module.exports.resetCache = module.exports.reset;
|
99 |
|
100 | module.exports.cacheKeys = function () {
|
101 | return cache.keys();
|
102 | };
|
103 |
|
104 | module.exports.cacheValues = function () {
|
105 | return cache.values();
|
106 | }; |
\ | No newline at end of file |