-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathutils.ts
347 lines (300 loc) · 8.21 KB
/
utils.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import type { ReactNode, ReactElement } from 'react'
import CssDimension from './vendor/parse-css-dimension/index.js'
import LineBreaker from 'linebreak'
export function isReactElement(node: ReactNode): node is ReactElement {
const type = typeof node
if (
type === 'number' ||
type === 'bigint' ||
type === 'string' ||
type === 'boolean'
) {
return false
}
return true
}
export function isClass(f: Function) {
return /^class\s/.test(f.toString())
}
export function hasDangerouslySetInnerHTMLProp(props: any) {
return 'dangerouslySetInnerHTML' in props
}
export function normalizeChildren(children: any) {
const flattend =
typeof children === 'undefined' ? [] : [].concat(children).flat(Infinity)
const res = []
for (let i = 0; i < flattend.length; i++) {
let value = flattend[i]
if (
typeof value === 'undefined' ||
typeof value === 'boolean' ||
value === null
) {
continue
}
if (typeof value === 'number') {
value = String(value)
}
if (
typeof value === 'string' &&
res.length &&
typeof res[res.length - 1] === 'string'
) {
res[res.length - 1] += value
} else {
res.push(value)
}
}
return res
}
export function lengthToNumber(
length: string | number,
baseFontSize: number,
baseLength: number,
inheritedStyle: Record<string, string | number>,
percentage = false
): number | undefined {
if (typeof length === 'number') return length
// Convert em and rem values to number (px), convert rad to deg.
try {
length = length.trim()
// Not length: `1px/2px`, `1px 2px`, `1px, 2px`, `calc(1px)`.
if (/[ /\(,]/.test(length)) return
// Just a number as string: '100'
if (length === String(+length)) return +length
const parsed = new CssDimension(length)
if (parsed.type === 'length') {
switch (parsed.unit) {
case 'em':
return parsed.value * baseFontSize
case 'rem':
return parsed.value * 16
case 'vw':
return ~~(
(parsed.value * (inheritedStyle._viewportWidth as number)) /
100
)
case 'vh':
return ~~(
(parsed.value * (inheritedStyle._viewportHeight as number)) /
100
)
default:
return parsed.value
}
} else if (parsed.type === 'angle') {
switch (parsed.unit) {
case 'deg':
return parsed.value
case 'rad':
return (parsed.value * 180) / Math.PI
default:
return parsed.value
}
} else if (parsed.type === 'percentage') {
if (percentage) {
return (parsed.value / 100) * baseLength
}
}
} catch {
// Not a length unit, silently ignore.
}
}
// Multiplies two 2d transform matrices.
export function multiply(m1: number[], m2: number[]) {
return [
m1[0] * m2[0] + m1[2] * m2[1],
m1[1] * m2[0] + m1[3] * m2[1],
m1[0] * m2[2] + m1[2] * m2[3],
m1[1] * m2[2] + m1[3] * m2[3],
m1[0] * m2[4] + m1[2] * m2[5] + m1[4],
m1[1] * m2[4] + m1[3] * m2[5] + m1[5],
]
}
export function v(
field: string | number | undefined,
map: Record<string, any>,
fallback: any,
errorIfNotAllowedForProperty?: string
) {
let value = map[field]
if (typeof value === 'undefined') {
if (errorIfNotAllowedForProperty && typeof field !== 'undefined') {
throw new Error(
`Invalid value for CSS property "${errorIfNotAllowedForProperty}". Allowed values: ${Object.keys(
map
)
.map((_v) => `"${_v}"`)
.join(' | ')}. Received: "${field}".`
)
}
value = fallback
}
return value
}
let wordSegmenter
let graphemeSegmenter
// Implementation modified from
// https://github.com/niklasvh/html2canvas/blob/6521a487d78172f7179f7c973c1a3af40eb92009/src/css/layout/text.ts
// https://drafts.csswg.org/css-text/#word-separator
export const wordSeparators = [
0x0020, 0x00a0, 0x1361, 0x10100, 0x10101, 0x1039, 0x1091, 0xa,
].map((point) => String.fromCodePoint(point))
export function segment(
content: string,
granularity: 'word' | 'grapheme',
locale?: string
): string[] {
if (!wordSegmenter || !graphemeSegmenter) {
if (!(typeof Intl !== 'undefined' && 'Segmenter' in Intl)) {
// https://caniuse.com/mdn-javascript_builtins_intl_segments
throw new Error(
'Intl.Segmenter does not exist, please use import a polyfill.'
)
}
wordSegmenter = new Intl.Segmenter(locale, { granularity: 'word' })
graphemeSegmenter = new Intl.Segmenter(locale, {
granularity: 'grapheme',
})
}
if (granularity === 'grapheme') {
return [...graphemeSegmenter.segment(content)].map((seg) => seg.segment)
} else {
const segmented = [...wordSegmenter.segment(content)].map(
(seg) => seg.segment
) as string[]
const output = []
let i = 0
// When there is a non-breaking space, join the previous and next words together.
// This change causes them to be treated as a single segment.
while (i < segmented.length) {
const s = segmented[i]
if (s == '\u00a0') {
const previousWord = i === 0 ? '' : output.pop()
const nextWord = i === segmented.length - 1 ? '' : segmented[i + 1]
output.push(previousWord + '\u00a0' + nextWord)
i += 2
} else {
output.push(s)
i++
}
}
return output
}
}
export function buildXMLString(
type: string,
attrs: Record<string, string | number>,
children?: string
) {
let attrString = ''
for (const [k, _v] of Object.entries(attrs)) {
if (typeof _v !== 'undefined') {
attrString += ` ${k}="${_v}"`
}
}
if (children) {
return `<${type}${attrString}>${children}</${type}>`
}
return `<${type}${attrString}/>`
}
export function createLRU<T>(max = 20) {
const store: Map<string, T> = new Map()
function set(key: string, value: T) {
if (store.size >= max) {
const keyToDelete = store.keys().next().value
store.delete(keyToDelete)
}
store.set(key, value)
}
function get(key: string): T | undefined {
const hasKey = store.has(key)
if (!hasKey) return undefined
const entry = store.get(key)!
store.delete(key)
store.set(key, entry)
return entry
}
function clear() {
store.clear()
}
return {
set,
get,
clear,
}
}
export function parseViewBox(viewBox?: string | null | undefined) {
return viewBox ? viewBox.split(/[, ]/).filter(Boolean).map(Number) : null
}
export function toString(x: unknown): string {
return Object.prototype.toString.call(x)
}
export function isString(x: unknown): x is string {
return typeof x === 'string'
}
export function isNumber(x: unknown): x is number {
return typeof x === 'number'
}
export function isUndefined(x: unknown): x is undefined {
return toString(x) === '[object Undefined]'
}
export function splitByBreakOpportunities(
content: string,
wordBreak: string
): {
words: string[]
requiredBreaks: boolean[]
} {
if (wordBreak === 'break-all') {
return { words: segment(content, 'grapheme'), requiredBreaks: [] }
}
if (wordBreak === 'keep-all') {
return { words: segment(content, 'word'), requiredBreaks: [] }
}
const breaker = new LineBreaker(content)
let last = 0
let bk = breaker.nextBreak()
const words = []
const requiredBreaks = [false]
while (bk) {
const word = content.slice(last, bk.position)
words.push(word)
if (bk.required) {
requiredBreaks.push(true)
} else {
requiredBreaks.push(false)
}
last = bk.position
bk = breaker.nextBreak()
}
return { words, requiredBreaks }
}
export const midline = (s: string) => {
return s.replaceAll(
/([A-Z])/g,
(_, letter: string) => `-${letter.toLowerCase()}`
)
}
export function splitEffects(
input: string,
separator: string | RegExp = ','
): string[] {
const result = []
let l = 0
let parenCount = 0
separator = new RegExp(separator)
for (let i = 0; i < input.length; i++) {
if (input[i] === '(') {
parenCount++
} else if (input[i] === ')') {
parenCount--
}
if (parenCount === 0 && separator.test(input[i])) {
result.push(input.slice(l, i).trim())
l = i + 1
}
}
result.push(input.slice(l).trim())
return result
}