-
-
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.
feat(utils):
overwriteWith
, overwrite
Signed-off-by: Lexus Drumgold <[email protected]>
- Loading branch information
1 parent
89323a2
commit d4948a4
Showing
10 changed files
with
253 additions
and
67 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 |
---|---|---|
|
@@ -6,6 +6,7 @@ booleanish | |
cefc | ||
codecov | ||
commitlintrc | ||
customizer | ||
dedupe | ||
dequal | ||
desegment | ||
|
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,20 @@ | ||
/** | ||
* @file Type Tests - overwriteWith | ||
* @module tutils/utils/tests/unit-d/overwriteWith | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type { Overwrite } from '#src/types' | ||
import type testSubject from '../overwrite-with' | ||
|
||
describe('unit-d:utils/overwriteWith', () => { | ||
it('should return Overwrite<T, U>', () => { | ||
// Arrange | ||
type T = Vehicle | ||
type U = [{ year: `${number}${number}${number}${number}` }] | ||
type Expect = Overwrite<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,31 @@ | ||
/** | ||
* @file Unit Tests - overwriteWith | ||
* @module tutils/utils/tests/unit/overwriteWith | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import isUndefined from '../is-undefined' | ||
import type { OverwriteCustomizer } from '../overwrite-with' | ||
import testSubject from '../overwrite-with' | ||
|
||
describe('unit:utils/overwriteWith', () => { | ||
let customizer: OverwriteCustomizer | ||
|
||
beforeAll(() => { | ||
customizer = (outgoing: any, incoming: any): any => { | ||
return isUndefined(incoming) ? outgoing : incoming | ||
} | ||
}) | ||
|
||
it('should return overwrite result', () => { | ||
// Arrange | ||
const base: { vin: Vehicle['vin'] } = { vin: '' } | ||
const s1: { vrm: string } = { vrm: faker.vehicle.vrm() } | ||
const s2: { vin: Vehicle['vin'] } = { vin: faker.vehicle.vin() } | ||
|
||
// Act + Expect | ||
expect(testSubject(customizer, base, s1, s2, { vin: undefined })) | ||
.to.eql(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* @file Type Tests - overwrite | ||
* @module tutils/utils/tests/unit-d/overwrite | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import type { Overwrite } from '#src/types' | ||
import type testSubject from '../overwrite' | ||
|
||
describe('unit-d:utils/overwrite', () => { | ||
it('should return Overwrite<T, U>', () => { | ||
// Arrange | ||
type T = Vehicle | ||
type U = [{ year: `${number}${number}${number}${number}` }] | ||
type Expect = Overwrite<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,19 @@ | ||
/** | ||
* @file Unit Tests - overwrite | ||
* @module tutils/utils/tests/unit/overwrite | ||
*/ | ||
|
||
import type Vehicle from '#fixtures/types/vehicle' | ||
import testSubject from '../overwrite' | ||
|
||
describe('unit:utils/overwrite', () => { | ||
it('should return overwrite result', () => { | ||
// Arrange | ||
const base: { vin: Vehicle['vin'] } = { vin: '' } | ||
const s1: { vrm: string } = { vrm: faker.vehicle.vrm() } | ||
const s2: { vin: Vehicle['vin'] } = { vin: faker.vehicle.vin() } | ||
|
||
// Act + Expect | ||
expect(testSubject(base, s1, s2)).to.eql(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* @file Utilities - overwriteWith | ||
* @module tutils/utils/overwriteWith | ||
*/ | ||
|
||
import type { Fn, ObjectCurly, Overwrite } from '#src/types' | ||
import cast from './cast' | ||
import clone from './clone' | ||
import define from './define' | ||
import descriptor from './descriptor' | ||
import hasOwn from './has-own' | ||
import properties from './properties' | ||
|
||
/** | ||
* Function used to customize assigned values. | ||
* | ||
* @param {unknown} outgoing - Outgoing property value | ||
* @param {unknown} incoming - Incoming property value from source object | ||
* @param {string | symbol} key - Current property key | ||
* @return {unknown} New property value | ||
*/ | ||
type OverwriteCustomizer = Fn<[unknown, unknown, string | symbol], unknown> | ||
|
||
/** | ||
* Assigns own properties from one or more `source` objects to a destination | ||
* object for all mutual properties in `base`. A mutual property is a property | ||
* that exists on both `T` and a source object. | ||
* | ||
* A `customizer` is used to produce assigned values. | ||
* | ||
* Source objects are applied from left to right. Subsequent sources overwrite | ||
* property assignments of previous sources. | ||
* | ||
* @see {@linkcode Overwrite} | ||
* @see {@linkcode OverwriteCustomizer} | ||
* | ||
* @todo examples | ||
* | ||
* @template T - Base object | ||
* @template U - Source object array | ||
* | ||
* @param {OverwriteCustomizer} customizer - Assigned value factory | ||
* @param {T} base - Base object | ||
* @param {U} source - Source object(s) | ||
* @return {Overwrite<T, U>} Overwrite result | ||
*/ | ||
const overwriteWith = <T extends ObjectCurly, U extends readonly ObjectCurly[]>( | ||
customizer: OverwriteCustomizer, | ||
base: T, | ||
...source: U | ||
): Overwrite<T, U> => { | ||
return source.reduce<Overwrite<T, U>>((acc, src) => { | ||
return properties(src).reduce<Overwrite<T, U>>((acc, key) => { | ||
return hasOwn(acc, key) | ||
? define(acc, key, { | ||
...descriptor(src, key), | ||
value: customizer(acc[key], src[key], key) | ||
}) | ||
: acc | ||
}, acc) | ||
}, cast(clone(base))) | ||
} | ||
|
||
export { overwriteWith as default, type OverwriteCustomizer } |
Oops, something went wrong.