Lua tables with superpowers 💪
Supports Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT 2.1.
View the changelog for more info about changes.
To install the library just clone the repo or install via LuaRocks:
luarocks install luatable
The test suite lives in spec/ and uses busted:
luarocks install busted
busted
or, through LuaRocks (installs busted first if it is missing):
luarocks test
-- load the library: it returns its own module and leaves Lua's global `table` alone
-- (the standard functions are still reachable: table.insert, table.concat, ...)
local table = require "luatable"
-- functions work on plain tables and return plain tables
local t = { 1, 2, 2, 3, -4, 5 }
t = table.accept(t, table.positive)
-- wrap a table to call the functions as methods (no copy: t itself stays plain)
local tb = table(t) -- same as table.new(t) or table.wrap(t)
-- table-returning methods return wrappers, so calls chain
local sum = tb:map(table.double):reduce(0, table.sum)
-- integer keys read and write the data; #tb works too
print(tb[1], #tb)
-- string keys are methods: read and write string-keyed data with get / set
local person = table { name = "Ada" }
print(person:get("name")) --> Ada
person:set("born", 1815)
print(person.info) --> nil, and a warning: `info` is not a method
print(table.ismethod("map")) --> true
-- get the plain table back
local plain = tb:unwrap() -- or table.unwrap(tb)
-- table.of always packs its arguments: a single table becomes the only element
local nested = table.of(t) --> wrapper of { t }
-- a table init value is deep-copied into each slot
local grid = table.init(3, { 0, 0 })
-- print every element, or the entire table (with key-value pairs)
tb:eachi(print)
print(tb)
-- clear and add values to table
tb:clear()
tb:append(4, 5, 6)
tb:push(1, 2, 3)
-- table equality
local t1 = { 1, 2, 3, x = { 4, 5 } }
local t2 = { 1, 2, 3, 4, 5 }
print(table.equal(t1, t2)) --> false
print(table.equal(t1, t2, true)) --> true
-- deep equality (and deepflat) visit hash keys in a fixed order, not pairs order:
-- numbers, strings, booleans, then tables (compared by content), then functions,
-- userdata and threads (by identity, so these keys only line up when both tables
-- use the same key objects)
-- operator overloads (on wrappers; a plain table is accepted on the other side)
local w1 = table { 1, 2, 3 }
local w2 = table { 2, 4, 5 }
print(w1 + w2) -- table.union
print(w1 - w2) -- table.negation
print(w1 * w2) -- table.intersect
print(w1 == w2) -- table.equal
print(w1 .. w2) -- table.merge
-- opt-in: add the functions to Lua's global table, as versions before 0.9 did
-- (names the global table already has are skipped, with a warning each)
require("luatable").install()A wrapper holds a plain table and gives it the library's methods and operators. The
table itself never gets a metatable, so a missing key in your data is always nil.
- String keys are methods only.
w.mapis themapmethod. String-keyed data is read withw:get(k [, default]), checked withw:haskey(k)and written withw:set(k, v). Assigningw.name = vraises an error. Reading an unknown string key returnsniland prints a warning (once per call site and key).table.strict(true)makes that an error at the caller's line.table.ismethod(k)tells which names are methods. - Every other key is data.
w[1],w[1] = v,w[true]andw[{...}]read and write the table. - No copy.
table(t)wrapstitself, so in-place methods (append,push,shuffle,reverse,clear,w[i] = v, ...) changet. Usetable(t):clone()for an independent copy. - Results follow the arguments. A function returns a wrapper when one of its table
arguments is a wrapper, and a plain table otherwise. Single values (
max,find,get, ...) and elements are never wrapped. Sets from a wrapper (w:valueSet()) are wrappers too: test membership with:haskey(k). - At boundaries, unwrap. Pass
w:unwrap()(ortable.unwrap(x), which also accepts plain tables) to other libraries, JSON encoders and so on.
- Lua 5.3 / 5.4: wrappers are tables.
#w,pairs(w),ipairs(w)and the standardtable.*functions all act on the data. - Lua 5.2: like 5.3, but
table.insert(w, ...)and the othertable.*functions bypass the wrapper. Use the methods (w:insert(x),w:concat(","), ...) orw:unwrap(). - Lua 5.1 / LuaJIT:
#ignores__lenon tables, so wrappers are userdata (newproxy).#wworks, whilepairs(w),ipairs(w)andtable.*on a wrapper raise an error. Usew:pairs(),w:ipairs(),w:len()orw:unwrap().type(w)is"userdata"; usetable.iswrapper(w). - Portable everywhere:
w:len(),w:pairs(),w:ipairs()(alsotable.len(x),table.pairs(x),table.ipairs(x)for plain tables or wrappers).
This section tracks what is still open. Update it when an item is fixed or a new one is found.
- Lua 5.2:
table.*on a wrapper. The 5.2 table library reads the length through__lenbut writes with raw access, sotable.insert(w, x)storesxin the wrapper instead of the data. Usew:insert(x)ortable.insert(w:unwrap(), x). Can't be fixed (no metamethod intercepts a raw write); tracked by a#knownbugtest inspec/wrapper_spec.luathat runs on 5.2 only. - Lua 5.1 / LuaJIT without
newproxy(for example some sandboxes): wrappers fall back to tables, and#wis then0. Usew:len().
