feat: enhance configuration and options handling with merge functions
This commit is contained in:
@@ -2,7 +2,9 @@ import { parseQuery, stringifyQuery } from '@/core/serializer/query-serializer'
|
|||||||
import { deepClone } from '@/shared/helpers/deep-clone'
|
import { deepClone } from '@/shared/helpers/deep-clone'
|
||||||
import { parseUrl } from './serializer/url-serializer'
|
import { parseUrl } from './serializer/url-serializer'
|
||||||
|
|
||||||
export function normalizeConfig(config?: FluentUrlConfig): FluentUrlConfig {
|
export function normalizeConfig(
|
||||||
|
config?: Partial<FluentUrlConfig>,
|
||||||
|
): FluentUrlConfig {
|
||||||
return {
|
return {
|
||||||
parseQuery: config?.parseQuery ?? parseQuery,
|
parseQuery: config?.parseQuery ?? parseQuery,
|
||||||
stringifyQuery: config?.stringifyQuery ?? stringifyQuery,
|
stringifyQuery: config?.stringifyQuery ?? stringifyQuery,
|
||||||
@@ -31,3 +33,27 @@ export function normalizeOptions(args: {
|
|||||||
queries: deepClone(urlOrOptions?.queries) ?? Object.create(null),
|
queries: deepClone(urlOrOptions?.queries) ?? Object.create(null),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function mergeConfig(
|
||||||
|
current: FluentUrlConfig,
|
||||||
|
merge?: Partial<FluentUrlConfig>,
|
||||||
|
): FluentUrlConfig {
|
||||||
|
return {
|
||||||
|
parseQuery: merge?.parseQuery ?? current.parseQuery,
|
||||||
|
stringifyQuery: merge?.stringifyQuery ?? current.stringifyQuery,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function mergeOptions(
|
||||||
|
current: FLuentUrlPlainObject,
|
||||||
|
merge?: Partial<FLuentUrlPlainObject>,
|
||||||
|
): FLuentUrlPlainObject {
|
||||||
|
return {
|
||||||
|
protocol: merge?.protocol ?? current.protocol,
|
||||||
|
hostname: merge?.hostname ?? current.hostname,
|
||||||
|
paths: merge?.paths ?? current.paths,
|
||||||
|
port: merge?.port ?? current.port,
|
||||||
|
fragment: merge?.fragment ?? current.fragment,
|
||||||
|
queries: merge?.queries ?? current.queries,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
import { normalizeConfig, normalizeOptions } from '@/core/config-defaults'
|
import {
|
||||||
|
mergeConfig,
|
||||||
|
mergeOptions,
|
||||||
|
normalizeConfig,
|
||||||
|
normalizeOptions,
|
||||||
|
} from '@/core/config-defaults'
|
||||||
import { deepClone } from '@/shared/helpers/deep-clone'
|
import { deepClone } from '@/shared/helpers/deep-clone'
|
||||||
import { stringifyUrl } from './serializer/url-serializer'
|
import { stringifyUrl } from './serializer/url-serializer'
|
||||||
|
|
||||||
@@ -13,7 +18,7 @@ export class FluentUrl {
|
|||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
urlOrOptions?: Partial<FLuentUrlPlainObject> | string,
|
urlOrOptions?: Partial<FLuentUrlPlainObject> | string,
|
||||||
config?: FluentUrlConfig,
|
config?: Partial<FluentUrlConfig>,
|
||||||
) {
|
) {
|
||||||
this.config = normalizeConfig(config)
|
this.config = normalizeConfig(config)
|
||||||
|
|
||||||
@@ -69,18 +74,18 @@ export class FluentUrl {
|
|||||||
config?: FluentUrlConfig,
|
config?: FluentUrlConfig,
|
||||||
): FluentUrl {
|
): FluentUrl {
|
||||||
return new FluentUrl(
|
return new FluentUrl(
|
||||||
|
mergeOptions(
|
||||||
{
|
{
|
||||||
protocol: options?.protocol ?? this.protocol,
|
protocol: this.protocol,
|
||||||
hostname: options?.hostname ?? this.hostname,
|
hostname: this.hostname,
|
||||||
paths: options?.paths ?? this.paths,
|
paths: this.paths,
|
||||||
port: options?.port ?? this.port,
|
port: this.port,
|
||||||
fragment: options?.fragment ?? this.fragment,
|
fragment: this.fragment,
|
||||||
queries: options?.queries ?? this.queries,
|
queries: this.queries,
|
||||||
},
|
|
||||||
{
|
|
||||||
parseQuery: config?.parseQuery ?? this.config.parseQuery,
|
|
||||||
stringifyQuery: config?.stringifyQuery ?? this.config.stringifyQuery,
|
|
||||||
},
|
},
|
||||||
|
options,
|
||||||
|
),
|
||||||
|
mergeConfig(this.config, config),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user