Selectors
Immutable JSON selectors.
A small lib that help manipulating JSON objects in an immutable way.
Example
'use strict'; var selectors = lift = selectorsliftSelection map = selectorsmapSelection select = selectorsselect has = selectorshasSelection set = selectorssetSelection ; var obj = a: b: 100 // Has with a path selectorconsole// Result: Has obj.a.b? true // Has with a prop selectorconsole;// Has obj.Z? false // Select with a path selectorconsole;// obj.a.b: 100 // Select with a prop selectorconsole;// obj.a: { b: 100 } // Set a new selected propertyvar immutable = ;console;// New: { a: { b: 100, c: 'I am new' } } Old: { a: { b: 100 } } // Create a new objectconsole;// New object: { x: { y: { z: 'I was just created' } } } // Create a new object from `undefined`console;// From undefined: { create: { from: { undefined: 'CHECK' } } } // Lift example {return x+y;}var addTen = add;console// obj.a.b + 10: 110 // Curried lift {return x*y;}var mulSelectionByEleven = ;console// obj.a.b * 11: 1100 // Curried map examplevar incrementSelection = ;var incremented = ;console;// Original: { a: { b: 100 } } Incremented: { a: { b: 101 } }
methods
var selectors = ;
All methods are auto curried and can be composed to new functions.
Common Attributes:
selector
- Several variations might applystring
- Selects the propertyarray
- Selects a pathUndefined|[]
- Selects the whole object
obj
- The JSON object to select from. It could be an empty object as well asUndefined
. When it's undefined it will simply create a new object.
hasSelection(selector, obj)
Returns false
if the selector is undefined.
select(selector, obj)
Returns the value of the selection.
setSelection(selector, value, obj)
Sets a new value
for the selection.
Returns a new object with updated value.
liftSelection(f, selector, obj)
f
- a function with the signature of f(x)
.
Lifts/Injects the value of the selection into f
.
Returns the value of f(x)
.
mapSelection(f, selector, obj);
f
- a function with a signature of f(x)
.
Maps the value of the selection with f
.
Returns a new object with the value of f(x)
at the selection.
install
With npm do:
npm install selectors
license
MIT