45 Commits

Author SHA1 Message Date
rxi
a6744d4ab3 Version 2.1.0 2015-02-28 21:03:42 +00:00
rxi
c3c2d31d37 Changed lume.reduce() to support non-array tables, updated tests 2015-02-24 21:02:04 +00:00
rxi
886bd43bce Changed getiter() to disallow nil values 2015-02-24 20:42:44 +00:00
rxi
976f813de9 Fixed example mistake in reject()'s README section 2015-02-20 19:52:10 +00:00
rxi
7911d62c53 Updated Iteratee functions part of README for lume.reject() 2015-02-20 19:27:34 +00:00
rxi
b1c9e3ec2e Changed lume.filter()'s implementation to mimic lume.reject() 2015-02-20 19:24:48 +00:00
rxi
48a4b43640 Added lume.reject(), updated tests and README.md 2015-02-20 19:22:42 +00:00
rxi
63e6d1daed Small fix to iteratee functions README section 2015-02-14 16:09:43 +00:00
rxi
685b7dc9c4 Added iteratee functions section to README 2015-02-14 16:02:40 +00:00
rxi
d2b26fa681 Changed lume.clear() to return the table, updated doc & tests 2015-02-08 15:13:05 +00:00
rxi
0a84bcd0d6 Fixed internal func isarray() 2015-02-08 15:01:17 +00:00
rxi
8e0b2d2ef8 Updated lume.push()'s README section 2015-02-07 23:04:23 +00:00
rxi
f27b15f9a2 Added tests for lume.remove()/lume.push() return values 2015-02-07 22:59:38 +00:00
rxi
abecb5b718 Updated README.md with new functions 2015-02-07 22:57:49 +00:00
rxi
fa85f83d70 Added lume.clear() and tests 2015-02-07 22:51:28 +00:00
rxi
14b9ee0ca2 Added lume.remove() and tests 2015-02-07 22:48:58 +00:00
rxi
3bec7b356d Added lume.push(), updated tests 2015-02-07 22:38:08 +00:00
rxi
9cdbc27b66 Minor optimisations to lume.combine(), updated tests 2015-02-05 21:32:21 +00:00
rxi
c6ab30cf7d Version 2.0.0 2015-01-24 15:21:35 +00:00
rxi
c07194490b Updated license copyright year 2015-01-24 15:20:58 +00:00
rxi
5305cb34c8 Lua 5.3 compatibility fixes 2015-01-12 19:51:24 +00:00
rxi
f7b3eb64bb Fixed typo in lume.sort()'s README section 2015-01-11 15:27:06 +00:00
rxi
edd7c99382 Added lume.ripairs() function, updated README and tests 2015-01-11 14:58:57 +00:00
rxi
c773acdcf4 Removed retainkeys argument from lume.set() 2015-01-11 14:33:56 +00:00
rxi
756d30416d Added lume.wordwrap(); updated README.md and tests 2015-01-10 21:53:52 +00:00
rxi
9b5abf3d58 Changed lume.merge() functionality, updated README and doc
- The old functionality of lume.merge() is now spread across
  lume.merge() (for tables) and lume.concat() (for arrays). Both these
  functions create new tables rather than operating in-place
