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
| 1 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | |
| 4 | export const lastSegment = (value: string) => { |
| 5 | const trimmed = value.replace(/\/+$/, ""); |
| 6 | const parts = trimmed.split("/"); |
| 7 | return parts[parts.length - 1] || trimmed; |
| 8 | }; |
| 9 | |
| 10 | export const organizationOf = ( |
| 11 | explicit: string | undefined, |
| 12 | existing: string | undefined, |
| 13 | project: string, |
| 14 | ) => explicit ?? existing ?? project; |
| 15 | |
| 16 | export const orgParent = (organization: string) => `organizations/${organization}`; |
| 17 | |
| 18 | export const organizationFromName = (name: string | undefined) => { |
| 19 | if (name === undefined) return undefined; |
| 20 | const match = name.match(/^organizations\/([^/]+)/); |
| 21 | return match?.[1]; |
| 22 | }; |
| 23 | |
| 24 | export const toResourceId = ( |
| 25 | id: string, |
| 26 | explicit: string | undefined, |
| 27 | existing: string | undefined, |
| 28 | maxLength: number, |
| 29 | ) => |
| 30 | Effect.gen(function* () { |
| 31 | if (explicit !== undefined) return explicit; |
| 32 | if (existing !== undefined) return existing; |
| 33 | let generated = yield* createPhysicalName({ |
| 34 | id, |
| 35 | maxLength, |
| 36 | lowercase: true, |
| 37 | }); |
| 38 | if (!/^[a-z]/.test(generated)) { |
| 39 | generated = `a${generated}`.slice(0, maxLength); |
| 40 | } |
| 41 | generated = generated.replace(/-+$/g, ""); |
| 42 | if (generated.length < 2) { |
| 43 | generated = `${generated}x`.slice(0, maxLength); |
| 44 | } |
| 45 | return generated; |
| 46 | }); |
| 47 | |
| 48 | export const sameJson = (left: unknown, right: unknown) => |
| 49 | JSON.stringify(left ?? null) === JSON.stringify(right ?? null); |
| 50 | |
| 51 | export const sameStringList = ( |
| 52 | left: readonly string[] | undefined, |
| 53 | right: readonly string[] | undefined, |
| 54 | ) => JSON.stringify([...(left ?? [])].sort()) === JSON.stringify([...(right ?? [])].sort()); |