UNPKG

@thi.ng/bidir-index

Version:

Bi-directional index mapping arbitrary keys to numeric IDs & vice versa

128 lines 4.2 kB
import type { BidirIndex } from "./bidir-index.js"; /** * Encodes given object into an array, using the given `index` to determine each * key's value position. The array will be pre-filled with `defaultValue`. * Unless `indexKeys` is disabled, the object's keys are first added to the * index. * * @remarks * Also see {@link decodeObject} for reverse operation. * * @example * ```ts tangle:../export/encode-object.ts * import { defBidirIndex, encodeObject } from "@thi.ng/bidir-index"; * * const index = defBidirIndex<string>(); * * console.log( * encodeObject(index, { r: 255, g: 128, b: 64, a: 1 }, 0) * ); * // [255, 128, 64, 1] * * // encode without updating index * console.log( * encodeObject(index, { b: 3, r: 1, g: 2 }, 0, false) * ); * // [1, 2, 3, 0] (key `a` uses default) * ``` * * @param index * @param obj * @param defaultValue * @param indexKeys */ export declare const encodeObject: <V, K extends string = string>(index: BidirIndex<K>, obj: Partial<Record<K, V>>, defaultValue: V, indexKeys?: boolean) => V[]; /** * Similar to {@link encodeObject}, but implemented as an iterator for * processing multiple objects into a single flat iterable. * * @example * ```ts tangle:../export/encode-object-iterator.ts * import { defBidirIndex, encodeObjectIterator } from "@thi.ng/bidir-index"; * * const index = defBidirIndex<string>(); * * // source data objects * const data = [ * { r: 1, g: 2, b: 3}, * { x: 4, y: 5, z: 6} * ]; * * // directly encode into a typedarray * const buf = new Uint8Array(encodeObjectIterator(index, data, 0)); * * console.log(buf); * // Uint8Array(12) [ 1, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6 ] * ``` * * @param index * @param objects * @param defaultValue * @param indexKeys */ export declare const encodeObjectIterator: <V, K extends string = string>(index: BidirIndex<K>, objects: Partial<Record<K, V>>[], defaultValue: V, indexKeys?: boolean) => Generator<V, void, unknown>; /** * Reverse op of {@link encodeObject}. Takes an array of `values` and returns an * object with values mapped to keys based on their indexed position. If the * `values` array has a nullish value for a keyed index, the optionally provided * `defaults` object will be used to obtain a value. The result object will only * have keys with non-nullish values. * * @remarks * Note: Irrespective of original key type used for this index instance, the * keys in the result object will be strings. * * Also see {@link encodeObject}. * * @example * ```ts tangle:../export/decode-object.ts * import { defBidirIndex, decodeObject } from "@thi.ng/bidir-index"; * * const index = defBidirIndex<string>(); * index.addAll(["r", "g", "b", "a", "foo"]); * * // decode with defaults/fallback * console.log( * decodeObject(index, [255, 128, 64], { a: 1 }) * ); * // { r: 255, g: 128, b: 64, a: 1 } (key `foo` is omitted in result) * * console.log( * decodeObject(index, [null, null, null, null, "bar"]) * ); * // { foo: "bar" } * ``` * * @param values * @param defaults */ export declare const decodeObject: <V, K extends string = string>(index: Iterable<[K, number]>, values: V[], defaults?: Partial<Record<K, V>>) => Partial<Record<K, V>>; /** * Reverse op of {@link encodeObjectIterator}. An iterator which takes an array * of encoded values and yields sequence of objects decoded via * {@link decodeObject}. * * @example * ```ts tangle:../export/decode-object-iterator.ts * import { defBidirIndex, decodeObjectIterator } from "@thi.ng/bidir-index"; * * const index = defBidirIndex<string>(); * index.addAll("rgbxyz"); * * const data = [1, 2, 3, 0, 0, 0, 0, 0, 0, 4, 5, 6]; * * for(let obj of decodeObjectIterator(index, data, 6)) { * console.log(obj); * } * * // { r: 1, g: 2, b: 3, x: 0, y: 0, z: 0 } * // { r: 0, g: 0, b: 0, x: 4, y: 5, z: 6 } * ``` * * @param index * @param values * @param size * @param defaults */ export declare function decodeObjectIterator<V, K extends string = string>(index: Iterable<[K, number]>, values: V[], size: number, defaults?: Partial<Record<K, V>>): Generator<Partial<Record<K, V>>, void, unknown>; //# sourceMappingURL=encode.d.ts.map