text

Text helpers: padding, wrapping, number formatting and random ids.

import text

Example

import text

say(text.pad_left("7", 3, "0"))
say(text.center("TB", 10, "*"))
say(text.format_number(1234567.891, 2))
say(text.title("hello there, tanisha"))
say(text.count("banana", "a"))

Functions

pad_left(value, width, fill?)

returns text

Add fill (a space by default) on the left until it is width letters: pad_left(7, 3, "0") is "007".

pad_right(value, width, fill?)

returns text

Add fill (a space by default) on the right until it is width letters.

center(value, width, fill?)

returns text

Put the text in the middle of width letters.

wrap(text, width)

returns text

Break long text into lines of at most width letters (between words).

is_digit(text)

returns bool

True if the text is only the digits 0-9 (and not empty).

format_number(n, decimals?)

returns text

The number with commas between thousands: format_number(1234.5, 2) is "1,234.50".

random_id(length?)

returns text

A random id of letters and digits (8 by default), safe for secrets.

reverse(text)

returns text

The text backwards.

title(text)

returns text

Every Word Starting With A Capital.

count(text, part)

returns number

How many times part appears in the text.