https://github.com/webreflection/indexed-values
An ever growing Set based utility to optimize JSON, IndexedDB, or postMessage.
https://github.com/webreflection/indexed-values
indexeddb json postmessage
Last synced: about 2 months ago
JSON representation
An ever growing Set based utility to optimize JSON, IndexedDB, or postMessage.
- Host: GitHub
- URL: https://github.com/webreflection/indexed-values
- Owner: WebReflection
- Created: 2021-09-07T08:40:01.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-09-22T08:55:24.000Z (over 3 years ago)
- Last Synced: 2024-12-31T15:24:57.258Z (5 months ago)
- Topics: indexeddb, json, postmessage
- Language: JavaScript
- Homepage: https://webreflection.medium.com/optimizing-json-indexeddb-or-postmessage-698112860899
- Size: 58.6 KB
- Stars: 21
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# indexed-values
[](https://github.com/WebReflection/indexed-values/actions) [](https://coveralls.io/github/WebReflection/indexed-values?branch=main)
**Social Media Photo by [Alexander Sinn](https://unsplash.com/@swimstaralex) on [Unsplash](https://unsplash.com/)**
[Related Blog Post](https://webreflection.medium.com/optimizing-json-indexeddb-or-postmessage-698112860899).
### Example
```js
import {IndexedValues} from 'indexed-values';// create a Set of unique values
const main = new IndexedValues;// derive a set
const sub1 = main.bindValues(["a", "b", "c"]);
const sub2 = main.bindValues(["a"]);// derived sets can make the main Set grow
if (!sub2.has("d"))
sub2.add("d");// {"main":["a","b","c","d"],"data":[[0,1,2],[0,3]]}
console.log(JSON.stringify({
main,
data: [sub1, sub2]
}));
```## API
The `IndexedValues` constructor extends the `Set` class, but it's a facade with the following peculiarities:
* it is not posible to `delete` a single value, or `clear` the instance. `IndexedValues` instances are ever-growing
* `.toJSON()` method returns an array of values held internally
* `.valueOf()` method returns a real `Set` that can resist `postMessage`, or be stored directly as `IndexedDB`
* `.bindValues(values = [])` returns a `Set` that can contain, add, or remove, any value. Added values will be reflected in the `IndexedValues` instance that created the bound `Set`.
* `.bindIndexes(indexes = [])` is like `.bindValues()` but uses indexes instead, to initialize the derived `Set`. This is mostly useful for revival.
* `.mapKeys(entries = [])` returns a `Map` where *keys* are derived from the `IndexedValues`. Added keys that are not known in the `IndexedValues` source, will be automatically added as part of the `Set`. Once serialized as *JSON*, these maps will represent all keys as indexes, because indeed these maps never hold keys as values, these hold keys as indexes.
* `.mapEntries(entries = [])` is the revival counter-part of `.mapKeys()`. It returns a *map* with keys revived from the `IndexedValues` instance.
* `.fromIndexes(indexes)` can revive right away `Set` instances, upgrading their prototype. This is a *quick and dirty* `.bindIndexes()` equivalent, available for performance reasons.
* `.fromEntries(entries)` can revive right away `Map` instances, upgrading their prototype. This is a *quick and dirty* `.mapEntries()` equivalent, available for performance reasons.