mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
41 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
258e523219 | ||
|
16848d83a5 | ||
|
ca338b8833 | ||
|
ca36473904 | ||
|
717745fe79 | ||
|
688de3368e | ||
|
7412706277 | ||
|
78144dbdb8 | ||
|
d8027db54d | ||
|
6bff74e856 | ||
|
037f5e3325 | ||
|
6dec4a8f56 | ||
|
044141fefa | ||
|
e0d55c8446 | ||
|
84e23cb82c | ||
|
a753f6e9a2 | ||
|
9b48d704d2 | ||
|
6c350d81d9 | ||
|
300f47456f | ||
|
cb95fea004 | ||
|
2e00c753d8 | ||
|
fce4a5e5df | ||
|
b25fee071f | ||
|
a6744d4ab3 | ||
|
c3c2d31d37 | ||
|
886bd43bce | ||
|
976f813de9 | ||
|
7911d62c53 | ||
|
b1c9e3ec2e | ||
|
48a4b43640 | ||
|
63e6d1daed | ||
|
685b7dc9c4 | ||
|
d2b26fa681 | ||
|
0a84bcd0d6 | ||
|
8e0b2d2ef8 | ||
|
f27b15f9a2 | ||
|
abecb5b718 | ||
|
fa85f83d70 | ||
|
14b9ee0ca2 | ||
|
3bec7b356d | ||
|
9cdbc27b66 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
||||
__*
|
||||
*.tmp
|
||||
*.swp
|
89
README.md
89
README.md
@@ -74,6 +74,38 @@ lume.weightedchoice({ ["cat"] = 10, ["dog"] = 5, ["frog"] = 0 })
|
||||
-- Returns either "cat" or "dog" with "cat" being twice as likely to be chosen.
|
||||
```
|
||||
|
||||
### lume.push(t, ...)
|
||||
Pushes all the given values to the end of the table `t` and returns the pushed
|
||||
values. Nil values are ignored.
|
||||
```lua
|
||||
local t = { 1, 2, 3 }
|
||||
lume.push(t, 4, 5) -- `t` becomes { 1, 2, 3, 4, 5 }
|
||||
```
|
||||
|
||||
### lume.remove(t, x)
|
||||
Removes the first instance of the value `x` if it exists in the table `t`.
|
||||
Returns `x`.
|
||||
```lua
|
||||
local t = { 1, 2, 3 }
|
||||
lume.remove(t, 2) -- `t` becomes { 1, 3 }
|
||||
```
|
||||
|
||||
### lume.clear(t)
|
||||
Nils all the values in the table `t`, this renders the table empty. Returns
|
||||
`t`.
|
||||
```lua
|
||||
local t = { 1, 2, 3 }
|
||||
lume.clear(t) -- `t` becomes {}
|
||||
```
|
||||
|
||||
### lume.extend(t, ...)
|
||||
Copies all the fields from the source tables to the table `t` and returns `t`.
|
||||
If a key exists in multiple tables the right-most table's value is used.
|
||||
```lua
|
||||
local t = { a = 1, b = 2 }
|
||||
lume.extend(t, { b = 4, c = 6 }) -- `t` becomes { a = 1, b = 4, c = 6 }
|
||||
```
|
||||
|
||||
### lume.shuffle(t)
|
||||
Returns a shuffled copy of the array `t`.
|
||||
|
||||
@@ -149,6 +181,14 @@ an array and retains its original keys.
|
||||
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
|
||||
```
|
||||
|
||||
### lume.reject(t, fn [, retainkeys])
|
||||
The opposite of `lume.filter()`: Calls `fn` on each value of `t` table; returns
|
||||
a new table with only the values where `fn` returned false. If `retainkeys` is
|
||||
true the table is not treated as an array and retains its original keys.
|
||||
```lua
|
||||
lume.reject({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {1, 3}
|
||||
```
|
||||
|
||||
### lume.merge(...)
|
||||
Returns a new table with all the given tables merged together. If a key exists
|
||||
in multiple tables the right-most table's value is used.
|
||||
@@ -212,6 +252,12 @@ values the keys.
|
||||
lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"}
|
||||
```
|
||||
|
||||
### lume.pick(t, ...)
|
||||
Returns a copy of the table filtered to only contain values for the given keys.
|
||||
```lua
|
||||
lume.pick({ a = 1, b = 2, c = 3 }, "a", "c") -- Returns { a = 1, c = 3 }
|
||||
```
|
||||
|
||||
### lume.keys(t)
|
||||
Returns an array containing each key of the table.
|
||||
|
||||
@@ -337,7 +383,7 @@ Prints the current filename and line number followed by each argument separated
|
||||
by a space.
|
||||
```lua
|
||||
-- Assuming the file is called "example.lua" and the next line is 12:
|
||||
lume.trace("hello", 1234) -- Prints "[example.lua:12] hello 1234"
|
||||
lume.trace("hello", 1234) -- Prints "example.lua:12: hello 1234"
|
||||
```
|
||||
|
||||
### lume.dostring(str)
|
||||
@@ -372,6 +418,17 @@ for i, v in lume.ripairs({ "a", "b", "c" }) do
|
||||
end
|
||||
```
|
||||
|
||||
### lume.color(str [, mul])
|
||||
Takes color string `str` and returns 4 values, one for each color channel (`r`,
|
||||
`g`, `b` and `a`). By default the returned values are between 0 and 1; the
|
||||
values are multiplied by the number `mul` if it is provided.
|
||||
```lua
|
||||
lume.color("#ff0000") -- Returns 1, 0, 0, 1
|
||||
lume.color("rgba(255, 0, 255, .5)") -- Returns 1, 0, 1, .5
|
||||
lume.color("#00ffff", 256) -- Returns 0, 256, 256, 256
|
||||
lume.color("rgb(255, 0, 0)", 256) -- Returns 256, 0, 0, 256
|
||||
```
|
||||
|
||||
### lume.rgba(color)
|
||||
Takes the 32bit integer `color` argument and returns 4 numbers, one for each
|
||||
channel, with a range of 0 - 255. The returned values can be used as the
|
||||
@@ -396,6 +453,36 @@ as calling `lume.chain()`.
|
||||
lume({1, 2, 3}):each(print) -- Prints 1, 2 then 3 on separate lines
|
||||
```
|
||||
|
||||
## Iteratee functions
|
||||
Several lume functions allow a `table`, `string` or `nil` to be used in place
|
||||
of their iteratee function argument. The functions that provide this behaviour
|
||||
are: `map()`, `all()`, `any()`, `filter()`, `reject()`, `match()` and
|
||||
`count()`.
|
||||
|
||||
If the argument is `nil` then each value will return itself.
|
||||
```lua
|
||||
lume.filter({ true, true, false, true }, nil) -- { true, true, true }
|
||||
```
|
||||
|
||||
If the argument is a `string` then each value will be assumed to be a table,
|
||||
and will return the value of the key which matches the string.
|
||||
``` lua
|
||||
local t = {{ z = "cat" }, { z = "dog" }, { z = "owl" }}
|
||||
lume.map(t, "z") -- Returns { "cat", "dog", "owl" }
|
||||
```
|
||||
|
||||
If the argument is a `table` then each value will return `true` or `false`,
|
||||
depending on whether the values at each of the table's keys match the
|
||||
collection's value's values.
|
||||
```lua
|
||||
local t = {
|
||||
{ age = 10, type = "cat" },
|
||||
{ age = 8, type = "dog" },
|
||||
{ age = 10, type = "owl" },
|
||||
}
|
||||
lume.count(t, { age = 10 }) -- returns 2
|
||||
```
|
||||
|
||||
|
||||
## License
|
||||
|
||||
|
225
lume.lua
225
lume.lua
@@ -1,13 +1,13 @@
|
||||
--
|
||||
-- lume
|
||||
--
|
||||
-- Copyright (c) 2015, rxi
|
||||
-- Copyright (c) 2015 rxi
|
||||
--
|
||||
-- This library is free software; you can redistribute it and/or modify it
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local lume = { _version = "2.0.0" }
|
||||
local lume = { _version = "2.2.1" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -43,21 +43,16 @@ local iscallable = function(x)
|
||||
end
|
||||
|
||||
local isarray = function(x)
|
||||
return (x and x[1]) and true or false
|
||||
end
|
||||
|
||||
local iternil = function()
|
||||
return noop
|
||||
return (type(x) == "table" and x[1] ~= nil) and true or false
|
||||
end
|
||||
|
||||
local getiter = function(x)
|
||||
if x == nil then
|
||||
return iternil
|
||||
elseif isarray(x) then
|
||||
if isarray(x) then
|
||||
return ipairs
|
||||
else
|
||||
elseif type(x) == "table" then
|
||||
return pairs
|
||||
end
|
||||
error("expected table", 3)
|
||||
end
|
||||
|
||||
local iteratee = function(x)
|
||||
@@ -83,7 +78,7 @@ end
|
||||
|
||||
function lume.round(x, increment)
|
||||
if increment then return lume.round(x / increment) * increment end
|
||||
return x > 0 and math_floor(x + .5) or math_ceil(x - .5)
|
||||
return x >= 0 and math_floor(x + .5) or math_ceil(x - .5)
|
||||
end
|
||||
|
||||
|
||||
@@ -149,6 +144,54 @@ function lume.weightedchoice(t)
|
||||
end
|
||||
|
||||
|
||||
function lume.push(t, ...)
|
||||
local n = select("#", ...)
|
||||
for i = 1, n do
|
||||
t[#t + 1] = select(i, ...)
|
||||
end
|
||||
return ...
|
||||
end
|
||||
|
||||
|
||||
function lume.remove(t, x)
|
||||
local iter = getiter(t)
|
||||
for i, v in iter(t) do
|
||||
if v == x then
|
||||
if isarray(t) then
|
||||
table.remove(t, i)
|
||||
break
|
||||
else
|
||||
t[i] = nil
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return x
|
||||
end
|
||||
|
||||
|
||||
function lume.clear(t)
|
||||
local iter = getiter(t)
|
||||
for k, v in iter(t) do
|
||||
t[k] = nil
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
function lume.extend(t, ...)
|
||||
for i = 1, select("#", ...) do
|
||||
local x = select(i, ...)
|
||||
if x then
|
||||
for k, v in pairs(x) do
|
||||
t[k] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
function lume.shuffle(t)
|
||||
local rtn = {}
|
||||
for i = 1, #t do
|
||||
@@ -225,9 +268,18 @@ end
|
||||
|
||||
|
||||
function lume.reduce(t, fn, first)
|
||||
local acc = first or t[1]
|
||||
assert(acc, "reduce of an empty array with no first value")
|
||||
for i = first and 1 or 2, #t do acc = fn(acc, t[i]) end
|
||||
local acc = first
|
||||
local started = first and true or false
|
||||
local iter = getiter(t)
|
||||
for k, v in iter(t) do
|
||||
if started then
|
||||
acc = fn(acc, v)
|
||||
else
|
||||
acc = v
|
||||
started = true
|
||||
end
|
||||
end
|
||||
assert(started, "reduce of an empty table with no first value")
|
||||
return acc
|
||||
end
|
||||
|
||||
@@ -245,8 +297,31 @@ function lume.filter(t, fn, retainkeys)
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
local rtn = {}
|
||||
for k, v in iter(t) do
|
||||
if fn(v) then rtn[retainkeys and k or (#rtn + 1)] = v end
|
||||
if retainkeys then
|
||||
for k, v in iter(t) do
|
||||
if fn(v) then rtn[k] = v end
|
||||
end
|
||||
else
|
||||
for k, v in iter(t) do
|
||||
if fn(v) then rtn[#rtn + 1] = v end
|
||||
end
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.reject(t, fn, retainkeys)
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
local rtn = {}
|
||||
if retainkeys then
|
||||
for k, v in iter(t) do
|
||||
if not fn(v) then rtn[k] = v end
|
||||
end
|
||||
else
|
||||
for k, v in iter(t) do
|
||||
if not fn(v) then rtn[#rtn + 1] = v end
|
||||
end
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
@@ -269,9 +344,11 @@ function lume.concat(...)
|
||||
local rtn = {}
|
||||
for i = 1, select("#", ...) do
|
||||
local t = select(i, ...)
|
||||
local iter = getiter(t)
|
||||
for k, v in iter(t) do
|
||||
rtn[#rtn + 1] = v
|
||||
if t ~= nil then
|
||||
local iter = getiter(t)
|
||||
for k, v in iter(t) do
|
||||
rtn[#rtn + 1] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
return rtn
|
||||
@@ -345,6 +422,16 @@ function lume.invert(t)
|
||||
end
|
||||
|
||||
|
||||
function lume.pick(t, ...)
|
||||
local rtn = {}
|
||||
for i = 1, select("#", ...) do
|
||||
local k = select(i, ...)
|
||||
rtn[k] = t[k]
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.keys(t)
|
||||
local rtn = {}
|
||||
local iter = getiter(t)
|
||||
@@ -400,8 +487,16 @@ end
|
||||
|
||||
|
||||
function lume.combine(...)
|
||||
local n = select('#', ...)
|
||||
if n == 0 then return noop end
|
||||
if n == 1 then
|
||||
local fn = select(1, ...)
|
||||
if not fn then return noop end
|
||||
assert(iscallable(fn), "expected a function or nil")
|
||||
return fn
|
||||
end
|
||||
local funcs = {}
|
||||
for i = 1, select("#", ...) do
|
||||
for i = 1, n do
|
||||
local fn = select(i, ...)
|
||||
if fn ~= nil then
|
||||
assert(iscallable(fn), "expected a function or nil")
|
||||
@@ -441,19 +536,36 @@ function lume.lambda(str)
|
||||
end
|
||||
|
||||
|
||||
function lume.serialize(x)
|
||||
local f = { string = function(v) return string.format("%q", v) end,
|
||||
number = tostring, boolean = tostring }
|
||||
f.table = function(t)
|
||||
local serialize
|
||||
|
||||
local serialize_map = {
|
||||
[ "number" ] = tostring,
|
||||
[ "boolean" ] = tostring,
|
||||
[ "nil" ] = tostring,
|
||||
[ "string" ] = function(v) return string.format("%q", v) end,
|
||||
[ "table" ] = function(t, stk)
|
||||
stk = stk or {}
|
||||
if stk[t] then error("circular reference") end
|
||||
local rtn = {}
|
||||
stk[t] = true
|
||||
for k, v in pairs(t) do
|
||||
rtn[#rtn + 1] = "[" .. f[type(k)](k) .. "]=" .. f[type(v)](v) .. ","
|
||||
rtn[#rtn + 1] = "[" .. serialize(k, stk) .. "]=" .. serialize(v, stk)
|
||||
end
|
||||
return "{" .. table.concat(rtn) .. "}"
|
||||
stk[t] = nil
|
||||
return "{" .. table.concat(rtn, ",") .. "}"
|
||||
end
|
||||
local err = function(t,k) error("unsupported serialize type: " .. k) end
|
||||
setmetatable(f, { __index = err })
|
||||
return f[type(x)](x)
|
||||
}
|
||||
|
||||
setmetatable(serialize_map, {
|
||||
__index = function(t, k) error("unsupported serialize type: " .. k) end
|
||||
})
|
||||
|
||||
serialize = function(x, stk)
|
||||
return serialize_map[type(x)](x, stk)
|
||||
end
|
||||
|
||||
function lume.serialize(x)
|
||||
return serialize(x)
|
||||
end
|
||||
|
||||
|
||||
@@ -489,20 +601,26 @@ function lume.wordwrap(str, limit)
|
||||
check = limit
|
||||
end
|
||||
local rtn = {}
|
||||
for j, line in ipairs(lume.split(str, "\n")) do
|
||||
local str
|
||||
for i, word in ipairs(lume.split(line)) do
|
||||
local s = (str and (str .. " ") or "") .. word
|
||||
if check(s) then
|
||||
rtn[#rtn + 1] = str
|
||||
str = word
|
||||
local line = ""
|
||||
for word, spaces in str:gmatch("(%S+)(%s*)") do
|
||||
local str = line .. word
|
||||
if check(str) then
|
||||
table.insert(rtn, line .. "\n")
|
||||
line = word
|
||||
else
|
||||
line = str
|
||||
end
|
||||
for c in spaces:gmatch(".") do
|
||||
if c == "\n" then
|
||||
table.insert(rtn, line .. "\n")
|
||||
line = ""
|
||||
else
|
||||
str = s
|
||||
line = line .. c
|
||||
end
|
||||
end
|
||||
rtn[#rtn + 1] = str
|
||||
end
|
||||
return table.concat(rtn, "\n")
|
||||
table.insert(rtn, line)
|
||||
return table.concat(rtn)
|
||||
end
|
||||
|
||||
|
||||
@@ -517,7 +635,7 @@ end
|
||||
|
||||
function lume.trace(...)
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
|
||||
local t = { info.short_src .. ":" .. info.currentline .. ":" }
|
||||
for i = 1, select("#", ...) do
|
||||
local x = select(i, ...)
|
||||
if type(x) == "number" then
|
||||
@@ -587,13 +705,32 @@ local ripairs_iter = function(t, i)
|
||||
end
|
||||
|
||||
function lume.ripairs(t)
|
||||
if t == nil then
|
||||
return noop
|
||||
end
|
||||
return ripairs_iter, t, (#t + 1)
|
||||
end
|
||||
|
||||
|
||||
function lume.color(str, mul)
|
||||
mul = mul or 1
|
||||
local r, g, b, a
|
||||
r, g, b = str:match("#(%x%x)(%x%x)(%x%x)")
|
||||
if r then
|
||||
r = tonumber(r, 16) / 0xff
|
||||
g = tonumber(g, 16) / 0xff
|
||||
b = tonumber(b, 16) / 0xff
|
||||
a = 1
|
||||
elseif str:match("rgba?%s*%([%d%s%.,]+%)") then
|
||||
local f = str:gmatch("[%d.]+")
|
||||
r = (f() or 0) / 0xff
|
||||
g = (f() or 0) / 0xff
|
||||
b = (f() or 0) / 0xff
|
||||
a = f() or 1
|
||||
else
|
||||
error(("bad color string '%s'"):format(str))
|
||||
end
|
||||
return r * mul, g * mul, b * mul, a * mul
|
||||
end
|
||||
|
||||
|
||||
function lume.rgba(color)
|
||||
local a = math_floor((color / 16777216) % 256)
|
||||
local r = math_floor((color / 65536) % 256)
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local tester = require "tester"
|
||||
local tester = require "util.tester"
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
|
||||
@@ -106,6 +106,55 @@ tests["lume.weightedchoice"] = function()
|
||||
tester.test.error( lume.weightedchoice, { a = 1, b = -1 } )
|
||||
end
|
||||
|
||||
-- lume.push
|
||||
tests["lume.push"] = function()
|
||||
local t = { 1, 2 }
|
||||
lume.push(t, 3, 4)
|
||||
testeq(t, { 1, 2, 3, 4 })
|
||||
lume.push(t, 5, nil, 6, nil, 7)
|
||||
testeq(t, { 1, 2, 3, 4, 5, 6, 7 })
|
||||
lume.push(t)
|
||||
testeq(t, { 1, 2, 3, 4, 5, 6, 7 })
|
||||
local x, y = lume.push(t, 123, 456)
|
||||
testeq(x, 123)
|
||||
testeq(y, 456)
|
||||
end
|
||||
|
||||
-- lume.remove
|
||||
tests["lume.remove"] = function()
|
||||
local t = { 1, 2, 3, 4, 5 }
|
||||
lume.remove(t, 3)
|
||||
testeq(t, { 1, 2, 4, 5 })
|
||||
lume.remove(t, 1)
|
||||
testeq(t, { 2, 4, 5 })
|
||||
lume.remove(t, 5)
|
||||
testeq(t, { 2, 4 })
|
||||
local m = { a = 1, b = 2, c = 3 }
|
||||
local x = lume.remove(t, 123)
|
||||
testeq(x, 123)
|
||||
end
|
||||
|
||||
-- lume.clear
|
||||
tests["lume.clear"] = function()
|
||||
local t = { 1, 2, 3 }
|
||||
lume.clear(t)
|
||||
testeq(t, {})
|
||||
local m = { a = 1, b = 2, c = 3 }
|
||||
lume.clear(m)
|
||||
testeq(m, {})
|
||||
testeq( lume.clear(t) == t, true )
|
||||
end
|
||||
|
||||
-- lume.extend
|
||||
tests["lume.extend"] = function()
|
||||
local t = { a = 10, b = 20, c = 30 }
|
||||
testeq( lume.extend(t) == t, true )
|
||||
lume.extend(t, { d = 40 }, { e = 50 })
|
||||
testeq( t, { a = 10, b = 20, c = 30, d = 40, e = 50 } )
|
||||
lume.extend(t, { a = "cat", b = "dog" }, { b = "owl", c = "fox" })
|
||||
testeq( t, { a = "cat", b = "owl", c = "fox", d = 40, e = 50 } )
|
||||
end
|
||||
|
||||
-- lume.shuffle
|
||||
tests["lume.shuffle"] = function()
|
||||
local t = {1, 2, 3, 4, 5}
|
||||
@@ -191,6 +240,9 @@ tests["lume.reduce"] = function()
|
||||
testeq( lume.reduce({1, 2, 3, 4}, add, 5), 15 )
|
||||
testeq( lume.reduce({1}, add), 1 )
|
||||
testeq( lume.reduce({}, concat, "potato"), "potato" )
|
||||
testeq( lume.reduce({a=1, b=2}, add, 5), 8 )
|
||||
testeq( lume.reduce({a=1, b=2}, add), 3 )
|
||||
tester.test.error(lume.reduce, {}, add)
|
||||
end
|
||||
|
||||
-- lume.set
|
||||
@@ -214,6 +266,16 @@ tests["lume.filter"] = function()
|
||||
testeq( t, {{ x=1, y=1 }, {x=1, y=3}} )
|
||||
end
|
||||
|
||||
-- lume.reject
|
||||
tests["lume.reject"] = function()
|
||||
local t = lume.reject({1, 2, 3, 4, 5}, function(x) return x % 2 == 0 end )
|
||||
testeq( t, {1, 3, 5} )
|
||||
local t = lume.reject({a=1, b=2, c=3}, function(x) return x == 2 end, true)
|
||||
testeq( t, {a=1, c=3} )
|
||||
local t = lume.reject({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 })
|
||||
testeq( t, {{ x=2, y=2 }} )
|
||||
end
|
||||
|
||||
-- lume.merge
|
||||
tests["lume.merge"] = function()
|
||||
testeq( lume.merge(), {} )
|
||||
@@ -311,6 +373,15 @@ tests["lume.invert"] = function()
|
||||
testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} )
|
||||
end
|
||||
|
||||
-- lume.pick
|
||||
tests["lume.pick"] = function()
|
||||
local t = { cat = 10, dog = 20, fox = 30, owl = 40 }
|
||||
testeq( lume.pick(t, "cat", "dog"), { cat = 10, dog = 20 } )
|
||||
testeq( lume.pick(t, "fox", "owl"), { fox = 30, owl = 40 } )
|
||||
testeq( lume.pick(t, "owl"), { owl = 40 } )
|
||||
testeq( lume.pick(t), {} )
|
||||
end
|
||||
|
||||
-- lume.keys
|
||||
tests["lume.keys"] = function()
|
||||
testeq( lume.keys({}), {} )
|
||||
@@ -370,6 +441,12 @@ tests["lume.combine"] = function()
|
||||
fn = lume.combine(nil, a, nil, b, nil)
|
||||
fn(10, 20)
|
||||
testeq( acc, 230 )
|
||||
local x = false
|
||||
fn = lume.combine(function() x = true end)
|
||||
fn()
|
||||
testeq( x, true )
|
||||
testeq( type(lume.combine(nil)), "function" )
|
||||
testeq( type(lume.combine()), "function" )
|
||||
end
|
||||
|
||||
-- lume.call
|
||||
@@ -433,8 +510,8 @@ end
|
||||
|
||||
-- lume.wordwrap
|
||||
tests["lume.wordwrap"] = function()
|
||||
local str = "A small string with some words and then some more words"
|
||||
local b = "A small string with\nsome words and then\nsome more words"
|
||||
local str = "A small string with some words and then some more words"
|
||||
local b = "A small string with \nsome words and then \nsome more words"
|
||||
local fn = function(str) return #str >= 20 end
|
||||
testeq( lume.wordwrap(str), str )
|
||||
testeq( lume.wordwrap(str, 20), b )
|
||||
@@ -458,7 +535,7 @@ tests["lume.trace"] = function()
|
||||
local oldprint = print
|
||||
local file, line, msg
|
||||
print = function(x)
|
||||
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
||||
file, line, msg = x:match("(.-):(.-): (.*)")
|
||||
end
|
||||
lume.trace("Hi world", 123.456, 1, nil)
|
||||
print = oldprint
|
||||
@@ -494,8 +571,24 @@ tests["lume.ripairs"] = function()
|
||||
table.insert(r, { i, v })
|
||||
end
|
||||
testeq( r, { { 3, "c" }, { 2, "b" }, { 1, "a" } })
|
||||
for i, v in lume.ripairs(nil) do
|
||||
end
|
||||
tester.test.error(lume.ripairs, nil)
|
||||
end
|
||||
|
||||
-- lume.color
|
||||
tests["lume.color"] = function()
|
||||
testeq({ lume.color("#ff0000") }, { 1, 0, 0, 1 } )
|
||||
testeq({ lume.color("#00ff00") }, { 0, 1, 0, 1 } )
|
||||
testeq({ lume.color("#0000ff") }, { 0, 0, 1, 1 } )
|
||||
testeq({ lume.color("rgb( 255, 255, 255 )") }, { 1, 1, 1, 1 } )
|
||||
testeq({ lume.color("rgb (0, 0, 0)") }, { 0, 0, 0, 1 } )
|
||||
testeq({ lume.color("rgba(255, 255, 255, .5)") }, { 1, 1, 1, .5 } )
|
||||
testeq({ lume.color("#ffffff", 2) }, { 2, 2, 2, 2 } )
|
||||
testeq({ lume.color("rgba(255, 255, 255, 1)", 3) }, { 3, 3, 3, 3 } )
|
||||
tester.test.error(lume.color, "#ff00f")
|
||||
tester.test.error(lume.color, "#xyzxyz")
|
||||
tester.test.error(lume.color, "rgba(hello)")
|
||||
tester.test.error(lume.color, "rgba()")
|
||||
tester.test.error(lume.color, "rgba(1, 1, 1, 1")
|
||||
end
|
||||
|
||||
-- lume.rgba
|
Reference in New Issue
Block a user