Skip to content

Commit

Permalink
fix: use custom merger for parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed May 29, 2023
1 parent fee53bd commit 4ff0ca5
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 28 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
},
"homepage": "https://github.com/Tada5hi/rapiq#readme",
"dependencies": {
"smob": "^1.0.0"
"smob": "^1.4.0"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.0.2",
Expand Down
10 changes: 2 additions & 8 deletions src/parameter/fields/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* view the LICENSE file that was distributed with this source code.
*/

import { createMerger } from 'smob';
import type { ObjectLiteral } from '../../type';
import type { FieldsBuildInput } from './type';
import { flattenToKeyPathArray, groupArrayByKeyPath } from '../../utils';
import { flattenToKeyPathArray, groupArrayByKeyPath, merge } from '../../utils';

export function buildQueryFields<T extends ObjectLiteral = ObjectLiteral>(
input?: FieldsBuildInput<T>,
Expand Down Expand Up @@ -39,12 +38,7 @@ export function mergeQueryFields(
source = groupArrayByKeyPath(source);
}

const merge = createMerger({
array: true,
arrayDistinct: true,
});

const data = merge({}, target, source);
const data = merge(target, source);

const keys = Object.keys(data);
if (keys.length === 1) {
Expand Down
5 changes: 2 additions & 3 deletions src/parameter/fields/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* view the LICENSE file that was distributed with this source code.
*/

import { isObject, merge } from 'smob';
import { isObject } from 'smob';
import type { ObjectLiteral } from '../../type';
import {
applyMapping, buildFieldWithPath, groupArrayByKeyPath, hasOwnProperty, isFieldPathAllowedByRelations,
applyMapping, buildFieldWithPath, groupArrayByKeyPath, hasOwnProperty, isFieldPathAllowedByRelations, merge,
} from '../../utils';
import { flattenParseAllowedOption } from '../utils';
import type {
Expand Down Expand Up @@ -51,7 +51,6 @@ export function parseQueryFields<T extends ObjectLiteral = ObjectLiteral>(
);

const domainFields = merge(
{},
defaultDomainFields,
allowedDomainFields,
);
Expand Down
5 changes: 2 additions & 3 deletions src/parameter/filters/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* view the LICENSE file that was distributed with this source code.
*/

import { merge } from 'smob';
import { flattenNestedObject, merge } from '../../utils';
import type { ObjectLiteral } from '../../type';
import type { FiltersBuildInput } from './type';
import { flattenNestedObject } from '../../utils';

export function buildQueryFilters<T extends ObjectLiteral = ObjectLiteral>(
data?: FiltersBuildInput<T>,
Expand Down Expand Up @@ -56,5 +55,5 @@ export function mergeQueryFilters(
target?: Record<string, any>,
source?: Record<string, any>,
) : Record<string, any> {
return merge({}, target || {}, source || {});
return merge(target || {}, source || {});
}
4 changes: 2 additions & 2 deletions src/parameter/pagination/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
* view the LICENSE file that was distributed with this source code.
*/

import { merge } from 'smob';
import { merge } from '../../utils';
import type { PaginationBuildInput } from './type';

export function mergeQueryPagination(
target?: PaginationBuildInput,
source?: PaginationBuildInput,
) : PaginationBuildInput {
return merge({}, target || {}, source || {});
return merge(target || {}, source || {});
}
5 changes: 2 additions & 3 deletions src/parameter/relations/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
* view the LICENSE file that was distributed with this source code.
*/

import { mergeArrays } from 'smob';
import type { ObjectLiteral } from '../../type';
import type { RelationsBuildInput } from './type';
import { flattenToKeyPathArray } from '../../utils';
import { flattenToKeyPathArray, merge } from '../../utils';

export function buildQueryRelations<T extends ObjectLiteral = ObjectLiteral>(
input?: RelationsBuildInput<T>,
Expand All @@ -24,5 +23,5 @@ export function mergeQueryRelations(
target?: string[],
source?: string[],
) : string[] {
return mergeArrays(target || [], source || [], true);
return merge(target || [], source || []);
}
6 changes: 2 additions & 4 deletions src/parameter/sort/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/

import { mergeArrays } from 'smob';
import type { ObjectLiteral } from '../../type';
import type { SortBuildInput } from './type';
import { SortDirection } from './type';
import { flattenToKeyPathArray } from '../../utils';
import { flattenToKeyPathArray, merge } from '../../utils';

export function buildQuerySort<T extends ObjectLiteral = ObjectLiteral>(
data?: SortBuildInput<T>,
Expand Down Expand Up @@ -50,5 +48,5 @@ export function mergeQuerySort(
target?: string[],
source?: string[],
) {
return mergeArrays(target || [], source || [], true);
return merge(target || [], source || []);
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * from './array';
export * from './mapping';
export * from './field';
export * from './key';
export * from './merge';
export * from './relation';
export * from './object';
export * from './simple';
Expand Down
15 changes: 15 additions & 0 deletions src/utils/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* 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 'smob';

export const merge = createMerger({
clone: true,
inPlace: false,
array: true,
arrayDistinct: true,
});

0 comments on commit 4ff0ca5

Please sign in to comment.