Created
August 5, 2017 13:25
-
-
Save doasync/92601ff4013ebe48766605816ae22c2d to your computer and use it in GitHub Desktop.
ES6 utils for arrays and objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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