Skip to content

Commit d4d7b4f

Browse files
committed
fix (objectify): Change return type from Record<Key, Value> to Partial<Record<Key, Value>> and update tests to handle possible undefined values.
1 parent 78fd135 commit d4d7b4f

File tree

2 files changed

+7
-10
lines changed

2 files changed

+7
-10
lines changed

src/array/objectify.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,9 @@ export function objectify<T, Key extends string | number | symbol, Value = T>(
2121
array: readonly T[],
2222
getKey: (item: T) => Key,
2323
getValue: (item: T) => Value = item => item as unknown as Value,
24-
): Record<Key, Value> {
25-
return array.reduce(
26-
(acc, item) => {
27-
acc[getKey(item)] = getValue(item)
28-
return acc
29-
},
30-
{} as Record<Key, Value>,
31-
)
24+
): Partial<Record<Key, Value>> {
25+
return array.reduce((acc, item) => {
26+
acc[getKey(item)] = getValue(item)
27+
return acc
28+
}, {} as Partial<Record<Key, Value>>)
3229
}

tests/array/objectify.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ describe('objectify', () => {
1414
x => x.id,
1515
x => x,
1616
)
17-
expect(result.a.word).toBe('hello')
18-
expect(result.b.word).toBe('bye')
17+
expect(result.a?.word).toBe('hello')
18+
expect(result.b?.word).toBe('bye')
1919
})
2020
test('does not fail on empty input list', () => {
2121
const result = _.objectify(

0 commit comments

Comments
 (0)