proxi-map
v0.1.2
Published
Immutable object mapping
Downloads
26
Maintainers
Readme
proxi-map
A lightweight JavaScript library to create variants of an object by using its immutable proxy.
Install
npm install proxi-map
Usage
import Imap from 'proxi-map';
const data = {
category1: {
activeProducts: 2,
products: [
{ name: 'product11', active: false },
{ name: 'product12', active: true },
{ name: 'product13', active: true },
],
},
category2: {
activeProducts: 2,
products: [
{ name: 'product21', active: true },
{ name: 'product22', active: true },
],
},
};
const newData = Imap(data)
.category1
.activeProducts(3)
.backtrack(-1) // 1 step back
.products[0].active(true)
.unwrap();
console.log(newData.category1);
// {
// activeProducts: 3,
// products: [
// { name: 'product11', active: true },
// { name: 'product12', active: true },
// { name: 'product13', active: true }
// ]
// }
console.log(
data == newData, // false
data.category1 == newData.category1, // false
data.category2 == newData.category2 // true
);
Usage with Ramda
npm install ramda
import Imap from 'proxi-map';
import * as R from 'ramda';
// remove inactive products from category1
const newData = Imap(data)
.category1
.products(R.filter(R.prop('active')))
// equivalent to: (arr) => arr.filter(obj => obj.active)
.unwrap();
console.log(newData.category1);
// {
// activeProducts: 2,
// products: [
// { name: 'product22', active: true },
// { name: 'product23', active: true }
// ]
// }