-
Notifications
You must be signed in to change notification settings - Fork 1
/
url.ts
64 lines (57 loc) · 2.22 KB
/
url.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
type QueryParameter = [string, string]
const buildParameterString = (
parameterMap: Map<string, string>,
queryParameters: QueryParameter[],
previousParametersArray: QueryParameter[],
previousParametersSet: Set<string>
) => {
const parsedParameterArray: string[] = []
previousParametersArray.forEach(([key]) => {
parsedParameterArray.push(`${key}=${parameterMap.get(key)}`)
})
queryParameters.forEach(([key]) => {
if (!previousParametersSet.has(key) && typeof parameterMap.get(key) === 'string')
parsedParameterArray.push(`${key}=${parameterMap.get(key)}`)
})
return parsedParameterArray.join('&')
}
const parseUrlPath = (path: string): string => {
const parts = path.split('?')
const [basePath] = parts
return basePath.endsWith('/') ? basePath : `${basePath}/`
}
export const appendQueryParameters = (
url: string,
queryParameters: QueryParameter[],
allowEmptyValues?: boolean
): string => {
const urlParts = url.split('?')
const parameterMap = new Map<string, string>()
const previousParameters = urlParts[1] ? urlParts[1].split('&') : []
const previousParametersSet = new Set<string>()
const previousParametersArray: [string, string][] = []
previousParameters.forEach((parameterString) => {
const parts = parameterString.split('=')
if ((parts[1] && parts[1] !== '') || allowEmptyValues) {
parameterMap.set(parts[0], parts[1])
previousParametersSet.add(parts[0])
previousParametersArray.push([parts[0], parts[1]])
}
})
queryParameters.forEach(([key, value]) => {
if (value !== '' || allowEmptyValues) parameterMap.set(key, value)
})
const parameterString = buildParameterString(
parameterMap,
queryParameters,
previousParametersArray,
previousParametersSet
)
return `${urlParts[0]}${parameterString === '' ? '' : '?'}${parameterString}`
}
export const isParentPath = (basePath: string, currentPath: string): boolean => {
if (basePath === '/') return false
const parsedBasePath = parseUrlPath(basePath)
const parsedCurrentPath = parseUrlPath(currentPath)
return parsedCurrentPath.startsWith(parsedBasePath)
}