import type { KeySet } from "./records.ts" export const capitalize = (s: s): Capitalize => (s.length === 0 ? s : s[0].toUpperCase() + s.slice(1)) as never export const uncapitalize = (s: s): Uncapitalize => (s.length === 0 ? s : s[0].toLowerCase() + s.slice(1)) as never export type firstChar = s extends `${infer head}${string}` ? head : "" export type charsAfterFirst = s extends `${string}${infer tail}` ? tail : "" export type lastChar = s extends `${infer head}${infer tail}` ? tail extends "" ? head : lastChar : s export type charsBeforeLast = s extends `${infer head}${infer tail}` ? tail extends "" ? "" : `${head}${charsBeforeLast}` : "" export type contains = s extends `${string}${sub}${string}` ? true : false export const deanchoredRegex = (regex: RegExp | string): RegExp => new RegExp( deanchoredSource(regex), typeof regex === "string" ? "" : regex.flags ) export const anchoredSource = (regex: RegExp | string): string => { const source = typeof regex === "string" ? regex : regex.source return `^(?:${source})$` } export const deanchoredSource = (regex: RegExp | string): string => { const source = typeof regex === "string" ? regex : regex.source if (source.startsWith("^(?:") && source.endsWith(")$")) return source.slice(4, -2) return source.slice( source[0] === "^" ? 1 : 0, source[source.length - 1] === "$" ? -1 : undefined ) } export const Backslash = "\\" export type Backslash = typeof Backslash export const whitespaceChars = { " ": 1, "\n": 1, "\t": 1 } as const satisfies KeySet export type WhitespaceChar = keyof typeof whitespaceChars export type trim = trimEnd> export type trimStart = s extends `${WhitespaceChar}${infer tail}` ? trimStart : s export type trimEnd = s extends `${infer init}${WhitespaceChar}` ? trimEnd : s // Credit to @gugaguichard for this! https://x.com/gugaguichard/status/1720528864500150534 export type isStringLiteral = [t] extends [string] ? [string] extends [t] ? false : Uppercase extends Uppercase> ? Lowercase extends Lowercase> ? true : false : false : false export const emojiToUnicode = (emoji: string): string => emoji .split("") .map(char => { const codePoint = char.codePointAt(0) return codePoint ? `\\u${codePoint.toString(16).padStart(4, "0")}` : "" }) .join("") export const alphabet = [ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" ] as const // cyrb53's two 32-bit lanes, a 64-bit string hash export const hashOf = (input: string): string => { let h1 = 0xdeadbeef let h2 = 0x41c6ce57 for (let i = 0; i < input.length; i++) { const code = input.charCodeAt(i) h1 = Math.imul(h1 ^ code, 2654435761) h2 = Math.imul(h2 ^ code, 1597334677) } h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909) h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909) return (h2 >>> 0).toString(36) + (h1 >>> 0).toString(36).padStart(7, "0") } /** * How many insertions, deletions, substitutions and swaps of neighbors turn a * into b, counted as far as a suggestion for a mistyped name reads it: 3 is 3 * or more */ export const editsBetween = (a: string, b: string): number => { if (Math.abs(a.length - b.length) > 2) return 3 let twoBack: number[] = [] let last = Array.from({ length: b.length + 1 }, (_, j) => j) for (let i = 1; i <= a.length; i++) { const row = [i] for (let j = 1; j <= b.length; j++) { row[j] = Math.min( last[j] + 1, row[j - 1] + 1, last[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1), i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1] ? twoBack[j - 2] + 1 : 3 ) } twoBack = last last = row } return Math.min(last[b.length], 3) }