forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.ts
More file actions
47 lines (42 loc) · 1.17 KB
/
Copy pathdiff.ts
File metadata and controls
47 lines (42 loc) · 1.17 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
import { createHash } from "node:crypto";
import { readFileSync, statSync } from "node:fs";
export function sha256(buf: Buffer | string): string {
return createHash("sha256").update(buf).digest("hex");
}
export function fileSha256(path: string): string {
return sha256(readFileSync(path));
}
export function fileExists(path: string): boolean {
try {
statSync(path);
return true;
} catch {
return false;
}
}
export function getByPath(obj: unknown, path: string): unknown {
const parts = path.split(".");
let cur: unknown = obj;
for (const part of parts) {
if (cur == null || typeof cur !== "object") return undefined;
cur = (cur as Record<string, unknown>)[part];
}
return cur;
}
export function setByPath(
obj: Record<string, unknown>,
path: string,
value: unknown,
): void {
const parts = path.split(".");
let cur: Record<string, unknown> = obj;
for (let i = 0; i < parts.length - 1; i++) {
const key = parts[i]!;
const next = cur[key];
if (next == null || typeof next !== "object" || Array.isArray(next)) {
cur[key] = {};
}
cur = cur[key] as Record<string, unknown>;
}
cur[parts[parts.length - 1]!] = value;
}