diff --git a/src/index.ts b/src/index.ts index 87aea71..ee8d3ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,4 +8,5 @@ export * from './constants'; export * from './module'; export * from './utils'; +export * from './presets'; export * from './type'; diff --git a/src/module.ts b/src/module.ts index 27c194c..a9a4c9b 100644 --- a/src/module.ts +++ b/src/module.ts @@ -29,10 +29,6 @@ function baseMerger( options: Options, ...sources: B ) : MergerResult { - if (!sources.length) { - throw new SyntaxError('At least one input element is required.'); - } - let target : MergerSourceUnwrap; let source : MergerSourceUnwrap | undefined; if (options.priority === PriorityName.RIGHT) { @@ -159,6 +155,10 @@ export function createMerger(input?: OptionsInput) : Merger { return ( ...sources: B ) : MergerResult => { + if (!sources.length) { + throw new SyntaxError('At least one input element is required.'); + } + if (options.clone) { return baseMerger(options, ...clone(sources)); } @@ -182,14 +182,3 @@ export function createMerger(input?: OptionsInput) : Merger { } export const merge = createMerger(); - -export function assign, B extends Record[]>( - target: A, - ...sources: B -) : A & MergerResult { - return createMerger({ - inPlace: true, - priority: 'left', - array: false, - })(target, ...sources) as A & MergerResult; -} diff --git a/src/presets.ts b/src/presets.ts new file mode 100644 index 0000000..99fadc3 --- /dev/null +++ b/src/presets.ts @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2023. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import { createMerger } from './module'; +import type { MergerResult } from './type'; + +/** + * Assign source attributes to a target object. + * + * @param target + * @param sources + */ +export function assign, B extends Record[]>( + target: A, + ...sources: B +) : A & MergerResult { + return createMerger({ + inPlace: true, + priority: 'left', + array: false, + })(target, ...sources) as A & MergerResult; +} diff --git a/src/utils/clone.ts b/src/utils/clone.ts index aa21fe9..a174db6 100644 --- a/src/utils/clone.ts +++ b/src/utils/clone.ts @@ -30,18 +30,22 @@ const gT = (() => { throw new Error('unable to locate global object'); })(); +/* istanbul ignore next */ export function clone(value: T) : T { if (gT.structuredClone) { return gT.structuredClone(value); } + /* istanbul ignore next */ if (isObject(value)) { return { ...value }; } + /* istanbul ignore next */ if (Array.isArray(value)) { return [...value] as T; } + /* istanbul ignore next */ return value; } diff --git a/test/unit/module.spec.ts b/test/unit/module.spec.ts index ae0c8bc..f4d9510 100644 --- a/test/unit/module.spec.ts +++ b/test/unit/module.spec.ts @@ -308,4 +308,8 @@ describe('src/module/*.ts', () => { expect(xB).toEqual(['baz', 'bar']); }); + + it('should throw error when input source is missing', () => { + expect(merge).toThrow(SyntaxError); + }); }) diff --git a/test/unit/presets.spec.ts b/test/unit/presets.spec.ts new file mode 100644 index 0000000..a785c77 --- /dev/null +++ b/test/unit/presets.spec.ts @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023. + * Author Peter Placzek (tada5hi) + * For the full copyright and license information, + * view the LICENSE file that was distributed with this source code. + */ + +import {assign} from "../../src"; + +describe('src/presets', () => { + it('should assign default properties', () => { + let target = { + foo: 'bar' + }; + + assign(target, { + baz: 'boz' + }); + + expect(target).toEqual({foo: 'bar', baz: 'boz'}) + }) +})