@thi.ng/bidir-index
v1.5.4
Published
Bi-directional index mapping arbitrary keys to numeric IDs & vice versa
Downloads
430
Maintainers
Readme
[!NOTE] This is one of 214 standalone projects, maintained as part of the @thi.ng/umbrella ecosystem and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me. Thank you! ❤️
About
Bi-directional index mapping arbitrary keys to numeric IDs & vice versa.
This package contains functionality which was previously part of and has been extracted from the @thi.ng/associative package.
Status
STABLE - used in production
Search or submit any issues for this package
Related packages
- @thi.ng/associative - ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
Installation
yarn add @thi.ng/bidir-indexESM import:
import * as bi from "@thi.ng/bidir-index";Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/bidir-index"></script>For Node.js REPL:
const bi = await import("@thi.ng/bidir-index");Package sizes (brotli'd, pre-treeshake): ESM: 1.04 KB
Dependencies
Note: @thi.ng/api is in most cases a type-only import (not used at runtime)
API
TODO
Basic usage example
import { defBidirIndex, encodeObject, decodeObject } from "@thi.ng/bidir-index";
const index = defBidirIndex<string>();
// given object keys are auto-indexed, array initialized with given default
console.log(
encodeObject(index, { r: 1, g: 2, b: 3, a: 4 }, 0)
);
// [1, 2, 3, 4]
// use custom default and without updating index
console.log(
encodeObject(index, { b: 3, r: 1, g: 2 }, -1, false)
);
// [1, 2, 3, -1] (missing key `a` mapped to given default value)
// decode with defaults/fallback
console.log(
decodeObject(index, [255, 128, 64], { a: 1 })
);
// { r: 255, g: 128, b: 64, a: 1 }
// add more keys to index (already known ones will be skipped)
// returns array of mapped IDs for given keys
index.addAll(["r", "g", "b", "a", "foo"]);
// [0, 1, 2, 3, 4]
// decoding will skip nullish values
console.log(
decodeObject(index, [null, null, null, null, "bar"])
);
// { foo: "bar" }Authors
If this project contributes to an academic publication, please cite it as:
@misc{thing-bidir-index,
title = "@thi.ng/bidir-index",
author = "Karsten Schmidt",
note = "https://thi.ng/bidir-index",
year = 2022
}License
© 2022 - 2026 Karsten Schmidt // Apache License 2.0
