返回 AiToEarn
platforms.utils.ts
1 export type PlatformGraphQueryValue = string | number | boolean | Date | undefined
2 export type PlatformGraphQuery = Record<string, PlatformGraphQueryValue>
3 export type PlatformGraphQueryInput = Record<string, unknown>
4
5 export function normalizePlatformGraphQuery(query: PlatformGraphQueryInput = {}): PlatformGraphQuery {
6 const normalized: PlatformGraphQuery = {}
7 for (const [key, value] of Object.entries(query)) {
8 if (isPlatformGraphQueryValue(value)) {
9 normalized[key] = value
10 }
11 }
12 return normalized
13 }
14
15 function isPlatformGraphQueryValue(value: unknown): value is PlatformGraphQueryValue {
16 return value === undefined
17 || typeof value === 'string'
18 || typeof value === 'number'
19 || typeof value === 'boolean'
20 || value instanceof Date
21 }
22
23 export function parsePlatformDate(value?: string | null): Date | undefined {
24 if (!value) {
25 return undefined
26 }
27 const date = new Date(value)
28 return Number.isNaN(date.getTime()) ? undefined : date
29 }
30
31 export function getUrlPathExtension(url: string): string {
32 const path = getUrlPath(url).toLowerCase()
33 const lastDotIndex = path.lastIndexOf('.')
34 if (lastDotIndex < 0) {
35 return ''
36 }
37 return path.slice(lastDotIndex)
38 }
39
40 export function hasUrlPathExtension(url: string, extensions: string[]): boolean {
41 return extensions.includes(getUrlPathExtension(url))
42 }
43
44 function getUrlPath(url: string): string {
45 try {
46 return new URL(url).pathname
47 }
48 catch {
49 return url.split('?')[0]?.split('#')[0] ?? url
50 }
51 }
52
52 lines TYPESCRIPT