json-lens
v0.0.1
Published
Lenses with JSON Pointer
Downloads
3
Readme
JSON Lens
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.