33 Commits

Author SHA1 Message Date
rxi
d8027db54d Version 2.2.0 2015-07-31 18:58:24 +01:00
rxi
6bff74e856 Changed lume.round() such that 0 results in 0 rather than -0 2015-05-12 20:30:05 +01:00
rxi
037f5e3325 Missing '.' in README 2015-05-09 15:44:52 +01:00
rxi
6dec4a8f56 Added lume.pick() to README 2015-05-09 15:42:38 +01:00
rxi
044141fefa Added lume.pick() and tests 2015-05-09 15:40:53 +01:00
rxi
e0d55c8446 Fixed lume.color example in README 2015-05-09 15:21:08 +01:00
rxi
84e23cb82c Added README section for lume.color() 2015-05-09 15:19:38 +01:00
rxi
a753f6e9a2 Added function lume.color() and tests 2015-05-09 15:12:52 +01:00
rxi
9b48d704d2 Removed unused function iternil() 2015-05-09 14:30:32 +01:00
rxi
6c350d81d9 Removed support of nil in lume.ripairs(), updated tests 2015-05-09 14:29:56 +01:00
rxi
300f47456f Improved description of lume.extend() in README 2015-05-09 14:27:38 +01:00
rxi
cb95fea004 Missing language hint on lume.extend's README example 2015-05-05 19:37:52 +01:00
rxi
2e00c753d8 Added function lume.extend(), updated README and tests 2015-05-05 19:32:46 +01:00
rxi
fce4a5e5df Updated test for lume.wordwrap() 2015-05-05 19:13:38 +01:00
rxi
b25fee071f Changed lume.wordwrap() to better retain existing whitespace 2015-05-05 19:12:41 +01:00
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
3 changed files with 338 additions and 38 deletions

View File

@@ -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.
@@ -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

188
lume.lua
View File

@@ -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.0" }
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")
@@ -489,20 +584,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
@@ -587,13 +688,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)

View File

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