- Changed lume.fn() to use lume.concat() internally
2015-01-10 17:31:35 +00:00
rxi
397dce4c5e Added lume.concat(), updated README and tests 2015-01-10 17:13:15 +00:00
rxi
ce86f4b0ea Fixed nil iterator in internal getiter() 2015-01-10 17:08:14 +00:00
rxi
c565f0739b Fixed lume.call() section of README.md 2015-01-10 16:56:32 +00:00
rxi
7a0f5a0831 Added lume.sort(), updated tests & README 2015-01-10 16:54:23 +00:00
rxi
b873c043c5 Added lume.call(), updated tests and README 2015-01-10 16:29:13 +00:00
rxi
4373a202dc Changed lume.shuffle() to not operate in-place, updated doc + tests 2015-01-10 16:24:31 +00:00
rxi
ac10d54b47 Changed internal func isarray() to handle nil 2015-01-10 14:32:58 +00:00
rxi
067e58b30f Changed lume.count() to return length if table is array 2015-01-10 14:31:55 +00:00
rxi
a42cbb12f0 Changed all applicable functions to use iter func based on table 2015-01-10 14:23:25 +00:00
rxi
0c21cfcf5b Updated lume.each() to use iter func based on table type 2015-01-10 14:13:07 +00:00
rxi
0e4256ef0d Version 1.5.0 2014-12-21 12:25:38 +00:00
rxi
bcad07fc5f Added explanation of calling lume() to the README 2014-12-13 01:00:47 +00:00
rxi
8f1267f967 Added lume.first() and .last(), updated README.md and tests 2014-12-12 21:33:41 +00:00
rxi
de37bd6d65 Added lume.keys() and tests, updated docs 2014-12-12 20:40:25 +00:00
rxi
ac920c8f5e Fixed lume.invert test name 2014-12-12 20:28:48 +00:00
rxi
5d258d4fd0 Made calling lume table an alias to calling lume.chain()
Updated tests
2014-12-12 20:25:54 +00:00
rxi
df26e7939d Added tests for funcitons which now use iteratee() 2014-12-12 20:22:29 +00:00
rxi
16e370cdf0 Changed all applicable functions to use iteratee() internally 2014-12-12 19:45:22 +00:00
rxi
0b991d7ea5 Added identity() and iteratee() local funcs 2014-12-12 18:56:13 +00:00
4 changed files with 569 additions and 48 deletions

View File

@@ -1,4 +1,4 @@
Copyright (c) 2014, rxi
Copyright (c) 2015, rxi
Permission is hereby granted, free of charge, to any person obtaining a copy of

144
README.md
View File

