text.layout
pad, center, wrap, truncate, dedent; snake_case, camelCase, slugs; words
Example results generated on 2026-10-08. Ones using
now,todayor randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.
Functions
| function | description |
|---|---|
pad(s: any, width: int, fill?: str) | pad on the right to width characters (left-align) |
pad_left(s: any, width: int, fill?: str) | pad on the left to width characters (right-align); numbers work too |
center(s: any, width: int, fill?: str) | center in width characters; extra fill goes right |
truncate(s: str, n: int) | cut to at most n characters, ending in … when cut |
wrap(s: str, width: int) | word-wrap each paragraph to width columns |
dedent(s: str) | remove the indentation every non-blank line shares |
title(s: str) | Capitalize Each Word |
snake(s: str) | snake_case; splits on spaces, punctuation and camelCase humps |
camel(s: str) | camelCase |
kebab(s: str) | kebab-case |
slug(s: str) | a lowercase URL slug: letters and digits joined by - |
words(s: str) | the words, without punctuation (apostrophes stay) |
word_count(s: str) | how many words |
pad
pad(s: any, width: int, fill?: str): pad on the right to width characters (left-align)
"ab".pad(5) + "|"
# → "ab |"
"ab".pad(5, ".")
# → "ab..."
pad_left
pad_left(s: any, width: int, fill?: str): pad on the left to width characters (right-align); numbers work too
42.pad_left(6, "0")
# → "000042"
center
center(s: any, width: int, fill?: str): center in width characters; extra fill goes right
"hi".center(8, "*")
# → "***hi***"
truncate
truncate(s: str, n: int): cut to at most n characters, ending in … when cut
"a long sentence here".truncate(10)
# → "a long se…"
See also: wrap
wrap
wrap(s: str, width: int): word-wrap each paragraph to width columns
"the quick brown fox jumps over the lazy dog".wrap(15)
# → the quick brown
# → fox jumps over
# → the lazy dog
dedent
dedent(s: str): remove the indentation every non-blank line shares
" a\n b\n c".dedent
# → a
# → b
# → c
title
title(s: str): Capitalize Each Word
"the lord of the rings".title
# → "The Lord Of The Rings"
See also: capitalize, snake
snake
snake(s: str): snake_case; splits on spaces, punctuation and camelCase humps
"parseHTTPRequest".snake
# → "parse_http_request"
"Total Price".snake
# → "total_price"
camel
camel(s: str): camelCase
"user_id_v2".camel
# → "userIdV2"
kebab
kebab(s: str): kebab-case
"MyComponentName".kebab
# → "my-component-name"
slug
slug(s: str): a lowercase URL slug: letters and digits joined by -
"Hello, World! It's 2026".slug
# → "hello-world-its-2026"
See also: kebab
words
words(s: str): the words, without punctuation (apostrophes stay)
"Hello, world! Don't panic.".words
# → ["Hello", "world", "Don't", "panic"]
See also: word_count, split
word_count
word_count(s: str): how many words
"Hello, world! Don't panic.".word_count
# → 4
See also: words
More examples
layout
# a receipt line
"coffee".pad(12, ".") + "$4.50".pad_left(8)
# → "coffee...... $4.50"
# a banner
"menu".upper.center(20, "=")
# → "========MENU========"
# column name to variable
"Total Price (USD)".snake
# → "total_price_usd"
# blog URL
"Hello, World! It's 2026".slug
# → "hello-world-its-2026"
# reading time
(lorem(450).word_count / 200).ceil
# → 3