Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

new builder options: tagNameProcessors and attrNameProcessors #462

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ node_modules
npm-debug.log
.nyc_output
coverage
package-lock.json
42 changes: 37 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ var xml = builder.buildObject(obj);
Processing attribute, tag names and values
------------------------------------------

Since 0.4.1 you can optionally provide the parser with attribute name and tag name processors as well as element value processors (Since 0.4.14, you can also optionally provide the parser with attribute value processors):
Since 0.4.1 you can optionally provide the parser with attribute name and tag name processors as well as element value processors. Since 0.4.14, you can also optionally provide the parser with attribute value processors. Since 0.4.20 you can optionally provide the builder with attribute name and tag name processors:

```javascript

Expand All @@ -189,8 +189,20 @@ parseString(xml, {
function (err, result) {
// processed data
});

const builder = new Builder({
tagNameProcessors: [nameToUpperCase],
attrNameProcessors: [nameToUpperCase]
});
xml = builder.buildObject(js);
```

Note that in most cases, you probably don't want to use the same
name and/or value processors for parser and builder
(e.g., the parser should strip namespace prefixes from tag names,
but the builder should add prefixes), so you **shouldn't**
use the same `options` object for parser and builder.

The `tagNameProcessors` and `attrNameProcessors` options
accept an `Array` of functions with the following signature:

