mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-29 11:32:20 +00:00
Compare commits
26 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
27278fb887 | ||
|
8627638db0 | ||
|
1559803c70 | ||
|
258e523219 | ||
|
16848d83a5 | ||
|
ca338b8833 | ||
|
ca36473904 | ||
|
717745fe79 | ||
|
688de3368e | ||
|
7412706277 | ||
|
78144dbdb8 | ||
|
d8027db54d | ||
|
6bff74e856 | ||
|
037f5e3325 | ||
|
6dec4a8f56 | ||
|
044141fefa | ||
|
e0d55c8446 | ||
|
84e23cb82c | ||
|
a753f6e9a2 | ||
|
9b48d704d2 | ||
|
6c350d81d9 | ||
|
300f47456f | ||
|
cb95fea004 | ||
|
2e00c753d8 | ||
|
fce4a5e5df | ||
|
b25fee071f |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
|||||||
__*
|
|
||||||
*.tmp
|
|
||||||
*.swp
|
|
29
README.md
29
README.md
@@ -98,6 +98,14 @@ local t = { 1, 2, 3 }
|
|||||||
lume.clear(t) -- `t` becomes {}
|
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)
|
### lume.shuffle(t)
|
||||||
Returns a shuffled copy of the array `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.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)
|
### lume.keys(t)
|
||||||
Returns an array containing each key of the table.
|
Returns an array containing each key of the table.
|
||||||
|
|
||||||
@@ -315,7 +329,7 @@ f(10, 5) -- Returns 25
|
|||||||
### lume.serialize(x)
|
### lume.serialize(x)
|
||||||
Serializes the argument `x` into a string which can be loaded again using
|
Serializes the argument `x` into a string which can be loaded again using
|
||||||
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
||||||
serialized. Circular references are not handled; all nested tables are
|
serialized. Circular references will result in an error; all nested tables are
|
||||||
serialized as unique tables.
|
serialized as unique tables.
|
||||||
```lua
|
```lua
|
||||||
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
||||||
@@ -369,7 +383,7 @@ Prints the current filename and line number followed by each argument separated
|
|||||||
by a space.
|
by a space.
|
||||||
```lua
|
```lua
|
||||||
-- Assuming the file is called "example.lua" and the next line is 12:
|
-- 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)
|
### lume.dostring(str)
|
||||||
@@ -404,6 +418,17 @@ for i, v in lume.ripairs({ "a", "b", "c" }) do
|
|||||||
end
|
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)
|
### lume.rgba(color)
|
||||||
Takes the 32bit integer `color` argument and returns 4 numbers, one for each
|
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
|
channel, with a range of 0 - 255. The returned values can be used as the
|
||||||
|
124
lume.lua
124
lume.lua
@@ -7,7 +7,7 @@
|
|||||||
-- under the terms of the MIT license. See LICENSE for details.
|
-- under the terms of the MIT license. See LICENSE for details.
|
||||||
--
|
--
|
||||||
|
|
||||||
local lume = { _version = "2.1.0" }
|
local lume = { _version = "2.2.2" }
|
||||||
|
|
||||||
local pairs, ipairs = pairs, ipairs
|
local pairs, ipairs = pairs, ipairs
|
||||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
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
|
return (type(x) == "table" and x[1] ~= nil) and true or false
|
||||||
end
|
end
|
||||||
|
|
||||||
local iternil = function()
|
|
||||||
return noop
|
|
||||||
end
|
|
||||||
|
|
||||||
local getiter = function(x)
|
local getiter = function(x)
|
||||||
if isarray(x) then
|
if isarray(x) then
|
||||||
return ipairs
|
return ipairs
|
||||||
@@ -82,7 +78,7 @@ end
|
|||||||
|
|
||||||
function lume.round(x, increment)
|
function lume.round(x, increment)
|
||||||
if increment then return lume.round(x / increment) * increment end
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -183,6 +179,19 @@ function lume.clear(t)
|
|||||||
end
|
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)
|
function lume.shuffle(t)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for i = 1, #t do
|
for i = 1, #t do
|
||||||
@@ -413,6 +422,16 @@ function lume.invert(t)
|
|||||||
end
|
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)
|
function lume.keys(t)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
local iter = getiter(t)
|
local iter = getiter(t)
|
||||||
@@ -517,19 +536,41 @@ function lume.lambda(str)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.serialize(x)
|
local serialize
|
||||||
local f = { string = function(v) return string.format("%q", v) end,
|
|
||||||
number = tostring, boolean = tostring }
|
local serialize_map = {
|
||||||
f.table = function(t)
|
[ "boolean" ] = tostring,
|
||||||
|
[ "nil" ] = tostring,
|
||||||
|
[ "string" ] = function(v) return string.format("%q", v) end,
|
||||||
|
[ "number" ] = function(v)
|
||||||
|
if v ~= v then return "0/0" -- nan
|
||||||
|
elseif v == 1 / 0 then return "1/0" -- inf
|
||||||
|
elseif v == -1 / 0 then return "-1/0" end -- -inf
|
||||||
|
return tostring(v)
|
||||||
|
end,
|
||||||
|
[ "table" ] = function(t, stk)
|
||||||
|
stk = stk or {}
|
||||||
|
if stk[t] then error("circular reference") end
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
|
stk[t] = true
|
||||||
for k, v in pairs(t) do
|
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
|
end
|
||||||
return "{" .. table.concat(rtn) .. "}"
|
stk[t] = nil
|
||||||
|
return "{" .. table.concat(rtn, ",") .. "}"
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -565,20 +606,26 @@ function lume.wordwrap(str, limit)
|
|||||||
check = limit
|
check = limit
|
||||||
end
|
end
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for j, line in ipairs(lume.split(str, "\n")) do
|
local line = ""
|
||||||
local str
|
for word, spaces in str:gmatch("(%S+)(%s*)") do
|
||||||
for i, word in ipairs(lume.split(line)) do
|
local str = line .. word
|
||||||
local s = (str and (str .. " ") or "") .. word
|
if check(str) then
|
||||||
if check(s) then
|
table.insert(rtn, line .. "\n")
|
||||||
rtn[#rtn + 1] = str
|
line = word
|
||||||
str = word
|
else
|
||||||
|
line = str
|
||||||
|
end
|
||||||
|
for c in spaces:gmatch(".") do
|
||||||
|
if c == "\n" then
|
||||||
|
table.insert(rtn, line .. "\n")
|
||||||
|
line = ""
|
||||||
else
|
else
|
||||||
str = s
|
line = line .. c
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
rtn[#rtn + 1] = str
|
|
||||||
end
|
end
|
||||||
return table.concat(rtn, "\n")
|
table.insert(rtn, line)
|
||||||
|
return table.concat(rtn)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -593,7 +640,7 @@ end
|
|||||||
|
|
||||||
function lume.trace(...)
|
function lume.trace(...)
|
||||||
local info = debug.getinfo(2, "Sl")
|
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
|
for i = 1, select("#", ...) do
|
||||||
local x = select(i, ...)
|
local x = select(i, ...)
|
||||||
if type(x) == "number" then
|
if type(x) == "number" then
|
||||||
@@ -663,13 +710,32 @@ local ripairs_iter = function(t, i)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function lume.ripairs(t)
|
function lume.ripairs(t)
|
||||||
if t == nil then
|
|
||||||
return noop
|
|
||||||
end
|
|
||||||
return ripairs_iter, t, (#t + 1)
|
return ripairs_iter, t, (#t + 1)
|
||||||
end
|
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)
|
function lume.rgba(color)
|
||||||
local a = math_floor((color / 16777216) % 256)
|
local a = math_floor((color / 16777216) % 256)
|
||||||
local r = math_floor((color / 65536) % 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
|
package.path = "../?.lua;" .. package.path
|
||||||
|
|
||||||
@@ -145,6 +145,16 @@ tests["lume.clear"] = function()
|
|||||||
testeq( lume.clear(t) == t, true )
|
testeq( lume.clear(t) == t, true )
|
||||||
end
|
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
|
-- lume.shuffle
|
||||||
tests["lume.shuffle"] = function()
|
tests["lume.shuffle"] = function()
|
||||||
local t = {1, 2, 3, 4, 5}
|
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} )
|
testeq( lume.invert(lume.invert{a = 1, b = 2}), {a = 1, b = 2} )
|
||||||
end
|
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
|
-- lume.keys
|
||||||
tests["lume.keys"] = function()
|
tests["lume.keys"] = function()
|
||||||
testeq( lume.keys({}), {} )
|
testeq( lume.keys({}), {} )
|
||||||
@@ -465,6 +484,10 @@ tests["lume.serialize, lume.deserialize"] = function()
|
|||||||
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
|
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
|
||||||
local s = lume.serialize(t)
|
local s = lume.serialize(t)
|
||||||
testeq( lume.deserialize(s), t )
|
testeq( lume.deserialize(s), t )
|
||||||
|
testeq( lume.deserialize(lume.serialize(math.huge)), math.huge )
|
||||||
|
testeq( lume.deserialize(lume.serialize(-math.huge)), -math.huge )
|
||||||
|
local x = lume.deserialize(lume.serialize(0 / 0)) -- nan
|
||||||
|
testeq( x ~= x, true )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.split
|
-- lume.split
|
||||||
@@ -491,8 +514,8 @@ end
|
|||||||
|
|
||||||
-- lume.wordwrap
|
-- lume.wordwrap
|
||||||
tests["lume.wordwrap"] = function()
|
tests["lume.wordwrap"] = function()
|
||||||
local str = "A small string with some words and then some 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 b = "A small string with \nsome words and then \nsome more words"
|
||||||
local fn = function(str) return #str >= 20 end
|
local fn = function(str) return #str >= 20 end
|
||||||
testeq( lume.wordwrap(str), str )
|
testeq( lume.wordwrap(str), str )
|
||||||
testeq( lume.wordwrap(str, 20), b )
|
testeq( lume.wordwrap(str, 20), b )
|
||||||
@@ -516,7 +539,7 @@ tests["lume.trace"] = function()
|
|||||||
local oldprint = print
|
local oldprint = print
|
||||||
local file, line, msg
|
local file, line, msg
|
||||||
print = function(x)
|
print = function(x)
|
||||||
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
file, line, msg = x:match("(.-):(.-): (.*)")
|
||||||
end
|
end
|
||||||
lume.trace("Hi world", 123.456, 1, nil)
|
lume.trace("Hi world", 123.456, 1, nil)
|
||||||
print = oldprint
|
print = oldprint
|
||||||
@@ -552,8 +575,24 @@ tests["lume.ripairs"] = function()
|
|||||||
table.insert(r, { i, v })
|
table.insert(r, { i, v })
|
||||||
end
|
end
|
||||||
testeq( r, { { 3, "c" }, { 2, "b" }, { 1, "a" } })
|
testeq( r, { { 3, "c" }, { 2, "b" }, { 1, "a" } })
|
||||||
for i, v in lume.ripairs(nil) do
|
tester.test.error(lume.ripairs, nil)
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
-- lume.rgba
|
-- lume.rgba
|
Reference in New Issue
Block a user