Skip to content

Commit

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

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

// Expect
expectTypeOf<typeof testSubject<T, K, V>>().returns.toEqualTypeOf<Expect>()
Expand Down
10 changes: 5 additions & 5 deletions src/utils/objectify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import cast from './cast'
import define from './define'

/**
* Converts an array to a plain object.
* Convert an array to a plain object.
*
* @see {@linkcode Mapper}
*
Expand All @@ -21,7 +21,7 @@ import define from './define'
* @param {T} arr - Array to convert
* @param {Mapper<T, K>} [key=(_,index)=>index] - Object key function
* @param {Mapper<T, V>} [value=item=>item] - Object value function
* @return {Partial<Record<K, V>>} New plain object
* @return {Record<K, V>} New plain object
*/
const objectify = <
T extends readonly unknown[],
Expand All @@ -31,10 +31,10 @@ const objectify = <
arr: T,
key: Mapper<T, K> = (_, index) => cast(index),
value: Mapper<T, V> = item => cast(item)
): { [H in K]?: V } => {
return arr.reduce<{ [H in K]?: V }>((acc, item, i) => {
): { [H in K]: V } => {
return arr.reduce<{ [H in K]: V }>((acc, item, i) => {
return define(acc, key(item, i, arr), { value: value(item, i, arr) })
}, {})
}, cast({}))
}

export default objectify

0 comments on commit f133bb1

Please sign in to comment.