Skip to content

Instantly share code, notes, and snippets.

@doasync
Created August 5, 2017 13:25
Show Gist options
  • Save doasync/92601ff4013ebe48766605816ae22c2d to your computer and use it in GitHub Desktop.
Save doasync/92601ff4013ebe48766605816ae22c2d to your computer and use it in GitHub Desktop.
ES6 utils for arrays and objects
function collapse (arr) {
return [...new Set(arr)];
}
function isIterable(obj) {
return obj != null && typeof obj[Symbol.iterator] === 'function';
}
function collapseAll (...args) {
const all = args.reduce((arr, arg) => {
return isIterable(arg) ? arr.concat(...arg) : arr;
}, []);
return collapse(all)
}
function collapseKeys (...args) {
return collapseAll(
...args.map(obj => obj && Object.keys(obj))
)
}
function mergeByKey(...args){
return collapseKeys(...args).reduce((result, key) => {
result[key] = collapseAll(obj1[key], obj2[key]);
return result;
}, {});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment