Skip to content

Commit

Permalink
feat: new function - groupBy
Browse files Browse the repository at this point in the history
  • Loading branch information
GreatAuk committed Mar 6, 2023
1 parent 60db067 commit 5c81139
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pnpm add @utopia-utils/dom
* pipe: 函数组合, 从左到右执行。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/pipe.ts)
* onlyResolvesLast: 解决竞态问题,只保留最后一次调用的结果。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/onlyResolvesLast.ts)
* parseQuery: 解析 url query。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/parseQuery.ts)
* groupBy: 数组根据指定的 key 分组。[source](https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/groupBy.ts)
### 类型判断

```bash
Expand Down
27 changes: 27 additions & 0 deletions packages/core/src/groupBy.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { groupBy } from './groupBy'

describe('groupBy', () => {
it('should group by a string', () => {
const array = [
{ name: 'John', age: 20 },
{ name: 'Jane', age: 25 },
{ name: 'Jack', age: 20 },
]

const result = groupBy(array, item => `${item.age}`)
expect(result).toEqual({
20: [
{ name: 'John', age: 20 },
{ name: 'Jack', age: 20 },
],
25: [{ name: 'Jane', age: 25 }],
})

const arr2 = [4.2, 6.3, 6.5]
const result2 = groupBy(arr2, v => `${Math.floor(v)}`)
expect(result2).toEqual({
4: [4.2],
6: [6.3, 6.5],
})
})
})
25 changes: 25 additions & 0 deletions packages/core/src/groupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* group an array by a string
* @param {T[]} array - The array to iterate over.
* @param iteratee - (item: T) => string
* @returns {Record<string, T[]>} Returns the composed aggregate object.
* @example
* ```
groupBy([4.2, 6.3, 6.5], v => `${Math.floor(v)}`)
// { '4': [ 4.2 ], '6': [ 6.3, 6.5 ] }
groupBy([{ name: 'John', age: 20 }, { name: 'Jane', age: 25 }], item => `${item.age}`)
// { '20': [ { name: 'John', age: 20 } ], '25': [ { name: 'Jane', age: 25 } ] }
* ```
*/
export function groupBy<T>(array: T[], iteratee: (item: T) => string): Record<string, T[]> {
return array.reduce<Record<string, T[]>>((acc, item) => {
const key = iteratee(item)
if (acc[key])
acc[key].push(item)
else
acc[key] = [item]

return acc
}, {})
}
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export * from './escapeStringRegexp'
export * from './getByPath'
export * from './getFileName'
export * from './getQueryParams'
export * from './groupBy'
export * from './intersection'
export * from './math'
export * from './memoize'
Expand Down

0 comments on commit 5c81139

Please sign in to comment.