UNPKG

node-jshashtable

Version:

A standalone implementation of hash table in node.

2 lines (1 loc) 4.22 kB
!function(n,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):n.Hashtable=t()}(this,function(){"use strict";var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(n){return typeof n}:function(n){return n&&"function"==typeof Symbol&&n.constructor===Symbol&&n!==Symbol.prototype?"symbol":typeof n};return function(t){function e(t){return(void 0===t?"undefined":n(t))==v?t:""+t}function r(t){var i=void 0;return(void 0===t?"undefined":n(t))==v?t:n(t.hashCode)==y?(i=t.hashCode(),(void 0===i?"undefined":n(i))==v?i:r(i)):e(t)}function i(n,t){for(var e in t)t.hasOwnProperty(e)&&(n[e]=t[e])}function o(n,t){return n.equals(t)}function u(t,e){return n(e.equals)==y?e.equals(t):t===e}function s(n){return function(e){if(null===e)throw new Error("null is not a valid "+n);if(e===t)throw new Error(n+" must not be undefined")}}function f(n,t,e,r){this[0]=n,this.entries=[],this.addEntry(t,e),null!==r&&(this.getEqualityFunction=function(){return r})}function l(n){return function(t){for(var e=this.entries.length,r=void 0,i=this.getEqualityFunction(t);e--;)if(r=this.entries[e],i(t,r[0]))switch(n){case m:return!0;case E:return r;case K:return[e,r[1]]}return!1}}function a(n){return function(t){for(var e=t.length,r=0,i=this.entries,o=i.length;r<o;++r)t[e+r]=i[r][n]}}function c(n,t){for(var e=n.length,r=void 0;e--;)if(r=n[e],t===r[0])return e;return null}function h(n,t){var e=n[t];return e&&e instanceof f?e:null}function d(){var n=[],e={},o={replaceDuplicateKey:!0,hashCode:r,equals:null},u=arguments[0],s=arguments[1];s!==t?(o.hashCode=u,o.equals=s):u!==t&&i(o,u);var l=o.hashCode,a=o.equals;this.properties=o,this.put=function(t,r){p(t),g(r);var i=l(t),u=void 0,s=void 0,c=null;return u=h(e,i),u?(s=u.getEntryForKey(t),s?(o.replaceDuplicateKey&&(s[0]=t),c=s[1],s[1]=r):u.addEntry(t,r)):(u=new f(i,t,r,a),n.push(u),e[i]=u),c},this.get=function(n){p(n);var t=l(n),r=h(e,t);if(r){var i=r.getEntryForKey(n);if(i)return i[1]}return null},this.containsKey=function(n){p(n);var t=l(n),r=h(e,t);return!!r&&r.containsKey(n)},this.containsValue=function(t){g(t);for(var e=n.length;e--;)if(n[e].containsValue(t))return!0;return!1},this.clear=function(){n.length=0,e={}},this.isEmpty=function(){return!n.length};var d=function(t){return function(){for(var e=[],r=n.length;r--;)n[r][t](e);return e}};this.keys=d("keys"),this.values=d("values"),this.entries=d("getEntries"),this.remove=function(t){p(t);var r=l(t),i=void 0,o=null,u=h(e,r);return u&&null!==(o=u.removeEntryForKey(t))&&0===u.entries.length&&(i=c(n,r),n.splice(i,1),delete e[r]),o},this.size=function(){for(var t=0,e=n.length;e--;)t+=n[e].entries.length;return t}}var y="function",v="string";if("undefined"==("undefined"==typeof encodeURIComponent?"undefined":n(encodeURIComponent))||Array.prototype.splice===t||Object.prototype.hasOwnProperty===t)return null;var p=s("key"),g=s("value"),m=0,E=1,K=2;return f.prototype={getEqualityFunction:function(t){return n(t.equals)==y?o:u},getEntryForKey:l(E),getEntryAndIndexForKey:l(K),removeEntryForKey:function(n){var t=this.getEntryAndIndexForKey(n);return t?(this.entries.splice(t[0],1),t[1]):null},addEntry:function(n,t){this.entries.push([n,t])},keys:a(0),values:a(1),getEntries:function(n){for(var t=n.length,e=0,r=this.entries,i=r.length;e<i;++e)n[t+e]=r[e].slice(0)},containsKey:l(m),containsValue:function(n){for(var t=this.entries,e=t.length;e--;)if(n===t[e][1])return!0;return!1}},d.prototype={each:function(n){for(var t=this.entries(),e=t.length,r=void 0;e--;)r=t[e],n(r[0],r[1])},equals:function(n){var t=void 0,e=void 0,r=void 0,i=this.size();if(i===n.size()){for(t=this.keys();i--;)if(e=t[i],null===(r=n.get(e))||r!==this.get(e))return!1;return!0}return!1},putAll:function(t,e){for(var r=t.entries(),i=void 0,o=void 0,u=void 0,s=void 0,f=r.length,l=(void 0===e?"undefined":n(e))==y;f--;)i=r[f],o=i[0],u=i[1],l&&(s=this.get(o))&&(u=e(o,s,u)),this.put(o,u)},clone:function(){var n=new d(this.properties);return n.putAll(this),n}},d.prototype.toQueryString=function(){for(var n=this.entries(),t=n.length,r=void 0,i=[];t--;)r=n[t],i[t]=encodeURIComponent(e(r[0]))+"="+encodeURIComponent(e(r[1]));return i.join("&")},d}()});