feat: implement FluentUrl class with configuration and options normalization
This commit is contained in:
33
lib/core/config-defaults.ts
Normal file
33
lib/core/config-defaults.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
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 {
|
||||
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),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user