DSL path language to finde nodes in JavaScript objects with focus of FRD (find, rule and deliver)
npm install crisp-path


``javascript
var myObject = Crisp.utilCreate({
ns: 'util.path'
}).objIni().objData({
a: {
b: 'B',
c: 'C'
}
});
// find first node of path
myObject.pathNode('a.*:'); // 'B'
// find all nodes of path optional asynchronous
myObject.pathFind({
path: 'a.*:',
success: function( e ) {
console.log('success:', e );
},
complete: function( e ) {
console.log('complete:', e );
}
});
// logs:
// success: B
// success: C
// complete: [ { data: 'B' }, { data: 'C' } ]
// check of path exists in object
myObject.pathExists('a.*:'); // true
`
for Node.js and io.js $ npm install crisp-path
`javascript
// use package
require("crisp-path");
`or use the OpenCrisp UtilJS wraper
$ npm install crisp-util
`javascript
// use package
require("crisp-util");
`$3
Use Bower to install crisp-path for Browsers APP's and other front-end workflows. $ bower install crisp-path
`html
`or use the OpenCrisp UtilJS wraper
$ bower install crisp-util
`html
`Development
Use Git to clone Crisp.PathJS from GitHub to develop the repository with Grunt # Clone:
$ git clone https://github.com/OpenCrisp/Crisp.PathJS.git
# Build: test, concat, test, minify, test
$ grunt
# Test: original sourcecode for developer (included in build)
$ grunt t
# Run all test-scripts on Unix
$ sh grunt-tests.sh
Usage
How to use Crisp.PathJS with JavaScript.$3
How to use Crisp.definePath( object ) module.`javascript
var myObject = { a: 'A', b: 'B' };// initialice path property functions on myObject
Crisp.definePath( myObject );
// find first node of path string
myObject.pathNode('a:'); // 'A'
myObject.pathNode('b:'); // 'B'
myObject.pathNode('*:'); // 'A'
`$3
How to use Crisp.utilCreate( option ) with util.path namespace.`javascript
// create object with util.path namespace
var myObject = Crisp.utilCreate({
ns: 'util.path'
}).objIni().objData({ a: 'A', b: 'B' });// find first node of path string
myObject.pathNode('a:'); // 'A'
myObject.pathNode('b:'); // 'B'
myObject.pathNode('*:'); // 'A'
`PathJS function
$3
Hot to use .pathNode( path OR option ) on myObject.`javascript
// find first node of path string
myObject.pathNode('b:'); // 'B'// or with option.path string
myObject.pathNode({ path: 'b:' }); // 'B'
` * path - find first node with path-string
* option
* path - find first node with path-string
* _preset_ - alternate value if path not found
> #### path (pathNode)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
// find first node of path string
myObject.pathNode('a:'); // 'A'
myObject.pathNode('b:'); // 'B'
myObject.pathNode('*:'); // 'A'
`> #### option.path (pathNode)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
// find first node of option.path string
myObject.pathNode({ path: 'a:' }); // 'A'
myObject.pathNode({ path: 'b:' }); // 'B'
myObject.pathNode({ path: '*:' }); // 'A'
`> #### option.preset (pathNode)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
// find first node of path string
myObject.pathNode({
path: 'x:',
preset: 'X'
});
// 'X'myObject.pathNode({
path: 'x:',
preset: function() { return 'X'; }
});
// 'X'
myObject.pathNode({
path: 'x:'
});
// undefined
`$3
Hot to use .pathFind( option ) on myObject.`javascript
var myObject = Crisp.utilCreate({
ns: 'util.path'
}).objIni().objData({ a: 'A', b: 'B' });myObject.pathFind({
path: 'a',
success: function( item ) {
console.log('Success:', item );
},
complete: function( e ) {
console.log('Complete:', e.List().xTo() );
}
});
console.log('End');
// logs:
// Success: A
// Complete: [{"data":"A"}]
// End
` * option
* path - find nodes with path-string filter
* _async_ - enable asynchronous callback
* _success_ - callback per find node
* _complete_ - callback on search end
* _start_ - start of find nodes
* _limit_ - limit to find nodes
* _self_ - thisArg for callback
> #### option.path (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
myObject.pathFind({
path: '*',
complete: function( e ) {
console.log('Complete:', e.List().xTo() );
}
});
console.log('End');// logs:
// Complete: [{"data":"A"},{"data":"B"}]
// End
`> #### option.async (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
myObject.pathFind({
path: '*',
async: true,
complete: function( e ) {
console.log('Complete:', e.List().xTo() );
}
});
console.log('End');// logs:
// End
// Complete: [{"data":"A"},{"data":"B"}]
`> #### option.success (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
myObject.pathFind({
path: '*',
success: function( item ) {
console.log('Success:', item );
}
});
console.log('End');// logs:
// Success: A
// Success: B
// End
`> #### option.complete (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
myObject.pathFind({
path: '*',
complete: function( e ) {
console.log('Complete:', e.List().xTo() );
}
});
console.log('End');// logs:
// Complete: [{"data":"A"},{"data":"B"}]
// End
`> #### option.start (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
myObject.pathFind({
path: '*',
start: 1,
complete: function( e ) {
console.log('Complete:', e.List().xTo() );
}
});
console.log('End');// logs:
// Complete: [{"data":"B"}]
// End
`> #### option.limit (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
myObject.pathFind({
path: '*',
limit: 1,
complete: function( e ) {
console.log('Complete:', e.List().xTo() );
}
});
console.log('End');// logs:
// Complete: [{"data":"A"}]
// End
`> #### option.self (pathFind)
> create
myObject with Crisp.utilCreate() or initalice with Crisp.definePath().`javascript
var myArg = {};
myObject.pathFind({
path: '*',
self: myArg,
complete: function( e ) {
console.log('Complete:', this === myArg );
}
});
console.log('End');// logs:
// Complete: true
// End
`$3
Hot to use .pathExists( path ) on myObject.`javascript
// check of path string exists in myObject
myObject.pathExists('b'); // true
myObject.pathExists('*'); // true
myObject.pathExists('x'); // false
`Path string examples
with callback of complete output`javascript
// ## path
// Object.key
'a.b:' // [ { data: 'B' } ]
'a.b:toString' // [ { data: 'B' } ]'a:xTo' // [ { data: '{"b":"B","c":"C"}' } ]
'x:' // []
'a.x:' // []
// Array.index
'g.0.h:' // [ { data: 'H0' } ]
// ## xEach
// Object.xEach
'a.*:' // [ { data: 'B' }, { data: 'C' } ]
// Object.xEach reverse
'a.^*:' // [ { data: 'C' }, { data: 'B' } ]
// Array.xEach
'g.*.h:'
// [ { data: 'H0' }, { data: 'H1' }, { data: 'H2' }, { data: 'H3' }, { data: 'H4' }, { data: 'H5' } ]
// Array.xEach reverse
'g.^*.h:'
// [ { data: 'H5' }, { data: 'H4' }, { data: 'H3' }, { data: 'H2' }, { data: 'H1' }, { data: 'H0' } ]
// ## limit
// Object.xEach( start, limit ) start
'a.0~1:' // [ { data: 'B' } ]
// Object.xEach( start, limit ) end
'a.-1:' // [ { data: 'C' } ]
'a.-1~1:' // [ { data: 'C' } ]
// Object.xEach( start, limit ) limit on Object.length
'a.0~10:' // [ { data: 'B' }, { data: 'C' } ]
'a.-10~10:' // [ { data: 'B' }, { data: 'C' } ]
// Object.xEach( start, limit ) out of range
'a.10~10:' // []
// Array.xEach( start, limit ) start
'g.0~2.h:' // [ { data: 'H0' }, { data: 'H1' } ]
// Array.xEach( start, limit ) start reverse
'g.^0~2.h:' // [ { data: 'H5' }, { data: 'H4' } ]
// Array.xEach( start, limit ) end
'g.-1.h:' // [ { data: 'H5' } ]
'g.-1~1.h:' // [ { data: 'H5' } ]
// Array.xEach( start, limit ) limit on Array.length
'g.0~10.h:'
'g.-10~10.h:'
'g.-~.h:'
// [ { data: 'H0' }, { data: 'H1' }, { data: 'H2' }, { data: 'H3' }, { data: 'H4' }, { data: 'H5' } ]
// Array.xEach( start, limit ) out of range
// default range 0~10
'g.10~10.h:' // []
// find all with filter
'#(h=="H2").i:' // [ { data: 'I2' } ]
'a.#(b=="B").c:' // [ { data: 'C' } ]
'a.#(b=="X").c:' // []
// find all with specific filter
'+(!:xType("Array")).#:xTo',
// [
// { data: '{"a":{"b":"B","c":"C"},"g":[{"h":"H0","i":"I0"},{"h":"H1","i":"I1"},{"h":"H2","i":"I2"},{"h":"H3","i":"I3"},{"h":"H4","i":"I4"},{"h":"H5","i":"I5"}]}' },
// { data: '{"b":"B","c":"C"}' },
// { data: '"B"' },
// { data: '"C"' }
// ]
// find all with specific filter and second filter
'+(!:xType("Array")).#(:xType("field")):' // [ { data: 'B' }, { data: 'C' } ]
``