mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
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
This commit is contained in:
@@ -151,12 +151,11 @@ an array and retains its original keys.
|
|||||||
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
|
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
|
||||||
```
|
```
|
||||||
|
|
||||||
### lume.merge(t, t2 [, retainkeys])
|
### lume.merge(...)
|
||||||
Merges all the values from the table `t2` into `t` in place. If `retainkeys` is
|
Returns a new table with all the given tables merged together. If a key exists
|
||||||
true the table is not treated as an array and retains its original keys; if `t`
|
in multiple tables the right-most table's value is used.
|
||||||
and `t2` have a conflicting key, the value from `t2` is used.
|
|
||||||
```lua
|
```lua
|
||||||
lume.merge({2, 3}, {4, 5}) -- Returns {2, 3, 4, 5}
|
lume.merge({a=1, b=2, c=3}, {c=8, d=9}) -- Returns {a=1, b=2, c=8, d=9}
|
||||||
```
|
```
|
||||||
|
|
||||||
### lume.concat(...)
|
### lume.concat(...)
|
||||||
|
17
lume.lua
17
lume.lua
@@ -252,11 +252,16 @@ function lume.filter(t, fn, retainkeys)
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.merge(t, t2, retainkeys)
|
function lume.merge(...)
|
||||||
for k, v in pairs(t2) do
|
local rtn = {}
|
||||||
t[retainkeys and k or (#t + 1)] = v
|
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
|
end
|
||||||
return t
|
return rtn
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -357,9 +362,9 @@ end
|
|||||||
|
|
||||||
function lume.fn(fn, ...)
|
function lume.fn(fn, ...)
|
||||||
assert(iscallable(fn), "expected a function as the first argument")
|
assert(iscallable(fn), "expected a function as the first argument")
|
||||||
local args = {...}
|
local args = { ... }
|
||||||
return function(...)
|
return function(...)
|
||||||
local a = lume.merge(lume.clone(args), {...})
|
local a = lume.concat(args, { ... })
|
||||||
return fn(unpack(a))
|
return fn(unpack(a))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@@ -216,8 +216,9 @@ end
|
|||||||
|
|
||||||
-- lume.merge
|
-- lume.merge
|
||||||
tests["lume.merge"] = function()
|
tests["lume.merge"] = function()
|
||||||
testeq( lume.merge({1, 2, 3}, {8, 9, 0}), {1, 2, 3, 8, 9, 0} )
|
testeq( lume.merge(), {} )
|
||||||
testeq( lume.merge({a=1, b=2}, {b=3, c=4}, true), {a=1, b=3, c=4} )
|
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
|
end
|
||||||
|
|
||||||
-- lume.concat
|
-- lume.concat
|
||||||
|
Reference in New Issue
Block a user