Skip to content

Commit

Permalink
fix(utils): [group] return type
Browse files Browse the repository at this point in the history
Signed-off-by: Lexus Drumgold <unicornware@flexdevelopment.llc>
  • Loading branch information
unicornware committed Sep 14, 2023
1 parent f133bb1 commit e34ced1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/utils/__tests__/group.spec-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import type Vehicle from '#fixtures/types/vehicle'
import type testSubject from '../group'

describe('unit-d:utils/group', () => {
it('should return { [H in K]?: T[number][] }', () => {
it('should return { [H in K]: T[number][] }', () => {
// Arrange
type T = Vehicle[]
type K = Vehicle['year']
type Expect = { [H in K]?: T[number][] }
type Expect = { [H in K]: T[number][] }

// Expect
expectTypeOf<typeof testSubject<T, K>>().returns.toEqualTypeOf<Expect>()
Expand Down
4 changes: 2 additions & 2 deletions src/utils/__tests__/group.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ describe('unit:utils/group', () => {
]

// Act + Expect
cases.forEach(([array, identity, expected]) => {
expect(testSubject(array, identity)).to.eql(expected)
cases.forEach(([arr, key, expected]) => {
expect(testSubject(arr, key)).to.eql(expected)
})
})
})
26 changes: 14 additions & 12 deletions src/utils/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
*/

import type { Mapper, PropertyKey } from '#src/types'
import cast from './cast'
import isUndefined from './is-undefined'

/**
* Groups each item in an array.
* Group each item in an array.
*
* The return value is a plain object with a key for each group, where each key
* value is an array containing group items.
*
* A `mapper` function is used to convert array items to group keys.
* A `key` function is used to map array items to group keys.
*
* @see {@linkcode Mapper}
*
Expand All @@ -21,32 +23,32 @@ import type { Mapper, PropertyKey } from '#src/types'
* @template K - Identity key type
*
* @param {T} arr - Array to group
* @param {Mapper<T, K>} mapper - Array item interpolator
* @return {Partial<Record<K, T[number][]>>} Groups object
* @param {Mapper<T, K>} key - Group key mapper
* @return {Record<K, T[number][]>} Groups object
*/
const group = <
T extends readonly unknown[],
K extends PropertyKey = PropertyKey
>(
arr: T,
mapper: Mapper<T, K>
): { [H in K]?: T[number][] } => {
return arr.reduce<{ [H in K]?: T[number][] }>((acc, item, i) => {
key: Mapper<T, K>
): { [H in K]: T[number][] } => {
return arr.reduce<{ [H in K]: T[number][] }>((acc, item, i) => {
/**
* Group key.
*
* @const {K} key
* @const {K} k
*/
const key: K = mapper(item, i, arr)
const k: K = key(item, i, arr)

// initialize group
!acc[key] && (acc[key] = [])
isUndefined(acc[k]) && (acc[k] = [])

// add group item
acc[key]!.push(item)
acc[k].push(item)

return acc
}, {})
}, cast({}))
}

export default group

0 comments on commit e34ced1

Please sign in to comment.