get-set-json
Immutable setter and getter library that works on JSON data
Install
$ npm install --save get-set-json
Features
- This library exposes 4 functions.
- get
- set
- getd
- setd
- The functions work immutably. This means the source data remains unaffected.
- The functions are curried up to the mandatory arguments.
- To help in functional compositions, the functions come in two flavors.
- Data last
- get
- set
- Data first
- getd
- setd
- Data last
- This is a zero-dependency library.
API
1) get(path, sourceJson, defaultValue) / getd(sourceJson, path, defaultValue)
Returns: the value at the given path
Curriable upto first 2 arguments. Since the third argument is optional, currying does not wait for it.
path
Type: Array
The sequence of keys (of intermediate objects) / indexes (of intermediate arrays) in array format.
sourceJson
Type: Object
or Array
The source JSON data.
defaultValue
Type: Any
The optional default value if the path does not exist. If not given, undefined
is returned.
2) set(path, value, sourceJson) / setd(sourceJson, path, value)
Returns: a new json after setting value at the given path
Curriable for all 3 arguments.
path
Type: Array
The sequence of keys (of intermediate objects) / indexes (of intermediate arrays) in array format.
value
Type: Any
The value to be set at a given path location.
sourceJson
Type: Object
or Array
The source JSON data.
Usage
Sample data for usage notes is given below
Usage - the get method
const valueF1 = // Hello // The get method takes a default value in case of an incorrect / missing path// In this case, it is missingData// If the default value is not provided, the get method returns undefinedconst valueF1BadPath = // missingData // Works with array on the way// Use integers for array indexes // JavaScript // Works with array // two // five // seven // Curriable up to 2 arguments as the get method needs 2 mandatory arguments.// The third argument (default value) is optional. Therefore, the currying does not wait for the third argument.const personFirstName = personLastName = personBadPath = // John // Smith // missingData // Joanne // Taylor // missingData // James // Bond // missingData
Usage - the set method
// A simple usage to set a value at a particular key// The set happens immutably, this means, the source json data_1 remains unaffected// The destination json dest_data_1 has the new valuesconst dest_data_1 = // It returns new data on every callconst dest_data_2 = dest_data_1 === dest_data_2 // falsedest_data_1 === data_1 // falsedest_data_2 === data_1 // false // New levels are created on missing keys/path // {a: { b: { c: 'Greetings' }}} // New levels are created on missing keys/path - arrays created when key is an integer // { a: { b: [undefined, undefined, 'Greetings'] }} // New levels are created on missing keys/path - objects created when key is a string // { a: { b: { 221: 'Greetings' }}}
Sample data for the Usage notes
const data_1 = Object data_2 = Object data_3 = Object persons = Object