Skip to content

Commit

Permalink
Merge pull request #12 from stopcoder/master
Browse files Browse the repository at this point in the history
Allow 'format' function to return an array to make a flat structure
  • Loading branch information
joelvh committed May 12, 2015
2 parents a5c08f6 + 084f0b7 commit 54aa5f3
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ The "choose" property defines an array of properties on the original JSON object
choose: ['SmallImage', 'MediumImage', 'LargeImage']

The "format" property defines a function that processes each of the values retrieved from the original JSON object
and returns an object with "key" and "value" properties.
and returns an object with "key" and "value" properties or an array which contains object(s) with "key" and "value" properties. If an array is returned, all entries in the array are added to the current context node in the new JSON.
This allows you to format the key and value however you wish.
(If a "key" or "value" property is not returned, the original value is used.)
The "node" parameter to the format function is the object or array in the original JSON that is being transformed.
Expand Down
5 changes: 4 additions & 1 deletion lib/ObjectTemplate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ class ObjectTemplate
updateContext: (context, node, value, key) =>
# format key and value
formatted = @config.applyFormatting node, value, key
@aggregateValue context, formatted.key, formatted.value
if sysmo.isArray(formatted)
@aggregateValue context, item.key, item.value for item in formatted
else if formatted?
@aggregateValue context, formatted.key, formatted.value

aggregateValue: (context, key, value) =>
return context unless value?
Expand Down

0 comments on commit 54aa5f3

Please sign in to comment.