mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-29 11:32:20 +00:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
0e4256ef0d | ||
|
bcad07fc5f | ||
|
8f1267f967 | ||
|
de37bd6d65 | ||
|
ac920c8f5e | ||
|
5d258d4fd0 | ||
|
df26e7939d | ||
|
16e370cdf0 | ||
|
0b991d7ea5 |
22
README.md
22
README.md
@@ -178,6 +178,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.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)
|
### lume.invert(t)
|
||||||
Returns a copy of the table where the keys have become the values and the
|
Returns a copy of the table where the keys have become the values and the
|
||||||
values the keys.
|
values the keys.
|
||||||
@@ -185,6 +199,9 @@ 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.keys(t)
|
||||||
|
Returns an array containing each key of the table.
|
||||||
|
|
||||||
### lume.clone(t)
|
### lume.clone(t)
|
||||||
Returns a shallow copy of the table `t`.
|
Returns a shallow copy of the table `t`.
|
||||||
|
|
||||||
@@ -333,6 +350,11 @@ lume.chain({1, 2, 3, 4})
|
|||||||
:map(function(x) return -x end)
|
:map(function(x) return -x end)
|
||||||
:result() -- Returns { -2, -4 }
|
: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
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
53
lume.lua
53
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 = "1.4.1" }
|
local lume = { _version = "1.5.0" }
|
||||||
|
|
||||||
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
|
||||||
@@ -35,6 +35,24 @@ local iscallable = function(x)
|
|||||||
return mt and mt.__call ~= nil
|
return mt and mt.__call ~= nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local identity = function(x)
|
||||||
|
return x
|
||||||
|
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)
|
function lume.clamp(x, min, max)
|
||||||
@@ -137,6 +155,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.map(t, fn)
|
function lume.map(t, fn)
|
||||||
|
fn = iteratee(fn)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do rtn[k] = fn(v) end
|
for k, v in pairs(t) do rtn[k] = fn(v) end
|
||||||
return rtn
|
return rtn
|
||||||
@@ -144,7 +163,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.all(t, fn)
|
function lume.all(t, fn)
|
||||||
fn = fn or function(x) return x end
|
fn = iteratee(fn)
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if not fn(v) then return false end
|
if not fn(v) then return false end
|
||||||
end
|
end
|
||||||
@@ -153,7 +172,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.any(t, fn)
|
function lume.any(t, fn)
|
||||||
fn = fn or function(x) return x end
|
fn = iteratee(fn)
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if fn(v) then return true end
|
if fn(v) then return true end
|
||||||
end
|
end
|
||||||
@@ -179,6 +198,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.filter(t, fn, retainkeys)
|
function lume.filter(t, fn, retainkeys)
|
||||||
|
fn = iteratee(fn)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if fn(v) then rtn[retainkeys and k or (#rtn + 1)] = v end
|
if fn(v) then rtn[retainkeys and k or (#rtn + 1)] = v end
|
||||||
@@ -204,6 +224,7 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.match(t, fn)
|
function lume.match(t, fn)
|
||||||
|
fn = iteratee(fn)
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if fn(v) then return v, k end
|
if fn(v) then return v, k end
|
||||||
end
|
end
|
||||||
@@ -214,6 +235,7 @@ end
|
|||||||
function lume.count(t, fn)
|
function lume.count(t, fn)
|
||||||
local count = 0
|
local count = 0
|
||||||
if fn then
|
if fn then
|
||||||
|
fn = iteratee(fn)
|
||||||
for k, v in pairs(t) do
|
for k, v in pairs(t) do
|
||||||
if fn(v) then count = count + 1 end
|
if fn(v) then count = count + 1 end
|
||||||
end
|
end
|
||||||
@@ -235,6 +257,18 @@ function lume.slice(t, i, j)
|
|||||||
end
|
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)
|
function lume.invert(t)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do rtn[v] = k end
|
for k, v in pairs(t) do rtn[v] = k end
|
||||||
@@ -242,6 +276,13 @@ function lume.invert(t)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
function lume.keys(t)
|
||||||
|
local rtn = {}
|
||||||
|
for k, v in pairs(t) do rtn[#rtn + 1] = k end
|
||||||
|
return rtn
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.clone(t)
|
function lume.clone(t)
|
||||||
local rtn = {}
|
local rtn = {}
|
||||||
for k, v in pairs(t) do rtn[k] = v end
|
for k, v in pairs(t) do rtn[k] = v end
|
||||||
@@ -457,5 +498,11 @@ function lume.chain(value)
|
|||||||
return setmetatable({ _value = value }, chain_mt)
|
return setmetatable({ _value = value }, chain_mt)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
setmetatable(lume, {
|
||||||
|
__call = function(t, ...)
|
||||||
|
return lume.chain(...)
|
||||||
|
end
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
return lume
|
return lume
|
||||||
|
@@ -141,6 +141,8 @@ end
|
|||||||
tests["lume.map"] = function()
|
tests["lume.map"] = function()
|
||||||
testeq( lume.map({1, 2, 3}, function(x) return x * 2 end), {2, 4, 6} )
|
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} )
|
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
|
end
|
||||||
|
|
||||||
-- lume.all
|
-- lume.all
|
||||||
@@ -149,6 +151,10 @@ tests["lume.all"] = function()
|
|||||||
testeq( lume.all({true, true, true, true}), true )
|
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, 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({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
|
end
|
||||||
|
|
||||||
-- lume.any
|
-- lume.any
|
||||||
@@ -157,6 +163,9 @@ tests["lume.any"] = function()
|
|||||||
testeq( lume.any({false, false, false}), false )
|
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({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 )
|
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
|
end
|
||||||
|
|
||||||
-- lume.reduce
|
-- lume.reduce
|
||||||
@@ -189,6 +198,8 @@ tests["lume.filter"] = function()
|
|||||||
testeq( t, {2, 4} )
|
testeq( t, {2, 4} )
|
||||||
local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true)
|
local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true)
|
||||||
testeq( t, {b=2} )
|
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
|
end
|
||||||
|
|
||||||
-- lume.merge
|
-- lume.merge
|
||||||
@@ -208,6 +219,7 @@ end
|
|||||||
tests["lume.match"] = function()
|
tests["lume.match"] = function()
|
||||||
local t = { "a", "b", "c", "d" }
|
local t = { "a", "b", "c", "d" }
|
||||||
local t2 = { a = 1, b = 2, c = 3, d = 4 }
|
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)
|
local v, k = lume.match(t, function(x) return x > "c" end)
|
||||||
testeq( v, "d" )
|
testeq( v, "d" )
|
||||||
testeq( k, 4 )
|
testeq( k, 4 )
|
||||||
@@ -220,6 +232,8 @@ tests["lume.match"] = function()
|
|||||||
local v, k = lume.match(t2, function(x) return x > 5 end)
|
local v, k = lume.match(t2, function(x) return x > 5 end)
|
||||||
testeq( v, nil )
|
testeq( v, nil )
|
||||||
testeq( k, nil )
|
testeq( k, nil )
|
||||||
|
local v, k = lume.match(t3, { x = 3, y = 4 })
|
||||||
|
testeq( k, 2 )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.count
|
-- lume.count
|
||||||
@@ -229,6 +243,10 @@ tests["lume.count"] = function()
|
|||||||
testeq( lume.count(t, function(x) return x % 2 == 0 end ), 3 )
|
testeq( lume.count(t, function(x) return x % 2 == 0 end ), 3 )
|
||||||
local a = { 5, 6, 7, 8, 9 }
|
local a = { 5, 6, 7, 8, 9 }
|
||||||
testeq( lume.count(a), #a )
|
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
|
end
|
||||||
|
|
||||||
-- lume.slice
|
-- lume.slice
|
||||||
@@ -248,14 +266,40 @@ tests["lume.slice"] = function()
|
|||||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 900), {"c", "d", "e"} )
|
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 900), {"c", "d", "e"} )
|
||||||
end
|
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
|
-- lume.invert
|
||||||
tests["lume_invert"] = function()
|
tests["lume.invert"] = function()
|
||||||
testeq( lume.invert({}), {} )
|
testeq( lume.invert({}), {} )
|
||||||
testeq( lume.invert{a = "x", b = "y"}, {x = "a", y = "b"} )
|
testeq( lume.invert{a = "x", b = "y"}, {x = "a", y = "b"} )
|
||||||
testeq( lume.invert{a = 1, b = 2}, {"a", "b"} )
|
testeq( lume.invert{a = 1, b = 2}, {"a", "b"} )
|
||||||
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.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
|
-- lume.clone
|
||||||
tests["lume.clone"] = function()
|
tests["lume.clone"] = function()
|
||||||
local t = {6, 7, 4, 5}
|
local t = {6, 7, 4, 5}
|
||||||
@@ -417,6 +461,8 @@ tests["lume.chain"] = function()
|
|||||||
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()
|
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()
|
||||||
testeq( t, { 2, 4 } )
|
testeq( t, { 2, 4 } )
|
||||||
testeq( lume.chain(10):result(), 10 )
|
testeq( lume.chain(10):result(), 10 )
|
||||||
|
local t = lume({1, 2}):map(function(x) return x * 2 end):result()
|
||||||
|
testeq( t, { 2, 4 } )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user