Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
Luca96Public

Latest commit

 

History

44 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

lua-table

Lua-Table 0.9.0

test

Lua tables with superpowers 💪

Supports Lua 5.1, 5.2, 5.3, 5.4 and LuaJIT 2.1.

Changes

View the changelog for more info about changes.

Installation

To install the library just clone the repo or install via LuaRocks:

luarocks install luatable

Running tests

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

Examples

-- 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()

Wrappers

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.map is the map method. String-keyed data is read with w:get(k [, default]), checked with w:haskey(k) and written with w:set(k, v). Assigning w.name = v raises an error. Reading an unknown string key returns nil and 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] and w[{...}] read and write the table.
  • No copy. table(t) wraps t itself, so in-place methods (append, push, shuffle, reverse, clear, w[i] = v, ...) change t. Use table(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() (or table.unwrap(x), which also accepts plain tables) to other libraries, JSON encoders and so on.

Portability

  • Lua 5.3 / 5.4: wrappers are tables. #w, pairs(w), ipairs(w) and the standard table.* functions all act on the data.
  • Lua 5.2: like 5.3, but table.insert(w, ...) and the other table.* functions bypass the wrapper. Use the methods (w:insert(x), w:concat(","), ...) or w:unwrap().
  • Lua 5.1 / LuaJIT: # ignores __len on tables, so wrappers are userdata (newproxy). #w works, while pairs(w), ipairs(w) and table.* on a wrapper raise an error. Use w:pairs(), w:ipairs(), w:len() or w:unwrap(). type(w) is "userdata"; use table.iswrapper(w).
  • Portable everywhere: w:len(), w:pairs(), w:ipairs() (also table.len(x), table.pairs(x), table.ipairs(x) for plain tables or wrappers).

Known issues & roadmap

This section tracks what is still open. Update it when an item is fixed or a new one is found.

Known issues

  • Lua 5.2: table.* on a wrapper. The 5.2 table library reads the length through __len but writes with raw access, so table.insert(w, x) stores x in the wrapper instead of the data. Use w:insert(x) or table.insert(w:unwrap(), x). Can't be fixed (no metamethod intercepts a raw write); tracked by a #knownbug test in spec/wrapper_spec.lua that runs on 5.2 only.
  • Lua 5.1 / LuaJIT without newproxy (for example some sandboxes): wrappers fall back to tables, and #w is then 0. Use w:len().

Roadmap

Releases

Packages

Contributors

Languages