Comparing version 0.7.1 to 0.8.0
@@ -16,2 +16,3 @@ /** | ||
var chars = { | ||
'&': '&', | ||
'<': '<', | ||
@@ -22,3 +23,2 @@ '>': '>', | ||
'#': '#', | ||
'&': '&', | ||
'"': '"', | ||
@@ -25,0 +25,0 @@ "'": ''' |
var expat = require('node-expat'); | ||
var sanitizer = require('./sanitize.js') | ||
var joi = require('joi'); | ||
var hoek = require('hoek'); | ||
@@ -140,14 +142,14 @@ // This object will hold the final result. | ||
options = { | ||
object: false, | ||
reversible: false, | ||
coerce: true, | ||
sanitize: true, | ||
trim: true | ||
var schema = { | ||
object: joi.boolean().default(false), | ||
reversible: joi.boolean().default(false), | ||
coerce: joi.boolean().default(true), | ||
sanitize: joi.boolean().default(true), | ||
trim: joi.boolean().default(true), | ||
arrayNotation: joi.boolean().default(false) | ||
}; | ||
var validation = joi.validate(_options, schema); | ||
hoek.assert(validation.error === null, validation.error); | ||
options = validation.value; | ||
for (var opt in _options) { | ||
options[opt] = _options[opt]; | ||
} | ||
if (!parser.parse(xml)) { | ||
@@ -154,0 +156,0 @@ throw new Error('There are errors in your xml file: ' + parser.getError()); |
@@ -1,19 +0,23 @@ | ||
{ "name" : "xml2json", | ||
"version": "0.7.1", | ||
"author": "Andrew Turley", | ||
"email": "aturley@buglabs.net", | ||
"description" : "Converts xml to json and vice-versa, using node-expat.", | ||
"repository": "git://github.com/buglabs/node-xml2json.git", | ||
"license": "MIT", | ||
"main": "index", | ||
"contributors":[ | ||
{"name": "Camilo Aguilar", "email": "camilo.aguilar@gmail.com"} | ||
], | ||
"dependencies": { | ||
"node-expat": "^2.3.7" | ||
}, | ||
"bin": { | ||
"xml2json": "bin/xml2json" | ||
} | ||
{ | ||
"name": "xml2json", | ||
"version": "0.8.0", | ||
"description": "Converts xml to json and vice-versa, using node-expat.", | ||
"repository": "git://github.com/buglabs/node-xml2json.git", | ||
"license": "MIT", | ||
"main": "index", | ||
"scripts": { | ||
"test": "make test" | ||
}, | ||
"dependencies": { | ||
"hoek": "^2.14.0", | ||
"joi": "^6.4.3", | ||
"node-expat": "^2.3.7" | ||
}, | ||
"bin": { | ||
"xml2json": "bin/xml2json" | ||
}, | ||
"devDependencies": { | ||
"code": "^1.4.1", | ||
"lab": "5.x.x" | ||
} | ||
} | ||
167
test/test.js
@@ -6,62 +6,127 @@ var fs = require('fs'); | ||
var fixturesPath = __dirname + '/fixtures'; | ||
var Code = require('code'); | ||
var Lab = require('lab'); | ||
fs.readdir(fixturesPath, function(err, files) { | ||
for (var i in files) { | ||
var file = files[i]; | ||
var ext = path.extname(file); | ||
if (ext == '.xml') { | ||
var basename = path.basename(file, '.xml'); | ||
// Test shortcuts | ||
var data = fs.readFileSync(fixturesPath + '/' + file); | ||
var result = parser.toJson(data, {reversible: true}); | ||
var lab = exports.lab = Lab.script(); | ||
var expect = Code.expect; | ||
var describe = lab.describe; | ||
var it = lab.test; | ||
var data2 = fs.readFileSync(fixturesPath + '/' + file); | ||
if (file.indexOf('spacetext') >= 0) { | ||
result = parser.toJson(data2, {trim: false, coerce: false}); | ||
} else if (file.indexOf('coerce') >= 0) { | ||
result = parser.toJson(data2, {coerce: false}); | ||
} else if (file.indexOf('domain') >= 0) { | ||
result = parser.toJson(data2, {coerce: false}); | ||
} else if (file.indexOf('large') >= 0) { | ||
result = parser.toJson(data2, {coerce: false, trim: true, sanitize: false}); | ||
} else if (file.indexOf('array-notation') >= 0) { | ||
result = parser.toJson(data2, {arrayNotation: true}); | ||
} else { | ||
result = parser.toJson(data2, {trim: false}); | ||
} | ||
var internals = {}; | ||
var jsonFile = basename + '.json'; | ||
var expected = fs.readFileSync(fixturesPath + '/' + jsonFile) + ''; | ||
if (expected) { | ||
expected = expected.trim(); | ||
} | ||
// console.log('============ Got ==============='); | ||
// console.log(result); | ||
// console.log('============ Expected ==============='); | ||
// console.log(expected) | ||
// console.log('====================================='); | ||
assert.deepEqual(result, expected, jsonFile + ' and ' + file + ' are different'); | ||
console.log('[xml2json: ' + file + '->' + jsonFile + '] passed!'); | ||
} else if( ext == '.json') { | ||
var basename = path.basename(file, '.json'); | ||
if (basename.match('reversible')) { | ||
var data = fs.readFileSync(fixturesPath + '/' + file); | ||
var result = parser.toXml(data); | ||
describe('xml2json', function () { | ||
var xmlFile = basename.split('-')[0] + '.xml'; | ||
var expected = fs.readFileSync(fixturesPath + '/' + xmlFile) + ''; | ||
it('converts with array-notation', function (done) { | ||
if (expected) { | ||
expected = expected.trim(); | ||
} | ||
//console.log(result + '<---'); | ||
assert.deepEqual(result, expected, xmlFile + ' and ' + file + ' are different'); | ||
console.log('[json2xml: ' + file + '->' + xmlFile + '] passed!'); | ||
} | ||
} | ||
} | ||
var xml = internals.readFixture('array-notation.xml'); | ||
var result = parser.toJson(xml, { arrayNotation: true }); | ||
var json = internals.readFixture('array-notation.json'); | ||
expect(result).to.deep.equal(json); | ||
done(); | ||
}); | ||
it('coerces', function (done) { | ||
var xml = internals.readFixture('coerce.xml'); | ||
var result = parser.toJson(xml, { coerce: false }); | ||
var json = internals.readFixture('coerce.json'); | ||
expect(result + '\n').to.deep.equal(json); | ||
done(); | ||
}); | ||
it('handles domain', function (done) { | ||
var xml = internals.readFixture('domain.xml'); | ||
var result = parser.toJson(xml, { coerce: false }); | ||
var json = internals.readFixture('domain.json'); | ||
expect(result + '\n').to.deep.equal(json); | ||
done(); | ||
}); | ||
it('does large file', function (done) { | ||
var xml = internals.readFixture('large.xml'); | ||
var result = parser.toJson(xml, { coerce: false, trim: true, sanitize: false }); | ||
var json = internals.readFixture('large.json'); | ||
expect(result + '\n').to.deep.equal(json); | ||
done(); | ||
}); | ||
it('handles reorder', function (done) { | ||
var xml = internals.readFixture('reorder.xml'); | ||
var result = parser.toJson(xml, {}); | ||
var json = internals.readFixture('reorder.json'); | ||
expect(result).to.deep.equal(json); | ||
done(); | ||
}); | ||
it('handles text with space', function (done) { | ||
var xml = internals.readFixture('spacetext.xml'); | ||
var result = parser.toJson(xml, { coerce: false, trim: false }); | ||
var json = internals.readFixture('spacetext.json'); | ||
expect(result).to.deep.equal(json); | ||
done(); | ||
}); | ||
it('does xmlsanitize', function (done) { | ||
var xml = internals.readFixture('xmlsanitize.xml'); | ||
var result = parser.toJson(xml, {}); | ||
var json = internals.readFixture('xmlsanitize.json'); | ||
expect(result).to.deep.equal(json); | ||
done(); | ||
}); | ||
it('throws error on bad options', function (done) { | ||
var throws = function() { | ||
var result = parser.toJson(xml, { derp: true}); | ||
}; | ||
expect(throws).to.throw(); | ||
done(); | ||
}); | ||
}); | ||
describe('json2xml', function () { | ||
it('converts domain to json', function (done) { | ||
var json = internals.readFixture('domain-reversible.json'); | ||
var result = parser.toXml(json); | ||
var xml = internals.readFixture('domain.xml'); | ||
expect(result+'\n').to.deep.equal(xml); | ||
done(); | ||
}); | ||
}); | ||
internals.readFixture = function (file) { | ||
return fs.readFileSync(__dirname + '/fixtures/' + file, { encoding: 'utf-8' }); | ||
}; |
Sorry, the diff of this file is not supported yet
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
No contributors or author data
MaintenancePackage does not specify a list of contributors or an author in package.json.
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
78336
37
551
3
2
2
+ Addedhoek@^2.14.0
+ Addedjoi@^6.4.3
+ Addedhoek@2.16.3(transitive)
+ Addedisemail@1.2.0(transitive)
+ Addedjoi@6.10.1(transitive)
+ Addedmoment@2.30.1(transitive)
+ Addedtopo@1.1.0(transitive)