Comparing version
@@ -30,3 +30,3 @@ "use strict"; | ||
var prop = function (name) { | ||
var node = first(); | ||
var node = get(0); | ||
if (node) { | ||
@@ -36,2 +36,14 @@ return node[name]; | ||
}; | ||
var text = function () { | ||
var res = ''; | ||
each(function (node) { | ||
if (node.type === 'text') { | ||
res += node.value; | ||
} | ||
else { | ||
res += xmlQuery(node).children().text(); | ||
} | ||
}); | ||
return res; | ||
}; | ||
return { | ||
@@ -50,4 +62,5 @@ attr: attr, | ||
size: size, | ||
text: text, | ||
}; | ||
}; | ||
module.exports = xmlQuery; |
{ | ||
"name": "xml-query", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Super small library to retrieve values and attributes from the XML AST generated by xml-reader", | ||
"main": "build/index.js", | ||
"scripts": { | ||
"test": "node test/test.js", | ||
"test": "ava test/test.js", | ||
"pretest": "tsc", | ||
@@ -26,3 +26,3 @@ "prepublish": "npm test" | ||
"devDependencies": { | ||
"tape": "^4.5.1", | ||
"ava": "^0.15.2", | ||
"xml-reader": "0.0.6" | ||
@@ -29,0 +29,0 @@ }, |
# XML Query | ||
Super small (~50 sloc) library to retrieve values and attributes from the XML AST generated by xml-reader. | ||
Super small (~60 sloc) library to retrieve values and attributes from the XML AST generated by xml-reader. | ||
@@ -117,10 +117,48 @@ Easy-to-use jQuery-like interface. | ||
### .text() | ||
Get the combined text contents of each element, including their descendants | ||
```javascript | ||
xmlQuery(ast).find('subject').text(); // 'hello' | ||
``` | ||
### .eq() | ||
Returns a new XmlQuery object for the selected element by index | ||
```javascript | ||
xmlQuery(ast).children().eq(2); // xmlQuery object containing the 'subject' node | ||
``` | ||
### .first() | ||
Returns a new XmlQuery object for the first element. Same as `.eq(0)` | ||
```javascript | ||
xmlQuery(ast).children().first(); // xmlQuery object containing the 'from' node | ||
``` | ||
### .last() | ||
Returns a new XmlQuery object for the last element. Same as `.eq(length - 1)` | ||
```javascript | ||
xmlQuery(ast).children().last(); // xmlQuery object containing the 'body' node | ||
``` | ||
### .length | ||
Number of elements. Returns the same result as `.size()` | ||
```javascript | ||
xmlQuery(ast).children().length; // 4 | ||
``` | ||
### .size() | ||
Number of elements. Returns the same result as `.length` | ||
```javascript | ||
xmlQuery(ast).children().size(); // 4 | ||
``` |
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
7341
19.89%63
26%164
30.16%0
-100%