diff --git a/src/object-mapper.js b/src/object-mapper.js index 041eeed..0d6e425 100644 --- a/src/object-mapper.js +++ b/src/object-mapper.js @@ -100,7 +100,7 @@ function select_arr(src, key, keys) // If we are not expecting an array, return the first node - kinda hacky if (typeof data[0] !== 'undefined' && key.name && data[0][key.name]) return data[0][key.name] - + // Otherwise, return nothing return null } @@ -117,7 +117,7 @@ function select_obj(src, key, keys) { // Make sure that there is data where we are looking if (src && key.name) { - + // Match all keys in the object if (key.name == '*') return select_obj_keys(src, keys) @@ -187,12 +187,12 @@ function setKeyValue(dest, keystr, data, context = {}) if (typeof d !== 'undefined') context.default = d dest = setKeyValue(dest, k, data, context) } - + // The substring value is in object notation - dig further else { if (typeof keystr[i].transform !== 'undefined') context.transform = keystr[i].transform if (typeof keystr[i].default !== 'undefined') context.default = keystr[i].default - + // If the substring value of the key is an array, parse the array. If this is parsed in a recursion, it is confused with arrays containing multiple values if (Array.isArray(keystr[i].key)) { let [k,t,d] = keystr[i].key @@ -200,7 +200,7 @@ function setKeyValue(dest, keystr, data, context = {}) if (typeof d !== 'undefined') context.default = d dest = setKeyValue(dest, k, data, context) } - + // The substring value is regular object notation - recurse with the key of the substring else dest = setKeyValue(dest, keystr[i].key, data, context) @@ -283,7 +283,7 @@ function update_obj(dest, key, data, keys, context) // Update the dest[] array with the data on each index function update_arr(dest, key, data, keys, context) { - // The 'add' instruction is set. This means to take the data and add it onto a new array node + // The 'add' instruction is set. This means to take the data and add it onto a new array node if (key.add) { if (data !== null && typeof data !== 'undefined') { dest = dest || [] @@ -305,7 +305,8 @@ function update_arr(dest, key, data, keys, context) dest = data.reduce(function(dest,d,i) { // If the instruction is to update all array indices ('') or the current index, update the child data element. Otherwise, don't bother if (key.ix == '' || key.ix == i) { - return update_arr_ix(dest, i, applyTransform(d,dest,context), keys.slice(), context) + //todo first + return update_arr_ix(dest, i, d, keys.slice(), context) } }, dest) @@ -313,7 +314,7 @@ function update_arr(dest, key, data, keys, context) } // Set the specific array index with the data - else + else return update_arr_ix(dest, '0', data, keys, context) } @@ -331,7 +332,7 @@ function update_arr_ix(dest, ix, data, keys, context) if (dest !== null && typeof dest !== 'undefined' && typeof dest[ix] !== 'undefined') o = (keys.length) ? update(dest[ix], data, keys, context) : data else - o = (keys.length) ? update(null, data, keys, context) : data + o = (keys.length) ? update(null, data, keys, context) : applyTransform(data, dest, context) // Only update (and create if needed) dest if there is data to be saved if (o !== null) { @@ -360,6 +361,7 @@ function set_data(dest, key, data, context) // If there is a transformation function, call the function. if (typeof context.transform == 'function') { dest = dest || {} + //todo second data = context.transform(data, context.src, dest, context.srckey, context.destkey) } @@ -378,7 +380,7 @@ function set_data(dest, key, data, context) // Turns a key string (like key1.key2[].key3 into ['key1','key2','[]','key3']...) -// +// function parse(key_str, delimiter = '.') { // Return null if the key_str is null @@ -425,7 +427,7 @@ function parse(key_str, delimiter = '.') } return keys -} +} // Perform the same function as split(), but keep track of escaped delimiters function split(str, delimiter) @@ -447,7 +449,7 @@ function split(str, delimiter) if (esc == (i-1)) { esc = -99 s += str[i-1] + str[i] - } else + } else esc = i break default : diff --git a/test/test.js b/test/test.js index 38d22f9..5a709a5 100644 --- a/test/test.js +++ b/test/test.js @@ -12,7 +12,7 @@ test('SPLIT with complicated key', function (t) { t.deepEqual(result, expect); t.end(); }); - + test('PARSE with complicated key', function (t) { var k = 'abc[].def[42]+.ghi?.j..k\\.l\\\\.m.' var expect = [{name: 'abc'},{ix: ''},{name: 'def'},{ix: '42', add: true},{name: 'ghi', nulls: true},{name: 'j'},{name: 'k.l\\\\'},{name: 'm'}] @@ -20,7 +20,7 @@ test('PARSE with complicated key', function (t) { t.deepEqual(result, expect); t.end(); }); - + test('PARSE with simple key', function (t) { var k = 'abc' var expect = [{name: 'abc'}] @@ -172,7 +172,7 @@ var expect = t.deepEqual(result, expect); t.end(); }); - + test('get value - one level deep', function (t) { @@ -2117,7 +2117,7 @@ test('Multi-level array issue #29', function (t) { "foo[].name": "bar[].label", "foo[].things[]": "bar[].values[]" }; - + var expect = {bar: [{ label: "a", values: ["a1", "a2"] @@ -2126,7 +2126,7 @@ test('Multi-level array issue #29', function (t) { label: "b", values: ["b1", "b2"] }]} - + var result = om(orig, map); t.deepEqual(result, expect); @@ -2172,7 +2172,7 @@ test('Ensure that multi-dimentional arrays work #41', function (t) { } ] }; - + var expect = null var result = om.getKeyValue(src, "arr[].arr[].id"); @@ -2188,13 +2188,13 @@ test('Ensure that multi-dimentional arrays work #41', function (t) { } ] }; - + var map = { "arr[].id": "arr[].id", "arr[].arr[].id": "arr[].arr[].id", "arr[].arr[].arr[].id": "arr[].arr[].arr[].id" }; - + var expect = {"arr":[{"id":1}]}; @@ -2211,18 +2211,18 @@ test('Make sure no objects are created without data #48', function (t) { "bar": null } }; - + var expect = { foo:{ a:1234 } }; - + var map = { 'foo.bar' : 'bar.bar', 'a': 'foo.a' }; - + var result = om(obj, map); @@ -2653,6 +2653,37 @@ test("issue #74: mapping empty array should result in empty array", t => { const result = om(src, map); + t.deepEqual(result, expect); + t.end(); +}); + +test('map array with function', function (t) { + var obj = { + theArray: [ + {text:"textvalue"}, + {text:"text"} + ] + }; + + var expect = { + newArray: [ + {textSize:9}, + {textSize:4} + ] + }; + + let transformFunc = { + key:'newArray[].textSize', + transform: function (value, src, dest, srcKey, destKey){ + return value.length + } + }; + var map = { + 'theArray[].text': transformFunc + }; + + var result = om(obj, map); + t.deepEqual(result, expect); t.end(); }); \ No newline at end of file