Skip to content

Commit

Permalink
determine the default value before apply transform
Browse files Browse the repository at this point in the history
in set_data() determine the default values if necessary
before applying the transform.

Fixes #71
  • Loading branch information
wankdanker committed Dec 27, 2019
1 parent dbfab93 commit 574acca
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.vscode/
12 changes: 6 additions & 6 deletions src/object-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,12 +345,6 @@ function update_arr_ix(dest, ix, data, keys, context)
// Set the given data into the given destination object
function set_data(dest, key, data, context)
{
// If there is a transformation function, call the function.
if (typeof context.transform == 'function') {
dest = dest || {}
data = context.transform(data, context.src, dest, context.srckey, context.destkey)
}

// See if data is null and there is a default
if (typeof context.default !== 'undefined' && (data == null || typeof data == 'undefined')) {
// There is a default function, call the function to set the default
Expand All @@ -363,6 +357,12 @@ function set_data(dest, key, data, context)
data = context.default
}

// If there is a transformation function, call the function.
if (typeof context.transform == 'function') {
dest = dest || {}
data = context.transform(data, context.src, dest, context.srckey, context.destkey)
}

// Set the object to the data if it is not undefined
if (typeof data !== 'undefined' && key && key.name) {
// Set the data if the data is not null, or if the 'allow nulls' key is set, or if there is a default (in the case of default=null, make sure to write this out)
Expand Down

0 comments on commit 574acca

Please sign in to comment.