feat: enhance configuration and options handling with merge functions

This commit is contained in:
2026-02-22 16:52:13 -05:00
parent f01755fda9
commit 0ce2d8a512
2 changed files with 46 additions and 15 deletions

View File

@@ -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,
}
}

View File

@@ -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, {
hostname: options?.hostname ?? this.hostname, protocol: this.protocol,
paths: options?.paths ?? this.paths, hostname: this.hostname,
port: options?.port ?? this.port, paths: this.paths,
fragment: options?.fragment ?? this.fragment, port: this.port,
queries: options?.queries ?? this.queries, fragment: this.fragment,
}, queries: this.queries,
{ },
parseQuery: config?.parseQuery ?? this.config.parseQuery, options,
stringifyQuery: config?.stringifyQuery ?? this.config.stringifyQuery, ),
}, mergeConfig(this.config, config),
) )
} }
} }