UNPKG

@amaui/hash-table

Version:
7 lines (6 loc) 5.16 kB
/** @license AmauiHashTable v1.0.1 * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e="undefined"!=typeof globalThis?globalThis:e||self).AmauiHashTable=t()}(this,(function(){"use strict";function e(t){return e="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},e(t)}function t(t){var r=function(t,r){if("object"!==e(t)||null===t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var o=n.call(t,r||"default");if("object"!==e(o))return o;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===r?String:Number)(t)}(t,"string");return"symbol"===e(r)?r:String(r)}function r(e,r,n){return(r=t(r))in e?Object.defineProperty(e,r,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[r]=n,e}var n="undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{};const o={},a="undefined"!=typeof window&&void 0!==window.document,i=!(void 0===n||"undefined"==typeof module||!module.exports);function u(e,t){var r;let n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};const s={...o,...n},{variant:c}=s,y=t&&"object"==typeof t&&Object.getPrototypeOf(t);switch(e){case"string":return"string"==typeof t;case"number":return"number"==typeof t&&!Number.isNaN(t);case"boolean":return"boolean"==typeof t;case"array":return Array.isArray(t);case"object":return"object"==typeof t&&!!t&&t.constructor===Object;case"object-like":return"object"==typeof t&&(null===t||t.constructor!==Object);case"class":return("object"==typeof t||"function"==typeof t)&&(/class/gi.test(String(t))||/class/gi.test(String(null==t?void 0:t.constructor)));case"function":return!!(t&&t instanceof Function);case"async":return!(!u("function",t)||!(a?"AsyncFunction"===t.constructor.name:t()instanceof Promise));case"map":return!(y!==Map.prototype);case"weakmap":return!(y!==WeakMap.prototype);case"set":return!(y!==Set.prototype);case"weakset":return!(y!==WeakSet.prototype);case"promise":return!(y!==Promise.prototype);case"int8array":return!(y!==Int8Array.prototype);case"uint8array":return!(y!==Uint8Array.prototype);case"uint8clampedarray":return!(y!==Uint8ClampedArray.prototype);case"int16array":return!(y!==Int16Array.prototype);case"uint16array":return!(y!==Uint16Array.prototype);case"int32array":return!(y!==Int32Array.prototype);case"uint32array":return!(y!==Uint32Array.prototype);case"float32array":return!(y!==Float32Array.prototype);case"float64array":return!(y!==Float64Array.prototype);case"bigint64array":return!(y!==BigInt64Array.prototype);case"biguint64array":return!(y!==BigUint64Array.prototype);case"typedarray":return u("int8array",t)||u("uint8array",t)||u("uint8clampedarray",t)||u("int16array",t)||u("uint16array",t)||u("int32array",t)||u("uint32array",t)||u("float32array",t)||u("float64array",t)||u("bigint64array",t)||u("biguint64array",t);case"dataview":return!(y!==DataView.prototype);case"arraybuffer":return!(y!==ArrayBuffer.prototype);case"sharedarraybuffer":return"undefined"!=typeof SharedArrayBuffer&&!(y!==SharedArrayBuffer.prototype);case"symbol":return!("symbol"!=typeof t);case"error":return!!(t&&t instanceof Error);case"date":return!!(t&&t instanceof Date);case"regexp":return!!(t&&t instanceof RegExp);case"arguments":return!(!t||"[object Arguments]"!==t.toString());case"null":return null===t;case"undefined":return void 0===t;case"blob":return a&&t instanceof Blob;case"buffer":return!(!i||"function"!=typeof(null==t||null===(r=t.constructor)||void 0===r?void 0:r.isBuffer)||!t.constructor.isBuffer(t));case"element":if(t)switch(c){case void 0:case"html":case"element":return a&&("object"==typeof HTMLElement?t instanceof HTMLElement:t&&"object"==typeof t&&null!==t&&1===t.nodeType&&"string"==typeof t.nodeName);case"node":return a&&("object"==typeof Node?t instanceof Node:t&&"object"==typeof t&&null!==t&&"number"==typeof t.nodeType&&"string"==typeof t.nodeName);case"react":return t.elementType||t.hasOwnProperty("$$typeof");default:return!1}return!1;case"simple":return u("string",t,s)||u("number",t,s)||u("boolean",t,s)||u("undefined",t,s)||u("null",t,s);case"not-array-object":return!u("array",t,s)&&!u("object",t,s);default:return!1}}return class{constructor(e){r(this,"value",[]),r(this,"length",0),r(this,"limit",127),u("number",e)&&e>0&&(this.limit=e),this.value=new Array(this.limit)}hash(e){let t=0;if(u("string",e))for(let r=0;r<e.length;r++)t+=e[r].charCodeAt(0);return t%this.limit}get(e){const t=this.hash(e);return(this.value[t]||[]).find((t=>t[0]===e))?.[1]}set(e,t){const r=this.hash(e);this.value[r]||(this.value[r]=[]);return this.value[r].push([e,t]),this.length++,this}remove(e){const t=this.hash(e),r=this.value[t];if(r){const t=r.findIndex((t=>t[0]===e));if(t>-1)return r.splice(t,1),this.length--,!0}return!1}clear(){return this.value=new Array(this.limit),this.length=0,this}}}));