json-lens

0.0.1 • Public • Published

JSON Lens

Build Status

Create Lenses from JSON Pointer.

Starting with this JSON:

var market = {
  foods: {
    fruits: ['apples', 'oranges'],
    veggies: ['peas', 'carrots']
  }
};

We can create lenses using JSON pointers.

var firstFruit = lens('/foods/fruits/0');
var secondVeggie = lens('/foods/veggies/1');

And use them to get values from our JSON object

t.equal(firstFruit(market), 'apples');
t.equal(secondVeggie(market), 'carrots');

Or set values within it

firstFruit.set('bananas', market);
t.equal(firstFruit(market), 'bananas');
 
secondVeggie.set('onions', market);
t.equal(secondVeggie(market), 'onions');

Or set by modifying existing values

function ohYeah(v){
  return v+'!';
}
 
firstFruit.map(ohYeah, market);
t.equal(firstFruit(market), 'bananas!');
 
secondVeggie.map(ohYeah, market);
t.equal(secondVeggie(market), 'onions!');

Retaining the structure of our JSON all along

var result = {
  foods: {
    fruits: ['bananas!', 'oranges'],
    veggies: ['peas', 'onions!']
  }
};
t.deepEqual(market, result);

Credits

Pointer get/set code modified from JSON Pointer and lens implementation from Ramda. Both projects are licensed under MIT.

Readme

Keywords

Package Sidebar

Install

npm i json-lens

Weekly Downloads

3

Version

0.0.1

License

MIT

Last publish

Collaborators

  • kevinbeaty