Skip to content

Commit

Permalink
feat: const for priority side + cleanup types
Browse files Browse the repository at this point in the history
BREAKING CHANGE: bump version to v1.x
  • Loading branch information
tada5hi committed Mar 20, 2023
1 parent 0489581 commit bf19728
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 13 deletions.
4 changes: 1 addition & 3 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,14 @@ const merge = createMerger({
target[key] += value;
return target;
}

return undefined;
}
});

console.log(merge({ a: 1 }, { a: 2 }, { a: 3 }));
// => { a: 6 }
```

Return **undefined** if the default merge behaviour should be used for the current target source pair.
A returned value indicates that the strategy has been applied.

## License

Expand Down
11 changes: 11 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* 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.
*/

export enum PriorityName {
LEFT = 'left',
RIGHT = 'right',
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* view the LICENSE file that was distributed with this source code.
*/

export * from './constants';
export * from './module';
export * from './utils';
export * from './type';
14 changes: 8 additions & 6 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
* view the LICENSE file that was distributed with this source code.
*/

import type { Merger, MergerResult, Options } from './type';
import { PriorityName } from './constants';
import type {
Merger, MergerResult, Options, OptionsInput,
} from './type';
import {
buildOptions,
hasOwnProperty,
Expand Down Expand Up @@ -39,7 +42,6 @@ export function baseMerger<A extends Record<string, any>, B extends Record<strin
if (hasOwnProperty(target, key)) {
if (options.strategy) {
const applied = options.strategy(target, key, source[key]);
// todo: maybe compare returned target and argument passed target
if (typeof applied !== 'undefined') {
continue;
}
Expand All @@ -64,12 +66,12 @@ export function baseMerger<A extends Record<string, any>, B extends Record<strin
Array.isArray(source[key])
) {
switch (options.priority) {
case 'left':
case PriorityName.LEFT:
Object.assign(target, {
[key]: mergeArrays(target[key], source[key], options.arrayDistinct),
});
break;
case 'right':
case PriorityName.RIGHT:
Object.assign(target, {
[key]: mergeArrays(source[key], target[key], options.arrayDistinct),
});
Expand All @@ -79,7 +81,7 @@ export function baseMerger<A extends Record<string, any>, B extends Record<strin
continue;
}

if (options.priority === 'right') {
if (options.priority === PriorityName.RIGHT) {
Object.assign(target, { [key]: source[key] });
}
} else {
Expand All @@ -91,7 +93,7 @@ export function baseMerger<A extends Record<string, any>, B extends Record<strin
return baseMerger(options, target, ...sources);
}

export function createMerger(input?: Partial<Options>) : Merger {
export function createMerger(input?: OptionsInput) : Merger {
const options = buildOptions(input);

return <A extends Record<string, any>, B extends Record<string, any>>(
Expand Down
6 changes: 5 additions & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* view the LICENSE file that was distributed with this source code.
*/

import type { PriorityName } from './constants';

export type MergerResult<A, B> = A extends B ?
B extends A ?
// eslint-disable-next-line @typescript-eslint/ban-types
Expand All @@ -18,5 +20,7 @@ export type Options = {
array: boolean,
arrayDistinct: boolean,
strategy?: (target: Record<string, any>, key: string, value: unknown) => Record<string, any> | undefined,
priority: 'left' | 'right'
priority: `${PriorityName}`
};

export type OptionsInput = Partial<Options>;
7 changes: 4 additions & 3 deletions src/utils/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
* view the LICENSE file that was distributed with this source code.
*/

import type { Options } from '../type';
import { PriorityName } from '../constants';
import type { Options, OptionsInput } from '../type';

export function buildOptions(options?: Partial<Options>) {
export function buildOptions(options?: OptionsInput) : Options {
options = options || {};

options.array = options.array ?? true;
options.arrayDistinct = options.arrayDistinct ?? false;
options.priority = options.priority || 'left';
options.priority = options.priority || PriorityName.LEFT;

return options as Options;
}

0 comments on commit bf19728

Please sign in to comment.