From 4ff0ca5fc18065c515090d01e87244f1a9fbba10 Mon Sep 17 00:00:00 2001 From: tada5hi Date: Mon, 29 May 2023 10:03:04 +0200 Subject: [PATCH] fix: use custom merger for parameters --- package-lock.json | 8 ++++---- package.json | 2 +- src/parameter/fields/build.ts | 10 ++-------- src/parameter/fields/parse.ts | 5 ++--- src/parameter/filters/build.ts | 5 ++--- src/parameter/pagination/build.ts | 4 ++-- src/parameter/relations/build.ts | 5 ++--- src/parameter/sort/build.ts | 6 ++---- src/utils/index.ts | 1 + src/utils/merge.ts | 15 +++++++++++++++ 10 files changed, 33 insertions(+), 28 deletions(-) create mode 100644 src/utils/merge.ts diff --git a/package-lock.json b/package-lock.json index 7671ab96..689583e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,7 @@ "version": "0.8.0", "license": "MIT", "dependencies": { - "smob": "^1.0.0" + "smob": "^1.4.0" }, "devDependencies": { "@rollup/plugin-node-resolve": "^15.0.2", @@ -12955,9 +12955,9 @@ } }, "node_modules/smob": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/smob/-/smob-1.0.0.tgz", - "integrity": "sha512-fnePEPpgGjAdBDk0nV7L9jcStbbcUsKS5TC+RYambCSU9Dm1k2rqDivdg5LBRVWF/NXe0Rq8yfnKKQI08kSXIg==" + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/smob/-/smob-1.4.0.tgz", + "integrity": "sha512-MqR3fVulhjWuRNSMydnTlweu38UhQ0HXM4buStD/S3mc/BzX3CuM9OmhyQpmtYCvoYdl5ris6TI0ZqH355Ymqg==" }, "node_modules/source-map": { "version": "0.6.1", diff --git a/package.json b/package.json index 72945d76..8b6c3e15 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/parameter/fields/build.ts b/src/parameter/fields/build.ts index dc74129c..45ee9700 100644 --- a/src/parameter/fields/build.ts +++ b/src/parameter/fields/build.ts @@ -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( input?: FieldsBuildInput, @@ -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) { diff --git a/src/parameter/fields/parse.ts b/src/parameter/fields/parse.ts index dd198211..a53f5918 100644 --- a/src/parameter/fields/parse.ts +++ b/src/parameter/fields/parse.ts @@ -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 { @@ -51,7 +51,6 @@ export function parseQueryFields( ); const domainFields = merge( - {}, defaultDomainFields, allowedDomainFields, ); diff --git a/src/parameter/filters/build.ts b/src/parameter/filters/build.ts index 124e8b6d..10a29f86 100644 --- a/src/parameter/filters/build.ts +++ b/src/parameter/filters/build.ts @@ -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( data?: FiltersBuildInput, @@ -56,5 +55,5 @@ export function mergeQueryFilters( target?: Record, source?: Record, ) : Record { - return merge({}, target || {}, source || {}); + return merge(target || {}, source || {}); } diff --git a/src/parameter/pagination/build.ts b/src/parameter/pagination/build.ts index ae958dcb..148ece85 100644 --- a/src/parameter/pagination/build.ts +++ b/src/parameter/pagination/build.ts @@ -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 || {}); } diff --git a/src/parameter/relations/build.ts b/src/parameter/relations/build.ts index a0eada61..6778dfba 100644 --- a/src/parameter/relations/build.ts +++ b/src/parameter/relations/build.ts @@ -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( input?: RelationsBuildInput, @@ -24,5 +23,5 @@ export function mergeQueryRelations( target?: string[], source?: string[], ) : string[] { - return mergeArrays(target || [], source || [], true); + return merge(target || [], source || []); } diff --git a/src/parameter/sort/build.ts b/src/parameter/sort/build.ts index 3e1ffc66..b56f7e29 100644 --- a/src/parameter/sort/build.ts +++ b/src/parameter/sort/build.ts @@ -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( data?: SortBuildInput, @@ -50,5 +48,5 @@ export function mergeQuerySort( target?: string[], source?: string[], ) { - return mergeArrays(target || [], source || [], true); + return merge(target || [], source || []); } diff --git a/src/utils/index.ts b/src/utils/index.ts index 95096afd..1a4800fa 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -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'; diff --git a/src/utils/merge.ts b/src/utils/merge.ts new file mode 100644 index 00000000..e919cec1 --- /dev/null +++ b/src/utils/merge.ts @@ -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, +});