Created
July 15, 2016 15:18
-
-
Save ste2425/e70a344fb228adc2ccaa00176d0579f3 to your computer and use it in GitHub Desktop.
Simple JavaScript Group
This file contains 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
"use strict"; | |
let group = (arr, by) => arr.reduce((prev, cur) => { | |
let val = isFunction(by) ? by(cur) : cur[by]; | |
prev.hasOwnProperty(val) ? prev[val].push(cur) : prev[val] = [cur]; | |
return prev; | |
}, {}), | |
isFunction = (x) => Object.prototype.toString.call(x) === "[object Function]"; | |
module.exports = () => ({group, isFunction}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment