-
-
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
50a7b99
commit 6da685c
Showing
8 changed files
with
96 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @file Type Tests - defaults | ||
* @module tutils/utils/tests/unit-d/defaults | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type { Defaults, Partial } from '#src/types' | ||
import type testSubject from '../defaults' | ||
|
||
describe('unit-d:utils/defaults', () => { | ||
it('should return Defaults<T, U>', () => { | ||
// Arrange | ||
type T = Partial<Vehicle> | ||
type U = [{ readonly vrm: number }, { readonly vin: string }] | ||
type Expect = Defaults<T, U> | ||
|
||
// Expect | ||
expectTypeOf<typeof testSubject<T, U>>().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,27 @@ | ||
/** | ||
* @file Unit Tests - defaults | ||
* @module tutils/utils/tests/unit/defaults | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type { Partial } from '#src/types' | ||
import testSubject from '../defaults' | ||
|
||
describe('unit:utils/defaults', () => { | ||
let base: Partial<Vehicle> & { vrm: string } | ||
|
||
beforeAll(() => { | ||
base = { vrm: faker.vehicle.vrm() } | ||
}) | ||
|
||
it('should return merge result', () => { | ||
// Arrange | ||
const s1: { vrm: number } = { vrm: faker.number.int() } | ||
const s2: { vin: string } = { vin: faker.vehicle.vin() } | ||
|
||
// Act + Expect | ||
expect(testSubject(base, s1, s2)) | ||
.to.eql({ ...base, ...s2 }) | ||
.but.not.equal(base) | ||
}) | ||
}) |
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
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 - defaults | ||
* @module tutils/utils/defaults | ||
*/ | ||
|
||
import type { Defaults, ObjectCurly, Objectify } from '#src/types' | ||
import assignWith from './assign-with' | ||
import cast from './cast' | ||
import isUndefined from './is-undefined' | ||
|
||
/** | ||
* Assigns own properties of one or more `source` objects to a target object for | ||
* all target properties that resolve to `undefined`. | ||
* | ||
* The initial `base` object **will not** be modified. | ||
* | ||
* Source objects are applied from left to right. Subsequent default values are | ||
* ignored if a property no longer resolves to `undefined`. | ||
* | ||
* @see {@linkcode Defaults} | ||
* | ||
* @todo examples | ||
* | ||
* @template T - Base object | ||
* @template U - Source object array | ||
* | ||
* @param {T} base - Base object | ||
* @param {U} source - Source object array | ||
* @return {Defaults<T, U>} Merge result | ||
*/ | ||
const defaults = <T extends Objectify<any>, U extends readonly ObjectCurly[]>( | ||
base: T, | ||
...source: U | ||
): Defaults<T, U> => { | ||
return cast( | ||
assignWith((curr, src) => (isUndefined(curr) ? src : curr), base, ...source) | ||
) | ||
} | ||
|
||
export default defaults |
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
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