Use URI path to get or set data.
$ npm install jsonuri --save
{
"menu": {
"id": 123,
"list": [0,1,2,3,4],
"popup": {
"menuitem": [
{
"value": "New",
"onclick": "CreateNewDoc()"
},
{
"value": "Open",
"onclick": "OpenDoc()"
},
{
"value": "Close",
"onclick": "CloseDoc()"
}
]
}
}
}
Get the value of the specified data for the path.
Example:
get(data, '/menu/id/');
//123
get(data, '/menu/popup/menuitem/0/value/');
//"New"
get(data, '/menu/popup/menuitem/0/value/../');
//{value: "New", onclick: "CreateNewDoc()"}
Set the value of the specified data for the path.
Example:
set(data, '/menu/id/',789);
get(data, '/menu/id/');
//789
Remove the value of the specified data for the path.
Example:
rm(data, '/menu/id/');
get(data, '/menu/id/');
//undefined
Data A moved to target B before or after.
Example:
mv(data, '/menu/list/0', '/menu/list/3');
get(data, '/menu/list/');
//[1,2,3,0,4]
set(data, '/menu/list/',[0,1,2,3,4]);
mv(data, '/menu/list/0', '/menu/list/3', 'before');
get(data, '/menu/list/');
//[1,2,0,3,4]
Data swap in an array.
Example:
swap(data, '/menu/list/0', '/menu/list/4');
get(data, '/menu/list/');
//[4,1,2,3,0]
swap(data, '/menu/list/0', '/menu/list/4');
get(data, '/menu/list/');
//[4,1,2,3,0]
Insert data into an array
that is described in the path.
Example:
insert(data, '/menu/list/0', 9999, 'before');
//[9999,0,1,2,3,4]
Traverse each data of each node and value.
Example:
walk({a:{a1:'x'}}, function(obj, key, raw, path){
console.log(obj, key, raw, path)
})
// { a1: 'x' } 'a' { a: { a1: 'x' } } '/a/'
// x a1 { a1: 'x' } /a/a1/
Example:
normalizePath('a', 'b')
// /a/b/
normalizePath(['a', 'b', '../'], 'c')
// /a/c/