Expand All @@ -215,7 +227,7 @@ function (value, name) {
Some processors are provided out-of-the-box and can be found in `lib/processors.js`:

- `normalize`: transforms the name to lowercase.
(Automatically used when `options.normalize` is set to `true`)
(Automatically used when `options.normalizeTags` is set to `true`)

- `firstCharLowerCase`: transforms the first character to lower case.
E.g. 'MyTagName' becomes 'myTagName'
Expand Down Expand Up @@ -294,7 +306,7 @@ value})``. Possible options are:
return name
}
```
Added in 0.4.14
Added in 0.4.1. Note: In most cases, you probably **don't** want to use the same `attrNameProcessors` for parser and builder, so you shouldn't use the same `options` object for parser and builder.
* `attrValueProcessors` (default: `null`): Allows the addition of attribute
value processing functions. Accepts an `Array` of functions with following
signature:
Expand All @@ -304,7 +316,7 @@ value})``. Possible options are:
return name
}
```
Added in 0.4.1
Added in 0.4.14
* `tagNameProcessors` (default: `null`): Allows the addition of tag name
processing functions. Accepts an `Array` of functions with following
signature:
Expand All @@ -314,7 +326,7 @@ value})``. Possible options are:
return name
}
```
Added in 0.4.1
Added in 0.4.1. Note: In most cases, you probably **don't** want to use the same `tagNameProcessors` for parser and builder, so you shouldn't use the same `options` object for parser and builder.
* `valueProcessors` (default: `null`): Allows the addition of element value
processing functions. Accepts an `Array` of functions with following
signature:
Expand Down Expand Up @@ -350,6 +362,26 @@ Possible options are:
* `cdata` (default: `false`): wrap text nodes in `<![CDATA[ ... ]]>` instead of
escaping when necessary. Does not add `<![CDATA[ ... ]]>` if it is not required.
Added in 0.4.5.
* `attrNameProcessors` (default: `null`): Allows the addition of attribute
name processing functions. Accepts an `Array` of functions with following
signature:
```javascript
function (name){
//do something with `name`
return name
}
```
Added in 0.4.20. Note: In most cases, you probably **don't** want to use the same `attrNameProcessors` for parser and builder, so you shouldn't use the same `options` object for parser and builder.
* `tagNameProcessors` (default: `null`): Allows the addition of tag name
processing functions. Accepts an `Array` of functions with following
signature:
```javascript
function (name){
//do something with `name`
return name
}
```
Added in 0.4.20. Note: In most cases, you probably **don't** want to use the same `tagNameProcessors` for parser and builder, so you shouldn't use the same `options` object for parser and builder.

`renderOpts`, `xmldec`,`doctype` and `headless` pass through to
[xmlbuilder-js](https://github.com/oozcitak/xmlbuilder-js).
Expand Down
33 changes: 24 additions & 9 deletions lib/builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 9 additions & 7 deletions lib/parser.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 17 additions & 8 deletions src/builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ escapeCDATA = (entry) ->
# When later parsed, it will be put back together as ']]>'
return entry.replace ']]>', ']]]]><![CDATA[>'

processItem = (processors, item, key) ->
if processors
item = process(item, key) for process in processors
return item

class exports.Builder
constructor: (opts) ->
# copy this versions default options
Expand All @@ -37,6 +42,7 @@ class exports.Builder
# we'll take the first element as the root element
rootName = Object.keys(rootObj)[0]
rootObj = rootObj[rootName]
rootName = processItem(@options.tagNameProcessors, rootName)
else
# otherwise we'll use whatever they've set, or the default
rootName = @options.rootName
Expand All @@ -51,15 +57,18 @@ class exports.Builder
else if Array.isArray obj
# fix issue #119
for own index, child of obj
for key, entry of child
element = render(element.ele(key), entry).up()
for key, entry of child # TODO: should this be 'for own key...'?
tag = processItem(@options.tagNameProcessors, key)
element = render(element.ele(tag), entry).up()
else
for own key, child of obj
tag = processItem(@options.tagNameProcessors, key)
# Case #1 Attribute
if key is attrkey
if typeof child is "object"
# Inserts tag attributes
for attr, value of child
attr = processItem(@options.attrNameProcessors, attr)
element = element.att(attr, value)

# Case #2 Char data (CDATA, etc.)
Expand All @@ -74,24 +83,24 @@ class exports.Builder
for own index, entry of child
if typeof entry is 'string'
if @options.cdata && requiresCDATA entry
element = element.ele(key).raw(wrapCDATA entry).up()
element = element.ele(tag).raw(wrapCDATA entry).up()
else
element = element.ele(key, entry).up()
element = element.ele(tag, entry).up()
else
element = render(element.ele(key), entry).up()
element = render(element.ele(tag), entry).up()

# Case #4 Objects
else if typeof child is "object"
element = render(element.ele(key), child).up()
element = render(element.ele(tag), child).up()

# Case #5 String and remaining types
else
if typeof child is 'string' && @options.cdata && requiresCDATA child
element = element.ele(key).raw(wrapCDATA child).up()
element = element.ele(tag).raw(wrapCDATA child).up()
else
if not child?
child = ''
element = element.ele(key, child.toString()).up()
element = element.ele(tag, child.toString()).up()

element

Expand Down
11 changes: 6 additions & 5 deletions src/parser.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ isEmpty = (thing) ->
return typeof thing is "object" && thing? && Object.keys(thing).length is 0

processItem = (processors, item, key) ->
item = process(item, key) for process in processors
if processors
item = process(item, key) for process in processors
return item

class exports.Parser extends events.EventEmitter
Expand Down Expand Up @@ -108,15 +109,15 @@ class exports.Parser extends events.EventEmitter
for own key of node.attributes
if attrkey not of obj and not @options.mergeAttrs
obj[attrkey] = {}
newValue = if @options.attrValueProcessors then processItem(@options.attrValueProcessors, node.attributes[key], key) else node.attributes[key]
processedKey = if @options.attrNameProcessors then processItem(@options.attrNameProcessors, key) else key
newValue = processItem(@options.attrValueProcessors, node.attributes[key], key)
processedKey = processItem(@options.attrNameProcessors, key)
if @options.mergeAttrs
@assignOrPush obj, processedKey, newValue
else
obj[attrkey][processedKey] = newValue

# need a place to store the node name
obj["#name"] = if @options.tagNameProcessors then processItem(@options.tagNameProcessors, node.name) else node.name
obj["#name"] = processItem(@options.tagNameProcessors, node.name)
if (@options.xmlns)
obj[@options.xmlnskey] = {uri: node.uri, local: node.local}
stack.push obj
Expand All @@ -138,7 +139,7 @@ class exports.Parser extends events.EventEmitter
else
obj[charkey] = obj[charkey].trim() if @options.trim
obj[charkey] = obj[charkey].replace(/\s{2,}/g, " ").trim() if @options.normalize
obj[charkey] = if @options.valueProcessors then processItem @options.valueProcessors, obj[charkey], nodeName else obj[charkey]
obj[charkey] = processItem(@options.valueProcessors, obj[charkey], nodeName)
# also do away with '#' key altogether, if there's no subkeys
# unless EXPLICIT_CHARKEY is set
if Object.keys(obj).length == 1 and charkey of obj and not @EXPLICIT_CHARKEY
Expand Down
Loading