mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d8027db54d | ||
|
6bff74e856 | ||
|
037f5e3325 | ||
|
6dec4a8f56 | ||
|
044141fefa | ||
|
e0d55c8446 | ||
|
84e23cb82c | ||
|
a753f6e9a2 | ||
|
9b48d704d2 | ||
|
6c350d81d9 | ||
|
300f47456f | ||
|
cb95fea004 | ||
|
2e00c753d8 | ||
|
fce4a5e5df | ||
|
b25fee071f |
25
README.md
25
README.md
@@ -98,6 +98,14 @@ 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`.
|
||||
|
||||
@@ -244,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.
|
||||
|
||||
@@ -404,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
|
||||
|
82
lume.lua
82
lume.lua
@@ -7,7 +7,7 @@
|
||||
-- under the terms of the MIT license. See LICENSE for details.
|
||||
--
|
||||
|
||||
local lume = { _version = "2.1.0" }
|
||||
local lume = { _version = "2.2.0" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -46,10 +46,6 @@ 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
|
||||
@@ -82,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
|
||||
|
||||
|
||||
@@ -183,6 +179,19 @@ function lume.clear(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
|
||||
@@ -413,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)
|
||||
@@ -565,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
|
||||
|
||||
|
||||
@@ -663,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)
|
||||
|
@@ -145,6 +145,16 @@ tests["lume.clear"] = function()
|
||||
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}
|
||||
@@ -363,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({}), {} )
|
||||
@@ -491,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 )
|
||||
@@ -552,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