Skip to content

Commit

Permalink
No more clone(); avoid duplicated values
Browse files Browse the repository at this point in the history
  • Loading branch information
pateketrueke committed Jun 24, 2017
1 parent af7148f commit 99ca009
Showing 1 changed file with 7 additions and 15 deletions.
22 changes: 7 additions & 15 deletions ts/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,25 +80,18 @@ function typecast(value: any, schema: JsonSchema): any {
}
}

function clone(arr: any[]): any[] {
var out: any[] = [];
arr.forEach(function(item: any, index: number) {
if (typeof item === 'object' && item !== null) {
out[index] = Array.isArray(item) ? clone(item) : merge({}, item);
} else {
out[index] = item;
}
});
return out;
}

// TODO refactor merge function
function merge(a: Object, b: Object): Object {
for (var key in b) {
if (typeof b[key] !== 'object' || b[key] === null) {
a[key] = b[key];
} else if (Array.isArray(b[key])) {
a[key] = (a[key] || []).concat(clone(b[key]));
a[key] = a[key] || [];
// fix #292 - skip duplicated values from merge object (b)
b[key].forEach(function(value) {
if (a[key].indexOf(value)) {
a[key].push(value);
}
});
} else if (typeof a[key] !== 'object' || a[key] === null || Array.isArray(a[key])) {
a[key] = merge({}, b[key]);
} else {
Expand Down Expand Up @@ -145,7 +138,6 @@ export default {
getSubAttribute: getSubAttribute,
hasProperties: hasProperties,
typecast: typecast,
clone: clone,
merge: merge,
clean: clean,
short: short,
Expand Down

0 comments on commit 99ca009

Please sign in to comment.