Skip to content

Commit c2af62b

Browse files
committed
Prevent errors if toKey is undefined
1 parent 75bfb8b commit c2af62b

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/object-mapper.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ function _mapKey(fromObject, fromKey, toObject, toKey) {
111111
fromValue = transform(fromValue, fromObject, toObject, fromKey, toKey);
112112
}
113113

114-
if (typeof fromValue === 'undefined') {
114+
if (typeof fromValue === 'undefined' || typeof toKey === 'undefined') {
115115
return toObject;
116116
}
117117

test/test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,31 @@ test('map object to another - with key object notation with default function whe
895895
t.end();
896896
});
897897

898+
test('map object to another - when target key is undefined it should be ignored', function (t) {
899+
var obj = {
900+
"a" : 1234,
901+
"foo": {
902+
"bar": "baz"
903+
}
904+
};
905+
906+
var expect = {
907+
bar: {
908+
bar : "baz",
909+
}
910+
};
911+
912+
var map = {
913+
'foo.bar' : 'bar.bar',
914+
'a': undefined
915+
};
916+
917+
var result = om(obj, map);
918+
919+
t.deepEqual(result, expect);
920+
t.end();
921+
});
922+
898923
test('map object to another - with key object notation with default function returning undefined when key does not exists', function (t) {
899924
var obj = {
900925
"a" : 1234,

0 commit comments

Comments
 (0)