Skip to content
Myr

text.ts

This file

Path

packages/agent-kit/src/text.ts

62 lines · 1.6 KiB · TypeScript

Last changed by

Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–62why, one step back
1import { Array as Arr, Order, Predicate } from "effect";
2import type { Schema } from "effect";34export const page = (lines: readonly string[]) =>5 `${lines.join("\n").replace(/\n+$/u, "")}\n`;67export const code = (text: string) => {8 const runs = text.match(/`+/gu) ?? [];9 const longest = Math.max(0, ...runs.map((run) => run.length));10 const fence = "`".repeat(longest + 1);1112 return longest === 013 ? `${fence}${text}${fence}`14 : `${fence} ${text} ${fence}`;15};1617export const cell = (text: string) => text.replaceAll("|", String.raw`\|`);1819export const table = (20 header: readonly string[],21 rows: readonly (readonly string[])[]22) => [23 `| ${header.join(" | ")} |`,24 `| ${header.map(() => "---").join(" | ")} |`,25 ...rows.map((row) => `| ${row.map(cell).join(" | ")} |`),26];2728export const listed = (words: readonly string[]) =>29 words.length < 230 ? words.join("")31 : `${words.slice(0, -1).join(", ")} and ${words.at(-1) ?? ""}`;3233const byKey = Order.mapInput(34 Order.String,35 ([key]: readonly [string, Schema.Json]) => key36);3738const isJsonArray = (value: Schema.Json): value is Schema.JsonArray =>39 Array.isArray(value);4041const isJsonObject = (value: Schema.Json): value is Schema.JsonObject =>42 Predicate.isObject(value);4344const sortKeys = (value: Schema.Json): Schema.Json => {45 if (isJsonArray(value)) {46 return value.map(sortKeys);47 }4849 if (isJsonObject(value)) {50 return Object.fromEntries(51 Arr.sort(Object.entries(value), byKey).map(([key, item]) => [52 key,53 sortKeys(item),54 ])55 );56 }5758 return value;59};6061export const printJson = (value: Schema.Json) =>62 `${JSON.stringify(sortKeys(value), null, 2)}\n`;