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 { parseUrl } from './serializer/url-serializer'
|
||||
|
||||
export function normalizeConfig(config?: FluentUrlConfig): FluentUrlConfig {
|
||||
export function normalizeConfig(
|
||||
config?: Partial<FluentUrlConfig>,
|
||||
): FluentUrlConfig {
|
||||
return {
|
||||
parseQuery: config?.parseQuery ?? parseQuery,
|
||||
stringifyQuery: config?.stringifyQuery ?? stringifyQuery,
|
||||
@@ -31,3 +33,27 @@ export function normalizeOptions(args: {
|
||||
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 { stringifyUrl } from './serializer/url-serializer'
|
||||
|
||||
@@ -13,7 +18,7 @@ export class FluentUrl {
|
||||
|
||||
constructor(
|
||||
urlOrOptions?: Partial<FLuentUrlPlainObject> | string,
|
||||
config?: FluentUrlConfig,
|
||||
config?: Partial<FluentUrlConfig>,
|
||||
) {
|
||||
this.config = normalizeConfig(config)
|
||||
|
||||
@@ -69,18 +74,18 @@ export class FluentUrl {
|
||||
config?: FluentUrlConfig,
|
||||
): FluentUrl {
|
||||
return new FluentUrl(
|
||||
mergeOptions(
|
||||
{
|
||||
protocol: options?.protocol ?? this.protocol,
|
||||
hostname: options?.hostname ?? this.hostname,
|
||||
paths: options?.paths ?? this.paths,
|
||||
port: options?.port ?? this.port,
|
||||
fragment: options?.fragment ?? this.fragment,
|
||||
queries: options?.queries ?? this.queries,
|
||||
},
|
||||
{
|
||||
parseQuery: config?.parseQuery ?? this.config.parseQuery,
|
||||
stringifyQuery: config?.stringifyQuery ?? this.config.stringifyQuery,
|
||||
protocol: this.protocol,
|
||||
hostname: this.hostname,
|
||||
paths: this.paths,
|
||||
port: this.port,
|
||||
fragment: this.fragment,
|
||||
queries: this.queries,
|
||||
},
|
||||
options,
|
||||
),
|
||||
mergeConfig(this.config, config),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user