▣
module · drop-in viewer
utils
Formatting and dispatch helpers shared across zui layers. Anything domain-agnostic and useful both inside zui and to library consumers goes here — numeric formatting (WASM-safe, no `string.format` dependency), stable hierarchical widget id construction, callback payload unwrap, a…
by·posted 2mo ago
What it does
utils
Formatting and dispatch helpers shared across zui layers. Anything
domain-agnostic and useful both inside zui and to library consumers
goes here — numeric formatting (WASM-safe, no string.format dependency),
stable hierarchical widget id construction, callback payload unwrap, and
small list comprehensions.
Exports
M.round(v: number?, decimals: number?) -> number— round todecimalsplaces (default 2).nil→ 0.M.fmt(v: number?, decimals: number?) -> string—tostring(round(v, decimals)).M.fmtVec3(vec: Vec3Array?, decimals: number?) -> string— format{x, y, z}as"(x, y, z)".M.id(parent: any, child: any) -> string— stable widget id ("parent/child"); empty parent drops the prefix.M.eventValue(data: EventEnvelope?) -> any— unwrapdata.valuefrom v2 event envelopes; pass through raw payloads (nilreturnsnil).M.map(items: { any }?, fn: (any, number) -> any?) -> { any }— map and dropnils.M.when(cond: any, widget: any) -> any—widgetifcond, elsenil. Pairs withcompact.M.compact(widgets: { any }?) -> { any }— dropnilentries.
Types:
Vec3Array = { number }— array form,{ x, y, z }.EventEnvelope = { value: any? } | any
Usage
local Utils = require("@builtin::modules.zui.utils")
Utils.round(1.2345) -- 1.23
Utils.fmt(1.2345, 1) -- "1.2"
Utils.fmtVec3({1, 2, 3}) -- "(1.00, 2.00, 3.00)"
Utils.id("toolbar", "save") -- "toolbar/save"
local rows = Utils.map(items, function(r) return Z.lbl(r.name) end)
local kids = Utils.compact{ Z.lbl("a"), Utils.when(showFoo, Z.lbl("foo")) }
Notes
- Pure module — no engine calls, no module state. Safe to call from any context.
round/fmt/fmtVec3are WASM-safe; they avoidstring.formatfor the rounding step and use repeated multiplication for10^n.eventValueis the canonical onCallback unwrap — preferred over manualdata.valueaccess because it transparently handles the older raw-payload flow too.map/compact/whenare the canonical list comprehensions; demos rely on thenilfiltering for conditional widgets.
Discussion
Scoped to this part · feeds back into the world's score.
Sign in to post.sign in
No comments yet. Be the first.