Comparing version 0.2.0 to 0.3.0
70
index.js
@@ -7,3 +7,2 @@ 'use strict'; | ||
* at that point. Can we used to return the parent node as well. | ||
* @private | ||
* @param {Object} tree Object graph | ||
@@ -14,3 +13,7 @@ * @param {Array} key_array An array of keys, traversable from the left | ||
*/ | ||
function _valueFromKeyArray(tree, key_array, getParent){ | ||
var valueFromPath = exports.valueFromPath = function(tree, key_array, getParent){ | ||
if(typeof key_array === 'string'){ | ||
key_array = key_array.split('.'); | ||
} | ||
if(getParent){ | ||
@@ -23,5 +26,46 @@ key_array.splice(key_array.length-1, 1); | ||
}, tree); | ||
}; | ||
/** | ||
* Cohearses `expr` into a RegExp or throws an error if it can't | ||
* @private | ||
* @param {Mixed} expr RegExp or string. | ||
* @return {Object} Strict Regular Expression version of string | ||
*/ | ||
function _regExpOrGTFO(expr){ | ||
var exp; | ||
if(typeof expr === 'string'){ | ||
exp = new RegExp('^'+expr+'$'); | ||
} else if (expr instanceof RegExp){ | ||
exp = expr; | ||
} else { | ||
throw new Error('Needle must be either a string or a RegExp'); | ||
} | ||
return exp; | ||
} | ||
/** | ||
* Gets the path to a particular key in the Object | ||
* @param {Object} tree Object graph | ||
* @param {Mixed} key String or RegExp | ||
* @return {Array} Array of decimal separated paths to object suitable to | ||
* use with valueFromKeyPath | ||
*/ | ||
exports.getPathToKey = function(tree, key){ | ||
var paths = []; | ||
var exp = _regExpOrGTFO(key); | ||
traverse(tree).forEach(function(nodeValue){ | ||
if(typeof this.key === 'string' && this.key.match(exp)){ | ||
paths.push(this.path.join('.')); | ||
} | ||
}); | ||
return paths; | ||
}; | ||
/** | ||
* Returns a node of the tree if any of the keys or values in the node match the | ||
@@ -36,12 +80,4 @@ * provided criteria | ||
var nodes = []; | ||
var exp; | ||
var exp = _regExpOrGTFO(needle); | ||
if(typeof needle === 'string'){ | ||
exp = new RegExp('^'+needle+'$'); | ||
} else if (needle instanceof RegExp){ | ||
exp = needle; | ||
} else { | ||
throw new Error('Needle must be either a string or a RegExp'); | ||
} | ||
traverse(tree).forEach(function(nodeValue){ | ||
@@ -51,3 +87,3 @@ var keyMatch = typeof this.key === 'string' && this.key.match(exp); | ||
if(keyMatch || nodeMatch){ | ||
nodes.push(_valueFromKeyArray(tree, this.path, parent)); | ||
nodes.push(valueFromPath(tree, this.path, parent)); | ||
} | ||
@@ -66,3 +102,3 @@ }); | ||
exports.getValuesByKeyName = function(tree, key, maxDepth){ | ||
var exp; | ||
var exp = _regExpOrGTFO(key); | ||
if(typeof maxDepth === 'undefined'){ | ||
@@ -72,10 +108,2 @@ maxDepth = 0; | ||
if(typeof key === 'string'){ | ||
exp = new RegExp('^'+key+'$'); | ||
} else if (key instanceof RegExp){ | ||
exp = key; | ||
} else { | ||
throw new Error('Key must be either a string or a RegExp'); | ||
} | ||
var values = []; | ||
@@ -82,0 +110,0 @@ traverse(tree).forEach(function(nodeValue){ |
{ | ||
"name": "jsondom", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A DOM-like interface for querying Javascript Objects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -58,2 +58,27 @@ [![build status](https://secure.travis-ci.org/CondeNast/jsondom.png)](http://travis-ci.org/CondeNast/jsondom) | ||
### `valueFromPath(tree, key_array, getParent)` | ||
``` | ||
/** | ||
* Takes an array of nodes to the content location and returns the value located | ||
* at that point. Can we used to return the parent node as well. | ||
* @param {Object} tree Object graph | ||
* @param {Array} key_array An array of keys, traversable from the left | ||
* @param {Boolean} (getParent) Flag to return parent or element. Default: element | ||
* @return {Mixed} The content located at that point in the tree | ||
*/ | ||
``` | ||
### `getPathToKey(tree, key)` | ||
``` | ||
/** | ||
* Gets the path to a particular key in the Object | ||
* @param {Object} tree Object graph | ||
* @param {Mixed} key String or RegExp | ||
* @return {Array} Array of decimal separated paths to object suitable to | ||
* use with valueFromKeyPath | ||
*/ | ||
``` | ||
### `getNodesMatching(tree, needle, parent)` | ||
@@ -60,0 +85,0 @@ |
@@ -22,2 +22,14 @@ /* global describe, it, afterEach, beforeEach, xdescribe, xit */ | ||
it('Should return a value to a path', function(){ | ||
var paths = jsd.getPathToKey(complex, /hello/); | ||
var value = jsd.valueFromPath(complex, paths[0]); | ||
expect(value).to.equal("it is me you're looking for"); | ||
}); | ||
it('Should return a path to a key', function(){ | ||
var paths = jsd.getPathToKey(complex, /hello/); | ||
expect(paths.length).to.equal(1); | ||
expect(paths[0]).to.equal('articles.1.hello'); | ||
}); | ||
it('Should find all titles', function(){ | ||
@@ -49,10 +61,2 @@ var titles = jsd.getValuesByKeyName(complex, 'title'); | ||
it('Should reject keys that aren\'t strings or RegExps', function(){ | ||
try { | ||
var articles = jsd.getValuesByKeyName(complex, {}); | ||
} catch (e){ | ||
expect(e.message).to.equal('Key must be either a string or a RegExp'); | ||
} | ||
}); | ||
it('Should get nodes matching using Regular Expressions', function(){ | ||
@@ -59,0 +63,0 @@ var title = jsd.getNodesMatching(complex, /sub/); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10524
158
110