arraybuffer-xml-parser
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -17,3 +17,3 @@ 'use strict'; | ||
trimValues: true, | ||
attributeNamePrefix: '@_', | ||
attributeNamePrefix: '$', | ||
attributesNodeName: false, | ||
@@ -146,11 +146,11 @@ ignoreAttributes: false, | ||
* @param {object} target | ||
* @param {object} a | ||
* @param {object} source | ||
*/ | ||
function merge(target, a, arrayMode) { | ||
if (!a) return; | ||
for (const key in a) { | ||
function merge(target, source, arrayMode) { | ||
if (!source) return; | ||
for (const key in source) { | ||
if (arrayMode === 'strict') { | ||
target[key] = [a[key]]; | ||
target[key] = [source[key]]; | ||
} else { | ||
target[key] = a[key]; | ||
target[key] = source[key]; | ||
} | ||
@@ -200,3 +200,3 @@ } | ||
match[4] = options.attributeValueProcessor(match[4], attrName); | ||
attributes[options.attributeNamePrefix + attrName] = stringParseValue( | ||
attributes[attrName] = stringParseValue( | ||
match[4], | ||
@@ -206,14 +206,7 @@ options.dynamicTypingAttributeValue, | ||
} else if (options.allowBooleanAttributes) { | ||
attributes[options.attributeNamePrefix + attrName] = true; | ||
attributes[attrName] = true; | ||
} | ||
} | ||
} | ||
if (!Object.keys(attributes).length) { | ||
return; | ||
} | ||
if (options.attributesNodeName) { | ||
const attrCollection = {}; | ||
attrCollection[options.attributesNodeName] = attributes; | ||
return attrCollection; | ||
} | ||
if (isEmptyObject(attributes)) return; | ||
return attributes; | ||
@@ -530,3 +523,20 @@ } | ||
merge(result, node.attributes, arrayMode); | ||
if (node.attributes && !isEmptyObject(node.attributes)) { | ||
let attributes = options.parseAttributesString ? {} : node.attributes; | ||
if (options.attributeNamePrefix) { | ||
// need to rename the attributes | ||
const renamedAttributes = {}; | ||
for (let key in node.attributes) { | ||
renamedAttributes[options.attributeNamePrefix + key] = | ||
node.attributes[key]; | ||
} | ||
attributes = renamedAttributes; | ||
} | ||
if (options.attributesNodeName) { | ||
let encapsulatedAttributes = {}; | ||
encapsulatedAttributes[options.attributesNodeName] = attributes; | ||
attributes = encapsulatedAttributes; | ||
} | ||
merge(result, attributes, arrayMode); | ||
} | ||
@@ -565,3 +575,3 @@ const keys = Object.keys(node.children); | ||
* @param {object} [options={}] | ||
* @param {string} [attributeNamePrefix='@_'] | ||
* @param {string} [attributeNamePrefix='$'] | ||
* @param {boolean} [attributesNodeName=false] | ||
@@ -568,0 +578,0 @@ * @param {string} [textNodeName='#text'] |
{ | ||
"name": "arraybuffer-xml-parser", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Parse XML files contained in an array buffer", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -51,3 +51,3 @@ # arraybuffer-xml-parser | ||
| trimValues | trim string values of an attribute or node | `true` | | ||
| attributeNamePrefix | prepend given string to attribute name for identification | `'@\_'` | | ||
| attributeNamePrefix | prepend given string to attribute name for identification | `'$'` | | ||
| attributesNodeName | (Valid name) Group all the attributes as properties of given name. | `false` | | ||
@@ -54,0 +54,0 @@ | ignoreAttributes | Ignore attributes to be parsed. | `false` | |
@@ -9,3 +9,3 @@ import { defaultOptions } from './traversable/defaultOptions'; | ||
* @param {object} [options={}] | ||
* @param {string} [attributeNamePrefix='@_'] | ||
* @param {string} [attributeNamePrefix='$'] | ||
* @param {boolean} [attributesNodeName=false] | ||
@@ -12,0 +12,0 @@ * @param {string} [textNodeName='#text'] |
@@ -11,3 +11,3 @@ const utf8Decoder = new TextDecoder(); | ||
trimValues: true, | ||
attributeNamePrefix: '@_', | ||
attributeNamePrefix: '$', | ||
attributesNodeName: false, | ||
@@ -14,0 +14,0 @@ ignoreAttributes: false, |
import { parseString } from 'dynamic-typing'; | ||
import { getAllMatches } from '../util'; | ||
import { getAllMatches, isEmptyObject } from '../util'; | ||
@@ -25,3 +25,3 @@ const newLocal = '([^\\s=]+)\\s*(=\\s*([\'"])(.*?)\\3)?'; | ||
match[4] = options.attributeValueProcessor(match[4], attrName); | ||
attributes[options.attributeNamePrefix + attrName] = stringParseValue( | ||
attributes[attrName] = stringParseValue( | ||
match[4], | ||
@@ -31,14 +31,7 @@ options.dynamicTypingAttributeValue, | ||
} else if (options.allowBooleanAttributes) { | ||
attributes[options.attributeNamePrefix + attrName] = true; | ||
attributes[attrName] = true; | ||
} | ||
} | ||
} | ||
if (!Object.keys(attributes).length) { | ||
return; | ||
} | ||
if (options.attributesNodeName) { | ||
const attrCollection = {}; | ||
attrCollection[options.attributesNodeName] = attributes; | ||
return attrCollection; | ||
} | ||
if (isEmptyObject(attributes)) return; | ||
return attributes; | ||
@@ -45,0 +38,0 @@ } |
@@ -41,3 +41,20 @@ import { parseString } from 'dynamic-typing'; | ||
merge(result, node.attributes, arrayMode); | ||
if (node.attributes && !isEmptyObject(node.attributes)) { | ||
let attributes = options.parseAttributesString ? {} : node.attributes; | ||
if (options.attributeNamePrefix) { | ||
// need to rename the attributes | ||
const renamedAttributes = {}; | ||
for (let key in node.attributes) { | ||
renamedAttributes[options.attributeNamePrefix + key] = | ||
node.attributes[key]; | ||
} | ||
attributes = renamedAttributes; | ||
} | ||
if (options.attributesNodeName) { | ||
let encapsulatedAttributes = {}; | ||
encapsulatedAttributes[options.attributesNodeName] = attributes; | ||
attributes = encapsulatedAttributes; | ||
} | ||
merge(result, attributes, arrayMode); | ||
} | ||
@@ -44,0 +61,0 @@ const keys = Object.keys(node.children); |
@@ -28,11 +28,11 @@ const nameStartChar = | ||
* @param {object} target | ||
* @param {object} a | ||
* @param {object} source | ||
*/ | ||
export function merge(target, a, arrayMode) { | ||
if (!a) return; | ||
for (const key in a) { | ||
export function merge(target, source, arrayMode) { | ||
if (!source) return; | ||
for (const key in source) { | ||
if (arrayMode === 'strict') { | ||
target[key] = [a[key]]; | ||
target[key] = [source[key]]; | ||
} else { | ||
target[key] = a[key]; | ||
target[key] = source[key]; | ||
} | ||
@@ -39,0 +39,0 @@ } |
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
52251
1189