-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.ts
More file actions
117 lines (103 loc) · 2.87 KB
/
Copy pathutils.ts
File metadata and controls
117 lines (103 loc) · 2.87 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import crypto from 'node:crypto'
import { randomBytes } from 'crypto'
import type { SMPStyle } from 'styled-map-package'
import type { BBox } from '../types.js'
/**
* If the argument is an `Error` instance, return its `code` property if it is a string.
* Otherwise, returns `undefined`.
*
* @param {unknown} maybeError
* @returns {undefined | string}
* @example
* try {
* // do something
* } catch (err) {
* console.error(getErrorCode(err))
* }
*/
export function getErrorCode(maybeError: unknown) {
if (
maybeError instanceof Error &&
'code' in maybeError &&
typeof maybeError.code === 'string'
) {
return maybeError.code
}
return undefined
}
export function noop() {}
export function generateId() {
return randomBytes(8).toString('hex')
}
export function getOrInsert<K, V>(map: Map<K, V>, key: K, value: V): V {
if (map.has(key)) {
return map.get(key)!
}
map.set(key, value)
return value
}
export function timingSafeEqual(a: string, b: string): boolean {
const aBuf = Buffer.from(a)
const bBuf = Buffer.from(b)
if (aBuf.length !== bBuf.length) {
return false
}
return crypto.timingSafeEqual(aBuf, bBuf)
}
/**
* Returns a bbox that is the smallest bounding box that contains all the input bboxes.
*
* @param bboxes
* @returns Bounding Box [w, s, e, n] of all input bboxes
*/
export function unionBBox(bboxes: [BBox, ...BBox[]]): BBox {
let [w, s, e, n] = bboxes[0]
for (let i = 1; i < bboxes.length; i++) {
const [w1, s1, e1, n1] = bboxes[i]
w = Math.min(w, w1)
s = Math.min(s, s1)
e = Math.max(e, e1)
n = Math.max(n, n1)
}
return [w, s, e, n]
}
export function getStyleBbox(style: SMPStyle): BBox {
const sourceBboxes: BBox[] = []
for (const source of Object.values(style.sources)) {
if (!('bounds' in source)) continue
sourceBboxes.push(source.bounds)
}
if (!isNonEmptyArray(sourceBboxes)) {
return [-180, -85.0511, 180, 85.0511]
}
return unionBBox(sourceBboxes)
}
export function getStyleMaxZoom(style: SMPStyle): number {
let maxzoom = -1
for (const source of Object.values(style.sources)) {
if (!('maxzoom' in source)) continue
maxzoom = Math.max(maxzoom, source.maxzoom ?? -1)
}
return maxzoom === -1 ? 22 : maxzoom
}
export function getStyleMinZoom(style: SMPStyle): number {
let minzoom = 99
for (const source of Object.values(style.sources)) {
if (!('minzoom' in source)) continue
minzoom = Math.min(minzoom, source.minzoom ?? 99)
}
return minzoom === 99 ? 0 : minzoom
}
function isNonEmptyArray<T>(arr: T[]): arr is [T, ...T[]] {
return arr.length > 0
}
export function addTrailingSlash(url: string): string {
return url.endsWith('/') ? url : url + '/'
}
// Typescript's Array.isArray definition does not work as a type guard for
// readonly arrays, so we need to define our own type guard for that
export function isArrayReadonly<T, U>(
value: U | readonly T[],
): value is readonly T[] {
return Array.isArray(value)
}