Comparing version 0.0.20240630 to 0.0.20250122
@@ -5,14 +5,14 @@ function asArrayBufferView(T, a) { | ||
} | ||
if (a instanceof ArrayBuffer) { | ||
return new T(a); | ||
if ("buffer" in a) { | ||
return new T(a.buffer, a.byteOffset, a.byteLength); | ||
} | ||
return new T(a.buffer, a.byteOffset, a.byteLength); | ||
return new T(a); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to Uint8Array. */ | ||
/** Convert (Shared)ArrayBuffer(View) to Uint8Array. */ | ||
export function asUint8Array(a) { | ||
return asArrayBufferView(Uint8Array, a); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
/** Convert (Shared)ArrayBuffer(View) to DataView. */ | ||
export function asDataView(a) { | ||
return asArrayBufferView(DataView, a); | ||
} |
@@ -5,14 +5,14 @@ function asArrayBufferView(T, a) { | ||
} | ||
if (a instanceof ArrayBuffer) { | ||
return new T(a); | ||
if ("buffer" in a) { | ||
return new T(a.buffer, a.byteOffset, a.byteLength); | ||
} | ||
return new T(a.buffer, a.byteOffset, a.byteLength); | ||
return new T(a); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to Uint8Array. */ | ||
/** Convert (Shared)ArrayBuffer(View) to Uint8Array. */ | ||
export function asUint8Array(a) { | ||
return asArrayBufferView(Uint8Array, a); | ||
} | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
/** Convert (Shared)ArrayBuffer(View) to DataView. */ | ||
export function asDataView(a) { | ||
return asArrayBufferView(DataView, a); | ||
} |
@@ -1,4 +0,4 @@ | ||
/** Convert ArrayBuffer or ArrayBufferView to Uint8Array. */ | ||
export declare function asUint8Array(a: BufferSource): Uint8Array; | ||
/** Convert ArrayBuffer or ArrayBufferView to DataView. */ | ||
export declare function asDataView(a: BufferSource): DataView; | ||
/** Convert (Shared)ArrayBuffer(View) to Uint8Array. */ | ||
export declare function asUint8Array(a: ArrayBufferLike | ArrayBufferView): Uint8Array; | ||
/** Convert (Shared)ArrayBuffer(View) to DataView. */ | ||
export declare function asDataView(a: ArrayBufferLike | ArrayBufferView): DataView; |
@@ -53,5 +53,5 @@ import "./polyfill_browser.js"; | ||
wait() { | ||
return new Promise((resolve) => { | ||
this.push({ close: () => resolve() }); | ||
}); | ||
const { promise, resolve } = Promise.withResolvers(); | ||
this.push({ close: resolve }); | ||
return promise; | ||
} | ||
@@ -58,0 +58,0 @@ } |
@@ -53,5 +53,5 @@ import "./polyfill_node.js"; | ||
wait() { | ||
return new Promise((resolve) => { | ||
this.push({ close: () => resolve() }); | ||
}); | ||
const { promise, resolve } = Promise.withResolvers(); | ||
this.push({ close: resolve }); | ||
return promise; | ||
} | ||
@@ -58,0 +58,0 @@ } |
@@ -1,2 +0,2 @@ | ||
import assert from "tiny-invariant"; | ||
import { getOrInsert } from "./iter_browser.js"; | ||
/** | ||
@@ -9,16 +9,44 @@ * Keep records on whether an event listener has been added. | ||
* This may allow `EventTarget` subclass to skip certain event generation code paths. | ||
* Tracking is imprecise: it does not consider `once()` and `removeEventListener()`. | ||
* Tracking is imprecise: it does not consider `options.once` and `options.signal`. | ||
*/ | ||
export function trackEventListener(target) { | ||
const maybeHaveEventListener = {}; | ||
const superAddEventListener = target.addEventListener; | ||
assert(superAddEventListener); | ||
Object.defineProperty(target, "addEventListener", { | ||
configurable: true, | ||
value(...args) { | ||
maybeHaveEventListener[args[0]] = true; | ||
superAddEventListener.call(this, ...args); | ||
const m = new Map(); | ||
const { addEventListener, removeEventListener } = target; | ||
Object.defineProperties(target, { | ||
addEventListener: { | ||
configurable: true, | ||
value(...args) { | ||
const [evt, fn] = args; | ||
const record = getOrInsert(m, evt, () => [0, new WeakSet()]); | ||
if (!record[1].has(fn)) { | ||
record[1].add(fn); | ||
++record[0]; | ||
} | ||
addEventListener.call(this, ...args); | ||
}, | ||
}, | ||
removeEventListener: { | ||
configurable: true, | ||
value(...args) { | ||
removeEventListener.call(this, ...args); | ||
const [evt, fn] = args; | ||
const record = m.get(evt); | ||
if (!record) { | ||
return; | ||
} | ||
if (record[1].delete(fn)) { | ||
--record[0]; | ||
} | ||
if (record[0] === 0) { | ||
m.delete(evt); | ||
} | ||
}, | ||
}, | ||
}); | ||
return maybeHaveEventListener; | ||
return new Proxy({}, { | ||
get(target, prop) { | ||
void target; | ||
return m.has(prop); | ||
}, | ||
}); | ||
} |
@@ -1,2 +0,2 @@ | ||
import assert from "tiny-invariant"; | ||
import { getOrInsert } from "./iter_node.js"; | ||
/** | ||
@@ -9,16 +9,44 @@ * Keep records on whether an event listener has been added. | ||
* This may allow `EventTarget` subclass to skip certain event generation code paths. | ||
* Tracking is imprecise: it does not consider `once()` and `removeEventListener()`. | ||
* Tracking is imprecise: it does not consider `options.once` and `options.signal`. | ||
*/ | ||
export function trackEventListener(target) { | ||
const maybeHaveEventListener = {}; | ||
const superAddEventListener = target.addEventListener; | ||
assert(superAddEventListener); | ||
Object.defineProperty(target, "addEventListener", { | ||
configurable: true, | ||
value(...args) { | ||
maybeHaveEventListener[args[0]] = true; | ||
superAddEventListener.call(this, ...args); | ||
const m = new Map(); | ||
const { addEventListener, removeEventListener } = target; | ||
Object.defineProperties(target, { | ||
addEventListener: { | ||
configurable: true, | ||
value(...args) { | ||
const [evt, fn] = args; | ||
const record = getOrInsert(m, evt, () => [0, new WeakSet()]); | ||
if (!record[1].has(fn)) { | ||
record[1].add(fn); | ||
++record[0]; | ||
} | ||
addEventListener.call(this, ...args); | ||
}, | ||
}, | ||
removeEventListener: { | ||
configurable: true, | ||
value(...args) { | ||
removeEventListener.call(this, ...args); | ||
const [evt, fn] = args; | ||
const record = m.get(evt); | ||
if (!record) { | ||
return; | ||
} | ||
if (record[1].delete(fn)) { | ||
--record[0]; | ||
} | ||
if (record[0] === 0) { | ||
m.delete(evt); | ||
} | ||
}, | ||
}, | ||
}); | ||
return maybeHaveEventListener; | ||
return new Proxy({}, { | ||
get(target, prop) { | ||
void target; | ||
return m.has(prop); | ||
}, | ||
}); | ||
} |
@@ -8,4 +8,4 @@ /** | ||
* This may allow `EventTarget` subclass to skip certain event generation code paths. | ||
* Tracking is imprecise: it does not consider `once()` and `removeEventListener()`. | ||
* Tracking is imprecise: it does not consider `options.once` and `options.signal`. | ||
*/ | ||
export declare function trackEventListener(target: EventTarget): Record<string, boolean>; |
@@ -50,2 +50,16 @@ import { __importDefault, __importStar } from "tslib"; | ||
} | ||
/** | ||
* Retrieve or insert value in a Map-like container. | ||
* @param ct - Map-like container. | ||
* @param key - Map key. | ||
* @param make - Function to create the value if needed. | ||
* @returns Existing or newly created value. | ||
*/ | ||
export function getOrInsert(ct, key, make) { | ||
let value = ct.get(key); | ||
if (value === undefined) { | ||
ct.set(key, value = make()); | ||
} | ||
return value; | ||
} | ||
/** Delete keys from a Set or Map until its size is below capacity. */ | ||
@@ -52,0 +66,0 @@ export function evict(capacity, ct) { |
@@ -50,2 +50,16 @@ import { __importDefault, __importStar } from "tslib"; | ||
} | ||
/** | ||
* Retrieve or insert value in a Map-like container. | ||
* @param ct - Map-like container. | ||
* @param key - Map key. | ||
* @param make - Function to create the value if needed. | ||
* @returns Existing or newly created value. | ||
*/ | ||
export function getOrInsert(ct, key, make) { | ||
let value = ct.get(key); | ||
if (value === undefined) { | ||
ct.set(key, value = make()); | ||
} | ||
return value; | ||
} | ||
/** Delete keys from a Set or Map until its size is below capacity. */ | ||
@@ -52,0 +66,0 @@ export function evict(capacity, ct) { |
@@ -35,2 +35,16 @@ import type { AnyIterable } from "streaming-iterables"; | ||
export declare function flatMapOnce<T, R>(f: (item: T) => AnyIterable<R>, iterable: AnyIterable<T>): AsyncIterable<R>; | ||
/** | ||
* Retrieve or insert value in a Map-like container. | ||
* @param ct - Map-like container. | ||
* @param key - Map key. | ||
* @param make - Function to create the value if needed. | ||
* @returns Existing or newly created value. | ||
*/ | ||
export declare function getOrInsert<C extends getOrInsert.Container>(ct: C, key: Parameters<C["get"]>[0] & Parameters<C["set"]>[0], make: () => Parameters<C["set"]>[1]): Parameters<C["set"]>[1]; | ||
export declare namespace getOrInsert { | ||
interface Container { | ||
get: (key: any) => any | undefined; | ||
set: (key: any, value: any) => void; | ||
} | ||
} | ||
/** Delete keys from a Set or Map until its size is below capacity. */ | ||
@@ -37,0 +51,0 @@ export declare function evict<K>(capacity: number, ct: evict.Container<K>): void; |
@@ -0,1 +1,2 @@ | ||
const emptySet = new Set(); | ||
/** | ||
@@ -63,3 +64,3 @@ * Map that transforms keys. | ||
list(key) { | ||
return this.m.get(key) ?? new Set(); | ||
return this.m.get(key) ?? emptySet; | ||
} | ||
@@ -66,0 +67,0 @@ /** |
@@ -0,1 +1,2 @@ | ||
const emptySet = new Set(); | ||
/** | ||
@@ -63,3 +64,3 @@ * Map that transforms keys. | ||
list(key) { | ||
return this.m.get(key) ?? new Set(); | ||
return this.m.get(key) ?? emptySet; | ||
} | ||
@@ -66,0 +67,0 @@ /** |
@@ -1,1 +0,1 @@ | ||
export {}; | ||
import "@ungap/with-resolvers"; |
@@ -1,1 +0,1 @@ | ||
export {}; | ||
import "@ungap/with-resolvers"; // needed for Node 20.x |
/** | ||
* Create a random jitter generator function. | ||
* @param r - Jitter factor around 1. | ||
* @param r - Jitter factor around median. | ||
* @param x - Median value. | ||
@@ -5,0 +5,0 @@ * @returns Jitter generator function. |
/** | ||
* Create a random jitter generator function. | ||
* @param r - Jitter factor around 1. | ||
* @param r - Jitter factor around median. | ||
* @param x - Median value. | ||
@@ -5,0 +5,0 @@ * @returns Jitter generator function. |
/** | ||
* Create a random jitter generator function. | ||
* @param r - Jitter factor around 1. | ||
* @param r - Jitter factor around median. | ||
* @param x - Median value. | ||
@@ -5,0 +5,0 @@ * @returns Jitter generator function. |
{ | ||
"name": "@ndn/util", | ||
"version": "0.0.20240630", | ||
"version": "0.0.20250122", | ||
"description": "NDNts: general utilities", | ||
@@ -28,13 +28,14 @@ "keywords": [ | ||
"@shigen/polyfill-symbol-dispose": "^1.0.1", | ||
"@ungap/with-resolvers": "^0.1.0", | ||
"event-iterator": "^2.0.0", | ||
"streaming-iterables": "^8.0.1", | ||
"tiny-invariant": "^1.3.3", | ||
"tslib": "^2.6.3", | ||
"type-fest": "^4.20.1", | ||
"tslib": "^2.8.1", | ||
"type-fest": "^4.33.0", | ||
"wait-your-turn": "^1.0.1" | ||
}, | ||
"engines": { | ||
"node": "^20.12.0 || ^22.0.0" | ||
"node": "^20.18.0 || ^22.11.0 || ^23.0.0" | ||
}, | ||
"types": "lib/mod.d.ts" | ||
} |
Sorry, the diff of this file is not supported yet
46085
1402
8
+ Added@ungap/with-resolvers@^0.1.0
+ Added@ungap/with-resolvers@0.1.0(transitive)
Updatedtslib@^2.8.1
Updatedtype-fest@^4.33.0