ts-micro-dict

Functions for representing plain objects as typesafe immutable dictionaries

Usage no npm install needed!

<script type="module">
  import tsMicroDict from 'https://cdn.skypack.dev/ts-micro-dict';
</script>

README

ts-micro-dict

npm CircleCI codecov Type Coverage Libraries.io dependency status for latest release Bundlephobia npm

Functions for representing plain objects as typesafe immutable dictionaries

Getting started

$ npm install ts-micro-dict --save

Usage

import { Dict } from 'ts-micro-dict'
import { pipeWith } from 'pipe-ts'

const dict: Dict<number> = { x: 1 } // Readonly<Record<string, number>>

// put,

const putDict = Dict.put('y', 1, dict) // Dict<number>
const putCurried = pipeWith(dict, Dict.put('y', 3)) // Dict<number>

// omit,

const omitDict = Dict.omit('x', dict) // Dict<number>
const omitCurried = pipeWith(dict, Dict.omit('x')) // Dict<number>

// filter,

const filterDict = Dict.filter((item, key) => true, dict) // Dict<number>
const filterCurried = pipeWith(
  dict,
  Dict.filter((item, key) => true)
) // Dict<number>

// map,

const mapDict = Dict.map((item, key) => true, dict) // Dict<boolean>
const mapCurried = pipeWith(
  dict,
  Dict.map((item, key) => true)
) // Dict<boolean>

// reduce,

const reduceDict = Dict.reduce((acc, item, key) => acc + item, 0, dict) // number
const reduceCurried = pipeWith(
  dict,
  Dict.reduce((acc, item, key) => acc + item, 0)
) // number

// some,

const someDict = Dict.some((item, key) => true, dict) // boolean
const someCurried = pipeWith(
  dict,
  Dict.some((item, key) => true)
) // boolean

// every,

const everyDict = Dict.every((item, key) => true, dict) // boolean
const everyCurried = pipeWith(
  dict,
  Dict.every((item, key) => true)
) // boolean

// toArray,

const toArray = Dict.toArray((item, key) => [key, item], dict) // readonly (readonly [string, number])[]
const toArrayCurried = pipeWith(
  dict,
  Dict.toArray((item, key) => [key, item])
) // readonly (readonly [string, number])[]

// fromArray

const array: number[] = [1, 2, 3]
const fromArray = Dict.fromArray((value, index) => [`${index}`, value], array) // Dict<number>
const fromArrayCurried = pipeWith(
  array,
  Dict.fromArray((value, index) => [`${index}`, value])
) // Dict<number>

// length

const length = Dict.length(dict) // number

// isEqual

const isEqual = Dict.isEqual(dict, { y: 2 }, (a, b) => a === b) // false╦ť