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

builder: support building Date object #510

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/builder.js

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

8 changes: 6 additions & 2 deletions src/builder.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,15 @@ class exports.Builder
else
element = render(element.ele(key), entry).up()

# Case #4 Objects
# Case #4 Date objects
else if child instanceof Date
element = element.ele(key, child.toISOString()).up()

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

# Case #5 String and remaining types
# Case #6 String and remaining types
else
if typeof child is 'string' && @options.cdata && requiresCDATA child
element = element.ele(key).raw(wrapCDATA child).up()
Expand Down
14 changes: 14 additions & 0 deletions test/builder.test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -281,3 +281,17 @@ module.exports =
actual = builder.buildObject obj
diffeq expected, actual
test.finish()

'test building Date object': (test) ->
expected = """
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xml>
<DateTime>2001-10-26T21:32:52.126Z</DateTime>
</xml>

"""
builder = new xml2js.Builder()
obj = { "xml": { "DateTime": new Date("2001-10-26T21:32:52.126Z") } }
actual = builder.buildObject obj
diffeq expected, actual
test.finish()