Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes mapping of arrays with transform functions #90

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions src/object-mapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down Expand Up @@ -187,20 +187,20 @@ 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
if (typeof t !== 'undefined') context.transform = t
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)
Expand Down Expand Up @@ -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 || []
Expand All @@ -305,15 +305,16 @@ 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)

return dest
}

// Set the specific array index with the data
else
else
return update_arr_ix(dest, '0', data, keys, context)
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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 :
Expand Down
53 changes: 42 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ 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'}]
var result = om.parse(k, '.')
t.deepEqual(result, expect);
t.end();
});

test('PARSE with simple key', function (t) {
var k = 'abc'
var expect = [{name: 'abc'}]
Expand Down Expand Up @@ -172,7 +172,7 @@ var expect =
t.deepEqual(result, expect);
t.end();
});


test('get value - one level deep', function (t) {

Expand Down Expand Up @@ -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"]
Expand All @@ -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);
Expand Down Expand Up @@ -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");

Expand All @@ -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}]};


Expand All @@ -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);

Expand Down Expand Up @@ -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();
});