1.0.2 • Published 5 years ago

object-cartographer v1.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

object-cartographer

map, reduce, and filter for objects, wrapping Object.entries().reduce()

Usage

const {
	mapObject,
	filterObject,
	reduceObject,
} = require('object-cartographer');

const colors = {
	red: 'ff0000',
	green: '00ff00',
	blue: '0000ff',
};

mapObject(colors, ([key, value]) => ({ [value]: key })) ===
	{
		'0000ff': 'blue',
		'00ff00': 'green',
		ff0000: 'red',
	};

filterObject(colors, ([key, value]) => key !== 'red') ===
	{
		green: '00ff00',
		blue: '0000ff',
	};

reduceObject(colors, (acc, [key, value]) => [...acc, key], []) ===
	['red', 'green', 'blue'];

For more use cases check index.test.js

Advanced usage

Just like with normal map, reduce, and filter, you can retrieve both the index and the original object as extra parameters in the callback. Be warned! Objects in Javascript don't have a guaranteed order so it's very unlikely you want to use the index