Comparing version
"use strict"; | ||
var flatMap = function (arr, fn) { return (_a = []).concat.apply(_a, arr.map(fn)); var _a; }; | ||
var flatMap = function (arr, fn) { | ||
return Array.prototype.concat.apply([], arr.map(fn)); | ||
}; | ||
var xmlQuery = function (ast) { | ||
@@ -4,0 +6,0 @@ var nodes = Array.isArray(ast) ? ast : (ast ? [ast] : []); |
{ | ||
"name": "xml-query", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Super small library to retrieve values and attributes from the XML AST generated by xml-reader", | ||
@@ -22,3 +22,4 @@ "main": "build/index.js", | ||
"values", | ||
"attributes" | ||
"attributes", | ||
"xpath" | ||
], | ||
@@ -25,0 +26,0 @@ "author": "Pedro Ladaria <pedro.ladaria@gmail.com>", |
# XML Query | ||
Super small (~60 sloc) library to retrieve values and attributes from the XML AST generated by xml-reader. | ||
Super small (~60 LOC) library for retrieving values and attributes from the XML AST generated by [xml-reader](http://github.com/pladaria/xml-reader). | ||
Easy-to-use jQuery-like interface. | ||
Provides an easy-to-use jQuery-like interface. | ||
# WORK IN PROGRESS!!! | ||
Everything here is subject to change | ||
## Install | ||
@@ -21,5 +17,16 @@ | ||
The following XML will be used to illustrate all examples | ||
```xml | ||
<message id="1001" date="2016-06-19"> | ||
<from>Bob</from> | ||
<to>Alice</to> | ||
<subject>Hello</subject> | ||
<body>Bla bla bla</body> | ||
</message> | ||
``` | ||
Let's read the XML using [xml-reader](http://github.com/pladaria/xml-reader). | ||
```javascript | ||
// given this xml | ||
const xml = | ||
@@ -30,16 +37,14 @@ `<message id="1001" date="2016-06-19"> | ||
<subject>Hello</subject> | ||
<body>Lorem ipsum dolor sit amet, consectetur adipiscing elit</body> | ||
<body>Bla bla bla</body> | ||
</message>`; | ||
// we can use the xml-reader module to get the ast from the xml source | ||
const reader = xmlReader.create(); | ||
const reader = xmlReader.create(); // see https://www.npmjs.com/package/xml-reader | ||
reader.on('done', ast => { | ||
// do something with the ast | ||
}); | ||
reader.parse(xml); | ||
``` | ||
Following examples will use this ast. | ||
### xmlQuery() | ||
@@ -70,3 +75,3 @@ | ||
```javascript | ||
xmlQuery(ast).find('body'); // returns a new xmlQuery object containing the body element | ||
xmlQuery(ast).find('body'); // xmlQuery containing the body element | ||
``` | ||
@@ -97,3 +102,2 @@ | ||
xmlQuery(ast).each(node => console.log(node.name)); | ||
// prints: | ||
// from | ||
@@ -134,3 +138,3 @@ // to | ||
```javascript | ||
xmlQuery(ast).children().eq(2); // xmlQuery object containing the 'subject' node | ||
xmlQuery(ast).children().eq(2); // xmlQuery containing the 'subject' node | ||
``` | ||
@@ -143,3 +147,3 @@ | ||
```javascript | ||
xmlQuery(ast).children().first(); // xmlQuery object containing the 'from' node | ||
xmlQuery(ast).children().first(); // xmlQuery containing the 'from' node | ||
``` | ||
@@ -152,3 +156,3 @@ | ||
```javascript | ||
xmlQuery(ast).children().last(); // xmlQuery object containing the 'body' node | ||
xmlQuery(ast).children().last(); // xmlQuery containing the 'body' node | ||
``` | ||
@@ -171,1 +175,5 @@ | ||
``` | ||
## License | ||
MIT |
@@ -105,29 +105,29 @@ const test = require('ava'); | ||
reader.on('done', ast => { | ||
let result, attr1, attr2; | ||
t.deepEqual(xQuery(ast).find('message').attr(), {id: '1001', type: 'letter'}, | ||
'attr from ast: get all'); | ||
result = xQuery(ast).find('message').attr(); | ||
t.deepEqual(result, {id: '1001', type: 'letter'}, 'attr from ast - get all'); | ||
t.is(xQuery(ast).find('message').attr('id'), '1001', | ||
'attr from ast: get by name'); | ||
attr1 = xQuery(ast).find('message').attr('id'); | ||
attr2 = xQuery(ast).find('message').attr('type'); | ||
t.is(attr1, '1001', 'attr from ast - get by name'); | ||
t.is(attr2, 'letter', 'attr from ast - get by name'); | ||
t.is(xQuery(ast).find('message').attr('type'), 'letter', | ||
'attr from ast: get by name'); | ||
result = xQuery(ast).find('message').attr('miss'); | ||
t.is(result, undefined, 'attr from ast - get by name miss'); | ||
t.is(xQuery(ast).find('message').attr('miss'), undefined, | ||
'attr from ast: get by name miss'); | ||
result = xQuery([ast, ast]).find('message').attr(); | ||
t.deepEqual(result, {id: '1001', type: 'letter'}, 'attr from ast array - get all'); | ||
t.deepEqual(xQuery([ast, ast]).find('message').attr(), {id: '1001', type: 'letter'}, | ||
'attr from ast array: get all'); | ||
attr1 = xQuery(ast).find('message').attr('id'); | ||
attr2 = xQuery(ast).find('message').attr('type'); | ||
t.is(attr1, '1001', 'attr from ast array - get by name'); | ||
t.is(attr2, 'letter', 'attr from ast array - get by name'); | ||
t.is(xQuery(ast).find('message').attr('id'), '1001', | ||
'attr from ast array: get by name'); | ||
result = xQuery([]).attr(); | ||
t.deepEqual(result, undefined, 'attr from empty array - get all'); | ||
t.is(xQuery(ast).find('message').attr('type'), 'letter', | ||
'attr from ast array: get by name'); | ||
result = xQuery([]).attr('miss'); | ||
t.deepEqual(result, undefined, 'attr from empty array - get by name'); | ||
t.deepEqual(xQuery([]).attr(), undefined, | ||
'attr from empty array - get all'); | ||
t.deepEqual(xQuery([]).attr('miss'), undefined, | ||
'attr from empty array - get by name'); | ||
t.end(); | ||
@@ -166,2 +166,3 @@ }); | ||
t.is(xQuery(ast).prop('unknown?'), undefined); | ||
t.is(xQuery([]).prop('zero'), undefined); | ||
t.end(); | ||
@@ -185,1 +186,25 @@ }); | ||
}); | ||
test.cb('map', t => { | ||
const reader = xmlReader.create(); | ||
reader.on('done', ast => { | ||
const result = xQuery(ast).children().map((node, index, array) => { | ||
t.deepEqual(node, array[index]); | ||
return node.name; | ||
}); | ||
const expected = ['to', 'from', 'subject', 'body']; | ||
t.deepEqual(result, expected); | ||
t.end(); | ||
}); | ||
reader.parse(xmlMessage1); | ||
}); | ||
test.cb('size, length', t => { | ||
const reader = xmlReader.create(); | ||
reader.on('done', ast => { | ||
t.is(xQuery(ast).children().size(), 4); | ||
t.is(xQuery(ast).children().length, 4); | ||
t.end(); | ||
}); | ||
reader.parse(xmlMessage1); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
57011
0.81%662
3.76%172
4.88%0
-100%