Skip to content

isumix/odict

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

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

About

Bunch of helper functions for working with objects as dictionaries.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published