-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.ts
84 lines (75 loc) · 1.99 KB
/
util.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
/*
* Copyright (C) 2024 Delusoire
* SPDX-License-Identifier: GPL-3.0-or-later
*/
type Predicate<A> = (input: A) => boolean;
export function findBy(...tests: Array<string | RegExp | Predicate<any>>) {
const testFns = tests.map((test): Predicate<any> => {
switch (typeof test) {
case "string":
return (x) => x.toString().includes(test);
case "function":
return (x) => test(x);
default: // assume regex
return (x) => test.test(x.toString());
}
});
const testFn = (x: any) => testFns.map((t) => t(x)).every(Boolean);
return <A>(xs: A[]) => xs.find(testFn)!;
}
// assumption: str[start] === pair[0]
export const findMatchingPos = (
str: string,
start: number,
direction: 1 | -1,
pair: [string, string],
scopes: number,
) => {
let l = scopes;
let i = start + direction;
while (l > 0 && i >= 0 && i < str.length) {
const c = str[i];
i += direction;
if (c === pair[0]) l++;
else if (c === pair[1]) l--;
}
return i;
};
export const matchLast = (str: string, pattern: RegExp) => {
const matches = str.matchAll(pattern);
return Array.from(matches).at(-1)!;
};
export function stringifyUrlSearchParams(
params: Record<string, string | string[]>,
) {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(params)) {
if (Array.isArray(value)) {
for (const v of value) {
searchParams.append(key, v);
}
} else {
searchParams.append(key, value);
}
}
return searchParams.toString();
}
declare global {
var __REACT_DEVTOOLS_GLOBAL_HOOK__: {
isDisabled: boolean;
supportsFiber: boolean;
inject: ($: any) => void;
};
var findHostInstanceByFiber: (fiber: any) => any;
var findFiberByHostInstance: (hostInstance: any) => any;
}
export function registerReactDevtoolsHook() {
globalThis["__REACT_DEVTOOLS_GLOBAL_HOOK__"] = {
isDisabled: false,
supportsFiber: true,
inject($) {
globalThis["findHostInstanceByFiber"] = $.findHostInstanceByFiber;
globalThis["findFiberByHostInstance"] = $.findFiberByHostInstance;
},
};
}