proxi-map
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

proxi-map

npm version Build Status Coverage Status Known Vulnerabilities npm downloads/month

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 }
//     ]
// }

Package Sidebar

Install

npm i proxi-map

Weekly Downloads

1

Version

0.1.2

License

ISC

Unpacked Size

13.9 kB

Total Files

16

Last publish

Collaborators

  • akshay9136