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 countBy<T extends { [key: string]: any }, K extends keyof T>( | |
| collection: T[], | |
| key: K | |
| ) { | |
| return Object.values(collection).reduce( | |
| (accum: { [key: string]: any }, curr) => { | |
| accum[curr[key]] = | |
| accum[curr[key]] !== undefined ? (accum[curr[key]] += 1) : 1; | |
| return accum; | |
| }, |
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 groupBy<T extends { [key: string]: any }, K extends keyof T>( | |
| collection: T[], | |
| key: K | |
| ) { | |
| return Object.values(collection).reduce( | |
| (accum: { [key: string]: any }, curr) => { | |
| accum[curr[key]] = | |
| accum[curr[key]] !== undefined ? [...accum[curr[key]], curr] : [curr]; | |
| return accum; | |
| }, |
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
| let toString = Object.prototype.toString; | |
| // strict object check | |
| function isObject(value) { | |
| return toString.call(value) === '[object Object]'; | |
| } | |
| // strict array check | |
| function isArray(value) { | |
| return toString.call(value) === '[object Array]'; |