Skip to content

Commit

Permalink
Merge pull request joelvh#19 from mikejpeters/ignore-empty
Browse files Browse the repository at this point in the history
Add ignoreEmpty option that excludes null and undefined
  • Loading branch information
joelvh committed Feb 21, 2016
2 parents 438f3c5 + 94de997 commit e772f5f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ The "all" option only works if "choose" is not defined (see below).
key: 'ASIN'
value: 'Title'
images:
path: '.'
path: '.'

Any properties on the matched object that are null or undefined will not be copied to the new JSON object.
To include these properties set "ignoreEmpty" to false.

ignoreEmpty: false

The "choose" property defines an array of properties on the original JSON object to transform and skip the rest.

Expand Down
2 changes: 1 addition & 1 deletion lib/ObjectTemplate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ class ObjectTemplate
@aggregateValue context, formatted.key, formatted.value

aggregateValue: (context, key, value) =>
return context unless value?
return context unless value? or !@config.ignoreEmpty

# if context is an array, just add the value
if sysmo.isArray(context)
Expand Down
1 change: 1 addition & 0 deletions lib/TemplateConfig.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class TemplateConfig
@nestTemplate = !!config.nested
@includeAll = !!config.all
@ensureArray = !!config.ensureArray
@ignoreEmpty = config.ignoreEmpty != false

@config = config

Expand Down
18 changes: 17 additions & 1 deletion test/transform.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ json =
{ id: 'yankees', name: 'New York Yankees', players: [ 'Alex', 'Starlin' ] }
{ id: 'cubs', name: 'Chicago Cubs', players: 'Jason' }
]
user:
name: 'Bob',
title: '',
age: undefined,
email: null

describe 'ObjectTemplate', ->

Expand All @@ -30,4 +35,15 @@ describe 'ObjectTemplate', ->
it 'should wrap the map values in array when `key` and `value` are set and `ensureArray` is `true`', ->
new json2json.ObjectTemplate { path: 'sportsTeams', key: 'id', value: 'players', ensureArray: true }
.transform json
.cubs.should.deep.equal [ 'Jason' ]
.cubs.should.deep.equal [ 'Jason' ]

it 'does not include properties with a `null` or `undefined` value by default', ->
new json2json.ObjectTemplate { path: 'user', all: true }
.transform json
.should.include.keys [ 'name', 'title' ]
.should.not.include.keys [ 'age', 'email' ]

it 'includes properties with a `null` or `undefined` value if `ignoreEmpty` is `false`', ->
new json2json.ObjectTemplate { path: 'user', all: true, ignoreEmpty: false }
.transform json
.should.include.keys [ 'age', 'email' ]

0 comments on commit e772f5f

Please sign in to comment.