# textkit: everyday text helpers. # Pure: it needs no permissions, so it can be installed with no `allow` line. # # use textkit from "cresset_packages/textkit.crs" # print(textkit.slugify("Hello, World!")) # hello-world # "Hello, World!" becomes "hello-world": lowercase, with runs of anything else turned into one dash fn slugify(text): slug = regex_replace("[^a-z0-9]+", text.lower(), "-") return slug.strip_prefix("-").strip_suffix("-") # cut text down to at most `limit` characters, ending in `ending` when something was cut off fn truncate(text, limit, ending = "..."): if text.len() <= limit: return text keep = limit - ending.len() if keep < 0: keep = 0 return text.slice(0, keep) + ending # break text into lines of at most `width` characters, splitting only between words fn wrap(text, width): lines = [] current = "" for word in text.split(): if current == "": current = word elif current.len() + 1 + word.len() <= width: current += " " + word else: lines.push(current) current = word if current != "": lines.push(current) return lines fn words(text): return text.split() fn word_count(text): return words(text).len() # put `text` in the middle of a field `width` wide fn center(text, width, fill = " "): extra = width - text.len() if extra <= 0: return text left = floor(extra / 2) return fill.repeat(left) + text + fill.repeat(extra - left) # put `spaces` spaces in front of every line fn indent(text, spaces = 4): pad = " ".repeat(spaces) return text.lines().map(line -> pad + line).join("\n") fn capitalize(text): return text.slice(0, 1).upper() + text.slice(1) # "hello big world" becomes "helloBigWorld" fn camel_case(text): parts = words(regex_replace("[^A-Za-z0-9]+", text, " ").lower()) out = "" for i in range(parts.len()): if i == 0: out += parts[i] else: out += capitalize(parts[i]) return out # "Hello Big World" becomes "hello_big_world" fn snake_case(text): return words(regex_replace("[^A-Za-z0-9]+", text, " ").lower()).join("_") # does it read the same backwards, ignoring case, spaces and punctuation? fn is_palindrome(text): letters = regex_replace("[^a-z0-9]", text.lower(), "") return letters == letters.reverse() test "slugify": assert_eq(slugify("Hello, World!"), "hello-world") assert_eq(slugify(" --Many spaces & symbols!! "), "many-spaces-symbols") assert_eq(slugify("already-fine"), "already-fine") assert_eq(slugify("!!!"), "") test "truncate": assert_eq(truncate("short", 10), "short") assert_eq(truncate("a longer sentence", 10), "a longe...") assert_eq(truncate("abcdefghij", 6, "~"), "abcde~") assert_eq(truncate("abcdef", 2, "..."), "...") test "wrap breaks between words": assert_eq(wrap("the quick brown fox jumps", 10), ["the quick", "brown fox", "jumps"]) assert_eq(wrap("", 10), []) assert_eq(wrap("unbreakableword and more", 5), ["unbreakableword", "and", "more"]) test "counting words": assert_eq(word_count(" one two\nthree "), 3) assert_eq(word_count(""), 0) test "center and indent": assert_eq(center("hi", 6), " hi ") assert_eq(center("hi", 5, "*"), "*hi**") assert_eq(center("toolong", 3), "toolong") assert_eq(indent("a\nb", 2), " a\n b") test "naming styles": assert_eq(camel_case("hello big world"), "helloBigWorld") assert_eq(camel_case("Hello-Big_World"), "helloBigWorld") assert_eq(snake_case("Hello Big World"), "hello_big_world") assert_eq(capitalize("word"), "Word") assert_eq(capitalize(""), "") test "palindromes": assert_eq(is_palindrome("A man, a plan, a canal: Panama"), true) assert_eq(is_palindrome("not one"), false) assert_eq(is_palindrome(""), true)