-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.ts
87 lines (72 loc) · 1.97 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
85
86
87
import { readableColor } from "polished";
export function stringToColours(inputString: string) {
let sum = 0;
for (const i in inputString.split("")) {
sum += inputString.charCodeAt(Number(i));
}
const r = ~~(
Number(
"0." +
Math.sin(sum + 1)
.toString()
.substr(6),
) * 256
);
const g = ~~(
Number(
"0." +
Math.sin(sum + 2)
.toString()
.substr(6),
) * 256
);
const b = ~~(
Number(
"0." +
Math.sin(sum + 3)
.toString()
.substr(6),
) * 256
);
return [r, g, b, 255];
}
export function stringToColour(inputString: string, alpha = 1) {
const [r, g, b] = stringToColours(inputString);
return `rgba(${r},${g},${b},${alpha})`;
}
export function getCorrectTextColor(hex: string, invert = false) {
return readableColor(
hex,
invert ? "#ffffff" : "#000000",
invert ? "#000000" : "#ffffff",
false,
);
}
declare abstract class As<Tag extends keyof never> {
private static readonly $as$: unique symbol;
private [As.$as$]: Record<Tag, true>;
}
export type Flavor<T, FlavorT extends string> = T & As<FlavorT>;
export const units = ["cl", "l", "pc", "ml", "g", "kg"] as const;
export type SizeUnit = (typeof units)[number];
export function removeItem<T>(items: T[], i: number) {
return items.slice(0, i).concat(items.slice(i + 1, items.length));
}
export const tagsToString = (tags: string[] = emptyArray) =>
[...tags].sort().join(",");
export const onProfilerRenderCallback: React.ProfilerOnRenderCallback = (
id,
phase,
actualDuration,
baseDuration,
) => {
// return;
const stats = `${id}(${phase}): ${actualDuration.toFixed(
2,
)}ms actual, ${baseDuration.toFixed(2)}ms base\n`;
console.log(stats);
document.querySelector("#performance-metrics")!.textContent =
stats + document.querySelector("#performance-metrics")!.textContent;
};
export const emptyObject = Object.freeze({});
export const emptyArray = [];