-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
index.ts
47 lines (39 loc) 路 1.22 KB
/
index.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
import {
RouteParams,
RouteComponent,
RouteParamsRaw,
RouteParamValueRaw,
} from '../types'
export * from './env'
export function isESModule(obj: any): obj is { default: RouteComponent } {
return obj.__esModule || obj[Symbol.toStringTag] === 'Module'
}
export const assign = Object.assign
export function applyToParams(
fn: (v: string | number | null | undefined) => string | null | undefined,
params: RouteParamsRaw | undefined
): RouteParamsRaw
export function applyToParams(
fn: (v: string | number | null | undefined) => string,
params: RouteParamsRaw | undefined
): RouteParams
export function applyToParams(
fn: (v: string | number | null | undefined) => string | null | undefined,
params: RouteParamsRaw | undefined
): RouteParams | RouteParamsRaw {
const newParams: RouteParams = {}
for (const key in params) {
const value = params[key]
newParams[key] = isArray(value)
? value.map(fn)
: fn(value as Exclude<RouteParamValueRaw, any[]>)
}
return newParams
}
export const noop = () => {}
/**
* Typesafe alternative to Array.isArray
* https://github.com/microsoft/TypeScript/pull/48228
*/
export const isArray: (arg: ArrayLike<any> | any) => arg is ReadonlyArray<any> =
Array.isArray