@@ -74,8 +74,42 @@ 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.shuffle(t)
Shuffles the values of array `t` in place, returns the array.
Returns a shuffled copy of the array `t`.
### lume.sort(t [, comp])
Returns a copy of the array `t` with all its items sorted. If `comp` is a
function it will be used to compare the items when sorting. If `comp` is a
string it will be used as the key to sort the items by.
```lua
lume.sort({ 1, 4, 3, 2, 5 }) -- Returns { 1, 2, 3, 4, 5 }
lume.sort({ {z=2}, {z=3}, {z=1} }, "z") -- Returns { {z=1}, {z=2}, {z=3} }
lume.sort({ 1, 3, 2 }, function(a, b) return a > b end) -- Returns { 3, 2, 1 }
```
### lume.array(...)
Iterates the supplied iterator and returns an array filled with the values.
@@ -125,10 +159,8 @@ an error is raised,
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
```
### lume.set(t [, retainkeys])
Returns a copy of the `t` table with all the duplicate values removed. If
`retainkeys` is true the table is not treated as an array and retains its
original keys.
### lume.set(t)
Returns a copy of the `t` array with all the duplicate values removed.
```lua
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"}
```
@@ -141,12 +173,25 @@ an array and retains its original keys.
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
```
### lume.merge(t, t2 [, retainkeys])
Merges all the values from the table `t2` into `t` in place. If `retainkeys` is
true the table is not treated as an array and retains its original keys; if `t`
and `t2` have a conflicting key, the value from `t2` is used.
### 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.merge({2, 3}, {4, 5}) -- Returns {2, 3, 4, 5}
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.
```lua
lume.merge({a=1, b=2, c=3}, {c=8, d=9}) -- Returns {a=1, b=2, c=8, d=9}
```
### lume.concat(...)
Returns a new array consisting of all the given arrays concatenated into one.
```lua
lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6}
```
### lume.find(t, value)
@@ -178,6 +223,20 @@ than a string. Creates and returns a new array of the given slice.
lume.slice({"a", "b", "c", "d", "e"}, 2, 4) -- Returns {"b", "c", "d"}
```
### lume.first(t [, n])
Returns the first element of an array or nil if the array is empty. If `n` is
specificed an array of the first `n` elements is returned.
```lua
lume.first({"a", "b", "c"}) -- Returns "a"
```
### lume.last(t [, n])
Returns the last element of an array or nil if the array is empty. If `n` is
specificed an array of the last `n` elements is returned.
```lua
lume.last({"a", "b", "c"}) -- Returns "c"
```
### lume.invert(t)
Returns a copy of the table where the keys have become the values and the
values the keys.
@@ -185,6 +244,9 @@ values the keys.
lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"}
```
### lume.keys(t)
Returns an array containing each key of the table.
### lume.clone(t)
Returns a shallow copy of the table `t`.
@@ -227,6 +289,13 @@ local f = lume.combine(function(a, b) print(a + b) end,
f(3, 4) -- Prints "7" then "12" on a new line
```
### lume.call(fn, ...)
Calls the given function with the provided arguments and returns its values. If
`fn` is `nil` then no action is performed and the function returns `nil`.
```lua
lume.call(print, "Hello world") -- Prints "Hello world"
```
### lume.time(fn, ...)
Inserts the arguments into function `fn` and calls it. Returns the time in
seconds the function `fn` took to execute followed by `fn`'s returned values.
@@ -277,6 +346,15 @@ instead of whitespace.
lume.trim(" Hello ") -- Returns "Hello"
```
### lume.wordwrap(str [, limit])
Returns `str` wrapped to `limit` number of characters per line, by default
`limit` is `72`. `limit` can also be a function which when passed a string,
returns `true` if it is too long for a single line.
```lua
-- Returns "Hello world\nThis is a\nshort string"
lume.wordwrap("Hello world. This is a short string", 14)
```
### lume.format(str [, vars])
Returns a formatted string. The values of keys in the table `vars` can be
inserted into the string by using the form `"{key}"` in `str`; numerical keys
@@ -315,6 +393,17 @@ lume.hotswap("lume") -- Reloads the lume module
assert(lume.hotswap("inexistant_module")) -- Raises an error
```
### lume.ripairs(t)
Performs the same function as `ipairs()` but iterates in reverse; this allows
the removal of items from the table during iteration without any items being
skipped.
```lua
-- Prints "3->c", "2->b" and "1->a" on separate lines
for i, v in lume.ripairs({ "a", "b", "c" }) do
print(i .. "->" .. v)
end
```
### 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
@@ -333,6 +422,41 @@ lume.chain({1, 2, 3, 4})
:map(function(x) return -x end)
:result() -- Returns { -2, -4 }
```
The table returned by the `lume` module, when called, acts in the same manner
as calling `lume.chain()`.
```lua
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

308
lume.lua
View File

@@ -1,13 +1,13 @@
--
-- lume
--
-- Copyright (c) 2014, 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 = "1.4.1" }
local lume = { _version = "2.1.0" }
local pairs, ipairs = pairs, ipairs
local type, assert, unpack = type, assert, unpack or table.unpack
@@ -16,11 +16,18 @@ local math_floor = math.floor
local math_ceil = math.ceil
local math_random = math.random
local math_cos = math.cos
local math_atan2 = math.atan2
local math_atan2 = math.atan2 or math.atan
local math_sqrt = math.sqrt
local math_abs = math.abs
local math_pi = math.pi
local noop = function()
end
local identity = function(x)
return x
end
local patternescape = function(str)
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
end
@@ -35,6 +42,37 @@ local iscallable = function(x)
return mt and mt.__call ~= nil
end
local isarray = function(x)
return (type(x) == "table" and x[1] ~= nil) and true or false
end
local iternil = function()
return noop
end
local getiter = function(x)
if isarray(x) then
return ipairs
elseif type(x) == "table" then
return pairs
end
error("expected table", 3)
end
local iteratee = function(x)
if x == nil then return identity end
if iscallable(x) then return x end
if type(x) == "table" then
return function(z)
for k, v in pairs(x) do
if z[k] ~= v then return false end
end
return true
end
end
return function(z) return z[x] end
end
function lume.clamp(x, min, max)
@@ -110,15 +148,69 @@ function lume.weightedchoice(t)
end
function lume.shuffle(t)
for i = 1, #t do
local r = math_random(#t)
t[i], t[r] = t[r], t[i]
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.shuffle(t)
local rtn = {}
for i = 1, #t do
local r = math_random(i)
if r ~= i then
rtn[i] = rtn[r]
end
rtn[r] = t[i]
end
return rtn
end
function lume.sort(t, comp)
local rtn = lume.clone(t)
if comp then
if type(comp) == "string" then
table.sort(rtn, function(a, b) return a[comp] < b[comp] end)
else
table.sort(rtn, comp)
end
else
table.sort(rtn)
end
return rtn
end
function lume.array(...)
local t = {}
for x in ... do t[#t + 1] = x end
@@ -127,25 +219,29 @@ end
function lume.each(t, fn, ...)
local iter = getiter(t)
if type(fn) == "string" then
for _, v in pairs(t) do v[fn](v, ...) end
for _, v in iter(t) do v[fn](v, ...) end
else
for _, v in pairs(t) do fn(v, ...) end
for _, v in iter(t) do fn(v, ...) end
end
return t
end
function lume.map(t, fn)
fn = iteratee(fn)
local iter = getiter(t)
local rtn = {}
for k, v in pairs(t) do rtn[k] = fn(v) end
for k, v in iter(t) do rtn[k] = fn(v) end
return rtn
end
function lume.all(t, fn)
fn = fn or function(x) return x end
for k, v in pairs(t) do
fn = iteratee(fn)
local iter = getiter(t)
for k, v in iter(t) do
if not fn(v) then return false end
end
return true
@@ -153,8 +249,9 @@ end
function lume.any(t, fn)
fn = fn or function(x) return x end
for k, v in pairs(t) do
fn = iteratee(fn)
local iter = getiter(t)
for k, v in iter(t) do
if fn(v) then return true end
end
return false
@@ -162,41 +259,96 @@ 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
function lume.set(t, retainkeys)
function lume.set(t)
local rtn = {}
for k, v in pairs(lume.invert(t)) do
rtn[retainkeys and v or (#rtn + 1)] = k
rtn[#rtn + 1] = k
end
return rtn
end
function lume.filter(t, fn, retainkeys)
fn = iteratee(fn)
local iter = getiter(t)
local rtn = {}
for k, v in pairs(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.merge(t, t2, retainkeys)
for k, v in pairs(t2) do
t[retainkeys and k or (#t + 1)] = v
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 t
return rtn
end
function lume.merge(...)
local rtn = {}
for i = 1, select("#", ...) do
local t = select(i, ...)
local iter = getiter(t)
for k, v in iter(t) do
rtn[k] = v
end
end
return rtn
end
function lume.concat(...)
local rtn = {}
for i = 1, select("#", ...) do
local t = select(i, ...)
if t ~= nil then
local iter = getiter(t)
for k, v in iter(t) do
rtn[#rtn + 1] = v
end
end
end
return rtn
end
function lume.find(t, value)
for k, v in pairs(t) do
local iter = getiter(t)
for k, v in iter(t) do
if v == value then return k end
end
return nil
@@ -204,7 +356,9 @@ end
function lume.match(t, fn)
for k, v in pairs(t) do
fn = iteratee(fn)
local iter = getiter(t)
for k, v in iter(t) do
if fn(v) then return v, k end
end
return nil
@@ -213,12 +367,17 @@ end
function lume.count(t, fn)
local count = 0
local iter = getiter(t)
if fn then
for k, v in pairs(t) do
fn = iteratee(fn)
for k, v in iter(t) do
if fn(v) then count = count + 1 end
end
else
for k in pairs(t) do count = count + 1 end
if isarray(t) then
return #t
end
for k in iter(t) do count = count + 1 end
end
return count
end
@@ -235,6 +394,18 @@ function lume.slice(t, i, j)
end
function lume.first(t, n)
if not n then return t[1] end
return lume.slice(t, 1, n)
end
function lume.last(t, n)
if not n then return t[#t] end
return lume.slice(t, -n, -1)
end
function lume.invert(t)
local rtn = {}
for k, v in pairs(t) do rtn[v] = k end
@@ -242,6 +413,14 @@ function lume.invert(t)
end
function lume.keys(t)
local rtn = {}
local iter = getiter(t)
for k, v in iter(t) do rtn[#rtn + 1] = k end
return rtn
end
function lume.clone(t)
local rtn = {}
for k, v in pairs(t) do rtn[k] = v end
@@ -251,9 +430,9 @@ end
function lume.fn(fn, ...)
assert(iscallable(fn), "expected a function as the first argument")
local args = {...}
local args = { ... }
return function(...)
local a = lume.merge(lume.clone(args), {...})
local a = lume.concat(args, { ... })
return fn(unpack(a))
end
end
@@ -289,8 +468,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")
@@ -303,6 +490,13 @@ function lume.combine(...)
end
function lume.call(fn, ...)
if fn then
return fn(...)
end
end
function lume.time(fn, ...)
local start = os.clock()
local rtn = {fn(...)}
@@ -362,6 +556,32 @@ function lume.trim(str, chars)
end
function lume.wordwrap(str, limit)
limit = limit or 72
local check
if type(limit) == "number" then
check = function(str) return #str >= limit end
else
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
else
str = s
end
end
rtn[#rtn + 1] = str
end
return table.concat(rtn, "\n")
end
function lume.format(str, vars)
if not vars then return str end
local f = function(x)
@@ -376,7 +596,9 @@ function lume.trace(...)
local t = { "[" .. info.short_src .. ":" .. info.currentline .. "]" }
for i = 1, select("#", ...) do
local x = select(i, ...)
x = (type(x) == "number") and lume.round(x, .01) or (x or "nil")
if type(x) == "number" then
x = string.format("%g", lume.round(x, .01))
end
t[#t + 1] = tostring(x)
end
print(table.concat(t, " "))
@@ -434,6 +656,20 @@ function lume.hotswap(modname)
end
local ripairs_iter = function(t, i)
i = i - 1
local v = t[i]
if v then return i, v end
end
function lume.ripairs(t)
if t == nil then
return noop
end
return ripairs_iter, t, (#t + 1)
end
function lume.rgba(color)
local a = math_floor((color / 16777216) % 256)
local r = math_floor((color / 65536) % 256)
@@ -457,5 +693,11 @@ function lume.chain(value)
return setmetatable({ _value = value }, chain_mt)
end
setmetatable(lume, {
__call = function(t, ...)
return lume.chain(...)
end
})
return lume

View File

@@ -106,15 +106,66 @@ 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.shuffle
tests["lume.shuffle"] = function()
local t = {1, 2, 3, 4, 5}
lume.shuffle(t)
t = lume.shuffle(t)
table.sort(t)
testeq( t, {1, 2, 3, 4, 5} )
testeq( lume.shuffle({}), {} )
end
-- lume.sort
tests["lume.sort"] = function()
local t = { 1, 5, 2, 4, 3 }
local fn = function(a, b) return a > b end
testeq( t == lume.sort(t), false )
testeq( lume.sort(t), { 1, 2, 3, 4, 5 } )
testeq( lume.sort(t, fn), { 5, 4, 3, 2, 1 } )
testeq( t, { 1, 5, 2, 4, 3 } )
local t = { { id = 2 }, { id = 3 }, { id = 1 } }
testeq( lume.sort(t, "id"), { { id = 1 }, { id = 2 }, { id = 3 } })
end
-- lume.array
tests["lume.array"] = function()
local t = lume.array(pairs({a=0, b=0, c=0}))
@@ -141,6 +192,8 @@ end
tests["lume.map"] = function()
testeq( lume.map({1, 2, 3}, function(x) return x * 2 end), {2, 4, 6} )
testeq( lume.map({a=2,b=3}, function(x) return x * 2 end), {a=4,b=6} )
local t = {{ id = 10 }, { id = 20 }, { id = 30 }}
testeq( lume.map(t, "id"), { 10, 20, 30 })
end
-- lume.all
@@ -149,6 +202,10 @@ tests["lume.all"] = function()
testeq( lume.all({true, true, true, true}), true )
testeq( lume.all({2, 3, 4, 5}, function(x) return x % 2 == 0 end), false )
testeq( lume.all({2, 4, 6, 8}, function(x) return x % 2 == 0 end), true )
testeq( lume.all({{ x = 1 }, {}, { x = 3 }}, "x"), false )
testeq( lume.all({{ x = 1 }, { x = 2 }, { x = 3 }}, "x"), true )
testeq( lume.all({{ x = 1 }, { x = 2 }, { x = 3 }}, { x = 2 }), false )
testeq( lume.all({{ x = 2 }, { x = 2 }, { x = 2 }}, { x = 2 }), true )
end
-- lume.any
@@ -157,6 +214,9 @@ tests["lume.any"] = function()
testeq( lume.any({false, false, false}), false )
testeq( lume.any({2, 3, 4, 5}, function(x) return x % 2 == 0 end), true )
testeq( lume.any({1, 3, 5, 7}, function(x) return x % 2 == 0 end), false )
local t = {{ id = 10 }, { id = 20 }, { id = 30 }}
testeq( lume.any(t, { id = 10 }), true )
testeq( lume.any(t, { id = 40 }), false )
end
-- lume.reduce
@@ -170,6 +230,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
@@ -189,12 +252,33 @@ tests["lume.filter"] = function()
testeq( t, {2, 4} )
local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true)
testeq( t, {b=2} )
local t = lume.filter({{ x=1, y=1 }, { x=2, y=2 }, { x=1, y=3 }}, { x = 1 })
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({1, 2, 3}, {8, 9, 0}), {1, 2, 3, 8, 9, 0} )
testeq( lume.merge({a=1, b=2}, {b=3, c=4}, true), {a=1, b=3, c=4} )
testeq( lume.merge(), {} )
testeq( lume.merge({x=1, y=2}), {x=1, y=2} )
testeq( lume.merge({a=1, b=2}, {b=3, c=4}), {a=1, b=3, c=4} )
end
-- lume.concat
tests["lume.concat"] = function()
testeq( lume.concat(nil), {} )
testeq( lume.concat({1, 2, 3}), {1, 2, 3} )
testeq( lume.concat({1, 2, 3}, {4, 5, 6}), {1, 2, 3, 4, 5, 6} )
testeq( lume.concat({1, 2, 3}, {4, 5, 6}, nil, {7}), {1, 2, 3, 4, 5, 6, 7} )
end
-- lume.find
@@ -208,6 +292,7 @@ end
tests["lume.match"] = function()
local t = { "a", "b", "c", "d" }
local t2 = { a = 1, b = 2, c = 3, d = 4 }
local t3 = { {x=1, y=2}, {x=3, y=4}, {x=5, y=6} }
local v, k = lume.match(t, function(x) return x > "c" end)
testeq( v, "d" )
testeq( k, 4 )
@@ -220,6 +305,8 @@ tests["lume.match"] = function()
local v, k = lume.match(t2, function(x) return x > 5 end)
testeq( v, nil )
testeq( k, nil )
local v, k = lume.match(t3, { x = 3, y = 4 })
testeq( k, 2 )
end
-- lume.count
@@ -229,6 +316,10 @@ tests["lume.count"] = function()
testeq( lume.count(t, function(x) return x % 2 == 0 end ), 3 )
local a = { 5, 6, 7, 8, 9 }
testeq( lume.count(a), #a )
local t = { { n = 20 }, { n = 30 }, { n = 40 }, { n = 20 } }
testeq( lume.count(t, { n = 20 }), 2 )
testeq( lume.count(t, { n = 30 }), 1 )
testeq( lume.count(t, { n = 50 }), 0 )
end
-- lume.slice
@@ -248,14 +339,40 @@ tests["lume.slice"] = function()
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 900), {"c", "d", "e"} )
end
-- lume.first
tests["lume.first"] = function()
local t = { "a", "b", "c", "d", "e" }
testeq( lume.first(t), "a" )
testeq( lume.first(t, 1), { "a" } )
testeq( lume.first(t, 2), { "a", "b" } )
end
-- lume.last
tests["lume.last"] = function()
local t = { "a", "b", "c", "d", "e" }
testeq( lume.last(t), "e" )
testeq( lume.last(t, 1), { "e" } )
testeq( lume.last(t, 2), { "d", "e" } )
end
-- lume.invert
tests["lume_invert"] = function()
tests["lume.invert"] = function()
testeq( lume.invert({}), {} )
testeq( lume.invert{a = "x", b = "y"}, {x = "a", y = "b"} )
testeq( lume.invert{a = 1, b = 2}, {"a", "b"} )
testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} )
end
-- lume.keys
tests["lume.keys"] = function()
testeq( lume.keys({}), {} )
local t = lume.keys({ aaa = 1, bbb = 2, ccc = 3 })
table.sort(t)
testeq( t, {"aaa", "bbb", "ccc"} )
local t = lume.keys({ "x", "x", "x" })
testeq( t, {1, 2, 3} )
end
-- lume.clone
tests["lume.clone"] = function()
local t = {6, 7, 4, 5}
@@ -305,6 +422,20 @@ 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
tests["lume.call"] = function()
local add = function(a, b) return a + b end
testeq( lume.call(), nil )
testeq( lume.call(nil, 1, 2, 3), nil )
testeq( lume.call(add, 1, 2), 3 )
end
-- lume.time
@@ -358,6 +489,16 @@ tests["lume.trim"] = function()
testeq( lume.trim("^.hello world]^", "^.]"), "hello world" )
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 fn = function(str) return #str >= 20 end
testeq( lume.wordwrap(str), str )
testeq( lume.wordwrap(str, 20), b )
testeq( lume.wordwrap(str, fn), b )
end
-- lume.format
tests["lume.format"] = function()
local str = lume.format("a {a} in a {b}", {a = "mouse", b = "house"})
@@ -403,6 +544,18 @@ tests["lume.hotswap"] = function()
testeq( type(err), "string" )
end
-- lume.ripairs
tests["lume.ripairs"] = function()
local t = { "a", "b", "c" }
local r = {}
for i, v in lume.ripairs(t) do
table.insert(r, { i, v })
end
testeq( r, { { 3, "c" }, { 2, "b" }, { 1, "a" } })
for i, v in lume.ripairs(nil) do
end
end
-- lume.rgba
tests["lume.rgba"] = function()
local r, g, b, a = lume.rgba(0x12345678)
@@ -417,6 +570,8 @@ tests["lume.chain"] = function()
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()
testeq( t, { 2, 4 } )
testeq( lume.chain(10):result(), 10 )
local t = lume({1, 2}):map(function(x) return x * 2 end):result()
testeq( t, { 2, 4 } )
end