@ocavue/utils 1.1.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +139 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +85 -229
- package/dist/index.js.map +1 -1
- package/package.json +16 -13
- package/src/__snapshots__/e2e.test.ts.snap +247 -0
- package/src/default-map.test.ts +290 -1
- package/src/default-map.ts +76 -0
- package/src/e2e.test.ts +36 -0
- package/src/get-id.test.ts +20 -1
- package/src/get-id.ts +15 -1
- package/src/index.ts +3 -1
- package/src/map-group-by.ts +3 -1
- package/src/map-values.test.ts +41 -0
- package/src/map-values.ts +43 -0
- package/src/object-entries.test.ts +33 -0
- package/src/object-entries.ts +58 -0
- package/src/object-group-by.ts +3 -1
- package/src/once-stub-1.ts +13 -0
- package/src/once-stub-2.ts +3 -0
- package/src/once.test.ts +29 -0
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["NodeType.ELEMENT_NODE","NodeType.TEXT_NODE","NodeType.DOCUMENT_NODE","NodeType.DOCUMENT_FRAGMENT_NODE","mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]>","result: Partial<Record<K, T[]>>","objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>>","result: T"],"sources":["../src/checker.ts","../src/default-map.ts","../src/dom-node-type.ts","../src/dom.ts","../src/format-bytes.ts","../src/get-id.ts","../src/is-deep-equal.ts","../src/map-group-by.ts","../src/object-group-by.ts","../src/once.ts","../src/sleep.ts"],"sourcesContent":["/**\n * Checks if the given value is an object.\n */\nexport function isObject(\n value: unknown,\n): value is Record<string | symbol | number, unknown> {\n return value != null && typeof value === 'object'\n}\n\n/**\n * Checks if the given value is a Map.\n */\nexport function isMap(value: unknown): value is Map<unknown, unknown> {\n return value instanceof Map\n}\n\n/**\n * Checks if the given value is a Set.\n */\nexport function isSet(value: unknown): value is Set<unknown> {\n return value instanceof Set\n}\n","/**\n * A map that automatically creates values for missing keys using a factory function.\n *\n * Similar to Python's [defaultdict](https://docs.python.org/3.13/library/collections.html#collections.defaultdict).\n */\nexport class DefaultMap<K, V> extends Map<K, V> {\n private readonly defaultFactory: () => V\n\n constructor(defaultFactory: () => V, iterable?: Iterable<readonly [K, V]>) {\n super(iterable)\n this.defaultFactory = defaultFactory\n }\n\n override get(key: K): V {\n if (this.has(key)) {\n return super.get(key)!\n }\n const value = this.defaultFactory()\n this.set(key, value)\n return value\n }\n}\n","// prettier-ignore\nexport const ELEMENT_NODE = 1 satisfies typeof Node.ELEMENT_NODE;\n// prettier-ignore\nexport const ATTRIBUTE_NODE = 2 satisfies typeof Node.ATTRIBUTE_NODE;\n// prettier-ignore\nexport const TEXT_NODE = 3 satisfies typeof Node.TEXT_NODE;\n// prettier-ignore\nexport const CDATA_SECTION_NODE = 4 satisfies typeof Node.CDATA_SECTION_NODE;\n// prettier-ignore\nexport const ENTITY_REFERENCE_NODE = 5 satisfies typeof Node.ENTITY_REFERENCE_NODE;\n// prettier-ignore\nexport const ENTITY_NODE = 6 satisfies typeof Node.ENTITY_NODE;\n// prettier-ignore\nexport const PROCESSING_INSTRUCTION_NODE = 7 satisfies typeof Node.PROCESSING_INSTRUCTION_NODE;\n// prettier-ignore\nexport const COMMENT_NODE = 8 satisfies typeof Node.COMMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_NODE = 9 satisfies typeof Node.DOCUMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_TYPE_NODE = 10 satisfies typeof Node.DOCUMENT_TYPE_NODE;\n// prettier-ignore\nexport const DOCUMENT_FRAGMENT_NODE = 11 satisfies typeof Node.DOCUMENT_FRAGMENT_NODE;\n// prettier-ignore\nexport const NOTATION_NODE = 12 satisfies typeof Node.NOTATION_NODE;\n","import { isObject } from './checker'\nimport * as NodeType from './dom-node-type'\n\n/**\n * Checks if the given DOM node is an Element.\n */\nexport function isElement(node: Node): node is Element {\n return node.nodeType === NodeType.ELEMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a Text node.\n */\nexport function isTextNode(node: Node): node is Text {\n return node.nodeType === NodeType.TEXT_NODE\n}\n\n/**\n * Checks if the given DOM node is an HTMLElement.\n */\nexport function isHTMLElement(node: Node): node is HTMLElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/1999/xhtml'\n}\n\n/**\n * Checks if the given DOM node is an SVGElement.\n */\nexport function isSVGElement(node: Node): node is SVGElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/2000/svg'\n}\n\n/**\n * Checks if the given DOM node is an MathMLElement.\n */\nexport function isMathMLElement(node: Node): node is MathMLElement {\n return (\n isElement(node) &&\n node.namespaceURI === 'http://www.w3.org/1998/Math/MathML'\n )\n}\n\n/**\n * Checks if the given DOM node is a Document.\n */\nexport function isDocument(node: Node): node is Document {\n return node.nodeType === NodeType.DOCUMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a DocumentFragment.\n */\nexport function isDocumentFragment(node: Node): node is DocumentFragment {\n return node.nodeType === NodeType.DOCUMENT_FRAGMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a ShadowRoot.\n */\nexport function isShadowRoot(node: Node): node is ShadowRoot {\n return isDocumentFragment(node) && 'host' in node && isElementLike(node.host)\n}\n\n/**\n * Checks if an unknown value is likely a DOM node.\n */\nexport function isNodeLike(value: unknown): value is Node {\n return isObject(value) && value.nodeType !== undefined\n}\n\n/**\n * Checks if an unknown value is likely a DOM element.\n */\nexport function isElementLike(value: unknown): value is Element {\n return (\n isObject(value) &&\n value.nodeType === NodeType.ELEMENT_NODE &&\n typeof value.nodeName === 'string'\n )\n}\n\n/**\n * Checks if the given value is likely a Window object.\n */\nexport function isWindowLike(value: unknown): value is Window {\n return isObject(value) && value.window === value\n}\n\n/**\n * Gets the window object for the given target or the global window object if no\n * target is provided.\n */\nexport function getWindow(\n target?: Node | ShadowRoot | Document | null,\n): Window & typeof globalThis {\n if (target) {\n if (isShadowRoot(target)) {\n return getWindow(target.host)\n }\n if (isDocument(target)) {\n return target.defaultView || window\n }\n if (isElement(target)) {\n return target.ownerDocument?.defaultView || window\n }\n }\n return window\n}\n\n/**\n * Gets the document for the given target or the global document if no target is\n * provided.\n */\nexport function getDocument(\n target?: Element | Window | Node | Document | null,\n): Document {\n if (target) {\n if (isWindowLike(target)) {\n return target.document\n }\n if (isDocument(target)) {\n return target\n }\n return target.ownerDocument || document\n }\n return document\n}\n\n/**\n * Gets a reference to the root node of the document based on the given target.\n */\nexport function getDocumentElement(\n target?: Element | Node | Window | Document | null,\n): HTMLElement {\n return getDocument(target).documentElement\n}\n","/**\n * Formats a number of bytes into a human-readable string.\n * @param bytes - The number of bytes to format.\n * @returns A string representing the number of bytes in a human-readable format.\n */\nexport function formatBytes(bytes: number): string {\n const units = ['B', 'KB', 'MB', 'GB']\n let unitIndex = 0\n let num = bytes\n while (Math.abs(num) >= 1024 && unitIndex < units.length - 1) {\n num /= 1024\n unitIndex++\n }\n const fraction = unitIndex === 0 && num % 1 === 0 ? 0 : 1\n return `${num.toFixed(fraction)}${units[unitIndex]}`\n}\n","let id = 0\n\n/**\n * Generates a unique positive integer.\n */\nexport function getId(): number {\n id = (id % Number.MAX_SAFE_INTEGER) + 1\n return id\n}\n","import { isMap, isSet } from './checker'\n\n/**\n * Whether two values are deeply equal.\n */\nexport function isDeepEqual(a: unknown, b: unknown): boolean {\n if (a === b) {\n return true\n }\n\n // Handle null and undefined early\n if (a == null || b == null) {\n return false\n }\n\n const aType = typeof a\n const bType = typeof b\n if (aType !== bType) {\n return false\n }\n\n if (aType === 'number' && Number.isNaN(a) && Number.isNaN(b)) {\n return true\n }\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) {\n return false\n }\n if (a.length !== b.length) {\n return false\n }\n const size = a.length\n for (let i = 0; i < size; i++) {\n if (!isDeepEqual(a[i], b[i])) {\n return false\n }\n }\n return true\n }\n\n if (isSet(a)) {\n if (!isSet(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const value of a) {\n if (!b.has(value)) {\n return false\n }\n }\n return true\n }\n\n if (isMap(a)) {\n if (!isMap(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const key of a.keys()) {\n if (!b.has(key)) {\n return false\n }\n if (!isDeepEqual(a.get(key), b.get(key))) {\n return false\n }\n }\n return true\n }\n\n if (typeof a === 'object' && typeof b === 'object') {\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n if (aKeys.length !== bKeys.length) {\n return false\n }\n for (const key of aKeys) {\n if (\n !isDeepEqual(\n (a as Record<string, unknown>)[key],\n (b as Record<string, unknown>)[key],\n )\n ) {\n return false\n }\n }\n return true\n }\n\n return false\n}\n","/**\n * @internal\n */\nexport function mapGroupByPolyfill<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n const map = new Map<K, T[]>()\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = map.get(key)\n if (group) {\n group.push(item)\n } else {\n map.set(key, [item])\n }\n index++\n }\n return map\n}\n\n/**\n * @internal\n */\nexport function mapGroupByNative<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n return Map.groupBy(items, keySelector)\n}\n\n/**\n * A polyfill for the `Map.groupBy` static method.\n *\n * @public\n */\nexport const mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]> = !!Map.groupBy ? mapGroupByNative : mapGroupByPolyfill\n","/**\n * @internal\n */\nexport function objectGroupByPolyfill<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n const result: Partial<Record<K, T[]>> = {}\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = result[key]\n if (group) {\n group.push(item)\n } else {\n result[key] = [item]\n }\n index++\n }\n return result\n}\n\n/**\n * @internal\n */\nexport function objectGroupByNative<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n return Object.groupBy(items, keySelector)\n}\n\n/**\n * A polyfill for the `Object.groupBy` static method.\n *\n * @public\n */\nexport const objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>> = !!Object.groupBy\n ? objectGroupByNative\n : objectGroupByPolyfill\n","/**\n * Creates a function that will only execute the provided function once.\n * Subsequent calls will return the cached result from the first execution.\n *\n * @param fn The function to execute once\n * @returns A function that will only execute the provided function once\n * @example\n * ```ts\n * const getValue = once(() => expensiveOperation())\n * getValue() // executes expensiveOperation\n * getValue() // returns cached result\n * ```\n */\nexport function once<T>(fn: () => T): () => T {\n let called = false\n let result: T\n return () => {\n if (!called) {\n result = fn()\n called = true\n // @ts-expect-error - micro-optimization for memory management\n fn = undefined\n }\n return result\n }\n}\n","/**\n * Sleep for a given number of milliseconds.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n"],"mappings":";;;;AAGA,SAAgB,SACd,OACoD;AACpD,QAAO,SAAS,QAAQ,OAAO,UAAU;;;;;AAM3C,SAAgB,MAAM,OAAgD;AACpE,QAAO,iBAAiB;;;;;AAM1B,SAAgB,MAAM,OAAuC;AAC3D,QAAO,iBAAiB;;;;;;;;;;ACf1B,IAAa,aAAb,cAAsC,IAAU;CAG9C,YAAY,gBAAyB,UAAsC;AACzE,QAAM,SAAS;AACf,OAAK,iBAAiB;;CAGxB,AAAS,IAAI,KAAW;AACtB,MAAI,KAAK,IAAI,IAAI,CACf,QAAO,MAAM,IAAI,IAAI;EAEvB,MAAM,QAAQ,KAAK,gBAAgB;AACnC,OAAK,IAAI,KAAK,MAAM;AACpB,SAAO;;;;;;AClBX,MAAa,eA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tB;;AAEF,QAAO;;;;;AAMT,SAAgB,oBACd,OACA,aACyB;AACzB,QAAO,OAAO,QAAQ,OAAO,YAAY;;;;;;;AAQ3C,MAAaC,gBAGkB,CAAC,CAAC,OAAO,UACpC,sBACA;;;;;;;;;;;;;;;;;AC7BJ,SAAgB,KAAQ,IAAsB;CAC5C,IAAI,SAAS;CACb,IAAIC;AACJ,cAAa;AACX,MAAI,CAAC,QAAQ;AACX,YAAS,IAAI;AACb,YAAS;AAET,QAAK;;AAEP,SAAO;;;;;;;;;ACpBX,SAAgB,MAAM,IAA2B;AAC/C,QAAO,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["NodeType.ELEMENT_NODE","NodeType.TEXT_NODE","NodeType.DOCUMENT_NODE","NodeType.DOCUMENT_FRAGMENT_NODE","hasMapGroupBy: boolean","mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]>","objectEntries: <T extends Record<string, any>>(\n obj: T,\n) => ObjectEntries<T>[]","result: Partial<Record<K, T[]>>","hasObjectGroupBy: boolean","objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>>","result: T"],"sources":["../src/checker.ts","../src/default-map.ts","../src/dom-node-type.ts","../src/dom.ts","../src/format-bytes.ts","../src/get-id.ts","../src/is-deep-equal.ts","../src/map-group-by.ts","../src/map-values.ts","../src/object-entries.ts","../src/object-group-by.ts","../src/once.ts","../src/sleep.ts"],"sourcesContent":["/**\n * Checks if the given value is an object.\n */\nexport function isObject(\n value: unknown,\n): value is Record<string | symbol | number, unknown> {\n return value != null && typeof value === 'object'\n}\n\n/**\n * Checks if the given value is a Map.\n */\nexport function isMap(value: unknown): value is Map<unknown, unknown> {\n return value instanceof Map\n}\n\n/**\n * Checks if the given value is a Set.\n */\nexport function isSet(value: unknown): value is Set<unknown> {\n return value instanceof Set\n}\n","/**\n * A map that automatically creates values for missing keys using a factory function.\n *\n * Similar to Python's [defaultdict](https://docs.python.org/3.13/library/collections.html#collections.defaultdict).\n */\nexport class DefaultMap<K, V> extends Map<K, V> {\n private readonly defaultFactory: () => V\n\n constructor(defaultFactory: () => V, iterable?: Iterable<readonly [K, V]>) {\n super(iterable)\n this.defaultFactory = defaultFactory\n }\n\n override get(key: K): V {\n if (this.has(key)) {\n return super.get(key)!\n }\n const value = this.defaultFactory()\n this.set(key, value)\n return value\n }\n}\n\n/**\n * A weak map that automatically creates values for missing keys using a factory function.\n *\n * Similar to DefaultMap but uses WeakMap as the base, allowing garbage collection of keys.\n */\nexport class DefaultWeakMap<K extends WeakKey, V> extends WeakMap<K, V> {\n private readonly defaultFactory: () => V\n\n constructor(\n defaultFactory: () => V,\n entries?: readonly (readonly [K, V])[] | null,\n ) {\n super(entries)\n this.defaultFactory = defaultFactory\n }\n\n override get(key: K): V {\n if (this.has(key)) {\n return super.get(key)!\n }\n const value = this.defaultFactory()\n this.set(key, value)\n return value\n }\n}\n\n/**\n * A map that counts occurrences of keys.\n *\n * Similar to Python's [Counter](https://docs.python.org/3.13/library/collections.html#collections.Counter).\n */\nexport class Counter<K> extends DefaultMap<K, number> {\n constructor(iterable?: Iterable<readonly [K, number]>) {\n super(() => 0, iterable)\n }\n\n /**\n * Increments the count for a key by a given amount (default 1).\n */\n increment(key: K, amount = 1): void {\n this.set(key, this.get(key) + amount)\n }\n\n /**\n * Decrements the count for a key by a given amount (default 1).\n */\n decrement(key: K, amount = 1): void {\n this.set(key, this.get(key) - amount)\n }\n}\n\n/**\n * A weak map that counts occurrences of object keys.\n *\n * Similar to Counter but uses WeakMap as the base, allowing garbage collection of keys.\n */\nexport class WeakCounter<K extends WeakKey> extends DefaultWeakMap<K, number> {\n constructor(entries?: readonly (readonly [K, number])[] | null) {\n super(() => 0, entries)\n }\n\n /**\n * Increments the count for a key by a given amount (default 1).\n */\n increment(key: K, amount = 1): void {\n this.set(key, this.get(key) + amount)\n }\n\n /**\n * Decrements the count for a key by a given amount (default 1).\n */\n decrement(key: K, amount = 1): void {\n this.set(key, this.get(key) - amount)\n }\n}\n","// prettier-ignore\nexport const ELEMENT_NODE = 1 satisfies typeof Node.ELEMENT_NODE;\n// prettier-ignore\nexport const ATTRIBUTE_NODE = 2 satisfies typeof Node.ATTRIBUTE_NODE;\n// prettier-ignore\nexport const TEXT_NODE = 3 satisfies typeof Node.TEXT_NODE;\n// prettier-ignore\nexport const CDATA_SECTION_NODE = 4 satisfies typeof Node.CDATA_SECTION_NODE;\n// prettier-ignore\nexport const ENTITY_REFERENCE_NODE = 5 satisfies typeof Node.ENTITY_REFERENCE_NODE;\n// prettier-ignore\nexport const ENTITY_NODE = 6 satisfies typeof Node.ENTITY_NODE;\n// prettier-ignore\nexport const PROCESSING_INSTRUCTION_NODE = 7 satisfies typeof Node.PROCESSING_INSTRUCTION_NODE;\n// prettier-ignore\nexport const COMMENT_NODE = 8 satisfies typeof Node.COMMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_NODE = 9 satisfies typeof Node.DOCUMENT_NODE;\n// prettier-ignore\nexport const DOCUMENT_TYPE_NODE = 10 satisfies typeof Node.DOCUMENT_TYPE_NODE;\n// prettier-ignore\nexport const DOCUMENT_FRAGMENT_NODE = 11 satisfies typeof Node.DOCUMENT_FRAGMENT_NODE;\n// prettier-ignore\nexport const NOTATION_NODE = 12 satisfies typeof Node.NOTATION_NODE;\n","import { isObject } from './checker'\nimport * as NodeType from './dom-node-type'\n\n/**\n * Checks if the given DOM node is an Element.\n */\nexport function isElement(node: Node): node is Element {\n return node.nodeType === NodeType.ELEMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a Text node.\n */\nexport function isTextNode(node: Node): node is Text {\n return node.nodeType === NodeType.TEXT_NODE\n}\n\n/**\n * Checks if the given DOM node is an HTMLElement.\n */\nexport function isHTMLElement(node: Node): node is HTMLElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/1999/xhtml'\n}\n\n/**\n * Checks if the given DOM node is an SVGElement.\n */\nexport function isSVGElement(node: Node): node is SVGElement {\n return isElement(node) && node.namespaceURI === 'http://www.w3.org/2000/svg'\n}\n\n/**\n * Checks if the given DOM node is an MathMLElement.\n */\nexport function isMathMLElement(node: Node): node is MathMLElement {\n return (\n isElement(node) &&\n node.namespaceURI === 'http://www.w3.org/1998/Math/MathML'\n )\n}\n\n/**\n * Checks if the given DOM node is a Document.\n */\nexport function isDocument(node: Node): node is Document {\n return node.nodeType === NodeType.DOCUMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a DocumentFragment.\n */\nexport function isDocumentFragment(node: Node): node is DocumentFragment {\n return node.nodeType === NodeType.DOCUMENT_FRAGMENT_NODE\n}\n\n/**\n * Checks if the given DOM node is a ShadowRoot.\n */\nexport function isShadowRoot(node: Node): node is ShadowRoot {\n return isDocumentFragment(node) && 'host' in node && isElementLike(node.host)\n}\n\n/**\n * Checks if an unknown value is likely a DOM node.\n */\nexport function isNodeLike(value: unknown): value is Node {\n return isObject(value) && value.nodeType !== undefined\n}\n\n/**\n * Checks if an unknown value is likely a DOM element.\n */\nexport function isElementLike(value: unknown): value is Element {\n return (\n isObject(value) &&\n value.nodeType === NodeType.ELEMENT_NODE &&\n typeof value.nodeName === 'string'\n )\n}\n\n/**\n * Checks if the given value is likely a Window object.\n */\nexport function isWindowLike(value: unknown): value is Window {\n return isObject(value) && value.window === value\n}\n\n/**\n * Gets the window object for the given target or the global window object if no\n * target is provided.\n */\nexport function getWindow(\n target?: Node | ShadowRoot | Document | null,\n): Window & typeof globalThis {\n if (target) {\n if (isShadowRoot(target)) {\n return getWindow(target.host)\n }\n if (isDocument(target)) {\n return target.defaultView || window\n }\n if (isElement(target)) {\n return target.ownerDocument?.defaultView || window\n }\n }\n return window\n}\n\n/**\n * Gets the document for the given target or the global document if no target is\n * provided.\n */\nexport function getDocument(\n target?: Element | Window | Node | Document | null,\n): Document {\n if (target) {\n if (isWindowLike(target)) {\n return target.document\n }\n if (isDocument(target)) {\n return target\n }\n return target.ownerDocument || document\n }\n return document\n}\n\n/**\n * Gets a reference to the root node of the document based on the given target.\n */\nexport function getDocumentElement(\n target?: Element | Node | Window | Document | null,\n): HTMLElement {\n return getDocument(target).documentElement\n}\n","/**\n * Formats a number of bytes into a human-readable string.\n * @param bytes - The number of bytes to format.\n * @returns A string representing the number of bytes in a human-readable format.\n */\nexport function formatBytes(bytes: number): string {\n const units = ['B', 'KB', 'MB', 'GB']\n let unitIndex = 0\n let num = bytes\n while (Math.abs(num) >= 1024 && unitIndex < units.length - 1) {\n num /= 1024\n unitIndex++\n }\n const fraction = unitIndex === 0 && num % 1 === 0 ? 0 : 1\n return `${num.toFixed(fraction)}${units[unitIndex]}`\n}\n","let id = 0\n\nlet maxSafeInteger = Number.MAX_SAFE_INTEGER\n\n/**\n * Sets the maximum safe integer for the id generator. Only for testing purposes.\n *\n * @internal\n */\nexport function setMaxSafeInteger(max: number) {\n maxSafeInteger = max\n}\n\n/**\n * Generates a unique positive integer.\n */\nexport function getId(): number {\n id++\n if (id >= maxSafeInteger) {\n id = 1\n }\n return id\n}\n","import { isMap, isSet } from './checker'\n\n/**\n * Whether two values are deeply equal.\n */\nexport function isDeepEqual(a: unknown, b: unknown): boolean {\n if (a === b) {\n return true\n }\n\n // Handle null and undefined early\n if (a == null || b == null) {\n return false\n }\n\n const aType = typeof a\n const bType = typeof b\n if (aType !== bType) {\n return false\n }\n\n if (aType === 'number' && Number.isNaN(a) && Number.isNaN(b)) {\n return true\n }\n\n if (Array.isArray(a)) {\n if (!Array.isArray(b)) {\n return false\n }\n if (a.length !== b.length) {\n return false\n }\n const size = a.length\n for (let i = 0; i < size; i++) {\n if (!isDeepEqual(a[i], b[i])) {\n return false\n }\n }\n return true\n }\n\n if (isSet(a)) {\n if (!isSet(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const value of a) {\n if (!b.has(value)) {\n return false\n }\n }\n return true\n }\n\n if (isMap(a)) {\n if (!isMap(b)) {\n return false\n }\n if (a.size !== b.size) {\n return false\n }\n for (const key of a.keys()) {\n if (!b.has(key)) {\n return false\n }\n if (!isDeepEqual(a.get(key), b.get(key))) {\n return false\n }\n }\n return true\n }\n\n if (typeof a === 'object' && typeof b === 'object') {\n const aKeys = Object.keys(a)\n const bKeys = Object.keys(b)\n if (aKeys.length !== bKeys.length) {\n return false\n }\n for (const key of aKeys) {\n if (\n !isDeepEqual(\n (a as Record<string, unknown>)[key],\n (b as Record<string, unknown>)[key],\n )\n ) {\n return false\n }\n }\n return true\n }\n\n return false\n}\n","/**\n * @internal\n */\nexport function mapGroupByPolyfill<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n const map = new Map<K, T[]>()\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = map.get(key)\n if (group) {\n group.push(item)\n } else {\n map.set(key, [item])\n }\n index++\n }\n return map\n}\n\n/**\n * @internal\n */\nexport function mapGroupByNative<K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Map<K, T[]> {\n return Map.groupBy(items, keySelector)\n}\n\nconst hasMapGroupBy: boolean = /* @__PURE__ */ (() => !!Map.groupBy)()\n\n/**\n * A polyfill for the `Map.groupBy` static method.\n *\n * @public\n */\nexport const mapGroupBy: <K, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Map<K, T[]> = hasMapGroupBy ? mapGroupByNative : mapGroupByPolyfill\n","/**\n * Creates a new object with the same keys as the input object, but with values\n * transformed by the provided callback function. Similar to `Array.prototype.map()`\n * but for object values.\n\n * @param object - The object whose values will be transformed.\n * @param callback - A function that transforms each value. Receives the value and\n * its corresponding key as arguments.\n * @returns A new object with the same keys but transformed values.\n *\n * @example\n * ```typescript\n * const prices = { apple: 1, banana: 2, orange: 3 }\n * const doubled = mapValues(prices, (price) => price * 2)\n * // Result: { apple: 2, banana: 4, orange: 6 }\n * ```\n *\n * @example\n * ```typescript\n * const users = { john: 25, jane: 30, bob: 35 }\n * const greetings = mapValues(users, (age, name) => `${name} is ${age} years old`)\n * // Result: { john: 'john is 25 years old', jane: 'jane is 30 years old', bob: 'bob is 35 years old' }\n * ```\n *\n * @example\n * ```typescript\n * const data = { a: '1', b: '2', c: '3' }\n * const numbers = mapValues(data, (str) => parseInt(str, 10))\n * // Result: { a: 1, b: 2, c: 3 }\n * ```\n *\n * @public\n */\nexport function mapValues<ValueIn, ValueOut>(\n object: Record<string, ValueIn>,\n callback: (value: ValueIn, key: string) => ValueOut,\n): Record<string, ValueOut> {\n let result = {} as Record<string, ValueOut>\n for (const [key, value] of Object.entries(object)) {\n result[key] = callback(value, key)\n }\n return result\n}\n","/**\n * A TypeScript utility type that represents the entries of an object as a union of tuple types.\n * Each tuple contains a key-value pair where the key and value types are precisely typed\n * according to the input object type.\n *\n * @example\n * ```typescript\n * type MyObject = { a: 1; b: 'B' }\n * type MyEntries = ObjectEntries<MyObject>\n * // ^ [\"a\", 1] | [\"b\", \"B\"]\n * ```\n *\n * @public\n */\nexport type ObjectEntries<T extends Record<string, any>> = {\n [K in keyof T]: [K, T[K]]\n}[keyof T]\n\n/**\n * A type-safe wrapper around `Object.entries()` that preserves the exact types of object keys\n * and values. Unlike the standard `Object.entries()` which returns `[string, any][]`, this\n * function returns an array of tuples where each tuple is precisely typed according to the\n * input object's structure.\n *\n * This is particularly useful when working with objects that have known, fixed property types\n * and you want to maintain type safety when iterating over entries.\n *\n * @example\n * ```typescript\n * const myObject = { a: 1, b: 'hello', c: true } as const\n * const entries = objectEntries(myObject)\n * // Type: ([\"a\", 1] | [\"b\", \"hello\"] | [\"c\", true])[]\n *\n * for (const [key, value] of entries) {\n * // key is typed as \"a\" | \"b\" | \"c\"\n * // value is typed as 1 | \"hello\" | true\n * console.log(`${key}: ${value}`)\n * }\n * ```\n *\n * @example\n * ```typescript\n * interface User {\n * name: string\n * age: number\n * active: boolean\n * }\n *\n * const user: User = { name: 'Alice', age: 30, active: true }\n * const entries = objectEntries(user)\n * // Type: ([\"name\", string] | [\"age\", number] | [\"active\", boolean])[]\n * ```\n *\n * @public\n */\nexport const objectEntries: <T extends Record<string, any>>(\n obj: T,\n) => ObjectEntries<T>[] = Object.entries\n","/**\n * @internal\n */\nexport function objectGroupByPolyfill<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n const result: Partial<Record<K, T[]>> = {}\n let index = 0\n for (const item of items) {\n const key = keySelector(item, index)\n const group = result[key]\n if (group) {\n group.push(item)\n } else {\n result[key] = [item]\n }\n index++\n }\n return result\n}\n\n/**\n * @internal\n */\nexport function objectGroupByNative<K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n): Partial<Record<K, T[]>> {\n return Object.groupBy(items, keySelector)\n}\n\nconst hasObjectGroupBy: boolean = /* @__PURE__ */ (() => !!Object.groupBy)()\n\n/**\n * A polyfill for the `Object.groupBy` static method.\n *\n * @public\n */\nexport const objectGroupBy: <K extends PropertyKey, T>(\n items: Iterable<T>,\n keySelector: (item: T, index: number) => K,\n) => Partial<Record<K, T[]>> = hasObjectGroupBy\n ? objectGroupByNative\n : objectGroupByPolyfill\n","/**\n * Creates a function that will only execute the provided function once.\n * Subsequent calls will return the cached result from the first execution.\n *\n * @param fn The function to execute once\n * @returns A function that will only execute the provided function once\n * @example\n * ```ts\n * const getValue = once(() => expensiveOperation())\n * getValue() // executes expensiveOperation\n * getValue() // returns cached result\n * ```\n */\nexport function once<T>(fn: () => T): () => T {\n let called = false\n let result: T\n return () => {\n if (!called) {\n result = fn()\n called = true\n // @ts-expect-error - micro-optimization for memory management\n fn = undefined\n }\n return result\n }\n}\n","/**\n * Sleep for a given number of milliseconds.\n */\nexport function sleep(ms: number): Promise<void> {\n return new Promise((resolve) => setTimeout(resolve, ms))\n}\n"],"mappings":"AAGA,SAAgB,SACd,OACoD;AACpD,QAAwB,OAAO,SAAU,cAAlC;;AAMT,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,IAET,KAAK,KAAA,IAEA;;ACpBX,SAAgB,MAAM,IAA2B;AAC/C,QAAO,IAAI,SAAS,YAAY,WAAW,SAAS,GAAG,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ocavue/utils",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.3.0",
|
|
5
5
|
"description": "A collection of utility functions for the browser and other environments",
|
|
6
6
|
"author": "ocavue <ocavue@gmail.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -37,20 +37,23 @@
|
|
|
37
37
|
"dist"
|
|
38
38
|
],
|
|
39
39
|
"devDependencies": {
|
|
40
|
-
"@ocavue/eslint-config": "^3.
|
|
41
|
-
"@ocavue/tsconfig": "^0.6.
|
|
42
|
-
"@size-limit/preset-small-lib": "^
|
|
40
|
+
"@ocavue/eslint-config": "^3.8.0",
|
|
41
|
+
"@ocavue/tsconfig": "^0.6.2",
|
|
42
|
+
"@size-limit/preset-small-lib": "^12.0.0",
|
|
43
43
|
"@types/node": "^24.9.2",
|
|
44
|
-
"@vitest/coverage-v8": "4.0.
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
"
|
|
50
|
-
"
|
|
44
|
+
"@vitest/coverage-v8": "^4.0.16",
|
|
45
|
+
"esbuild": "^0.27.2",
|
|
46
|
+
"eslint": "^9.39.2",
|
|
47
|
+
"jsdom": "^27.3.0",
|
|
48
|
+
"pkg-pr-new": "^0.0.62",
|
|
49
|
+
"prettier": "^3.7.4",
|
|
50
|
+
"size-limit": "^12.0.0",
|
|
51
|
+
"tinyexec": "^1.0.2",
|
|
52
|
+
"tinyglobby": "^0.2.15",
|
|
53
|
+
"tsdown": "^0.18.3",
|
|
51
54
|
"typescript": "^5.9.3",
|
|
52
|
-
"vite": "^7.
|
|
53
|
-
"vitest": "^4.0.
|
|
55
|
+
"vite": "^7.3.0",
|
|
56
|
+
"vitest": "^4.0.16"
|
|
54
57
|
},
|
|
55
58
|
"publishConfig": {
|
|
56
59
|
"access": "public"
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
|
+
|
|
3
|
+
exports[`e2e > bundler outputs match snapshot 1`] = `
|
|
4
|
+
"
|
|
5
|
+
################################################################################
|
|
6
|
+
esbuild/fn1.js
|
|
7
|
+
--------------------------------------------------------------------------------
|
|
8
|
+
var s = 2 ** 53 - 1
|
|
9
|
+
function e(t) {
|
|
10
|
+
let n = !1,
|
|
11
|
+
r
|
|
12
|
+
return () => (n || ((r = t()), (n = !0), (t = void 0)), r)
|
|
13
|
+
}
|
|
14
|
+
function o() {
|
|
15
|
+
console.log('function_1')
|
|
16
|
+
}
|
|
17
|
+
function u() {
|
|
18
|
+
console.log('function_2')
|
|
19
|
+
}
|
|
20
|
+
var l = e(u)
|
|
21
|
+
function i() {
|
|
22
|
+
console.log('function_3')
|
|
23
|
+
}
|
|
24
|
+
var p = e(i)
|
|
25
|
+
export { o as fn1 }
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
################################################################################
|
|
29
|
+
esbuild/fn2.js
|
|
30
|
+
--------------------------------------------------------------------------------
|
|
31
|
+
var i = 2 ** 53 - 1
|
|
32
|
+
function e(t) {
|
|
33
|
+
let n = !1,
|
|
34
|
+
r
|
|
35
|
+
return () => (n || ((r = t()), (n = !0), (t = void 0)), r)
|
|
36
|
+
}
|
|
37
|
+
function o() {
|
|
38
|
+
console.log('function_2')
|
|
39
|
+
}
|
|
40
|
+
var f = e(o)
|
|
41
|
+
function u() {
|
|
42
|
+
console.log('function_3')
|
|
43
|
+
}
|
|
44
|
+
var l = e(u)
|
|
45
|
+
export { o as fn2 }
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
################################################################################
|
|
49
|
+
esbuild/fn3.js
|
|
50
|
+
--------------------------------------------------------------------------------
|
|
51
|
+
var i = 2 ** 53 - 1
|
|
52
|
+
function e(t) {
|
|
53
|
+
let n = !1,
|
|
54
|
+
r
|
|
55
|
+
return () => (n || ((r = t()), (n = !0), (t = void 0)), r)
|
|
56
|
+
}
|
|
57
|
+
function u() {
|
|
58
|
+
console.log('function_2')
|
|
59
|
+
}
|
|
60
|
+
var f = e(u)
|
|
61
|
+
function o() {
|
|
62
|
+
console.log('function_3')
|
|
63
|
+
}
|
|
64
|
+
var l = e(o)
|
|
65
|
+
export { o as fn3 }
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
################################################################################
|
|
69
|
+
esbuild/fn4.js
|
|
70
|
+
--------------------------------------------------------------------------------
|
|
71
|
+
var s = 2 ** 53 - 1
|
|
72
|
+
function e(t) {
|
|
73
|
+
let n = !1,
|
|
74
|
+
r
|
|
75
|
+
return () => (n || ((r = t()), (n = !0), (t = void 0)), r)
|
|
76
|
+
}
|
|
77
|
+
function o() {
|
|
78
|
+
console.log('function_2')
|
|
79
|
+
}
|
|
80
|
+
var l = e(o)
|
|
81
|
+
function u() {
|
|
82
|
+
console.log('function_3')
|
|
83
|
+
}
|
|
84
|
+
var p = e(u)
|
|
85
|
+
function i() {
|
|
86
|
+
console.log('function_4')
|
|
87
|
+
}
|
|
88
|
+
export { i as fn4 }
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
################################################################################
|
|
92
|
+
rolldown/fn1.js
|
|
93
|
+
--------------------------------------------------------------------------------
|
|
94
|
+
function e() {
|
|
95
|
+
console.log(\`function_1\`)
|
|
96
|
+
}
|
|
97
|
+
export { e as fn1 }
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
################################################################################
|
|
101
|
+
rolldown/fn2.js
|
|
102
|
+
--------------------------------------------------------------------------------
|
|
103
|
+
function e() {
|
|
104
|
+
console.log(\`function_2\`)
|
|
105
|
+
}
|
|
106
|
+
export { e as fn2 }
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
################################################################################
|
|
110
|
+
rolldown/fn3.js
|
|
111
|
+
--------------------------------------------------------------------------------
|
|
112
|
+
function e() {
|
|
113
|
+
console.log(\`function_3\`)
|
|
114
|
+
}
|
|
115
|
+
export { e as fn3 }
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
################################################################################
|
|
119
|
+
rolldown/fn4.js
|
|
120
|
+
--------------------------------------------------------------------------------
|
|
121
|
+
function e() {
|
|
122
|
+
console.log(\`function_4\`)
|
|
123
|
+
}
|
|
124
|
+
export { e as fn4 }
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
################################################################################
|
|
128
|
+
rollup/fn1.js
|
|
129
|
+
--------------------------------------------------------------------------------
|
|
130
|
+
// A function that is not wrapped by \`once()\`
|
|
131
|
+
function fn1() {
|
|
132
|
+
console.log('function_1')
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export { fn1 }
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
################################################################################
|
|
139
|
+
rollup/fn2.js
|
|
140
|
+
--------------------------------------------------------------------------------
|
|
141
|
+
// A function that is wrapped by \`once()\`, but the wrapper is not exported
|
|
142
|
+
function fn2() {
|
|
143
|
+
console.log('function_2')
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export { fn2 }
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
################################################################################
|
|
150
|
+
rollup/fn3.js
|
|
151
|
+
--------------------------------------------------------------------------------
|
|
152
|
+
// A function that is wrapped by \`once()\`, and the wrapper is exported
|
|
153
|
+
function fn3() {
|
|
154
|
+
console.log('function_3')
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
export { fn3 }
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
################################################################################
|
|
161
|
+
rollup/fn4.js
|
|
162
|
+
--------------------------------------------------------------------------------
|
|
163
|
+
// A function that is wrapped by \`once()\`, but the wrapper is marked as \`__PURE__\`
|
|
164
|
+
function fn4() {
|
|
165
|
+
console.log('function_4')
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export { fn4 }
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
################################################################################
|
|
172
|
+
rslib/fn1.js
|
|
173
|
+
--------------------------------------------------------------------------------
|
|
174
|
+
function n(n) {
|
|
175
|
+
let o = !1,
|
|
176
|
+
c
|
|
177
|
+
return () => (o || ((c = n()), (o = !0), (n = void 0)), c)
|
|
178
|
+
}
|
|
179
|
+
function o() {
|
|
180
|
+
console.log('function_1')
|
|
181
|
+
}
|
|
182
|
+
;(n(function () {
|
|
183
|
+
console.log('function_2')
|
|
184
|
+
}),
|
|
185
|
+
n(function () {
|
|
186
|
+
console.log('function_3')
|
|
187
|
+
}))
|
|
188
|
+
export { o as fn1 }
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
################################################################################
|
|
192
|
+
rslib/fn2.js
|
|
193
|
+
--------------------------------------------------------------------------------
|
|
194
|
+
function n(n) {
|
|
195
|
+
let o = !1,
|
|
196
|
+
t
|
|
197
|
+
return () => (o || ((t = n()), (o = !0), (n = void 0)), t)
|
|
198
|
+
}
|
|
199
|
+
function o() {
|
|
200
|
+
console.log('function_2')
|
|
201
|
+
}
|
|
202
|
+
;(n(o),
|
|
203
|
+
n(function () {
|
|
204
|
+
console.log('function_3')
|
|
205
|
+
}))
|
|
206
|
+
export { o as fn2 }
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
################################################################################
|
|
210
|
+
rslib/fn3.js
|
|
211
|
+
--------------------------------------------------------------------------------
|
|
212
|
+
function n(n) {
|
|
213
|
+
let o = !1,
|
|
214
|
+
t
|
|
215
|
+
return () => (o || ((t = n()), (o = !0), (n = void 0)), t)
|
|
216
|
+
}
|
|
217
|
+
function o() {
|
|
218
|
+
console.log('function_3')
|
|
219
|
+
}
|
|
220
|
+
;(n(function () {
|
|
221
|
+
console.log('function_2')
|
|
222
|
+
}),
|
|
223
|
+
n(o))
|
|
224
|
+
export { o as fn3 }
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
################################################################################
|
|
228
|
+
rslib/fn4.js
|
|
229
|
+
--------------------------------------------------------------------------------
|
|
230
|
+
function n(n) {
|
|
231
|
+
let o = !1,
|
|
232
|
+
c
|
|
233
|
+
return () => (o || ((c = n()), (o = !0), (n = void 0)), c)
|
|
234
|
+
}
|
|
235
|
+
function o() {
|
|
236
|
+
console.log('function_4')
|
|
237
|
+
}
|
|
238
|
+
;(n(function () {
|
|
239
|
+
console.log('function_2')
|
|
240
|
+
}),
|
|
241
|
+
n(function () {
|
|
242
|
+
console.log('function_3')
|
|
243
|
+
}))
|
|
244
|
+
export { o as fn4 }
|
|
245
|
+
|
|
246
|
+
"
|
|
247
|
+
`;
|
package/src/default-map.test.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import { describe, it, expect } from 'vitest'
|
|
4
4
|
|
|
5
|
-
import { DefaultMap } from './default-map'
|
|
5
|
+
import { Counter, DefaultMap, DefaultWeakMap, WeakCounter } from './default-map'
|
|
6
6
|
|
|
7
7
|
describe('DefaultMap', () => {
|
|
8
8
|
it('creates default values for missing keys', () => {
|
|
@@ -184,3 +184,292 @@ describe('DefaultMap', () => {
|
|
|
184
184
|
expect(map.get('group2').get('item1')).toBe(0)
|
|
185
185
|
})
|
|
186
186
|
})
|
|
187
|
+
|
|
188
|
+
describe('DefaultWeakMap', () => {
|
|
189
|
+
it('creates default values for missing keys', () => {
|
|
190
|
+
const map = new DefaultWeakMap<object, number>(() => 0)
|
|
191
|
+
const key1 = {}
|
|
192
|
+
const key2 = {}
|
|
193
|
+
|
|
194
|
+
expect(map.get(key1)).toBe(0)
|
|
195
|
+
expect(map.get(key2)).toBe(0)
|
|
196
|
+
})
|
|
197
|
+
|
|
198
|
+
it('returns existing values for set keys', () => {
|
|
199
|
+
const map = new DefaultWeakMap<object, number>(() => 0)
|
|
200
|
+
const key = {}
|
|
201
|
+
|
|
202
|
+
map.set(key, 42)
|
|
203
|
+
expect(map.get(key)).toBe(42)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
it('stores the default value when accessing missing key', () => {
|
|
207
|
+
const map = new DefaultWeakMap<object, number>(() => 5)
|
|
208
|
+
const key = {}
|
|
209
|
+
|
|
210
|
+
const value = map.get(key)
|
|
211
|
+
expect(value).toBe(5)
|
|
212
|
+
expect(map.has(key)).toBe(true)
|
|
213
|
+
expect(map.get(key)).toBe(5)
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('works with array factory', () => {
|
|
217
|
+
const map = new DefaultWeakMap<object, string[]>(() => [])
|
|
218
|
+
const key1 = {}
|
|
219
|
+
const key2 = {}
|
|
220
|
+
|
|
221
|
+
map.get(key1).push('item1')
|
|
222
|
+
map.get(key1).push('item2')
|
|
223
|
+
map.get(key2).push('item3')
|
|
224
|
+
|
|
225
|
+
expect(map.get(key1)).toEqual(['item1', 'item2'])
|
|
226
|
+
expect(map.get(key2)).toEqual(['item3'])
|
|
227
|
+
})
|
|
228
|
+
|
|
229
|
+
it('accepts initial entries', () => {
|
|
230
|
+
const key1 = {}
|
|
231
|
+
const key2 = {}
|
|
232
|
+
const key3 = {}
|
|
233
|
+
const initialEntries: [object, number][] = [
|
|
234
|
+
[key1, 1],
|
|
235
|
+
[key2, 2],
|
|
236
|
+
[key3, 3],
|
|
237
|
+
]
|
|
238
|
+
const map = new DefaultWeakMap<object, number>(() => 0, initialEntries)
|
|
239
|
+
|
|
240
|
+
expect(map.get(key1)).toBe(1)
|
|
241
|
+
expect(map.get(key2)).toBe(2)
|
|
242
|
+
expect(map.get(key3)).toBe(3)
|
|
243
|
+
|
|
244
|
+
const key4 = {}
|
|
245
|
+
expect(map.get(key4)).toBe(0)
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
it('calls factory function only when key is missing', () => {
|
|
249
|
+
let callCount = 0
|
|
250
|
+
const map = new DefaultWeakMap<object, number>(() => {
|
|
251
|
+
callCount++
|
|
252
|
+
return 42
|
|
253
|
+
})
|
|
254
|
+
const existing = {}
|
|
255
|
+
const newKey = {}
|
|
256
|
+
|
|
257
|
+
map.set(existing, 100)
|
|
258
|
+
|
|
259
|
+
map.get(existing)
|
|
260
|
+
expect(callCount).toBe(0)
|
|
261
|
+
|
|
262
|
+
map.get(newKey)
|
|
263
|
+
expect(callCount).toBe(1)
|
|
264
|
+
|
|
265
|
+
map.get(newKey)
|
|
266
|
+
expect(callCount).toBe(1)
|
|
267
|
+
})
|
|
268
|
+
|
|
269
|
+
it('works with delete method', () => {
|
|
270
|
+
const map = new DefaultWeakMap<object, number>(() => 10)
|
|
271
|
+
const key = {}
|
|
272
|
+
|
|
273
|
+
map.get(key)
|
|
274
|
+
expect(map.has(key)).toBe(true)
|
|
275
|
+
|
|
276
|
+
map.delete(key)
|
|
277
|
+
expect(map.has(key)).toBe(false)
|
|
278
|
+
|
|
279
|
+
const value = map.get(key)
|
|
280
|
+
expect(value).toBe(10)
|
|
281
|
+
expect(map.has(key)).toBe(true)
|
|
282
|
+
})
|
|
283
|
+
})
|
|
284
|
+
|
|
285
|
+
describe('Counter', () => {
|
|
286
|
+
it('initializes counts to 0', () => {
|
|
287
|
+
const counter = new Counter<string>()
|
|
288
|
+
|
|
289
|
+
expect(counter.get('key1')).toBe(0)
|
|
290
|
+
expect(counter.get('key2')).toBe(0)
|
|
291
|
+
})
|
|
292
|
+
|
|
293
|
+
it('increments counts', () => {
|
|
294
|
+
const counter = new Counter<string>()
|
|
295
|
+
|
|
296
|
+
counter.increment('key1')
|
|
297
|
+
expect(counter.get('key1')).toBe(1)
|
|
298
|
+
|
|
299
|
+
counter.increment('key1')
|
|
300
|
+
expect(counter.get('key1')).toBe(2)
|
|
301
|
+
|
|
302
|
+
counter.increment('key2')
|
|
303
|
+
expect(counter.get('key2')).toBe(1)
|
|
304
|
+
})
|
|
305
|
+
|
|
306
|
+
it('increments by custom amounts', () => {
|
|
307
|
+
const counter = new Counter<string>()
|
|
308
|
+
|
|
309
|
+
counter.increment('key1', 5)
|
|
310
|
+
expect(counter.get('key1')).toBe(5)
|
|
311
|
+
|
|
312
|
+
counter.increment('key1', 3)
|
|
313
|
+
expect(counter.get('key1')).toBe(8)
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it('decrements counts', () => {
|
|
317
|
+
const counter = new Counter<string>()
|
|
318
|
+
|
|
319
|
+
counter.set('key1', 10)
|
|
320
|
+
counter.decrement('key1')
|
|
321
|
+
expect(counter.get('key1')).toBe(9)
|
|
322
|
+
|
|
323
|
+
counter.decrement('key1')
|
|
324
|
+
expect(counter.get('key1')).toBe(8)
|
|
325
|
+
})
|
|
326
|
+
|
|
327
|
+
it('decrements by custom amounts', () => {
|
|
328
|
+
const counter = new Counter<string>()
|
|
329
|
+
|
|
330
|
+
counter.set('key1', 10)
|
|
331
|
+
counter.decrement('key1', 3)
|
|
332
|
+
expect(counter.get('key1')).toBe(7)
|
|
333
|
+
|
|
334
|
+
counter.decrement('key1', 2)
|
|
335
|
+
expect(counter.get('key1')).toBe(5)
|
|
336
|
+
})
|
|
337
|
+
|
|
338
|
+
it('allows negative counts', () => {
|
|
339
|
+
const counter = new Counter<string>()
|
|
340
|
+
|
|
341
|
+
counter.decrement('key1')
|
|
342
|
+
expect(counter.get('key1')).toBe(-1)
|
|
343
|
+
|
|
344
|
+
counter.decrement('key1', 5)
|
|
345
|
+
expect(counter.get('key1')).toBe(-6)
|
|
346
|
+
})
|
|
347
|
+
|
|
348
|
+
it('accepts initial entries', () => {
|
|
349
|
+
const initialEntries: [string, number][] = [
|
|
350
|
+
['a', 5],
|
|
351
|
+
['b', 10],
|
|
352
|
+
['c', 15],
|
|
353
|
+
]
|
|
354
|
+
const counter = new Counter<string>(initialEntries)
|
|
355
|
+
|
|
356
|
+
expect(counter.get('a')).toBe(5)
|
|
357
|
+
expect(counter.get('b')).toBe(10)
|
|
358
|
+
expect(counter.get('c')).toBe(15)
|
|
359
|
+
expect(counter.get('d')).toBe(0)
|
|
360
|
+
})
|
|
361
|
+
|
|
362
|
+
it('works with all Map methods', () => {
|
|
363
|
+
const counter = new Counter<string>()
|
|
364
|
+
|
|
365
|
+
counter.increment('key1')
|
|
366
|
+
counter.increment('key2', 2)
|
|
367
|
+
|
|
368
|
+
expect(counter.size).toBe(2)
|
|
369
|
+
expect(counter.has('key1')).toBe(true)
|
|
370
|
+
expect([...counter.keys()]).toEqual(['key1', 'key2'])
|
|
371
|
+
expect([...counter.values()]).toEqual([1, 2])
|
|
372
|
+
})
|
|
373
|
+
})
|
|
374
|
+
|
|
375
|
+
describe('WeakCounter', () => {
|
|
376
|
+
it('initializes counts to 0', () => {
|
|
377
|
+
const counter = new WeakCounter<object>()
|
|
378
|
+
const key1 = {}
|
|
379
|
+
const key2 = {}
|
|
380
|
+
|
|
381
|
+
expect(counter.get(key1)).toBe(0)
|
|
382
|
+
expect(counter.get(key2)).toBe(0)
|
|
383
|
+
})
|
|
384
|
+
|
|
385
|
+
it('increments counts', () => {
|
|
386
|
+
const counter = new WeakCounter<object>()
|
|
387
|
+
const key = {}
|
|
388
|
+
|
|
389
|
+
counter.increment(key)
|
|
390
|
+
expect(counter.get(key)).toBe(1)
|
|
391
|
+
|
|
392
|
+
counter.increment(key)
|
|
393
|
+
expect(counter.get(key)).toBe(2)
|
|
394
|
+
})
|
|
395
|
+
|
|
396
|
+
it('increments by custom amounts', () => {
|
|
397
|
+
const counter = new WeakCounter<object>()
|
|
398
|
+
const key = {}
|
|
399
|
+
|
|
400
|
+
counter.increment(key, 5)
|
|
401
|
+
expect(counter.get(key)).toBe(5)
|
|
402
|
+
|
|
403
|
+
counter.increment(key, 3)
|
|
404
|
+
expect(counter.get(key)).toBe(8)
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
it('decrements counts', () => {
|
|
408
|
+
const counter = new WeakCounter<object>()
|
|
409
|
+
const key = {}
|
|
410
|
+
|
|
411
|
+
counter.set(key, 10)
|
|
412
|
+
counter.decrement(key)
|
|
413
|
+
expect(counter.get(key)).toBe(9)
|
|
414
|
+
|
|
415
|
+
counter.decrement(key)
|
|
416
|
+
expect(counter.get(key)).toBe(8)
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
it('decrements by custom amounts', () => {
|
|
420
|
+
const counter = new WeakCounter<object>()
|
|
421
|
+
const key = {}
|
|
422
|
+
|
|
423
|
+
counter.set(key, 10)
|
|
424
|
+
counter.decrement(key, 3)
|
|
425
|
+
expect(counter.get(key)).toBe(7)
|
|
426
|
+
|
|
427
|
+
counter.decrement(key, 2)
|
|
428
|
+
expect(counter.get(key)).toBe(5)
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
it('allows negative counts', () => {
|
|
432
|
+
const counter = new WeakCounter<object>()
|
|
433
|
+
const key = {}
|
|
434
|
+
|
|
435
|
+
counter.decrement(key)
|
|
436
|
+
expect(counter.get(key)).toBe(-1)
|
|
437
|
+
|
|
438
|
+
counter.decrement(key, 5)
|
|
439
|
+
expect(counter.get(key)).toBe(-6)
|
|
440
|
+
})
|
|
441
|
+
|
|
442
|
+
it('accepts initial entries', () => {
|
|
443
|
+
const key1 = {}
|
|
444
|
+
const key2 = {}
|
|
445
|
+
const key3 = {}
|
|
446
|
+
const initialEntries: [object, number][] = [
|
|
447
|
+
[key1, 5],
|
|
448
|
+
[key2, 10],
|
|
449
|
+
[key3, 15],
|
|
450
|
+
]
|
|
451
|
+
const counter = new WeakCounter<object>(initialEntries)
|
|
452
|
+
|
|
453
|
+
expect(counter.get(key1)).toBe(5)
|
|
454
|
+
expect(counter.get(key2)).toBe(10)
|
|
455
|
+
expect(counter.get(key3)).toBe(15)
|
|
456
|
+
|
|
457
|
+
const key4 = {}
|
|
458
|
+
expect(counter.get(key4)).toBe(0)
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
it('works with WeakMap methods', () => {
|
|
462
|
+
const counter = new WeakCounter<object>()
|
|
463
|
+
const key1 = {}
|
|
464
|
+
const key2 = {}
|
|
465
|
+
|
|
466
|
+
counter.increment(key1)
|
|
467
|
+
counter.increment(key2, 2)
|
|
468
|
+
|
|
469
|
+
expect(counter.has(key1)).toBe(true)
|
|
470
|
+
expect(counter.has(key2)).toBe(true)
|
|
471
|
+
|
|
472
|
+
counter.delete(key1)
|
|
473
|
+
expect(counter.has(key1)).toBe(false)
|
|
474
|
+
})
|
|
475
|
+
})
|