Skip to content
@skiffyard/texttransform

@skiffyard/texttransform

Text transformations in the style of printf's modifiers: case conversions (snake, kebab, camel, pascal, constant, leet, ...), slicing, truncation, trimming. A spec is a |-separated list, applied left to right, so "trim|fold|case=kebab" turns " Crème Brûlée " into "creme-brulee".

This deck is part of Skiffyard, small community decks for Jennifer.

Why a function and not a printf verb

printf's modifiers shape how a value is printed (%d|base=2 writes 5 as 101) and never change the value. Transformations that do (case=upper, slice=0:5) were deliberately kept out of the format language. This deck brings them back as t(text, spec), which composes with the real io.sprintf: the format string keeps every verb and presentation modifier on its typed arguments, and the transformed parts are plain strings.

jennifer
import "@skiffyard/texttransform/" as texttransform;

io.printf("%s=%d\n", texttransform.t("max retries", "case=constant"), 3);   # MAX_RETRIES=3
io.printf("[%s]\n", texttransform.t($title, "squash|truncate=30"));

A user function cannot take a variable number of arguments of different types, so a deck cannot offer its own xprintf; transforming each value with t before sprintf is the composable form.

Modifiers

ModifierDoes
case=STYLEone of the case styles below
slice=START:ENDcharacters START to END, half-open; either side may be empty, negative counts from the end
truncate=Nat most N characters, ending in ... when cut
trimleading and trailing whitespace removed; trim=left only leading, trim=right only trailing
squashevery whitespace run one space, trimmed
foldLatin diacritics and ligatures folded (é to e, æ to ae)
reversethe characters in reverse order

Case styles

For "jenniferDecks parseJSONFiles v2Fast":

StyleResult
upperJENNIFERDECKS PARSEJSONFILES V2FAST
lowerjenniferdecks parsejsonfiles v2fast
titleJenniferdecks Parsejsonfiles V2fast
sentenceJenniferdecks parsejsonfiles v2fast
snakejennifer_decks_parse_json_files_v2_fast
kebabjennifer-decks-parse-json-files-v2-fast
cameljenniferDecksParseJsonFilesV2Fast
pascalJenniferDecksParseJsonFilesV2Fast
constantJENNIFER_DECKS_PARSE_JSON_FILES_V2_FAST
dotjennifer.decks.parse.json.files.v2.fast
pathjennifer/decks/parse/json/files/v2/fast
swapJENNIFERdECKS PARSEjsonfILES V2fAST
leetj3nn1f3rD3ck5 p4r53J50NF1l35 v2F457

upper, lower, title, sentence, swap and leet keep the text's characters and spacing. The joined styles (snake to path) rebuild the text from its words, which words shows: anything that is neither a letter nor a digit separates words, and a word also ends where a lower-case letter or a digit meets an upper-case one (fooBar, v2Go) and where an upper-case run meets a capitalised word (HTTPServer is HTTP and Server). Digits stay with the word before them. Letters of any script count; a script without case (漢字) is one word per run.

leet replaces a, b, e, g, i, o, s, t and z (either case) with 4, 8, 3, 6, 1, 0, 5, 7 and 2.

Characters

A character is what a reader sees as one: a letter or digit together with the combining marks after it. cafe\u0301 (an e followed by a combining acute accent, as macOS™ file names and some keyboards write é) is four characters, so slice, truncate and reverse never strip the accent from its letter, and words keeps it in its word. This is not Unicode's full grapheme clustering: an emoji sequence or a flag is still several characters.

Errors

A spec is strict: an empty spec, an unknown modifier (names are case sensitive), a value on a modifier that takes none or none where one is needed, a malformed number, or a slice outside the text throws Error{kind: "texttransform"}. Nothing is guessed.

Trademarks

macOS™ is a trademark of Apple Inc., named here only to describe how its file names write accents. Other product and company names may be trademarks of their respective owners. This deck is an independent project, not affiliated with, sponsored or endorsed by Apple Inc.