Utilities
Strings and tables
stringx and tablex.
stringx
stringx.split(s, separator = ",", plain = true)
Splits a string into an array. With an empty separator it splits into single characters. By default the separator is plain text. Pass plain = false to use a Lua pattern.
print(table.concat(stringx.split("a,b,c"), "|")) -- a|b|c
stringx.trim(s), stringx.ltrim(s), stringx.rtrim(s)
Remove whitespace from both ends, the start, or the end.
stringx.startswith(s, prefix), stringx.endswith(s, suffix)
Return true if the string starts or ends with the text.
stringx.contains(s, text)
Returns true if the text appears in the string (plain match).
stringx.count(s, text)
Counts non-overlapping occurrences.
stringx.lpad(s, n, char = " "), stringx.rpad(s, n, char = " ")
Pad to a width of n on the left or the right.
stringx.capitalize(s)
First letter upper case, the rest lower case.
stringx.truncate(s, n, tail = "...")
Shorten to at most n characters, adding tail when it was cut.
stringx.hex(s), stringx.fromhex(hex)
Convert a string to hexadecimal text and back.
stringx.commas(n)
Format a number with thousands separators: 1234567 becomes 1,234,567.
stringx.template(s, vars)
Replaces {name} with vars.name. Dots reach into nested tables ({player.name}). Unknown names are left as they are.
stringx.wrap(s, width)
Splits text into an array of lines no longer than width, breaking at spaces.
stringx.random(length = 8, charset)
A random string. The default charset is letters and digits.
stringx.levenshtein(a, b)
The edit distance between two strings.
tablex
Inspecting
| Function | Result |
|---|---|
tablex.keys(t) | Array of keys |
tablex.values(t) | Array of values |
tablex.count(t) | Number of entries (counts non-array keys too) |
tablex.isempty(t) | true if there are no entries |
tablex.isarray(t) | true if the keys are exactly 1..n |
tablex.find(t, value) | The first key holding that value |
tablex.indexof(t, value) | The array index of that value |
tablex.equals(a, b) | Deep comparison |
Transforming
| Function | Result |
|---|---|
tablex.map(t, f) | New table with f(value, key) applied to every entry |
tablex.filter(t, f) | New array with the elements for which f(value, index) is true |
tablex.reduce(t, f, initial) | Folds an array with f(accumulator, value, index) |
tablex.any(t, f) | true if f(value, key) is true for any entry |
tablex.all(t, f) | true if it is true for every entry |
tablex.reverse(t) | New reversed array |
tablex.slice(t, a, b) | New array from index a to b |
tablex.flatten(t) | New array with nested arrays flattened |
tablex.unique(t) | New array without duplicates |
tablex.zip(a, b) | Array of {a[i], b[i]} pairs |
tablex.invert(t) | Swaps keys and values |
tablex.pick(t, keys) | New table with only those keys |
tablex.omit(t, keys) | New table without those keys |
tablex.groupby(t, f) | Table of arrays, grouped by f(value) |
Combining and copying
| Function | Result |
|---|---|
tablex.extend(a, b) | Adds the elements of b to the end of a and returns a |
tablex.merge(...) | New table from all arguments. Later ones win |
tablex.clone(t) | Shallow copy |
tablex.deepclone(t) | Deep copy that handles cycles |
Ordering and picking
| Function | Result |
|---|---|
tablex.shuffle(t) | Shuffles in place and returns t |
tablex.sortby(t, key, descending) | Sorts in place by a field name or a function |
tablex.minby(t, f), tablex.maxby(t, f) | The element with the smallest or largest f(value), and that value |
tablex.choice(t) | A random element |
tablex.sum(t) | Sum of an array of numbers |
tablex.range(a, b, step = 1) | Array of numbers from a to b |
local players = mc.getEntities({ range = 50 })
tablex.sortby(players, "distance")
local names = tablex.map(players, function(e) return e.name end)