Skip to content

l0.DependencyControl.utils

utils = require "l0.DependencyControl.utils"
local utils = require("l0.DependencyControl.utils")

Functions

equals

Utils.equals a, b
Utils.equals(a, b)

Deep equality comparison. Tables compared recursively; other types use ==. Circular references are handled. Metatables are included in the comparison.

param type description
a any
b any

Returns:

  • equal boolean

itemsEqual

Utils.itemsEqual a, b, onlyNumKeys, ignoreExtraAItems, requireIdenticalItems
Utils.itemsEqual(a, b, onlyNumKeys, ignoreExtraAItems, requireIdenticalItems)

Compares table items for equality, ignoring keys. By default only numerical indexes are compared.

param type description
a table
b table
onlyNumKeys? boolean Compare only sequential numeric indices (default true).
ignoreExtraAItems? boolean Allow a to contain items absent from b (default false).
requireIdenticalItems? boolean Require identical (not merely equal) table items (default false).

Returns:

  • equal boolean

copy

Utils.copy tbl
Utils.copy(tbl)

Shallow-copies a table (no metatable).

param type description
tbl table The table to copy.

Returns:

  • copy table — The copied table.

deepCopy

Utils.deepCopy tbl
Utils.deepCopy(tbl)

Deep-copies a table recursively (no metatables).

param type description
tbl table The table to deep-copy.

Returns:

  • copy table — The deep-copied table.

makeSet

Utils.makeSet source, target, overwrite, value
Utils.makeSet(source, target, overwrite, value)

Builds (or extends) a set from an array's values: each value becomes a key mapped to value.

param type description
source any[] Array whose values become the set's keys.
target? table Table to populate (default a new table).
overwrite? boolean Overwrite keys already present in target (default true).
value? any Value to map each key to (default true).

Returns:

  • set table — The populated target.

listIncludes

Utils.listIncludes list, value
Utils.listIncludes(list, value)

Reports whether an array contains a value (compared with ==).

param type description
list any[] The array to search.
value any The value to look for.

Returns:

  • included boolean — Whether value is an element of list.

addDefaults

Utils.addDefaults tbl, defaults, predicate
Utils.addDefaults(tbl, defaults, predicate)

Fills in missing entries of tbl from defaults, mutating tbl in place.

param type description
tbl table The table to fill in.
defaults table Default key/value pairs.
predicate? fun(value: any, key: any, tbl: table): boolean Per-key test for whether to apply the default; when omitted, defaults apply wherever tbl[key] is nil.

Returns:

  • addedCount number — The number of defaults applied.

trim

Utils.trim str
Utils.trim(str)

Strips leading and trailing whitespace from a string.

param type description
str string The string to trim.

Returns:

  • trimmed string — The trimmed string.

escapePattern

Utils.escapePattern str
Utils.escapePattern(str)

Escapes all Lua pattern magic characters in a string so it matches literally.

param type description
str string The string to escape.

Returns:

  • escaped string — The escaped string.

flatten

Utils.flatten value, depth, toArrayTable
Utils.flatten(value, depth, toArrayTable)

Flattens nested array tables into a single array up to the specified depth. Values that are not (or not converted to) pure array tables are included as-is.

param type description
value any The value to flatten.
depth? number Maximum depth to flatten (default 1).
toArrayTable? fun(value: any, valueType: string): table?, boolean? Converts a non-array value to an array table.

Returns:

  • flattened table — A flattened array table containing the flattened values.
  • flattenedCount number — The number of elements in the flattened array.

mergeSearchPath

Utils.mergeSearchPath pathStr, add, remove
Utils.mergeSearchPath(pathStr, add, remove)

Merges new entries into a semicolon-separated Lua search path, appending only the ones not already present and dropping any listed for removal, with the order of kept entries preserved.

param type description
pathStr string The existing search path, e.g. package.path.
add string[] Path entries to append, each only when not already present.
remove? string[] Path entries to drop before merging, e.g. to undo an earlier addition.

Returns:

  • pathStr string — The merged search-path string.
  • added string[] — The entries actually appended, empty when all were already present.

extendPackagePath

Utils.extendPackagePath field, entries
Utils.extendPackagePath(field, entries)

Extends one of package's search-path fields in place, folding in the entries of a semicolon- separated string and appending only those not already present. Does nothing when there is nothing to add.

param type description
field "path"|"cpath"|"moonpath" Which package search path to extend.
entries? string A semicolon-separated path string to fold in, such as an env var's value.

Returns:

  • added string[] — The entries actually added, empty when all were present or nothing was passed.

getRandomSeed

Utils.getRandomSeed!
Utils.getRandomSeed()

Returns a random-number seed unique to this script's Lua state, differing from one launch to the next.

Returns:

  • seed number

seedRandom

Utils.seedRandom!
Utils.seedRandom()

Reseeds this Lua state's random number generator so math.random yields a stream unique to the script and to this launch. DependencyControl seeds it on load; call this to reseed it yourself.

Returns:

  • seed number — The applied seed.

uuid

Utils.uuid!
Utils.uuid()

Generates a random RFC-4122 version-4 UUID string.

Returns:

  • uuid string