Comparing version 0.4.0 to 0.5.0
53
index.js
@@ -15,2 +15,14 @@ var traverse = require("traverse"), | ||
}, | ||
update: function(cb) { | ||
this.forEach(function(node) { | ||
this.update(typeof cb == "function" ? cb(node) : cb); | ||
}); | ||
}, | ||
remove: function() { | ||
this.forEach(function(node) { | ||
this.remove(); | ||
}) | ||
}, | ||
@@ -37,3 +49,3 @@ forEach: function(cb) { | ||
function getSelectors(parsed) { | ||
if (parsed[0] == ",") { | ||
if (parsed[0] == ",") { // "selector1, selector2" | ||
return parsed.slice(1); | ||
@@ -73,3 +85,3 @@ } | ||
} | ||
else if(must) { | ||
else if (must) { | ||
return false; | ||
@@ -93,19 +105,16 @@ } | ||
if (part.type) { | ||
if (part.type == "null") { | ||
if (context.node !== null) { | ||
return false; | ||
} | ||
var type = part.type; | ||
if (type == "null" && node !== null) { | ||
return false; | ||
} | ||
else if (part.type == "array") { | ||
if (!isArray(context.node)) { | ||
return false; | ||
} | ||
else if (type == "array" && !isArray(node)) { | ||
return false; | ||
} | ||
else if (part.type == "object") { | ||
if (typeof context.node != "object" | ||
|| context.node === null || isArray(context.node)) { | ||
return false; | ||
} | ||
else if (type == "object" && (typeof node != "object" | ||
|| node === null || isArray(node))) { | ||
return false; | ||
} | ||
else if (part.type != typeof context.node) { | ||
else if ((type == "boolean" || type == "string" || type == "number") | ||
&& type != typeof node) { | ||
return false; | ||
@@ -123,13 +132,9 @@ } | ||
} | ||
if (part.pf == ":nth-last-child") { | ||
var n = context.parent && context.parent.node.length; | ||
if (!n || key != n - part.b) { | ||
if (part.pf == ":nth-last-child" | ||
&& (!parent || key != parent.node.length - part.b)) { | ||
return false; | ||
} | ||
} | ||
if (part.pc == ":only-child") { | ||
var n = context.parent && context.parent.node.length; | ||
if (!n || n != 1) { | ||
if (part.pc == ":only-child" | ||
&& (!parent || parent.node.length != 1)) { | ||
return false; | ||
} | ||
} | ||
@@ -136,0 +141,0 @@ if (part.pc == ":root" && key !== undefined) { |
{ | ||
"name": "js-select", | ||
"description": "Traverse and modify objects with JSONSelect selectors", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"author": "Heather Arthur <fayearthur@gmail.com>", | ||
@@ -13,3 +13,3 @@ "repository": { | ||
"traverse": "0.4.x", | ||
"JSONSelect": "0.2.x" | ||
"JSONSelect": "0.2.1" | ||
}, | ||
@@ -16,0 +16,0 @@ "devDependencies": { |
@@ -17,13 +17,15 @@ # js-select | ||
select(people, ".age").forEach(function(age) { | ||
this.update(age - 5); | ||
}) | ||
select(people, ".age").nodes(); // [35, 15] | ||
select(people, ".age").nodes(); // [30, 10] | ||
select(people, ".age").update(function(age) { | ||
return age - 5; | ||
}); | ||
select(people, ".age").remove(); | ||
``` | ||
See [js-traverse](https://github.com/substack/js-traverse) for all the things you can do to modify the node. The `forEach()` callback will get the same `this` context as the `forEach()` callback from [js-traverse](https://github.com/substack/js-traverse), plus a `this.matches()` which will test if the node matches a selector: | ||
There's also a `forEach()` which gets a special `this` context. See [js-traverse](https://github.com/substack/js-traverse) for all the things you can do to modify and inspect the node with the context. In addition, js-select adds a `this.matches()` which will test if the node matches a selector: | ||
```javascript | ||
select(obj).forEach(function(node) { | ||
select(people).forEach(function(node) { | ||
if (this.matches(".mary > .movie")) { | ||
@@ -43,5 +45,6 @@ this.remove(); | ||
.key | ||
.ancestor .key | ||
.parent > .key | ||
.sibling ~ .key | ||
ancestor selector | ||
parent > selector | ||
sibling ~ selector | ||
selector1, selector2 | ||
:root | ||
@@ -48,0 +51,0 @@ :nth-child(n) |
@@ -34,3 +34,3 @@ var assert = require("assert"), | ||
} | ||
var people2 = traverse.clone(people); | ||
assert.deepEqual(select(people, "*").nodes(), [{"george":{"age":35,"movies":[{"name":"Repo Man","stars":5}]},"mary":{"age":15,"movies":[{"name":"Twilight","stars":3},{"name":"Trudy","stars":2},{"name":"The Fighter","stars":4}]},"chris":{"car":null,"male":true}},{"age":35,"movies":[{"name":"Repo Man","stars":5}]},35,[{"name":"Repo Man","stars":5}],{"name":"Repo Man","stars":5},"Repo Man",5,{"age":15,"movies":[{"name":"Twilight","stars":3},{"name":"Trudy","stars":2},{"name":"The Fighter","stars":4}]},15,[{"name":"Twilight","stars":3},{"name":"Trudy","stars":2},{"name":"The Fighter","stars":4}],{"name":"Twilight","stars":3},"Twilight",3,{"name":"Trudy","stars":2},"Trudy",2,{"name":"The Fighter","stars":4},"The Fighter",4,{"car":null,"male":true},null,true]); | ||
@@ -86,10 +86,31 @@ assert.deepEqual(select(people, ".george").nodes(), [{"age":35,"movies":[{"name":"Repo Man","stars":5}]}]); | ||
// modding | ||
select(people, ".age").forEach(function(age) { | ||
// update() | ||
var people2 = traverse.clone(people); | ||
select(people2, ".age").update(function(age) { | ||
return age - 5; | ||
}) | ||
assert.deepEqual(select(people2, ".age").nodes(), [30, 10]); | ||
select(people2, ".age").update(3) | ||
assert.deepEqual(select(people2, ".age").nodes(), [3, 3]); | ||
// remove() | ||
people2 = traverse.clone(people); | ||
select(people2, ".age").remove(); | ||
assert.deepEqual(select(people2, ".age").nodes(), []); | ||
// forEach() | ||
people2 = traverse.clone(people); | ||
select(people2, ".age").forEach(function(age) { | ||
this.update(age - 5); | ||
}) | ||
assert.deepEqual(select(people, ".age").nodes(), [30, 10]); | ||
assert.deepEqual(select(people2, ".age").nodes(), [30, 10]); | ||
// this.matches() | ||
var people2 = traverse.clone(people); | ||
select(people2).forEach(function(node) { | ||
@@ -96,0 +117,0 @@ if (this.matches(".age")) { |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Uses eval
Supply chain riskPackage uses dynamic code execution (e.g., eval()), which is a dangerous practice. This can prevent the code from running in certain environments and increases the risk that the code may contain exploits or malicious behavior.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
165703
16
1794
84
7
2
+ AddedJSONSelect@0.2.1(transitive)
- RemovedJSONSelect@0.2.3(transitive)
UpdatedJSONSelect@0.2.1