65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import { parseQuery, stringifyQuery } from '@/core/serializer/query-serializer'
|
|
import { parseUrl } from '@/core/serializer/url-serializer'
|
|
import { deepClone } from '@/shared/helpers/deep-clone'
|
|
import type {
|
|
FLuentUrlPlainObject,
|
|
FluentUrlConfig,
|
|
QuerySerializer,
|
|
} from '@/shared/types'
|
|
|
|
export function normalizeConfig(
|
|
config?: Partial<FluentUrlConfig>,
|
|
): FluentUrlConfig {
|
|
return {
|
|
parseQuery: config?.parseQuery ?? parseQuery,
|
|
stringifyQuery: config?.stringifyQuery ?? stringifyQuery,
|
|
}
|
|
}
|
|
|
|
export function normalizeOptions(args: {
|
|
urlOptions?: Partial<FLuentUrlPlainObject> | string
|
|
parseQuery: QuerySerializer['parseQuery']
|
|
}): FLuentUrlPlainObject {
|
|
const { urlOptions: urlOrOptions, parseQuery } = args
|
|
|
|
if (typeof urlOrOptions === 'string') {
|
|
return parseUrl({
|
|
str: urlOrOptions,
|
|
parseQuery: parseQuery,
|
|
})
|
|
}
|
|
|
|
return {
|
|
protocol: urlOrOptions?.protocol,
|
|
hostname: urlOrOptions?.hostname,
|
|
paths: deepClone(urlOrOptions?.paths) ?? [],
|
|
port: urlOrOptions?.port,
|
|
fragment: urlOrOptions?.fragment,
|
|
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,
|
|
}
|
|
}
|