缩减代码
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
function transform_color(hex_color: string, transform: (channel: number) => number): string {
|
||||
const hex = hex_color.replace("#", "");
|
||||
return `#${[0, 2, 4].map(offset => Math.max(0, Math.min(255,
|
||||
Math.floor(transform(parseInt(hex.slice(offset, offset + 2), 16))))).toString(16).padStart(2, "0")).join("")}`;
|
||||
}
|
||||
|
||||
export function lightenColor(color: string, factor = 0.2): string {
|
||||
return transform_color(color, channel => channel + (255 - channel) * factor);
|
||||
}
|
||||
|
||||
export function darkenColor(color: string, factor = 0.2): string {
|
||||
return transform_color(color, channel => channel * (1 - factor));
|
||||
}
|
||||
|
||||
export type Tree_Node = {title: string, key: string, children: Tree_Node[] | null};
|
||||
|
||||
export function transformToTreeData(data: Record<string, any>, parent_key = ""): Tree_Node[] {
|
||||
return Object.entries(data).map(([key, value], index) => {
|
||||
const node_key = parent_key ? `${parent_key}-${index}` : String(index);
|
||||
const suffix = value === null ? ": null" : typeof value === "object" ? "" : `: ${value}`;
|
||||
return {
|
||||
title: `${key}${suffix}`,
|
||||
key: node_key,
|
||||
children: value && typeof value === "object" ? transformToTreeData(value, node_key) : null
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
export class StateHolder<T> {
|
||||
constructor(public cur: T, private readonly hook: (old_value: T, new_value: T) => void = () => {}) {}
|
||||
set(next: T) {
|
||||
if (next === this.cur) return;
|
||||
const old = this.cur;
|
||||
this.cur = next;
|
||||
this.hook(old, next);
|
||||
}
|
||||
}
|
||||
|
||||
export const server_url = window.location.host;
|
||||
export const baseURL = "/api";
|
||||
Reference in New Issue
Block a user