Cotton code
32 lines · 1.1 KB · TypeScriptRaw
/** * The string rules the Rust engine spells out because Rust is not JavaScript * (`cotton_store::protocol::js`, `cotton_core::messages::{js_trim, slice_utf16}`). * Here they are the language's own: `\s` is ECMAScript's whitespace class and * `.length` counts UTF-16 code units, which is what the Rust ports measure. *//** `text.replace(/\s+/g, " ").trim()`. */export function collapse(text: string): string {  return text.replace(/\s+/g, " ").trim();}/** A string that is not empty, or null (`truthy_str`). */export function truthy(value: string | null | undefined): string | null {  return value ? value : null;}/** The first `max` UTF-16 code units, never cutting a surrogate pair in half (`slice_utf16`). */export function sliceUtf16(text: string, max: number): string {  if (text.length <= max) return text;  let out = "";  for (const ch of text) {    if (out.length + ch.length > max) break;    out += ch;  }  return out;}/** A plain object (not an array, not null). */export function isRecord(value: unknown): value is Record<string, unknown> {  return typeof value === "object" && value !== null && !Array.isArray(value);}