Skip to content

Commit

Permalink
fix: move sources length check to create-merger
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 28, 2023
1 parent 6e26ae2 commit c55bfc5
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
export * from './constants';
export * from './module';
export * from './utils';
export * from './presets';
export * from './type';
19 changes: 4 additions & 15 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ function baseMerger<B extends MergerSource[]>(
options: Options,
...sources: B
) : MergerResult<B> {
if (!sources.length) {
throw new SyntaxError('At least one input element is required.');
}

let target : MergerSourceUnwrap<B>;
let source : MergerSourceUnwrap<B> | undefined;
if (options.priority === PriorityName.RIGHT) {
Expand Down Expand Up @@ -159,6 +155,10 @@ export function createMerger(input?: OptionsInput) : Merger {
return <B extends MergerSource[]>(
...sources: B
) : MergerResult<B> => {
if (!sources.length) {
throw new SyntaxError('At least one input element is required.');
}

if (options.clone) {
return baseMerger(options, ...clone(sources));
}
Expand All @@ -182,14 +182,3 @@ export function createMerger(input?: OptionsInput) : Merger {
}

export const merge = createMerger();

export function assign<A extends Record<string, any>, B extends Record<string, any>[]>(
target: A,
...sources: B
) : A & MergerResult<B> {
return createMerger({
inPlace: true,
priority: 'left',
array: false,
})(target, ...sources) as A & MergerResult<B>;
}
26 changes: 26 additions & 0 deletions src/presets.ts
Original file line number Diff line number Diff line change
@@ -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<A extends Record<string, any>, B extends Record<string, any>[]>(
target: A,
...sources: B
) : A & MergerResult<B> {
return createMerger({
inPlace: true,
priority: 'left',
array: false,
})(target, ...sources) as A & MergerResult<B>;
}
4 changes: 4 additions & 0 deletions src/utils/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,22 @@ const gT = (() => {
throw new Error('unable to locate global object');
})();

/* istanbul ignore next */
export function clone<T>(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;
}
4 changes: 4 additions & 0 deletions test/unit/module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
})
22 changes: 22 additions & 0 deletions test/unit/presets.spec.ts
Original file line number Diff line number Diff line change
@@ -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'})
})
})

0 comments on commit c55bfc5

Please sign in to comment.