@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 84 lines 2.86 KB
Raw
1 import { alchemyLabelKeys } from "../Labels.ts";
2
3 export const lastSegment = (value: string) => {
4 const trimmed = value.replace(/\/+$/, "");
5 const parts = trimmed.split("/");
6 return parts[parts.length - 1] || trimmed;
7 };
8
9 export const parentOf = (project: string) => `projects/${project}`;
10
11 const markerOf = (labels: Record<string, string>) =>
12 `[alchemy ${alchemyLabelKeys.stack}=${labels[alchemyLabelKeys.stack]} ${alchemyLabelKeys.stage}=${labels[alchemyLabelKeys.stage]} ${alchemyLabelKeys.id}=${labels[alchemyLabelKeys.id]}]`;
13
14 export const encodeMarker = (
15 labels: Record<string, string>,
16 rest: string | undefined,
17 separator: string,
18 ): string => {
19 const marker = markerOf(labels);
20 return rest ? `${marker}${separator}${rest}` : marker;
21 };
22
23 export const encodeDescription = (
24 labels: Record<string, string>,
25 description: string | undefined,
26 ): string => encodeMarker(labels, description, "\n");
27
28 export const encodeDisplayName = (
29 labels: Record<string, string>,
30 displayName: string | undefined,
31 ): string => encodeMarker(labels, displayName, " ");
32
33 export const parseMarker = (
34 value: string | undefined,
35 ): {
36 labels: Record<string, string>;
37 rest: string | undefined;
38 } => {
39 if (!value?.startsWith("[alchemy ")) {
40 return { labels: {}, rest: value };
41 }
42 const end = value.indexOf("]");
43 if (end < 0) return { labels: {}, rest: value };
44 const labels: Record<string, string> = {};
45 for (const part of value.slice("[alchemy ".length, end).split(/\s+/)) {
46 const eq = part.indexOf("=");
47 if (eq > 0) {
48 labels[part.slice(0, eq)] = part.slice(eq + 1);
49 }
50 }
51 const rest = value.slice(end + 1).replace(/^[\n\s]+/, "");
52 return { labels, rest: rest.length > 0 ? rest : undefined };
53 };
54
55 export const hasOwnershipMarker = (value: string | undefined) =>
56 Object.keys(parseMarker(value).labels).some((key) => key.startsWith("alchemy-"));
57
58 export const compactStringMap = (
59 value: Record<string, string | undefined> | null | undefined,
60 ): Record<string, string> | undefined => {
61 if (value == null) return undefined;
62 return Object.fromEntries(
63 Object.entries(value).filter((entry): entry is [string, string] => entry[1] !== undefined),
64 );
65 };
66
67 export const toEmptyObject = (value: unknown): Record<string, never> | undefined =>
68 value === undefined ? undefined : {};
69
70 export const jsonEqual = (left: unknown, right: unknown) =>
71 JSON.stringify(canonicalize(left)) === JSON.stringify(canonicalize(right));
72
73 const canonicalize = (value: unknown): unknown => {
74 if (Array.isArray(value)) return value.map(canonicalize);
75 if (value !== null && typeof value === "object") {
76 return Object.fromEntries(
77 Object.entries(value as Record<string, unknown>)
78 .filter(([, item]) => item !== undefined)
79 .sort(([left], [right]) => left.localeCompare(right))
80 .map(([key, item]) => [key, canonicalize(item)]),
81 );
82 }
83 return value;
84 };