2023-02-20 00:22:01 +01:00
|
|
|
import type { ObjectType } from './types'
|
2023-02-07 23:22:32 +01:00
|
|
|
|
|
|
|
export const mergePath = (base: string, path: string) => {
|
|
|
|
base = base.replace(/\/+$/, '')
|
|
|
|
base = base + '/'
|
|
|
|
path = path.replace(/^\/+/, '')
|
|
|
|
return base + path
|
|
|
|
}
|
|
|
|
|
2024-08-21 03:53:54 +02:00
|
|
|
export const replaceUrlParam = (urlString: string, params: Record<string, string | undefined>) => {
|
2023-02-07 23:22:32 +01:00
|
|
|
for (const [k, v] of Object.entries(params)) {
|
2024-08-21 03:53:54 +02:00
|
|
|
const reg = new RegExp('/:' + k + '(?:{[^/]+})?\\??')
|
|
|
|
urlString = urlString.replace(reg, v ? `/${v}` : '')
|
2023-02-07 23:22:32 +01:00
|
|
|
}
|
|
|
|
return urlString
|
|
|
|
}
|
|
|
|
|
2024-04-09 10:01:58 +02:00
|
|
|
export const replaceUrlProtocol = (urlString: string, protocol: 'ws' | 'http') => {
|
|
|
|
switch (protocol) {
|
|
|
|
case 'ws':
|
|
|
|
return urlString.replace(/^http/, 'ws')
|
|
|
|
case 'http':
|
|
|
|
return urlString.replace(/^ws/, 'http')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 23:22:32 +01:00
|
|
|
export const removeIndexString = (urlSting: string) => {
|
2024-04-18 02:55:11 +02:00
|
|
|
if (/^https?:\/\/[^\/]+?\/index$/.test(urlSting)) {
|
|
|
|
return urlSting.replace(/\/index$/, '/')
|
|
|
|
}
|
2024-03-13 09:22:45 +01:00
|
|
|
return urlSting.replace(/\/index$/, '')
|
2023-02-07 23:22:32 +01:00
|
|
|
}
|
2023-02-20 00:22:01 +01:00
|
|
|
|
|
|
|
function isObject(item: unknown): item is ObjectType {
|
|
|
|
return typeof item === 'object' && item !== null && !Array.isArray(item)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deepMerge<T>(target: T, source: Record<string, unknown>): T {
|
|
|
|
if (!isObject(target) && !isObject(source)) {
|
|
|
|
return source as T
|
|
|
|
}
|
|
|
|
const merged = { ...target } as ObjectType<T>
|
|
|
|
|
|
|
|
for (const key in source) {
|
|
|
|
const value = source[key]
|
|
|
|
if (isObject(merged[key]) && isObject(value)) {
|
|
|
|
merged[key] = deepMerge(merged[key], value)
|
|
|
|
} else {
|
|
|
|
merged[key] = value as T[keyof T] & T
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return merged as T
|
|
|
|
}
|