oniyi-utils-xml
Advanced tools
Comparing version 0.1.0 to 1.0.0
'use strict'; | ||
/** | ||
* XML NodeTypes | ||
* | ||
* http://www.w3schools.com/dom/dom_nodetype.asp | ||
* | ||
* 1 ELEMENT_NODE | ||
* 2 ATTRIBUTE_NODE | ||
* 3 TEXT_NODE | ||
* 4 CDATA_SECTION_NODE | ||
* 5 ENTITY_REFERENCE_NODE | ||
* 6 ENTITY_NODE | ||
* 7 PROCESSING_INSTRUCTION_NODE | ||
* 8 COMMENT_NODE | ||
* 9 DOCUMENT_NODE | ||
* 10 DOCUMENT_TYPE_NODE | ||
* 11 DOCUMENT_FRAGMENT_NODE | ||
* 12 NOTATION_NODE | ||
*/ | ||
// core node modules | ||
var xmldom = require('xmldom'), | ||
DOMParser = xmldom.DOMParser, | ||
XMLSerializer = xmldom.XMLSerializer, | ||
xpath = require('xpath'); | ||
// 3rd party modules | ||
const xmldom = require('xmldom'); | ||
const xpath = require('xpath'); | ||
const isString = require('lodash.isstring'); | ||
exports.parse = function parse(xmlString, mimeType, parserOptions) { | ||
parserOptions = parserOptions || {}; | ||
mimeType = (typeof mimeType === 'string') ? mimeType : 'text/xml'; | ||
// internal modules | ||
var parser = new DOMParser(parserOptions); | ||
return parser.parseFromString(xmlString, mimeType); | ||
}; | ||
const DOMParser = xmldom.DOMParser; | ||
const XMLSerializer = xmldom.XMLSerializer; | ||
exports.selectUseNamespaces = function selectUseNamespaces(nameSpaces) { | ||
return xpath.useNamespaces(nameSpaces); | ||
const parse = (xmlString, mimeType = 'text/xml', parserOptions = {}) => { | ||
const safeMimeType = (isString(mimeType) && mimeType) || 'text/xml'; | ||
const parser = new DOMParser(parserOptions); | ||
return parser.parseFromString(xmlString, safeMimeType); | ||
}; | ||
exports.serialize = function serialize() { | ||
var serializer = new XMLSerializer(); | ||
return serializer.serializeToString.apply(serializer, Array.prototype.slice.call(arguments, 0)); | ||
const selectUseNamespaces = nameSpaces => xpath.useNamespaces(nameSpaces); | ||
const serialize = (...args) => (new XMLSerializer()).serializeToString(...args); | ||
const select = (...args) => xpath.select(...args); | ||
const ensureXMLDoc = stringOrXMLDoc => (isString(stringOrXMLDoc) && parse(stringOrXMLDoc)) || stringOrXMLDoc; | ||
module.exports = { | ||
parse, | ||
select, | ||
serialize, | ||
selectUseNamespaces, | ||
ensureXMLDoc, | ||
}; | ||
exports.select = function select() { | ||
return xpath.select.apply(xpath, Array.prototype.slice.call(arguments, 0)); | ||
}; |
@@ -1,19 +0,17 @@ | ||
var util = require('util'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var Transform = require('stream').Transform; | ||
/* eslint-disable no-underscore-dangle */ | ||
var xmlUtils = require('./utils'); | ||
'use strict'; | ||
var allowedEncodings = [ | ||
'ascii', // for 7 bit ASCII data only. This encoding method is way fast, but is limited to the ascii character set. To convert a null character into 0x00, you should use 'utf8'. | ||
'utf8', // Multibyte encoded Unicode characters. It has become the dominant character encoding for the world wide web. | ||
'utf16le', // 2 or 4 bytes, little endian encoded Unicode characters, surrogate pairs (U+10000 to U+10FFFF) are supported. | ||
'ucs2', // Alias of 'utf16le'. | ||
'base64', // Base64 string encoding. | ||
'binary', // Method of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated. | ||
'hex' // This method is used to encode each byte as two hexadecimal characters. | ||
]; | ||
// core node modules | ||
const util = require('util'); | ||
const StringDecoder = require('string_decoder').StringDecoder; | ||
const Transform = require('stream').Transform; | ||
// 3rd party modules | ||
// internal modules | ||
const { parse } = require('./utils'); | ||
// Gets XML string data and emits the parsed XMLDocument object | ||
function XMLParseStream(options, encoding) { | ||
function XMLParseStream(options = {}, encoding) { | ||
if (!(this instanceof XMLParseStream)) { | ||
@@ -23,7 +21,6 @@ return new XMLParseStream(options, encoding); | ||
options = options || {}; | ||
options.readableObjectMode = true; | ||
Object.assign(options, { readableObjectMode: true }); | ||
Transform.call(this, options); | ||
this._encoding = (encoding && allowedEncodings.indexOf(encoding) > -1) ? encoding : 'utf8'; | ||
this._encoding = (Buffer.isEncoding(encoding)) ? encoding : 'utf8'; | ||
this._decoder = new StringDecoder(this._encoding); | ||
@@ -34,3 +31,3 @@ this._buffer = ''; | ||
XMLParseStream.prototype._transform = function(data, encoding, callback) { | ||
XMLParseStream.prototype._transform = function transform(data, encoding, callback) { | ||
// concatenate decoded string chunks in _buffer | ||
@@ -41,8 +38,8 @@ this._buffer += this._decoder.write(data); | ||
XMLParseStream.prototype._flush = function(callback) { | ||
XMLParseStream.prototype._flush = function flush(callback) { | ||
// make xmlDoc object from our _buffer | ||
var xmlString = this._buffer.trim(); | ||
const xmlString = this._buffer.trim(); | ||
try { | ||
var xmlDoc = xmlUtils.parse(xmlString); | ||
const xmlDoc = parse(xmlString); | ||
this.push(xmlDoc); | ||
@@ -49,0 +46,0 @@ } catch (err) { |
@@ -1,8 +0,16 @@ | ||
var util = require('util'); | ||
var Transform = require('stream').Transform; | ||
/* eslint-disable no-underscore-dangle */ | ||
var xmlUtils = require('./utils'); | ||
'use strict'; | ||
// core node modules | ||
const util = require('util'); | ||
const Transform = require('stream').Transform; | ||
// 3rd party modules | ||
// internal modules | ||
const { serialize } = require('./utils'); | ||
// Gets XML Object data and emits the stringified XMLDocument | ||
function XMLSerializeStream(options) { | ||
function XMLSerializeStream(options = {}) { | ||
if (!(this instanceof XMLSerializeStream)) { | ||
@@ -12,4 +20,3 @@ return new XMLSerializeStream(); | ||
options = options || {}; | ||
options.writableObjectMode = true; | ||
Object.assign(options, { writableObjectMode: true }); | ||
Transform.call(this, options); | ||
@@ -19,5 +26,5 @@ } | ||
XMLSerializeStream.prototype._transform = function(xmlDocument, encoding, callback) { | ||
XMLSerializeStream.prototype._transform = function transform(xmlDocument, encoding, callback) { | ||
try { | ||
var xmlString = xmlUtils.serialize(xmlDocument); | ||
const xmlString = serialize(xmlDocument); | ||
this.push(xmlString); | ||
@@ -24,0 +31,0 @@ } catch (err) { |
{ | ||
"name": "oniyi-utils-xml", | ||
"version": "0.1.0", | ||
"version": "1.0.0", | ||
"description": "utils for handling xml data", | ||
"author": { | ||
"name": "Benjamin Kroeger", | ||
"email": "benjamin.kroeger@gmail.com" | ||
"author": "Benjamin Kroeger <benjamin.kroeger@gmail.com>", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/benkroeger/oniyi-utils-xml.git" | ||
}, | ||
"repository": "benkroeger/oniyi-utils-xml", | ||
"license": "MIT", | ||
"main": "lib/index.js", | ||
"files": [ | ||
@@ -26,18 +27,24 @@ "lib", | ||
"dependencies": { | ||
"xmldom": "^0.1.19", | ||
"xpath": "0.0.9" | ||
"lodash.isstring": "4.0.1", | ||
"xmldom": "0.1.19", | ||
"xpath": "0.0.24" | ||
}, | ||
"devDependencies": { | ||
"grunt-cli": "^0.1.13", | ||
"grunt-contrib-jshint": "^0.11.0", | ||
"grunt-contrib-nodeunit": "^0.4.1", | ||
"grunt-contrib-watch": "^0.6.1", | ||
"grunt-mocha-cli": "^1.12.0", | ||
"jshint-stylish": "^1.0.1", | ||
"load-grunt-tasks": "^3.1.0", | ||
"time-grunt": "^1.1.0" | ||
"ava": "0.19.1", | ||
"eslint": "3.15.0", | ||
"eslint-config-oniyi": "4.4.0", | ||
"eslint-plugin-ava": "4.2.1" | ||
}, | ||
"scripts": { | ||
"test": "grunt" | ||
"lint-fix": "eslint . --fix", | ||
"lint": "eslint .", | ||
"test": "ava --verbose" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/benkroeger/oniyi-utils-xml/issues" | ||
}, | ||
"homepage": "https://github.com/benkroeger/oniyi-utils-xml#readme", | ||
"directories": { | ||
"test": "test" | ||
} | ||
} |
@@ -29,4 +29,23 @@ > utils for handling xml data | ||
## XML NodeTypes | ||
http://www.w3schools.com/dom/dom_nodetype.asp | ||
1 ELEMENT_NODE | ||
2 ATTRIBUTE_NODE | ||
3 TEXT_NODE | ||
4 CDATA_SECTION_NODE | ||
5 ENTITY_REFERENCE_NODE | ||
6 ENTITY_NODE | ||
7 PROCESSING_INSTRUCTION_NODE | ||
8 COMMENT_NODE | ||
9 DOCUMENT_NODE | ||
10 DOCUMENT_TYPE_NODE | ||
11 DOCUMENT_FRAGMENT_NODE | ||
12 NOTATION_NODE | ||
## License | ||
MIT © [Benjamin Kroeger]() |
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
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 bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
4
7
1
0
51
0
5385
3
109
+ Addedlodash.isstring@4.0.1
+ Addedlodash.isstring@4.0.1(transitive)
+ Addedxmldom@0.1.19(transitive)
+ Addedxpath@0.0.24(transitive)
- Removedxmldom@0.1.31(transitive)
- Removedxpath@0.0.9(transitive)
Updatedxmldom@0.1.19
Updatedxpath@0.0.24