key-index
TypeScript icon, indicating that this package has built-in type declarations

1.7.5 • Public • Published

Key Index

Key based index store.

Installation

 $ npm i key-index

Usage

Create an index

Import

import keyIndex from "key-index"

Create an instance

let initer = (pointer: string) => {
  return {name: pointer}
}

let index = keyIndex(initer)

Query

When querying a key that is not assigned to a value yet, one is automatically created via the given initer function.

let a = index("keyA")     // {name: "keyA"}
a.prop = "val"
index("keyA")             // {name: "keyA", prop: "val"}

(Mostly for debugging purposes) you can also query the whole document

let a = index()    // Map: {
                   //   "keyA": {
                   //     "name": "keyA"
                   //     "prop": "val"
                   //   }
                   // }

The index is stored in a Map by default. If youd like to use a regular object as index instead use

import { constructObjectIndex as keyIndex } from "key-index"

All functionality is the same here. The only difference is the underlying technology used.

Note: that native objects do only support strings | number | symbol as indices!

Using other initers

This is the default initer

keyIndex(/* () => {return {}} */)

Using values directly

You can use values directly. If your use case does only need one value.

let valueIndex = keyIndex((key) => key)
valueIndex("keyA")                 // "keyA"

In that (and every other) case you can change the value like so:

valueIndex("keyA", "keyAValue")    // "keyAValue"
valueIndex("keyA")                 // "keyAValue"

Nested indices

If using your use case requires nested indices consider this

let nestedIndex = keyIndex(() => keyIndex(/* () => {return {}} */))
nestedIndex("keyA")("keyB")         // {}

Querying the whole document does also work here

nestedIndex("keyA")("keyB").key = "val"
nestedIndex("keyA")("keyC").key = "val"
nestedIndex("keyD")("keyE").key1 = "val1"
nestedIndex("keyD")("keyE").key2 = "val2"


nestedIndex()                      // Map {
                                   //   "keyA": Map {
                                   //     "keyB": { key: "val" }
                                   //     "keyC": { key: "val" }
                                   //   }
                                   //   "keyD": Map {
                                   //     "keyE": { key1: "val1", key2: "val2" }
                                   //   }
                                   // }

Note: Depending on the underlying technology (maps / objects) this may yield slightly different results (all maps would be regular objects).

Contribute

All feedback is appreciated. Create a pull request or write an issue.

Versions

Current Tags

VersionDownloads (Last 7 Days)Tag
1.7.55latest

Version History

VersionDownloads (Last 7 Days)Published
1.7.55
1.7.40
1.7.30
1.7.21
1.7.10
1.7.00
1.6.11
1.6.00
1.5.00
1.4.190
1.4.181
1.4.170
1.4.160
1.4.150
1.4.140
1.4.131
1.4.120
1.4.111
1.4.100
1.4.90
1.4.80
1.4.70
1.4.60
1.4.50
1.4.40
1.4.30
1.4.20
1.4.10
1.4.00
1.3.20
1.3.10
1.3.00
1.2.10
1.2.00
1.1.20
1.1.10
1.1.00
1.0.00
0.0.10

Package Sidebar

Install

npm i key-index

Weekly Downloads

10

Version

1.7.5

License

ISC

Unpacked Size

33.5 kB

Total Files

15

Last publish

Collaborators

  • zzrv