multi-key-cache
Version:
A JavaScript (JS) cache that can have multiple complex values as keys
1 lines • 13.7 kB
JavaScript
{"filter":false,"title":"multi-key-cache.js","tooltip":"/multi-key-cache.js","undoManager":{"mark":1,"position":1,"stack":[[{"start":{"row":0,"column":0},"end":{"row":249,"column":3},"action":"remove","lines":["(function(f){if(typeof exports===\"object\"&&typeof module!==\"undefined\"){module.exports=f()}else if(typeof define===\"function\"&&define.amd){define([],f)}else{var g;if(typeof window!==\"undefined\"){g=window}else if(typeof global!==\"undefined\"){g=global}else if(typeof self!==\"undefined\"){g=self}else{g=this}g.multiKeyCache = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error(\"Cannot find module '\"+o+\"'\");throw f.code=\"MODULE_NOT_FOUND\",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){","if (typeof Map !== 'function' || (process && process.env && process.env.TEST_MAPORSIMILAR === 'true')) {"," module.exports = _dereq_('./similar');","}","else {"," module.exports = Map;","}","},{\"./similar\":2}],2:[function(_dereq_,module,exports){","function Similar() {"," this.list = [];"," this.lastItem = undefined;"," this.size = 0;",""," return this;","}","","Similar.prototype.get = function(key) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," return this.lastItem.val;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.lastItem = this.list[index];"," return this.list[index].val;"," }",""," return undefined;","};","","Similar.prototype.set = function(key, val) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," this.lastItem.val = val;"," return this;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.lastItem = this.list[index];"," this.list[index].val = val;"," return this;"," }",""," this.lastItem = { key: key, val: val };"," this.list.push(this.lastItem);"," this.size++;",""," return this;","};","","Similar.prototype.delete = function(key) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," this.lastItem = undefined;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.size--;"," return this.list.splice(index, 1)[0];"," }",""," return undefined;","};","","","// important that has() doesn't use get() in case an existing key has a falsy value, in which case has() would return false","Similar.prototype.has = function(key) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," return true;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.lastItem = this.list[index];"," return true;"," }",""," return false;","};","","Similar.prototype.forEach = function(callback, thisArg) {"," var i;"," for (i = 0; i < this.size; i++) {"," callback.call(thisArg || this, this.list[i].val, this.list[i].key, this);"," }","};","","Similar.prototype.indexOf = function(key) {"," var i;"," for (i = 0; i < this.size; i++) {"," if (this.list[i].key === key) {"," return i;"," }"," }"," return -1;","};","","module.exports = Similar;","},{}],3:[function(_dereq_,module,exports){","var MapOrSimilar = _dereq_('map-or-similar');","","function MultiKeyCache() {","\tthis.cache = new MapOrSimilar();","\tthis._vAl_kEY_nAMe_no_cOLLision_ = '_vAl_kEY_nAMe_no_cOLLision_';","}","","MultiKeyCache.prototype.set = function(keys, value) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\tnewSubCache,","\t\ti;","","\tif (!keysLen) {","\t\treturn this;","\t}","","\tfor (i = 0; i < keysLen; i++) {","\t\tif (currentCache.has(keys[i])) {","\t\t\tcurrentCache = currentCache.get(keys[i]);","\t\t\tcontinue;","\t\t}","\t\tnewSubCache = new MapOrSimilar();","\t\tcurrentCache.set(keys[i], newSubCache);","\t\tcurrentCache = newSubCache;","\t}","","\tcurrentCache.set(this._vAl_kEY_nAMe_no_cOLLision_, value);","","\treturn this;","};","","MultiKeyCache.prototype.get = function(keys) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\ti;","","\tif (!keysLen) {","\t\treturn undefined;","\t}","","\tfor (i = 0; i < keysLen; i++) {","\t\tcurrentCache = currentCache.get(keys[i]);","\t\tif (!currentCache) {","\t\t\treturn undefined;","\t\t}","\t}","","\treturn currentCache.get(this._vAl_kEY_nAMe_no_cOLLision_);","};","","MultiKeyCache.prototype.delete = function(keys) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\tcachePath = [currentCache],","\t\ti;","","\tif (!keysLen) {","\t\treturn false;","\t}","","\t// walk up the tree gathering the maps","\tfor (i = 0; i < keysLen; i++) {","\t\tcurrentCache = currentCache.get(keys[i]);","\t\tcachePath.push(currentCache);","\t\tif (!currentCache) {","\t\t\treturn false;","\t\t}","\t}","","\t// delete the value","\tcurrentCache.delete(this._vAl_kEY_nAMe_no_cOLLision_);","","\t// walk back down the tree deleting any empty maps","\tfor (i = keysLen - 1; i >= 0; i--) {","\t\tcurrentCache = cachePath[i].get(keys[i]);","\t\tif (currentCache.size) {","\t\t\tbreak;","\t\t}","\t\tcachePath[i].delete(keys[i]);","\t}","","\treturn true;","};","","MultiKeyCache.prototype.has = function(keys) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\ti;","","\tif (!keysLen) {","\t\treturn false;","\t}","","\tfor (i = 0; i < keysLen; i++) {","\t\tcurrentCache = currentCache.get(keys[i]);","\t\tif (!currentCache) {","\t\t\treturn false;","\t\t}","\t}","\treturn true;","};","","MultiKeyCache.prototype.values = function() {","\tvar self = this,","\t\tvalues = [];","","\tfunction getCacheValues(currentCache) {","\t\tcurrentCache.forEach((val, key) => {","\t\t\tif (key === self._vAl_kEY_nAMe_no_cOLLision_) {","\t\t\t\tvalues.push(val);","\t\t\t}","\t\t\telse {","\t\t\t\tgetCacheValues(val);","\t\t\t}","\t\t});","\t}","","\tgetCacheValues(this.cache);","","\treturn values;","};","","MultiKeyCache.prototype.keyNodes = function() {","\tvar keys = [];","","\tfunction getCacheKeys(currentCache) {","\t\tcurrentCache.forEach((val, key) => {","\t\t\tkeys.push(key);","\t\t\tif (val && val.size) {","\t\t\t\tgetCacheKeys(val);","\t\t\t}","\t\t});","\t}","","\tgetCacheKeys(this.cache);","","\treturn keys;","};","","module.exports = MultiKeyCache;","},{\"map-or-similar\":1}]},{},[3])(3)","});"],"id":2,"ignore":true}],[{"start":{"row":0,"column":0},"end":{"row":249,"column":3},"action":"insert","lines":["(function(f){if(typeof exports===\"object\"&&typeof module!==\"undefined\"){module.exports=f()}else if(typeof define===\"function\"&&define.amd){define([],f)}else{var g;if(typeof window!==\"undefined\"){g=window}else if(typeof global!==\"undefined\"){g=global}else if(typeof self!==\"undefined\"){g=self}else{g=this}g.multiKeyCache = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require==\"function\"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error(\"Cannot find module '\"+o+\"'\");throw f.code=\"MODULE_NOT_FOUND\",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require==\"function\"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){","if (typeof Map !== 'function' || (process && process.env && process.env.TEST_MAPORSIMILAR === 'true')) {"," module.exports = _dereq_('./similar');","}","else {"," module.exports = Map;","}","},{\"./similar\":2}],2:[function(_dereq_,module,exports){","function Similar() {"," this.list = [];"," this.lastItem = undefined;"," this.size = 0;",""," return this;","}","","Similar.prototype.get = function(key) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," return this.lastItem.val;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.lastItem = this.list[index];"," return this.list[index].val;"," }",""," return undefined;","};","","Similar.prototype.set = function(key, val) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," this.lastItem.val = val;"," return this;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.lastItem = this.list[index];"," this.list[index].val = val;"," return this;"," }",""," this.lastItem = { key: key, val: val };"," this.list.push(this.lastItem);"," this.size++;",""," return this;","};","","Similar.prototype.delete = function(key) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," this.lastItem = undefined;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.size--;"," return this.list.splice(index, 1)[0];"," }",""," return undefined;","};","","","// important that has() doesn't use get() in case an existing key has a falsy value, in which case has() would return false","Similar.prototype.has = function(key) {"," var index;",""," if (this.lastItem && this.lastItem.key === key) {"," return true;"," }",""," index = this.indexOf(key);"," if (index >= 0) {"," this.lastItem = this.list[index];"," return true;"," }",""," return false;","};","","Similar.prototype.forEach = function(callback, thisArg) {"," var i;"," for (i = 0; i < this.size; i++) {"," callback.call(thisArg || this, this.list[i].val, this.list[i].key, this);"," }","};","","Similar.prototype.indexOf = function(key) {"," var i;"," for (i = 0; i < this.size; i++) {"," if (this.list[i].key === key) {"," return i;"," }"," }"," return -1;","};","","module.exports = Similar;","},{}],3:[function(_dereq_,module,exports){","var MapOrSimilar = _dereq_('map-or-similar');","","function MultiKeyCache() {","\tthis.cache = new MapOrSimilar();","\tthis._vAl_kEY_nAMe_no_cOLLision_ = '_vAl_kEY_nAMe_no_cOLLision_';","}","","MultiKeyCache.prototype.set = function(keys, value) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\tnewSubCache,","\t\ti;","","\tif (!keysLen) {","\t\treturn this;","\t}","","\tfor (i = 0; i < keysLen; i++) {","\t\tif (currentCache.has(keys[i])) {","\t\t\tcurrentCache = currentCache.get(keys[i]);","\t\t\tcontinue;","\t\t}","\t\tnewSubCache = new MapOrSimilar();","\t\tcurrentCache.set(keys[i], newSubCache);","\t\tcurrentCache = newSubCache;","\t}","","\tcurrentCache.set(this._vAl_kEY_nAMe_no_cOLLision_, value);","","\treturn this;","};","","MultiKeyCache.prototype.get = function(keys) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\ti;","","\tif (!keysLen) {","\t\treturn undefined;","\t}","","\tfor (i = 0; i < keysLen; i++) {","\t\tcurrentCache = currentCache.get(keys[i]);","\t\tif (!currentCache) {","\t\t\treturn undefined;","\t\t}","\t}","","\treturn currentCache.get(this._vAl_kEY_nAMe_no_cOLLision_);","};","","MultiKeyCache.prototype.delete = function(keys) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\tcachePath = [currentCache],","\t\ti;","","\tif (!keysLen) {","\t\treturn false;","\t}","","\t// walk up the tree gathering the maps","\tfor (i = 0; i < keysLen; i++) {","\t\tcurrentCache = currentCache.get(keys[i]);","\t\tcachePath.push(currentCache);","\t\tif (!currentCache) {","\t\t\treturn false;","\t\t}","\t}","","\t// delete the value","\tcurrentCache.delete(this._vAl_kEY_nAMe_no_cOLLision_);","","\t// walk back down the tree deleting any empty maps","\tfor (i = keysLen - 1; i >= 0; i--) {","\t\tcurrentCache = cachePath[i].get(keys[i]);","\t\tif (currentCache.size) {","\t\t\tbreak;","\t\t}","\t\tcachePath[i].delete(keys[i]);","\t}","","\treturn true;","};","","MultiKeyCache.prototype.has = function(keys) {","\tvar keysLen = keys.length,","\t\tcurrentCache = this.cache,","\t\ti;","","\tif (!keysLen) {","\t\treturn false;","\t}","","\tfor (i = 0; i < keysLen; i++) {","\t\tcurrentCache = currentCache.get(keys[i]);","\t\tif (!currentCache) {","\t\t\treturn false;","\t\t}","\t}","\treturn true;","};","","MultiKeyCache.prototype.values = function() {","\tvar self = this,","\t\tvalues = [];","","\tfunction getCacheValues(currentCache) {","\t\tcurrentCache.forEach(function(val, key) {","\t\t\tif (key === self._vAl_kEY_nAMe_no_cOLLision_) {","\t\t\t\tvalues.push(val);","\t\t\t}","\t\t\telse {","\t\t\t\tgetCacheValues(val);","\t\t\t}","\t\t});","\t}","","\tgetCacheValues(this.cache);","","\treturn values;","};","","MultiKeyCache.prototype.keyNodes = function() {","\tvar keys = [];","","\tfunction getCacheKeys(currentCache) {","\t\tcurrentCache.forEach(function(val, key) {","\t\t\tkeys.push(key);","\t\t\tif (val && val.size) {","\t\t\t\tgetCacheKeys(val);","\t\t\t}","\t\t});","\t}","","\tgetCacheKeys(this.cache);","","\treturn keys;","};","","module.exports = MultiKeyCache;","},{\"map-or-similar\":1}]},{},[3])(3)","});"],"id":3,"ignore":true}]]},"ace":{"folds":[],"scrolltop":2880,"scrollleft":0,"selection":{"start":{"row":0,"column":0},"end":{"row":0,"column":0},"isBackwards":false},"options":{"guessTabSize":true,"useWrapMode":false,"wrapToView":true},"firstLineState":0},"timestamp":1454574889379,"hash":"85d808c0fe31975f3cfa5a0a1ed1183ae2d1bbe2"}