-
Notifications
You must be signed in to change notification settings - Fork 144
Extensions
Discordia has some built-in Lua standard library extensions. These provide complementary or supplementary, commonly used functions that the Lua standard library does not provide.
Extensions can be used directly...
local str = " abc "
print(discordia.extensions.string.trim(str)) -- "abc"
... or they can be loaded into the global tables:
local str = " abc "
discordia.extensions.string()
print(string.trim(str)) -- "abc"
Note that calling the whole extensions module will load all sub-modules:
discordia.extensions()
- tbl: table
Returns the total number of elements in a table. This uses the global pairs
function and by default respects any __pairs
metamethods.
- tbl: table
Returns the total number of elements in a table, recursively. If a table is
encountered, it is recursively counted instead of being directly added to the
total count. This uses the global pairs
function and by default respects
any __pairs
metamethods.
- tbl: table
Returns a copy of the original table, one layer deep.
- tbl: table
Returns a copy of the original table, recursively. If a table is encountered, it is recursively deep-copied. Metatables are not copied.
- tbl: table
Reverses the elements of an array-like table in place.
- tbl: table
Returns a copy of an array-like table with its elements in reverse order. The original table remains unchanged.
- tbl: table
Returns a new array-like table where all of its values are the keys of the original table.
- tbl: table
Returns a new array-like table where all of its values are the values of the original table.
- tbl: table
Returns a random (index, value) pair from an array-like table.
- tbl: table
Returns a random (key, value) pair from a dictionary-like table.
- tbl: table
- fn: function
Returns a copy of an array-like table sorted using Lua's table.sort
.
- tbl: table
- value: *
Iterates through a table until it finds a value that is equal to value
according to the ==
operator. The key is returned if a match is found.
- tbl: table
Returns a new table that is a slice of the original, defined by the start and stop bounds and the step size. Default start, stop, and step values are 1, #tbl, and 1, respectively.
- str: string
Splits a string into a table of specifically delimited sub-strings. If the delimiter is omitted or empty, the string is split into a table of characters.
- str: string
Returns a new string with all whitespace removed from the left and right sides of the original string.
- str: string
- len: number
Returns a new string that is padded up to the desired length. The alignment,
either left
, right
, or center
with left
being the default, defines the
placement of the original string. The default pattern is a single space.
- str: string
- pattern: string
Returns whether a string starts swith a specified sub-string or pattern. The
plain
parameter is the same as that used in Lua's string.find
.
- str: string
- pattern: string
Returns whether a string ends swith a specified sub-string or pattern. The
plain
parameter is the same as that used in Lua's string.find
.
- str1: string
- str2: string
Returns the Levenshtein distance between two strings. A higher number indicates a greter distance.
- len: number
Returns a string of random characters with the specified length. If provided, the min and max bounds cannot be outside 0 to 255. Use 32 to 126 for printable ASCII characters.
- n: number
- min: number
- max: number
Returns a number that is at least as small as the minimum value and at most as large as the maximum value, inclusively. If the original number is already with the bounds, the same number is returned.
- n: number
Returns a number that is rounded to the nearest defined digit. The nearest integer is returned if the digit is omitted. Negative values can be used for higher order places.