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

xml-js

Package Overview
Dependencies
Maintainers
1
Versions
49
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

xml-js - npm Package Compare versions

Comparing version 0.9.2 to 0.9.3

31

bin/cli.js

@@ -12,3 +12,3 @@ #!/usr/bin/env node

var requiredArgs = [
{arg: 'src', type: 'any', option: 'src', desc: 'Input file that need to be converted.'}
{arg: 'src', type: 'file', option: 'src', desc: 'Input file that need to be converted.'}
];

@@ -18,3 +18,2 @@ var optionalArgs = [

{arg: 'version', alias: 'v', type: 'flag', option: 'version', desc: 'Display version number of this module.'},
{arg: 'src', type: 'file', option: 'src', desc: 'Input file that need to be processed.'},
{arg: 'out', type: 'file', option: 'out', desc: 'Output file where result should be written.'},

@@ -55,4 +54,5 @@ {arg: 'to-json', type: 'flag', option:'toJason', desc: 'Convert.'},

options = common.mapCommandLineArgs(optionalArgs);
options = common.mapCommandLineArgs(requiredArgs, optionalArgs);
if (options.version) {

@@ -64,16 +64,19 @@ console.log(package.version);

process.exit(process.argv.length <= 2 ? 1 : 0);
} else if ('raw' in options && fs.statSync(options.raw[0]).isFile()) {
if (options.raw[0].split('.').pop() === 'xml') {
output = xml2json(fs.readFileSync(options.raw[0], 'utf8'), options);
} else if (options.raw[0].split('.').pop() === 'json') {
output = json2xml(fs.readFileSync(options.raw[0], 'utf8'), options);
} else if ('src' in options) {
//console.log('---------------' + fs.statSync(options.src).isFile());
if (fs.statSync(options.src).isFile()) {
if (options.src.split('.').pop() === 'xml') {
output = xml2json(fs.readFileSync(options.src, 'utf8'), options);
} else if (options.src.split('.').pop() === 'json') {
output = json2xml(fs.readFileSync(options.src, 'utf8'), options);
}
if (options.out) {
fs.writeFileSync(options.out, output, 'utf8');
} else {
console.log(output);
}
process.exit(0);
}
if (options.out) {
fs.writeFileSync(options.out, output, 'utf8');
} else {
console.log(output);
}
process.exit(0);
} else {
process.exit(1);
}

@@ -43,31 +43,27 @@ /*jslint node:true */

},
mapCommandLineArgs: function (optionalArgs) {
var r, i, j, raw = [], options = {};
function findIndex (argument, index) {
if (argument.alias === process.argv[i].slice(1) || argument.arg === process.argv[i].slice(2)) {
j = index;
mapCommandLineArgs: function (requiredArgs, optionalArgs) {
var options = {}, r, o, a = 2;
for (r = 0; r < requiredArgs.length; r += 1) {
if (a < process.argv.length && process.argv[a].substr(0, 1) !== '-' && process.argv[a] !== 'JASMINE_CONFIG_PATH=./jasmine.json') {
options[requiredArgs[r].option] = process.argv[a++];
} else {
break;
}
}
for (r = 2; r < process.argv.length; r += 1) {
if (process.argv[r].substr(0, 1) !== '-' && process.argv[r] !== 'JASMINE_CONFIG_PATH=./jasmine.json') {
if (!('raw' in options)) {
options.raw = [];
for (; a < process.argv.length; a += 1) {
for (o = 0; o < optionalArgs.length; o += 1) {
if (optionalArgs[o].alias === process.argv[a].slice(1) || optionalArgs[o].arg === process.argv[a].slice(2)) {
break;
}
options.raw.push(process.argv[r]);
} else {
break;
}
}
for (i = r; i < process.argv.length; i += 1) {
j = -1;
optionalArgs.forEach(findIndex);
if (j >= 0) {
switch (optionalArgs[j].type) {
if (o < optionalArgs.length) {
switch (optionalArgs[o].type) {
case 'file': case 'string': case 'number':
if (i + 1 < process.argv.length) {
options[optionalArgs[j].option] = (optionalArgs[j].type === 'number' ? Number(process.argv[++i]) : process.argv[++i]);
if (a + 1 < process.argv.length) {
a += 1;
options[optionalArgs[o].option] = (optionalArgs[o].type === 'number' ? Number(process.argv[a]) : process.argv[a]);
}
break;
case 'flag':
options[optionalArgs[j].option] = true; break;
options[optionalArgs[o].option] = true; break;
}

@@ -74,0 +70,0 @@ }

{
"name": "xml-js",
"version": "0.9.2",
"version": "0.9.3",
"description": "A convertor between XML text and Javascript object / JSON text.",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -28,3 +28,4 @@ ![XML ⇔ JS/JSON](http://nashwaan.github.io/xml-js/images/logo.svg)

* **Maintain Order of Sub-elements**:
I wanted `<a/><b/><a/>` to give output as `{"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"},{"type":"element","name":"a"}]}` instead of `{a:[{},{}],b:{}}`.
Instead of converting `<a/><b/><a/>` to `{a:[{},{}],b:{}}`, I wanted to preserve order of elements by doing this:
`{"elements":[{"type":"element","name":"a"},{"type":"element","name":"b"},{"type":"element","name":"a"}]}`.

@@ -55,9 +56,10 @@ * **Fully XML Compliant**:

Most XML parsers (including online parsers) convert `<a/>` to some compact result like `{"a":{}}`
instead of non-compact result like `{"elements":[{"type":"element","name":"a"}]}`.
While this result might work in most cases, there are cases when different elements are mixed inside a parent element: `<n><a x="1"/><b x="2"/><a x="3"/></n>`.
Most XML parsers (including online parsers) convert `<a/>` to some compact output like `{"a":{}}`
instead of non-compact output like `{"elements":[{"type":"element","name":"a"}]}`.
While compact output might work in most situations, there are cases when different elements are mixed inside a parent element: `<n><a x="1"/><b x="2"/><a x="3"/></n>`.
In this case, the compact output will be `{n:{a:[{_:{x:"1"}},{_:{x:"3"}}],b:{_:{x:"2"}}}}`,
which has merged the second `<a/>` with the first `<a/>` into an array and so the order is not preserved.
Although non-compact output is more accurate representation of original XML than compact version, the non-compact consumes more space on disk.
Although non-compact output is more accurate representation of original XML than compact version, the non-compact consumes more space.
This library provides both options. Use `{compact: false}` if you are not sure because it preserves everything;

@@ -74,3 +76,3 @@ otherwise use `{compact: true}` if you want to save space and you don't care about mixing elements of same type.

You can also installed it globally to use it as command line convertor.
You can also installed it globally to use it as a command line convertor.

@@ -77,0 +79,0 @@ ```bash

@@ -58,5 +58,5 @@ /*jslint node:true*/

xit('should convert xml file', function (done) {
exec('node ./bin/cli note.xml', function (error, stdout, stderr) {
expect(stdout).toEqual(packageInfo.version + '\n');
it('should convert xml file', function (done) {
exec('node ./bin/cli ./bin/test.xml', function (error, stdout, stderr) {
expect(stdout).toEqual('{"elements":[{"type":"element","name":"a","attributes":{"x":"1"},"elements":[{"type":"element","name":"b","elements":[{"type":"text","text":"bye!"}]}]}]}' + '\n');
done();

@@ -66,2 +66,9 @@ });

it('should convert xml file, --compact', function (done) {
exec('node ./bin/cli ./bin/test.xml --compact', function (error, stdout, stderr) {
expect(stdout).toEqual('{"a":{"_attributes":{"x":"1"},"b":{"_text":"bye!"}}}' + '\n');
done();
});
});
});

@@ -68,0 +75,0 @@

@@ -89,3 +89,3 @@ /*jslint node:true*/

process.argv.push('-v');
expect(convert.mapCommandLineArgs(possibleArgs)).toEqual({version: true});
expect(convert.mapCommandLineArgs({}, possibleArgs)).toEqual({version: true});
process.argv.pop();

@@ -97,3 +97,3 @@ });

process.argv.push('--spaces'); process.argv.push('5');
expect(convert.mapCommandLineArgs(possibleArgs)).toEqual({spaces: 5});
expect(convert.mapCommandLineArgs({}, possibleArgs)).toEqual({spaces: 5});
process.argv.pop(); process.argv.pop();

@@ -105,3 +105,3 @@ });

process.argv.push('--name'); process.argv.push('Foo');
expect(convert.mapCommandLineArgs(possibleArgs)).toEqual({name: 'Foo'});
expect(convert.mapCommandLineArgs({}, possibleArgs)).toEqual({name: 'Foo'});
process.argv.pop(); process.argv.pop();

@@ -113,3 +113,3 @@ });

process.argv.push('--input'); process.argv.push('test.txt');
expect(convert.mapCommandLineArgs(possibleArgs)).toEqual({input: 'test.txt'});
expect(convert.mapCommandLineArgs({}, possibleArgs)).toEqual({input: 'test.txt'});
process.argv.pop(); process.argv.pop();

@@ -121,3 +121,3 @@ });

process.argv.push('v');
expect(convert.mapCommandLineArgs(possibleArgs)).toEqual({});
expect(convert.mapCommandLineArgs({}, possibleArgs)).not.toEqual({version: true});
process.argv.pop();

@@ -129,3 +129,3 @@ });

process.argv.push('--input');
expect(convert.mapCommandLineArgs(possibleArgs)).toEqual({});
expect(convert.mapCommandLineArgs({}, possibleArgs)).toEqual({});
process.argv.pop();

@@ -132,0 +132,0 @@ });

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 too big to display

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