Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

xml

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

13

lib/xml.js

@@ -53,7 +53,8 @@ var escapeForXML = require('./escapeForXML');

var data = output;
delay(function () { stream.emit('data', data) });
stream.emit('end');
stream.readable = false;
stream.emit('close');
delay(function () {
stream.emit('data', data);
stream.emit('end');
stream.readable = false;
stream.emit('close');
});
}

@@ -282,2 +283,2 @@ }

module.exports = xml;
module.exports.element = module.exports.Element = element;
module.exports.element = module.exports.Element = element;
{
"name": "xml",
"version": "1.0.0",
"version": "1.0.1",
"description": "Fast and simple xml generator. Supports attributes, CDATA, etc. Includes tests and examples.",

@@ -31,7 +31,6 @@ "homepage": "http://github.com/dylang/node-xml",

"devDependencies": {
"chai": "~1.9.0",
"mocha": "~1.21.4"
"ava": "^0.11.0"
},
"scripts": {
"test": "mocha --reporter spec"
"test": "ava"
},

@@ -38,0 +37,0 @@ "main": "lib/xml.js",

@@ -1,4 +0,4 @@

# XML for Node [![Build Status](https://secure.travis-ci.org/dylang/node-xml.png)](http://travis-ci.org/dylang/node-xml)
# xml [![Build Status](https://api.travis-ci.org/dylang/node-xml.svg)](http://travis-ci.org/dylang/node-xml)
[![NPM](https://nodei.co/npm/xml.png?downloads=true)](https://nodei.co/npm/xml/)
[![NPM](https://nodei.co/npm/xml.png?downloads=true)](https://nodei.co/npm/xml/)

@@ -30,3 +30,3 @@ > Fast and simple Javascript-based XML generator/builder for Node projects.

```js
xml({a: 1})) === '<a>1</a>'
xml({a: 1}) === '<a>1</a>'
xml({nested: [{ keys: [{ fun: 'hi' }]}]}) === '<nested><keys><fun>hi</fun></keys></nested>'

@@ -175,3 +175,3 @@ ```

Tests included use Mocha. Use `npm test` to run the tests.
Tests included use [AVA](https://ava.li). Use `npm test` to run the tests.

@@ -186,28 +186,2 @@ $ npm test

Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests and issues,
especially when tests are included.
# License
(The MIT License)
Copyright (c) 2011-2014 Dylan Greene <dylang@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Contributions to the project are welcome. Feel free to fork and improve. I accept pull requests when tests are included.

@@ -1,101 +0,150 @@

/*
Run `npm test` to run tests
*/
import test from 'ava';
import xml from '../lib/xml';
var xml = require('../lib/xml');
var expect = require('chai').expect;
test('no elements', t => {
t.is(xml(), '');
t.is(xml([]), '');
t.is(xml('test'), 'test');
t.is(xml('scotch & whisky'), 'scotch &amp; whisky');
t.is(xml('bob\'s escape character'), 'bob&apos;s escape character');
});
describe('xml module', function(done) {
test('simple options', t => {
t.is(xml([{a: {}}]), '<a/>');
t.is(xml([{a: null}]), '<a/>');
t.is(xml([{a: []}]), '<a></a>');
t.is(xml([{a: -1}]), '<a>-1</a>');
t.is(xml([{a: false}]), '<a>false</a>');
t.is(xml([{a: 'test'}]), '<a>test</a>');
t.is(xml({a: {}}), '<a/>');
t.is(xml({a: null}), '<a/>');
t.is(xml({a: []}), '<a></a>');
t.is(xml({a: -1}), '<a>-1</a>');
t.is(xml({a: false}), '<a>false</a>');
t.is(xml({a: 'test'}), '<a>test</a>');
t.is(xml([{a: 'test'}, {b: 123}, {c: -0.5}]), '<a>test</a><b>123</b><c>-0.5</c>');
});
it('can be have no elements', function(done) {
expect(xml()).to.be.empty;
expect(xml([])).to.be.empty;
expect(xml('test')).to.equal('test');
expect(xml('test')).to.equal('test');
expect(xml('scotch & whisky')).to.equal('scotch &amp; whisky');
expect(xml('bob\'s escape character')).to.equal('bob&apos;s escape character');
done();
});
test('deeply nested objects', t => {
t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}]), '<a><b><c>1</c><c>2</c><c>3</c></b></a>');
});
it('works with simple options', function(done) {
expect(xml([ { a: {} }])).to.equal('<a/>');
expect(xml([ { a: null }])).to.equal('<a/>');
expect(xml([ { a: [] }])).to.equal('<a></a>');
expect(xml([ { a: -1 }])).to.equal('<a>-1</a>');
expect(xml([ { a: false }])).to.equal('<a>false</a>');
expect(xml([ { a: 'test' }])).to.equal('<a>test</a>');
expect(xml( { a: {} })).to.equal('<a/>');
expect(xml( { a: null })).to.equal('<a/>');
expect(xml( { a: [] })).to.equal('<a></a>');
expect(xml( { a: -1 })).to.equal('<a>-1</a>');
expect(xml( { a: false })).to.equal('<a>false</a>');
expect(xml( { a: 'test' })).to.equal('<a>test</a>');
expect(xml([ { a: 'test' }, { b: 123 }, { c: -0.5 } ])).to.equal('<a>test</a><b>123</b><c>-0.5</c>');
done();
});
test('indents property', t => {
t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], true), '<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], ' '), '<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
t.is(xml([{a: [{b: [{c: 1}, {c: 2}, {c: 3}]}]}], '\t'), '<a>\n\t<b>\n\t\t<c>1</c>\n\t\t<c>2</c>\n\t\t<c>3</c>\n\t</b>\n</a>');
t.is(xml({guid: [{_attr: {premalink: true}}, 'content']}, true), '<guid premalink="true">content</guid>');
});
it('works with deeply nested objects', function(done) {
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }]), '<a><b><c>1</c><c>2</c><c>3</c></b></a>');
done();
});
test('supports xml attributes', t => {
t.is(xml([{b: {_attr: {}}}]), '<b/>');
t.is(xml([{
a: {
_attr: {
attribute1: 'some value',
attribute2: 12345
}
}
}]), '<a attribute1="some value" attribute2="12345"/>');
t.is(xml([{
a: [{
_attr: {
attribute1: 'some value',
attribute2: 12345
}
}]
}]), '<a attribute1="some value" attribute2="12345"></a>');
t.is(xml([{
a: [{
_attr: {
attribute1: 'some value',
attribute2: 12345
}
}, 'content']
}]), '<a attribute1="some value" attribute2="12345">content</a>');
});
it('indents property', function(done) {
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], true)).to.equal('<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], ' ')).to.equal('<a>\n <b>\n <c>1</c>\n <c>2</c>\n <c>3</c>\n </b>\n</a>');
expect(xml([ { a: [ { b: [ { c: 1 }, { c: 2 }, { c: 3 } ] } ] }], '\t')).to.equal('<a>\n\t<b>\n\t\t<c>1</c>\n\t\t<c>2</c>\n\t\t<c>3</c>\n\t</b>\n</a>');
expect(xml({guid:[{_attr:{premalink:true}},'content']},true)).to.equal('<guid premalink="true">content</guid>');
done();
});
test('supports cdata', t => {
t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong>]]></a>');
t.is(xml([{
a: {
_attr: {attribute1: 'some value', attribute2: 12345},
_cdata: 'This is some <strong>CDATA</strong>'
}
}]), '<a attribute1="some value" attribute2="12345"><![CDATA[This is some <strong>CDATA</strong>]]></a>');
t.is(xml([{a: {_cdata: 'This is some <strong>CDATA</strong> with ]]> and then again ]]>'}}]), '<a><![CDATA[This is some <strong>CDATA</strong> with ]]]]><![CDATA[> and then again ]]]]><![CDATA[>]]></a>');
});
it('supports xml attributes', function(done) {
expect(xml([ { b: { _attr: {} } } ]), '<b/>');
expect(xml([ { a: { _attr: { attribute1: 'some value', attribute2: 12345 } } } ])).to.equal('<a attribute1="some value" attribute2="12345"/>');
expect(xml([ { a: [{ _attr: { attribute1: 'some value', attribute2: 12345 } }] } ])).to.equal('<a attribute1="some value" attribute2="12345"></a>');
expect(xml([ { a: [{ _attr: { attribute1: 'some value', attribute2: 12345 } }, 'content'] } ])).to.equal('<a attribute1="some value" attribute2="12345">content</a>');
done();
});
test('supports encoding', t => {
t.is(xml([{
a: [{
_attr: {
anglebrackets: 'this is <strong>strong</strong>',
url: 'http://google.com?s=opower&y=fun'
}
}, 'text']
}]), '<a anglebrackets="this is &lt;strong&gt;strong&lt;/strong&gt;" url="http://google.com?s=opower&amp;y=fun">text</a>');
});
it('supports cdata', function(done) {
expect(xml([ { a: { _cdata: 'This is some <strong>CDATA</strong>' } } ])).to.equal('<a><![CDATA[This is some <strong>CDATA</strong>]]></a>');
expect(xml([ { a: { _attr: { attribute1: 'some value', attribute2: 12345 }, _cdata: 'This is some <strong>CDATA</strong>' } } ])).to.equal('<a attribute1="some value" attribute2="12345"><![CDATA[This is some <strong>CDATA</strong>]]></a>');
expect(xml([ { a: { _cdata: 'This is some <strong>CDATA</strong> with ]]> and then again ]]>' } } ])).to.equal('<a><![CDATA[This is some <strong>CDATA</strong> with ]]]]><![CDATA[> and then again ]]]]><![CDATA[>]]></a>');
done();
test('supports stream interface', t => {
const elem = xml.element({_attr: {decade: '80s', locale: 'US'}});
const xmlStream = xml({toys: elem}, {stream: true});
const results = ['<toys decade="80s" locale="US">', '<toy>Transformers</toy>', '<toy><name>He-man</name></toy>', '<toy>GI Joe</toy>', '</toys>'];
elem.push({toy: 'Transformers'});
elem.push({toy: [{name: 'He-man'}]});
elem.push({toy: 'GI Joe'});
elem.close();
xmlStream.on('data', stanza => {
t.is(stanza, results.shift());
});
it('supports encoding', function(done) {
expect(xml([ { a: [ { _attr: { anglebrackets: 'this is <strong>strong</strong>', url: 'http://google.com?s=opower&y=fun' } }, 'text'] } ]), '<a anglebrackets="this is &lt;strong&gt;strong&lt;/strong&gt;" url="http://google.com?s=opower&amp;y=fun">text</a>');
done();
return new Promise( (resolve, reject) => {
xmlStream.on('close', () => {
t.same(results, []);
resolve();
});
xmlStream.on('error', reject);
});
});
it('supports stream interface', function (done) {
var elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
var xmlStream = xml({ toys: elem }, { stream: true });
var results = ['<toys decade="80s" locale="US">','<toy>Transformers</toy>','<toy><name>He-man</name></toy>','<toy>GI Joe</toy>','</toys>'];
test('streams end properly', t => {
const elem = xml.element({ _attr: { decade: '80s', locale: 'US'} });
const xmlStream = xml({ toys: elem }, { stream: true });
xmlStream.on('data', function (stanza) {
expect(stanza).to.equal(results.shift());
});
xmlStream.on('close', function () {
expect(results).to.be.be.empty;
done();
});
let gotData;
elem.push({ toy: 'Transformers' });
elem.push({ toy: [ { name: 'He-man' } ] });
setTimeout(function () {
elem.push({ toy: 'GI Joe' });
elem.close();
}, 10);
t.plan(7);
elem.push({ toy: 'Transformers' });
elem.push({ toy: 'GI Joe' });
elem.push({ toy: [{name:'He-man'}] });
elem.close();
xmlStream.on('data', data => {
t.ok(data);
gotData = true;
});
it('xml declaration options', function(done) {
expect(xml([ { a: 'test' }], { declaration: true })).to.equal('<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
expect(xml([ { a: 'test' }], { declaration: {encoding: 'foo' }})).to.equal('<?xml version="1.0" encoding="foo"?><a>test</a>');
expect(xml([ { a: 'test' }], { declaration: {standalone: 'yes' }})).to.equal('<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
expect(xml([ { a: 'test' }], { declaration: false })).to.equal('<a>test</a>');
expect(xml([ { a: 'test' }], { declaration: true, indent: '\n' })).to.equal('<?xml version="1.0" encoding="UTF-8"?>\n<a>test</a>');
expect(xml([ { a: 'test' }], {})).to.equal('<a>test</a>');
done();
xmlStream.on('end', () => {
t.ok(gotData);
});
});
return new Promise( (resolve, reject) => {
xmlStream.on('close', () => {
t.ok(gotData);
resolve();
});
xmlStream.on('error', reject);
});
});
test('xml declaration options', t => {
t.is(xml([{a: 'test'}], {declaration: true}), '<?xml version="1.0" encoding="UTF-8"?><a>test</a>');
t.is(xml([{a: 'test'}], {declaration: {encoding: 'foo'}}), '<?xml version="1.0" encoding="foo"?><a>test</a>');
t.is(xml([{a: 'test'}], {declaration: {standalone: 'yes'}}), '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><a>test</a>');
t.is(xml([{a: 'test'}], {declaration: false}), '<a>test</a>');
t.is(xml([{a: 'test'}], {declaration: true, indent: '\n'}), '<?xml version="1.0" encoding="UTF-8"?>\n<a>test</a>');
t.is(xml([{a: 'test'}], {}), '<a>test</a>');
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc