odict
v1.0.2
Published
Bunch of helper functions for working with objects as dictionaries.
Downloads
4
Readme
odict - Object Dictionary
Bunch of helper functions for working with objects as dictionaries.
These methods work on all enumerable properties, including inherited.
Install
npm i --save odict
Import
Import all functions into odict
namespace:
import * as odict from "odict";
Or import only needed functions:
import {isDict, isEmpty, reduce, map, iterable} from "odict";
Usage
For example object like this:
const people = {
"a": "Igor",
"b": "Mike",
"c": "Luke"
};
isDict
Check if value is a dictionary:
isDict(people); // true
isDict([1, 2]); // false
isEmpty
Check dictionary for emptiness:
isEmpty(people); // false
isEmpty({ }); // true
reduce
Reduce dictionary, which works very much like Array.prototype.reduce():
const reducer = (acc, value, key) => (acc.push(`${key} - ${value}`), acc);
reduce(people, reducer, []); // ["a - Igor", "b - Mike", "c - Luke"]
map
Map dictionary, which works very much like Array.prototype.map():
const mapper = (value, key) => `${key} - ${value}`;
map(people, mapper); // ["a - Igor", "b - Mike", "c - Luke"]
iterable
Create generator object:
for (const [key, value] of iterable(people)) console.log(`${key} - ${value}`);
// Outputs: a - Igor\b - Mike\c - Luke