-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
a814536
commit d2c2018
Showing
10 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ keyid | |
larsgw | ||
lcov | ||
lintstagedrc | ||
listify | ||
mkbuild | ||
mlly | ||
nocheck | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* @file Test Fixtures - VEHICLES_ARRAY | ||
* @module fixtures/VEHICLES_ARRAY | ||
*/ | ||
|
||
import cast from '#src/utils/cast' | ||
import values from '#src/utils/values' | ||
import type Vehicle from './types/vehicle' | ||
import VEHICLES_DICTIONARY from './vehicles-dictionary' | ||
|
||
export default cast<readonly [Vehicle, Vehicle]>(values(VEHICLES_DICTIONARY)) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* @file Test Fixtures - VEHICLES_DICTIONARY | ||
* @module fixtures/VEHICLES_DICTIONARY | ||
*/ | ||
|
||
import type Vehicle from './types/vehicle' | ||
|
||
export default { | ||
0: { | ||
make: faker.vehicle.manufacturer(), | ||
model: faker.vehicle.model(), | ||
vin: faker.vehicle.vin(), | ||
year: faker.date.past({ years: 3 }).getFullYear() | ||
}, | ||
1: { | ||
make: faker.vehicle.manufacturer(), | ||
model: faker.vehicle.model(), | ||
vin: faker.vehicle.vin(), | ||
year: faker.date.past({ years: 5 }).getFullYear() | ||
} | ||
} as { [N in 0 | 1]: Vehicle } |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* @file Type Tests - listify | ||
* @module tutils/utils/tests/unit-d/listify | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type testSubject from '../listify' | ||
|
||
describe('unit-d:utils/listify', () => { | ||
it('should return U[]', () => { | ||
// Arrange | ||
type T = readonly [Vehicle, Vehicle] | ||
type U = Vehicle['vin'] | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T, U>>().returns.toEqualTypeOf<U[]>() | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @file Unit Tests - listify | ||
* @module tutils/utils/tests/unit/listify | ||
*/ | ||
|
||
import VEHICLES_DICTIONARY from '#fixtures/vehicles-dictionary' | ||
import testSubject from '../listify' | ||
|
||
describe('unit:utils/listify', () => { | ||
it('should return obj as array', () => { | ||
// Arrange | ||
const obj: typeof VEHICLES_DICTIONARY = VEHICLES_DICTIONARY | ||
|
||
// Act + Expect | ||
expect(testSubject(obj, ([, vehicle]) => vehicle.vin)).to.deep.equal([ | ||
obj[0].vin, | ||
obj[1].vin | ||
]) | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @file Type Tests - objectify | ||
* @module tutils/utils/tests/unit-d/objectify | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type testSubject from '../objectify' | ||
|
||
describe('unit-d:utils/objectify', () => { | ||
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 } | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T, K, V>>().returns.toEqualTypeOf<Expect>() | ||
}) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @file Unit Tests - objectify | ||
* @module tutils/utils/tests/unit/objectify | ||
*/ | ||
|
||
import VEHICLES_ARRAY from '#fixtures/vehicles-array' | ||
import VEHICLES_DICTIONARY from '#fixtures/vehicles-dictionary' | ||
import isObjectPlain from '../is-object-plain' | ||
import testSubject from '../objectify' | ||
|
||
describe('unit:utils/objectify', () => { | ||
it('should return arr as plain object', () => { | ||
// Act | ||
const result = testSubject(VEHICLES_ARRAY) | ||
|
||
// Expect | ||
expect(result).to.eql(VEHICLES_DICTIONARY).and.satisfy(isObjectPlain) | ||
}) | ||
}) |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* @file Utilities - listify | ||
* @module tutils/utils/listify | ||
*/ | ||
|
||
import type { Entries, Mapper, Nilable } from '#src/types' | ||
import entries from './entries' | ||
import select from './select' | ||
|
||
/** | ||
* Converts an object to an array. | ||
* | ||
* @see {@linkcode Mapper} | ||
* | ||
* @template T - Object to convert | ||
* @template U - Array item type | ||
* | ||
* @param {T} obj - Object to convert | ||
* @param {Mapper<Entries<T>, U>} mapper - Object entry interpolator | ||
* @return {U[]} New array | ||
*/ | ||
const listify = <T extends Nilable<object>, U>( | ||
obj: T, | ||
mapper: Mapper<Entries<T>, U> | ||
): U[] => { | ||
/** | ||
* Own enumerable string-keyed property key-value pairs of {@linkcode obj}. | ||
* | ||
* @const {Entries<T>} pairs | ||
*/ | ||
const pairs: Entries<T> = entries(obj) | ||
|
||
return select(pairs, null, (entry, i) => mapper(entry, i, pairs)) | ||
} | ||
|
||
export default listify |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @file Utilities - objectify | ||
* @module tutils/utils/objectify | ||
*/ | ||
|
||
import type { Mapper, PropertyKey } from '#src/types' | ||
import cast from './cast' | ||
import define from './define' | ||
|
||
/** | ||
* Converts an array to a plain object. | ||
* | ||
* @see {@linkcode Mapper} | ||
* | ||
* @todo examples | ||
* | ||
* @template T - Array to convert | ||
* @template K - Object key type | ||
* @template V - Object value type | ||
* | ||
* @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 | ||
*/ | ||
const objectify = < | ||
T extends readonly unknown[], | ||
K extends PropertyKey = number, | ||
V = T[number] | ||
>( | ||
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) => { | ||
return define(acc, key(item, i, arr), { value: value(item, i, arr) }) | ||
}, {}) | ||
} | ||
|
||
export default objectify |