Skip to content

Latest commit

 

History

History
96 lines (62 loc) · 2.06 KB

File metadata and controls

96 lines (62 loc) · 2.06 KB
description Small shared utility helpers used by modules in this library.

utils

Small shared utility helpers used by modules in this library.

Usage

utils = require "mods.utils"

print(utils.quote('hello "world"')) --> 'hello "world"'

Functions

quote(v)

Smart-quote a string for readable Lua-like output.

Parameters:

  • v (string): String to quote.

Return:

  • out (string): Quoted string.

Example:

print(utils.quote('He said "hi"')) -- 'He said "hi"'
print(utils.quote('say "hi" and \\'bye\\'')) -- "say \"hi\" and 'bye'"

keypath(...)

Format a key chain as a Lua-like table access path.

Parameters:

  • ... (any): Additional arguments.

Return:

  • path (string): Rendered key path.

Example:

p1 = utils.keypath("t", "a", "b", "c")        --> "t.a.b.c"
p2 = utils.keypath("ctx", "users", 1, "name") --> "ctx.users[1].name"
p3 = utils.keypath("ctx", "invalid-key")      --> 'ctx["invalid-key"]'
p4 = utils.keypath()                          --> ""

assert_arg(argn, v, tp?, level?, msg?)

Assert argument value using mods.validate and raise a Lua error on failure.

Parameters:

  • argn (integer): Argument index for error context.
  • v (T): Value to check.
  • tp? (modsIsType): Validator name (defaults to "truthy").
  • level? (integer): Optional error level for error(...) (defaults to 2).
  • msg? (string): Optional override template passed to mods.validate.

Return:

  • v (T): Same input value on success.

Example:

utils.assert_arg(1, "ok", "string") --> "ok"
utils.assert_arg(2, 123, "string")
--> raises: bad argument #2 (expected string, got number)
utils.assert_arg(3, "x", "number", 2, "need {{expected}}, got {{got}}")
--> raises: bad argument #3 (need number, got string)

Note

When the caller function name is available, error text includes to '<function>' (Lua-style bad argument context).