mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 11:02:20 +00:00
Compare commits
126 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
a20a39c8ee | ||
|
faa5d8252f | ||
|
bf32432dac | ||
|
b861303333 | ||
|
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 | ||
|
a6744d4ab3 | ||
|
c3c2d31d37 | ||
|
886bd43bce | ||
|
976f813de9 | ||
|
7911d62c53 | ||
|
b1c9e3ec2e | ||
|
48a4b43640 | ||
|
63e6d1daed | ||
|
685b7dc9c4 | ||
|
d2b26fa681 | ||
|
0a84bcd0d6 | ||
|
8e0b2d2ef8 | ||
|
f27b15f9a2 | ||
|
abecb5b718 | ||
|
fa85f83d70 | ||
|
14b9ee0ca2 | ||
|
3bec7b356d | ||
|
9cdbc27b66 | ||
|
c6ab30cf7d | ||
|
c07194490b | ||
|
5305cb34c8 | ||
|
f7b3eb64bb | ||
|
edd7c99382 | ||
|
c773acdcf4 | ||
|
756d30416d | ||
|
9b5abf3d58 | ||
|
397dce4c5e | ||
|
ce86f4b0ea | ||
|
c565f0739b | ||
|
7a0f5a0831 | ||
|
b873c043c5 | ||
|
4373a202dc | ||
|
ac10d54b47 | ||
|
067e58b30f | ||
|
a42cbb12f0 | ||
|
0c21cfcf5b | ||
|
0e4256ef0d | ||
|
bcad07fc5f | ||
|
8f1267f967 | ||
|
de37bd6d65 | ||
|
ac920c8f5e | ||
|
5d258d4fd0 | ||
|
df26e7939d | ||
|
16e370cdf0 | ||
|
0b991d7ea5 | ||
|
f6174a4b68 | ||
|
d99bf9549a | ||
|
0cc52cd24e | ||
|
0a8258d608 | ||
|
95c3f8960d | ||
|
6b73aaa8ad | ||
|
1a82d308af | ||
|
17b58ec63d | ||
|
5ab25046a2 | ||
|
3f61d823ae | ||
|
0716caf6a1 | ||
|
6a160a3afe | ||
|
fff0d780bb | ||
|
283f7ee787 | ||
|
076ca7972c | ||
|
c2311f9821 | ||
|
beced85d6b | ||
|
8a2765a41b | ||
|
09847bd266 | ||
|
841cee30e1 | ||
|
cd3c0a1eea | ||
|
3190d65130 | ||
|
c7471b32fb | ||
|
bb56f1ce3a | ||
|
098754056a | ||
|
330779fb0f | ||
|
acdb58a447 | ||
|
88b428cb4d | ||
|
fdf01937e2 | ||
|
3a4ce4fe3b | ||
|
1a087efe99 | ||
|
9a82d6318e | ||
|
30991649f8 | ||
|
dbd93b3861 | ||
|
8311519e3f | ||
|
82c697b08e | ||
|
5db6be936a | ||
|
335b928cae | ||
|
94977b4f7e | ||
|
e6246834b7 | ||
|
9bf2d24ee2 | ||
|
89931c1ad8 | ||
|
9ff4637201 | ||
|
617729e261 | ||
|
ad64d7af05 | ||
|
d69f419f5a | ||
|
ad34f6ce33 | ||
|
5882ca1303 | ||
|
828d23e6f6 | ||
|
13a8edb2e7 | ||
|
425a52b99f |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,3 +0,0 @@
|
||||
__*
|
||||
*.tmp
|
||||
*.swp
|
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2014, rxi
|
||||
Copyright (c) 2015, rxi
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
|
235
README.md
235
README.md
@@ -38,7 +38,7 @@ lume.lerp(100, 200, .5) -- Returns 150
|
||||
```
|
||||
|
||||
### lume.smooth(a, b, amount)
|
||||
Similar to `lume.lerp()` but uses cosine interpolation instead of linear
|
||||
Similar to `lume.lerp()` but uses cubic interpolation instead of linear
|
||||
interpolation.
|
||||
|
||||
### lume.pingpong(x)
|
||||
@@ -74,8 +74,50 @@ 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)
|
||||
Shuffles the values of array `t` in place, returns the array.
|
||||
Returns a shuffled copy of the array `t`.
|
||||
|
||||
### lume.sort(t [, comp])
|
||||
Returns a copy of the array `t` with all its items sorted. If `comp` is a
|
||||
function it will be used to compare the items when sorting. If `comp` is a
|
||||
string it will be used as the key to sort the items by.
|
||||
```lua
|
||||
lume.sort({ 1, 4, 3, 2, 5 }) -- Returns { 1, 2, 3, 4, 5 }
|
||||
lume.sort({ {z=2}, {z=3}, {z=1} }, "z") -- Returns { {z=1}, {z=2}, {z=3} }
|
||||
lume.sort({ 1, 3, 2 }, function(a, b) return a > b end) -- Returns { 3, 2, 1 }
|
||||
```
|
||||
|
||||
### lume.array(...)
|
||||
Iterates the supplied iterator and returns an array filled with the values.
|
||||
@@ -125,12 +167,10 @@ an error is raised,
|
||||
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
|
||||
```
|
||||
|
||||
### lume.set(t [, retainkeys])
|
||||
Returns a copy of the `t` table with all the duplicate values removed. If
|
||||
`retainkeys` is true the table is not treated as an array and retains its
|
||||
original keys.
|
||||
### lume.set(t)
|
||||
Returns a copy of the `t` array with all the duplicate values removed.
|
||||
```lua
|
||||
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, cat}
|
||||
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"}
|
||||
```
|
||||
|
||||
### lume.filter(t, fn [, retainkeys])
|
||||
@@ -141,12 +181,25 @@ an array and retains its original keys.
|
||||
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
|
||||
```
|
||||
|
||||
### lume.merge(t, t2 [, retainkeys])
|
||||
Merges all the values from the table `t2` into `t` in place. If `retainkeys` is
|
||||
true the table is not treated as an array and retains its original keys; if `t`
|
||||
and `t2` have a conflicting key, the value from `t2` is used.
|
||||
### 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.merge({2, 3}, {4, 5}) -- Returns {2, 3, 4, 5}
|
||||
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.
|
||||
```lua
|
||||
lume.merge({a=1, b=2, c=3}, {c=8, d=9}) -- Returns {a=1, b=2, c=8, d=9}
|
||||
```
|
||||
|
||||
### lume.concat(...)
|
||||
Returns a new array consisting of all the given arrays concatenated into one.
|
||||
```lua
|
||||
lume.concat({1, 2}, {3, 4}, {5, 6}) -- Returns {1, 2, 3, 4, 5, 6}
|
||||
```
|
||||
|
||||
### lume.find(t, value)
|
||||
@@ -156,6 +209,21 @@ exist in the table.
|
||||
lume.find({"a", "b", "c"}, "b") -- Returns 2
|
||||
```
|
||||
|
||||
### lume.match(t, fn)
|
||||
Returns the value and key of the value in table `t` which returns true when
|
||||
`fn` is called on it. Returns `nil` if no such value exists.
|
||||
```lua
|
||||
lume.match({1, 5, 8, 7}, function(x) return x % 2 == 0 end) -- Returns 8, 3
|
||||
```
|
||||
|
||||
### lume.count(t [, fn])
|
||||
Counts the number of values in the table `t`. If a `fn` function is supplied it
|
||||
is called on each value, the number of times it returns true is counted.
|
||||
```lua
|
||||
lume.count({a = 2, b = 3, c = 4, d = 5}) -- Returns 4
|
||||
lume.count({1, 2, 4, 6}, function(x) return x % 2 == 0 end) -- Returns 3
|
||||
```
|
||||
|
||||
### lume.slice(t [, i [, j]])
|
||||
Mimics the behaviour of Lua's `string.sub`, but operates on an array rather
|
||||
than a string. Creates and returns a new array of the given slice.
|
||||
@@ -163,6 +231,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.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)
|
||||
Returns a copy of the table where the keys have become the values and the
|
||||
values the keys.
|
||||
@@ -170,6 +252,15 @@ 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.
|
||||
|
||||
### lume.clone(t)
|
||||
Returns a shallow copy of the table `t`.
|
||||
|
||||
@@ -193,6 +284,32 @@ f() -- Prints "Hello"
|
||||
f() -- Does nothing
|
||||
```
|
||||
|
||||
### lume.memoize(fn)
|
||||
Returns a wrapper function to `fn` where the results for any given set of
|
||||
arguments are cached. `lume.memoize()` is useful when used on functions with
|
||||
slow-running computations.
|
||||
```lua
|
||||
fib = lume.memoize(function(n) return n < 2 and n or fib(n-1) + fib(n-2) end)
|
||||
```
|
||||
|
||||
### lume.combine(...)
|
||||
Creates a wrapper function which calls each supplied argument in the order they
|
||||
were passed to `lume.combine()`; nil arguments are ignored. The wrapper
|
||||
function passes its own arguments to each of its wrapped functions when it is
|
||||
called.
|
||||
```lua
|
||||
local f = lume.combine(function(a, b) print(a + b) end,
|
||||
function(a, b) print(a * b) end)
|
||||
f(3, 4) -- Prints "7" then "12" on a new line
|
||||
```
|
||||
|
||||
### lume.call(fn, ...)
|
||||
Calls the given function with the provided arguments and returns its values. If
|
||||
`fn` is `nil` then no action is performed and the function returns `nil`.
|
||||
```lua
|
||||
lume.call(print, "Hello world") -- Prints "Hello world"
|
||||
```
|
||||
|
||||
### lume.time(fn, ...)
|
||||
Inserts the arguments into function `fn` and calls it. Returns the time in
|
||||
seconds the function `fn` took to execute followed by `fn`'s returned values.
|
||||
@@ -202,7 +319,7 @@ lume.time(function(x) return x end, "hello") -- Returns 0, "hello"
|
||||
|
||||
### lume.lambda(str)
|
||||
Takes a string lambda and returns a function. `str` should be a list of
|
||||
comma-seperated parameters, followed by `->`, followed by the expression which
|
||||
comma-separated parameters, followed by `->`, followed by the expression which
|
||||
will be evaluated and returned.
|
||||
```lua
|
||||
local f = lume.lambda "x,y -> 2*x+y"
|
||||
@@ -212,7 +329,7 @@ f(10, 5) -- Returns 25
|
||||
### lume.serialize(x)
|
||||
Serializes the argument `x` into a string which can be loaded again using
|
||||
`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.
|
||||
```lua
|
||||
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
||||
@@ -227,11 +344,12 @@ lume.deserialize("{1, 2, 3}") -- Returns {1, 2, 3}
|
||||
```
|
||||
|
||||
### lume.split(str [, sep])
|
||||
Splits the string `str` into words and returns a table of the sub strings. If
|
||||
`sep` is provided the string will be split at any of the characters in `sep`
|
||||
instead of on whitespace.
|
||||
Returns an array of the words in the string `str`. If `sep` is provided it is
|
||||
used as the delimiter, consecutive delimiters are not grouped together and will
|
||||
delimit empty strings.
|
||||
```lua
|
||||
lume.split("One two three") -- Returns {"One", "two", "three"}
|
||||
lume.split("a,b,,c", ",") -- Returns {"a", "b", "", "c"}
|
||||
```
|
||||
|
||||
### lume.trim(str [, chars])
|
||||
@@ -242,6 +360,15 @@ instead of whitespace.
|
||||
lume.trim(" Hello ") -- Returns "Hello"
|
||||
```
|
||||
|
||||
### lume.wordwrap(str [, limit])
|
||||
Returns `str` wrapped to `limit` number of characters per line, by default
|
||||
`limit` is `72`. `limit` can also be a function which when passed a string,
|
||||
returns `true` if it is too long for a single line.
|
||||
```lua
|
||||
-- Returns "Hello world\nThis is a\nshort string"
|
||||
lume.wordwrap("Hello world. This is a short string", 14)
|
||||
```
|
||||
|
||||
### lume.format(str [, vars])
|
||||
Returns a formatted string. The values of keys in the table `vars` can be
|
||||
inserted into the string by using the form `"{key}"` in `str`; numerical keys
|
||||
@@ -256,7 +383,7 @@ Prints the current filename and line number followed by each argument separated
|
||||
by a space.
|
||||
```lua
|
||||
-- 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)
|
||||
@@ -265,6 +392,10 @@ Executes the lua code inside `str`.
|
||||
lume.dostring("print('Hello!')") -- Prints "Hello!"
|
||||
```
|
||||
|
||||
### lume.uuid()
|
||||
Generates a random UUID string; version 4 as specified in
|
||||
[RFC 4122](http://www.ietf.org/rfc/rfc4122.txt).
|
||||
|
||||
### lume.hotswap(modname)
|
||||
Reloads an already loaded module in place, allowing you to immediately see the
|
||||
effects of code changes without having to restart the program. `modname` should
|
||||
@@ -276,6 +407,28 @@ lume.hotswap("lume") -- Reloads the lume module
|
||||
assert(lume.hotswap("inexistant_module")) -- Raises an error
|
||||
```
|
||||
|
||||
### lume.ripairs(t)
|
||||
Performs the same function as `ipairs()` but iterates in reverse; this allows
|
||||
the removal of items from the table during iteration without any items being
|
||||
skipped.
|
||||
```lua
|
||||
-- Prints "3->c", "2->b" and "1->a" on separate lines
|
||||
for i, v in lume.ripairs({ "a", "b", "c" }) do
|
||||
print(i .. "->" .. v)
|
||||
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
|
||||
@@ -284,6 +437,52 @@ arguments to [LÖVE](http://love2d.org)'s setColor() function.
|
||||
lume.rgba(0xFF304050) -- Returns 48, 64, 80, 255
|
||||
```
|
||||
|
||||
### lume.chain(value)
|
||||
Returns a wrapped object which allows chaining of lume functions. The function
|
||||
result() should be called at the end of the chain to return the resulting
|
||||
value.
|
||||
```lua
|
||||
lume.chain({1, 2, 3, 4})
|
||||
:filter(function(x) return x % 2 == 0 end)
|
||||
:map(function(x) return -x end)
|
||||
: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
|
||||
```
|
||||
|
||||
## 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
|
||||
|
||||
|
545
lume.lua
545
lume.lua
@@ -1,27 +1,72 @@
|
||||
--
|
||||
-- lume
|
||||
--
|
||||
-- Copyright (c) 2014, 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 = "1.2.0" }
|
||||
local lume = { _version = "2.2.3" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
local tostring, tonumber = tostring, tonumber
|
||||
local math_floor = math.floor
|
||||
local math_ceil = math.ceil
|
||||
local math_random = math.random
|
||||
local math_cos = math.cos
|
||||
local math_atan2 = math.atan2
|
||||
local math_atan2 = math.atan2 or math.atan
|
||||
local math_sqrt = math.sqrt
|
||||
local math_abs = math.abs
|
||||
local math_min = math.min
|
||||
local math_max = math.max
|
||||
local math_pi = math.pi
|
||||
|
||||
local noop = function()
|
||||
end
|
||||
|
||||
local identity = function(x)
|
||||
return x
|
||||
end
|
||||
|
||||
local patternescape = function(str)
|
||||
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
|
||||
end
|
||||
|
||||
local absindex = function(len, i)
|
||||
return i < 0 and (len + i + 1) or i
|
||||
end
|
||||
|
||||
local iscallable = function(x)
|
||||
if type(x) == "function" then return true end
|
||||
local mt = getmetatable(x)
|
||||
return mt and mt.__call ~= nil
|
||||
end
|
||||
|
||||
local isarray = function(x)
|
||||
return (type(x) == "table" and x[1] ~= nil) and true or false
|
||||
end
|
||||
|
||||
local getiter = function(x)
|
||||
if isarray(x) then
|
||||
return ipairs
|
||||
elseif type(x) == "table" then
|
||||
return pairs
|
||||
end
|
||||
error("expected table", 3)
|
||||
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)
|
||||
@@ -31,7 +76,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
|
||||
|
||||
|
||||
@@ -46,7 +91,8 @@ end
|
||||
|
||||
|
||||
function lume.smooth(a, b, amount)
|
||||
local m = (1 - math_cos(lume.clamp(amount, 0, 1) * math_pi)) / 2
|
||||
local t = lume.clamp(amount, 0, 1)
|
||||
local m = t * t * (3 - 2 * t)
|
||||
return a + (b - a) * m
|
||||
end
|
||||
|
||||
@@ -83,7 +129,7 @@ end
|
||||
|
||||
function lume.weightedchoice(t)
|
||||
local sum = 0
|
||||
for k, v in pairs(t) do
|
||||
for _, v in pairs(t) do
|
||||
assert(v >= 0, "weight value less than zero")
|
||||
sum = sum + v
|
||||
end
|
||||
@@ -96,42 +142,113 @@ function lume.weightedchoice(t)
|
||||
end
|
||||
|
||||
|
||||
function lume.shuffle(t)
|
||||
for i = 1, #t do
|
||||
local r = math_random(#t)
|
||||
t[i], t[r] = t[r], t[i]
|
||||
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 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
|
||||
local r = math_random(i)
|
||||
if r ~= i then
|
||||
rtn[i] = rtn[r]
|
||||
end
|
||||
rtn[r] = t[i]
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.sort(t, comp)
|
||||
local rtn = lume.clone(t)
|
||||
if comp then
|
||||
if type(comp) == "string" then
|
||||
table.sort(rtn, function(a, b) return a[comp] < b[comp] end)
|
||||
else
|
||||
table.sort(rtn, comp)
|
||||
end
|
||||
else
|
||||
table.sort(rtn)
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.array(...)
|
||||
local t = {}
|
||||
for x in unpack({...}) do t[#t + 1] = x end
|
||||
for x in ... do t[#t + 1] = x end
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
function lume.each(t, fn, ...)
|
||||
local iter = getiter(t)
|
||||
if type(fn) == "string" then
|
||||
for _, v in pairs(t) do v[fn](v, ...) end
|
||||
for _, v in iter(t) do v[fn](v, ...) end
|
||||
else
|
||||
for _, v in pairs(t) do fn(v, ...) end
|
||||
for _, v in iter(t) do fn(v, ...) end
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
|
||||
function lume.map(t, fn)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[k] = fn(v) end
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
local rtn = {}
|
||||
for k, v in iter(t) do rtn[k] = fn(v) end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.all(t, fn)
|
||||
fn = fn or function(x) return x end
|
||||
for k, v in pairs(t) do
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
for _, v in iter(t) do
|
||||
if not fn(v) then return false end
|
||||
end
|
||||
return true
|
||||
@@ -139,8 +256,9 @@ end
|
||||
|
||||
|
||||
function lume.any(t, fn)
|
||||
fn = fn or function(x) return x end
|
||||
for k, v in pairs(t) do
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
for _, v in iter(t) do
|
||||
if fn(v) then return true end
|
||||
end
|
||||
return false
|
||||
@@ -148,59 +266,153 @@ 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 _, 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
|
||||
|
||||
|
||||
function lume.set(t, retainkeys)
|
||||
function lume.set(t)
|
||||
local rtn = {}
|
||||
for k, v in pairs(lume.invert(t)) do
|
||||
rtn[retainkeys and v or (#rtn + 1)] = k
|
||||
for k in pairs(lume.invert(t)) do
|
||||
rtn[#rtn + 1] = k
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.filter(t, fn, retainkeys)
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
local rtn = {}
|
||||
for k, v in pairs(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 _, v in iter(t) do
|
||||
if fn(v) then rtn[#rtn + 1] = v end
|
||||
end
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.merge(t, t2, retainkeys)
|
||||
for k, v in pairs(t2) do
|
||||
t[retainkeys and k or (#t + 1)] = v
|
||||
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 _, v in iter(t) do
|
||||
if not fn(v) then rtn[#rtn + 1] = v end
|
||||
end
|
||||
end
|
||||
return t
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.merge(...)
|
||||
local rtn = {}
|
||||
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
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.concat(...)
|
||||
local rtn = {}
|
||||
for i = 1, select("#", ...) do
|
||||
local t = select(i, ...)
|
||||
if t ~= nil then
|
||||
local iter = getiter(t)
|
||||
for _, v in iter(t) do
|
||||
rtn[#rtn + 1] = v
|
||||
end
|
||||
end
|
||||
end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.find(t, value)
|
||||
for k, v in pairs(t) do
|
||||
local iter = getiter(t)
|
||||
for k, v in iter(t) do
|
||||
if v == value then return k end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
function lume.match(t, fn)
|
||||
fn = iteratee(fn)
|
||||
local iter = getiter(t)
|
||||
for k, v in iter(t) do
|
||||
if fn(v) then return v, k end
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
|
||||
function lume.count(t, fn)
|
||||
local count = 0
|
||||
local iter = getiter(t)
|
||||
if fn then
|
||||
fn = iteratee(fn)
|
||||
for _, v in iter(t) do
|
||||
if fn(v) then count = count + 1 end
|
||||
end
|
||||
else
|
||||
if isarray(t) then
|
||||
return #t
|
||||
end
|
||||
for _ in iter(t) do count = count + 1 end
|
||||
end
|
||||
return count
|
||||
end
|
||||
|
||||
|
||||
function lume.slice(t, i, j)
|
||||
local function index(x) return x < 0 and (#t + x + 1) or x end
|
||||
i = i and index(i) or 1
|
||||
j = j and index(j) or #t
|
||||
i = i and absindex(#t, i) or 1
|
||||
j = j and absindex(#t, j) or #t
|
||||
local rtn = {}
|
||||
for i = math_max(i, 1), math_min(j, #t) do
|
||||
rtn[#rtn + 1] = t[i]
|
||||
for x = i < 1 and 1 or i, j > #t and #t or j do
|
||||
rtn[#rtn + 1] = t[x]
|
||||
end
|
||||
return rtn
|
||||
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)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[v] = k end
|
||||
@@ -208,6 +420,24 @@ 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)
|
||||
for k in iter(t) do rtn[#rtn + 1] = k end
|
||||
return rtn
|
||||
end
|
||||
|
||||
|
||||
function lume.clone(t)
|
||||
local rtn = {}
|
||||
for k, v in pairs(t) do rtn[k] = v end
|
||||
@@ -216,20 +446,69 @@ end
|
||||
|
||||
|
||||
function lume.fn(fn, ...)
|
||||
local args = {...}
|
||||
assert(iscallable(fn), "expected a function as the first argument")
|
||||
local args = { ... }
|
||||
return function(...)
|
||||
local a = lume.merge(lume.clone(args), {...})
|
||||
return fn(unpack(a))
|
||||
local a = lume.concat(args, { ... })
|
||||
return fn(unpack(a))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function lume.once(fn, ...)
|
||||
local fn = lume.fn(fn, ...)
|
||||
local f = lume.fn(fn, ...)
|
||||
local done = false
|
||||
return function(...)
|
||||
if done then return end
|
||||
done = true
|
||||
return f(...)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
local memoize_fnkey = {}
|
||||
local memoize_nil = {}
|
||||
|
||||
function lume.memoize(fn)
|
||||
local cache = {}
|
||||
return function(...)
|
||||
local c = cache
|
||||
for i = 1, select("#", ...) do
|
||||
local a = select(i, ...) or memoize_nil
|
||||
c[a] = c[a] or {}
|
||||
c = c[a]
|
||||
end
|
||||
c[memoize_fnkey] = c[memoize_fnkey] or {fn(...)}
|
||||
return unpack(c[memoize_fnkey])
|
||||
end
|
||||
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, n do
|
||||
local fn = select(i, ...)
|
||||
if fn ~= nil then
|
||||
assert(iscallable(fn), "expected a function or nil")
|
||||
funcs[#funcs + 1] = fn
|
||||
end
|
||||
end
|
||||
return function(...)
|
||||
for _, f in ipairs(funcs) do f(...) end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function lume.call(fn, ...)
|
||||
if fn then
|
||||
return fn(...)
|
||||
end
|
||||
end
|
||||
@@ -255,19 +534,41 @@ function lume.lambda(str)
|
||||
end
|
||||
|
||||
|
||||
function lume.serialize(x)
|
||||
local f = { string = function(v) return string.format("%q", v) end,
|
||||
number = tostring, boolean = tostring }
|
||||
f.table = function(t)
|
||||
local serialize
|
||||
|
||||
local serialize_map = {
|
||||
[ "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 = {}
|
||||
stk[t] = true
|
||||
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
|
||||
return "{" .. table.concat(rtn) .. "}"
|
||||
stk[t] = nil
|
||||
return "{" .. table.concat(rtn, ",") .. "}"
|
||||
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(_, 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
|
||||
|
||||
|
||||
@@ -277,20 +578,59 @@ end
|
||||
|
||||
|
||||
function lume.split(str, sep)
|
||||
return lume.array(str:gmatch("([^" .. (sep or "%s") .. "]+)"))
|
||||
if not sep then
|
||||
return lume.array(str:gmatch("([%S]+)"))
|
||||
else
|
||||
assert(sep ~= "", "empty separator")
|
||||
local psep = patternescape(sep)
|
||||
return lume.array((str..sep):gmatch("(.-)("..psep..")"))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
function lume.trim(str, chars)
|
||||
chars = chars or "%s"
|
||||
if not chars then return str:match("^[%s]*(.-)[%s]*$") end
|
||||
chars = patternescape(chars)
|
||||
return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$")
|
||||
end
|
||||
|
||||
|
||||
function lume.wordwrap(str, limit)
|
||||
limit = limit or 72
|
||||
local check
|
||||
if type(limit) == "number" then
|
||||
check = function(s) return #s >= limit end
|
||||
else
|
||||
check = limit
|
||||
end
|
||||
local rtn = {}
|
||||
local line = ""
|
||||
for word, spaces in str:gmatch("(%S+)(%s*)") do
|
||||
local s = line .. word
|
||||
if check(s) then
|
||||
table.insert(rtn, line .. "\n")
|
||||
line = word
|
||||
else
|
||||
line = s
|
||||
end
|
||||
for c in spaces:gmatch(".") do
|
||||
if c == "\n" then
|
||||
table.insert(rtn, line .. "\n")
|
||||
line = ""
|
||||
else
|
||||
line = line .. c
|
||||
end
|
||||
end
|
||||
end
|
||||
table.insert(rtn, line)
|
||||
return table.concat(rtn)
|
||||
end
|
||||
|
||||
|
||||
function lume.format(str, vars)
|
||||
if not vars then return str end
|
||||
local f = function(x)
|
||||
return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}")
|
||||
local f = function(x)
|
||||
return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}")
|
||||
end
|
||||
return (str:gsub("{(.-)}", f))
|
||||
end
|
||||
@@ -298,8 +638,15 @@ end
|
||||
|
||||
function lume.trace(...)
|
||||
local info = debug.getinfo(2, "Sl")
|
||||
local head = "[" .. info.short_src .. ":" .. info.currentline .. "] "
|
||||
print(head .. table.concat(lume.map({...}, tostring), " "))
|
||||
local t = { info.short_src .. ":" .. info.currentline .. ":" }
|
||||
for i = 1, select("#", ...) do
|
||||
local x = select(i, ...)
|
||||
if type(x) == "number" then
|
||||
x = string.format("%g", lume.round(x, .01))
|
||||
end
|
||||
t[#t + 1] = tostring(x)
|
||||
end
|
||||
print(table.concat(t, " "))
|
||||
end
|
||||
|
||||
|
||||
@@ -308,11 +655,21 @@ function lume.dostring(str)
|
||||
end
|
||||
|
||||
|
||||
function lume.uuid()
|
||||
local fn = function(x)
|
||||
local r = math_random(16) - 1
|
||||
r = (x == "x") and (r + 1) or (r % 4) + 9
|
||||
return ("0123456789abcdef"):sub(r, r)
|
||||
end
|
||||
return (("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"):gsub("[xy]", fn))
|
||||
end
|
||||
|
||||
|
||||
function lume.hotswap(modname)
|
||||
local oldglobal = lume.clone(_G)
|
||||
local updated = {}
|
||||
local function update(old, new)
|
||||
if updated[old] then return end
|
||||
if updated[old] then return end
|
||||
updated[old] = true
|
||||
local oldmt, newmt = getmetatable(old), getmetatable(new)
|
||||
if oldmt and newmt then update(oldmt, newmt) end
|
||||
@@ -322,7 +679,7 @@ function lume.hotswap(modname)
|
||||
end
|
||||
local err = nil
|
||||
local function onerror(e)
|
||||
for k, v in pairs(_G) do _G[k] = oldglobal[k] end
|
||||
for k in pairs(_G) do _G[k] = oldglobal[k] end
|
||||
err = lume.trim(e)
|
||||
end
|
||||
local ok, oldmod = pcall(require, modname)
|
||||
@@ -332,7 +689,7 @@ function lume.hotswap(modname)
|
||||
local newmod = require(modname)
|
||||
if type(oldmod) == "table" then update(oldmod, newmod) end
|
||||
for k, v in pairs(oldglobal) do
|
||||
if v ~= _G[k] and type(v) == "table" then
|
||||
if v ~= _G[k] and type(v) == "table" then
|
||||
update(v, _G[k])
|
||||
_G[k] = v
|
||||
end
|
||||
@@ -344,13 +701,67 @@ function lume.hotswap(modname)
|
||||
end
|
||||
|
||||
|
||||
local ripairs_iter = function(t, i)
|
||||
i = i - 1
|
||||
local v = t[i]
|
||||
if v then return i, v end
|
||||
end
|
||||
|
||||
function lume.ripairs(t)
|
||||
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 / 2 ^ 24) % 256)
|
||||
local r = math_floor((color / 2 ^ 16) % 256)
|
||||
local g = math_floor((color / 2 ^ 08) % 256)
|
||||
local a = math_floor((color / 16777216) % 256)
|
||||
local r = math_floor((color / 65536) % 256)
|
||||
local g = math_floor((color / 256) % 256)
|
||||
local b = math_floor((color) % 256)
|
||||
return r, g, b, a
|
||||
end
|
||||
|
||||
|
||||
local chain_mt = {}
|
||||
chain_mt.__index = lume.map(lume.filter(lume, iscallable, true),
|
||||
function(fn)
|
||||
return function(self, ...)
|
||||
self._value = fn(self._value, ...)
|
||||
return self
|
||||
end
|
||||
end)
|
||||
chain_mt.__index.result = function(x) return x._value end
|
||||
|
||||
function lume.chain(value)
|
||||
return setmetatable({ _value = value }, chain_mt)
|
||||
end
|
||||
|
||||
setmetatable(lume, {
|
||||
__call = function(_, ...)
|
||||
return lume.chain(...)
|
||||
end
|
||||
})
|
||||
|
||||
|
||||
return lume
|
||||
|
@@ -1,4 +1,4 @@
|
||||
local tester = require "tester"
|
||||
local tester = require "util.tester"
|
||||
|
||||
package.path = "../?.lua;" .. package.path
|
||||
|
||||
@@ -106,15 +106,76 @@ 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}
|
||||
lume.shuffle(t)
|
||||
t = lume.shuffle(t)
|
||||
table.sort(t)
|
||||
testeq( t, {1, 2, 3, 4, 5} )
|
||||
testeq( lume.shuffle({}), {} )
|
||||
end
|
||||
|
||||
-- lume.sort
|
||||
tests["lume.sort"] = function()
|
||||
local t = { 1, 5, 2, 4, 3 }
|
||||
local fn = function(a, b) return a > b end
|
||||
testeq( t == lume.sort(t), false )
|
||||
testeq( lume.sort(t), { 1, 2, 3, 4, 5 } )
|
||||
testeq( lume.sort(t, fn), { 5, 4, 3, 2, 1 } )
|
||||
testeq( t, { 1, 5, 2, 4, 3 } )
|
||||
local t = { { id = 2 }, { id = 3 }, { id = 1 } }
|
||||
testeq( lume.sort(t, "id"), { { id = 1 }, { id = 2 }, { id = 3 } })
|
||||
end
|
||||
|
||||
-- lume.array
|
||||
tests["lume.array"] = function()
|
||||
local t = lume.array(pairs({a=0, b=0, c=0}))
|
||||
@@ -141,6 +202,8 @@ end
|
||||
tests["lume.map"] = function()
|
||||
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} )
|
||||
local t = {{ id = 10 }, { id = 20 }, { id = 30 }}
|
||||
testeq( lume.map(t, "id"), { 10, 20, 30 })
|
||||
end
|
||||
|
||||
-- lume.all
|
||||
@@ -149,6 +212,10 @@ tests["lume.all"] = function()
|
||||
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, 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
|
||||
|
||||
-- lume.any
|
||||
@@ -157,6 +224,9 @@ tests["lume.any"] = function()
|
||||
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({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
|
||||
|
||||
-- lume.reduce
|
||||
@@ -170,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
|
||||
@@ -189,12 +262,33 @@ tests["lume.filter"] = function()
|
||||
testeq( t, {2, 4} )
|
||||
local t = lume.filter({a=1, b=2, c=3}, function(x) return x == 2 end, true)
|
||||
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
|
||||
|
||||
-- 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({1, 2, 3}, {8, 9, 0}), {1, 2, 3, 8, 9, 0} )
|
||||
testeq( lume.merge({a=1, b=2}, {b=3, c=4}, true), {a=1, b=3, c=4} )
|
||||
testeq( lume.merge(), {} )
|
||||
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
|
||||
|
||||
-- lume.concat
|
||||
tests["lume.concat"] = function()
|
||||
testeq( lume.concat(nil), {} )
|
||||
testeq( lume.concat({1, 2, 3}), {1, 2, 3} )
|
||||
testeq( lume.concat({1, 2, 3}, {4, 5, 6}), {1, 2, 3, 4, 5, 6} )
|
||||
testeq( lume.concat({1, 2, 3}, {4, 5, 6}, nil, {7}), {1, 2, 3, 4, 5, 6, 7} )
|
||||
end
|
||||
|
||||
-- lume.find
|
||||
@@ -204,6 +298,40 @@ tests["lume.find"] = function()
|
||||
testeq( lume.find({a=1, b=5, c=7}, 5), "b" )
|
||||
end
|
||||
|
||||
-- lume.match
|
||||
tests["lume.match"] = function()
|
||||
local t = { "a", "b", "c", "d" }
|
||||
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)
|
||||
testeq( v, "d" )
|
||||
testeq( k, 4 )
|
||||
local v, k = lume.match(t, function(x) return x < "b" end)
|
||||
testeq( v, "a" )
|
||||
testeq( k, 1 )
|
||||
local v, k = lume.match(t2, function(x) return x < 2 end)
|
||||
testeq( v, 1 )
|
||||
testeq( k, "a" )
|
||||
local v, k = lume.match(t2, function(x) return x > 5 end)
|
||||
testeq( v, nil )
|
||||
testeq( k, nil )
|
||||
local v, k = lume.match(t3, { x = 3, y = 4 })
|
||||
testeq( k, 2 )
|
||||
end
|
||||
|
||||
-- lume.count
|
||||
tests["lume.count"] = function()
|
||||
local t = { a = 1, b = 2, c = 5, [13] = 22, z = 8 }
|
||||
testeq( lume.count(t), 5 )
|
||||
testeq( lume.count(t, function(x) return x % 2 == 0 end ), 3 )
|
||||
local a = { 5, 6, 7, 8, 9 }
|
||||
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
|
||||
|
||||
-- lume.slice
|
||||
tests["lume.slice"] = function()
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 2, 4), {"b", "c", "d"} )
|
||||
@@ -218,16 +346,52 @@ tests["lume.slice"] = function()
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 0, 1), {"a"} )
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, 0, 0), {} )
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3), {"c", "d", "e"} )
|
||||
testeq( lume.slice({"a", "b", "c", "d", "e"}, -3, 900), {"c", "d", "e"} )
|
||||
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
|
||||
tests["lume_invert"] = function()
|
||||
tests["lume.invert"] = function()
|
||||
testeq( lume.invert({}), {} )
|
||||
testeq( lume.invert{a = "x", b = "y"}, {x = "a", y = "b"} )
|
||||
testeq( lume.invert{a = 1, b = 2}, {"a", "b"} )
|
||||
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({}), {} )
|
||||
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
|
||||
tests["lume.clone"] = function()
|
||||
local t = {6, 7, 4, 5}
|
||||
@@ -240,6 +404,7 @@ end
|
||||
tests["lume.fn"] = function()
|
||||
local f = lume.fn(function(a, b) return a + b end, 10)
|
||||
testeq( f(5), 15 )
|
||||
tester.test.error( lume.fn, 123 )
|
||||
end
|
||||
|
||||
-- lume.once
|
||||
@@ -247,6 +412,49 @@ tests["lume.once"] = function()
|
||||
local f = lume.once(function(a, b) return a + b end, 10)
|
||||
testeq( f(5), 15 )
|
||||
testeq( f(5), nil )
|
||||
tester.test.error( lume.once, 123 )
|
||||
end
|
||||
|
||||
-- lume.memoize
|
||||
tests["lume.memoize"] = function()
|
||||
local f = lume.memoize(
|
||||
function(a, b, c)
|
||||
return tostring(a) .. tostring(b) .. tostring(c)
|
||||
end)
|
||||
testeq( f("hello", nil, 15), "hellonil15" )
|
||||
testeq( f("hello", nil, 15), "hellonil15" )
|
||||
testeq( f(), "nilnilnil" )
|
||||
testeq( f(), "nilnilnil" )
|
||||
local f2 = lume.memoize(function() end)
|
||||
testeq( f2(), nil )
|
||||
end
|
||||
|
||||
-- lume.combine
|
||||
tests["lume.combine"] = function()
|
||||
local acc = 0
|
||||
local a = function(x, y) acc = acc + x + y end
|
||||
local b = function(x, y) acc = acc + x * y end
|
||||
local fn = lume.combine(a, b)
|
||||
fn(10, 20)
|
||||
testeq( acc, 230 )
|
||||
acc = 0
|
||||
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
|
||||
tests["lume.call"] = function()
|
||||
local add = function(a, b) return a + b end
|
||||
testeq( lume.call(), nil )
|
||||
testeq( lume.call(nil, 1, 2, 3), nil )
|
||||
testeq( lume.call(add, 1, 2), 3 )
|
||||
end
|
||||
|
||||
-- lume.time
|
||||
@@ -276,13 +484,23 @@ tests["lume.serialize, lume.deserialize"] = function()
|
||||
local t = { 1, 2, 3, 4, true, false, "cat", "dog", {1, 2, 3} }
|
||||
local s = lume.serialize(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
|
||||
|
||||
-- lume.split
|
||||
tests["lume.split"] = function()
|
||||
testeq( lume.split("cat dog pig"), {"cat", "dog", "pig"} )
|
||||
testeq( lume.split(",cat,dog,pig", ","), {"cat", "dog", "pig"} )
|
||||
testeq( lume.split(",cat,dog;pig", ",;"), {"cat", "dog", "pig"} )
|
||||
testeq( lume.split("cat dog pig"), {"cat", "dog", "pig"} )
|
||||
testeq( lume.split("cat,dog,pig", ","), {"cat", "dog", "pig"} )
|
||||
testeq( lume.split("cat,dog;pig", ";"), {"cat,dog", "pig"} )
|
||||
testeq( lume.split("cat,dog,,pig", ","), {"cat", "dog", "", "pig"} )
|
||||
testeq( lume.split(";;;cat;", ";"), {"", "", "", "cat", ""} )
|
||||
testeq( lume.split("cat.dog", "."), {"cat", "dog"} )
|
||||
testeq( lume.split("cat%dog", "%"), {"cat", "dog"} )
|
||||
testeq( lume.split("1<>2<>3", "<>"), {"1", "2", "3"} )
|
||||
tester.test.error( lume.split, "abc", "" )
|
||||
end
|
||||
|
||||
-- lume.trim
|
||||
@@ -290,6 +508,18 @@ tests["lume.trim"] = function()
|
||||
testeq( lume.trim(" hello world "), "hello world" )
|
||||
testeq( lume.trim("-=-hello-world===", "-="), "hello-world" )
|
||||
testeq( lume.trim("***hello world*-*", "*"), "hello world*-" )
|
||||
testeq( lume.trim("...hello world.", "."), "hello world" )
|
||||
testeq( lume.trim("^.hello world]^", "^.]"), "hello world" )
|
||||
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 fn = function(str) return #str >= 20 end
|
||||
testeq( lume.wordwrap(str), str )
|
||||
testeq( lume.wordwrap(str, 20), b )
|
||||
testeq( lume.wordwrap(str, fn), b )
|
||||
end
|
||||
|
||||
-- lume.format
|
||||
@@ -309,13 +539,13 @@ tests["lume.trace"] = function()
|
||||
local oldprint = print
|
||||
local file, line, msg
|
||||
print = function(x)
|
||||
file, line, msg = x:match("%[(.-):(.-)%] (.*)")
|
||||
file, line, msg = x:match("(.-):(.-): (.*)")
|
||||
end
|
||||
lume.trace("Hi world")
|
||||
lume.trace("Hi world", 123.456, 1, nil)
|
||||
print = oldprint
|
||||
testeq( file:match(".lua$"), ".lua" )
|
||||
testeq( tonumber(line) ~= nil, true )
|
||||
testeq( msg, "Hi world" )
|
||||
testeq( file:match(".lua$"), ".lua" )
|
||||
testeq( tonumber(line) ~= nil, true )
|
||||
testeq( msg, "Hi world 123.46 1 nil" )
|
||||
end
|
||||
|
||||
-- lume.dostring
|
||||
@@ -324,6 +554,12 @@ tests["lume.dostring"] = function()
|
||||
testeq( lume.dostring([[return 12345]]), 12345 )
|
||||
end
|
||||
|
||||
-- lume.uuid
|
||||
tests["lume.uuid"] = function()
|
||||
testeq( type(lume.uuid()), "string" )
|
||||
testeq( #lume.uuid(), 36 )
|
||||
end
|
||||
|
||||
-- lume.hotswap
|
||||
tests["lume.hotswap"] = function()
|
||||
local ok, err = lume.hotswap("bad_module_name")
|
||||
@@ -331,6 +567,34 @@ tests["lume.hotswap"] = function()
|
||||
testeq( type(err), "string" )
|
||||
end
|
||||
|
||||
-- lume.ripairs
|
||||
tests["lume.ripairs"] = function()
|
||||
local t = { "a", "b", "c" }
|
||||
local r = {}
|
||||
for i, v in lume.ripairs(t) do
|
||||
table.insert(r, { i, v })
|
||||
end
|
||||
testeq( r, { { 3, "c" }, { 2, "b" }, { 1, "a" } })
|
||||
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
|
||||
tests["lume.rgba"] = function()
|
||||
local r, g, b, a = lume.rgba(0x12345678)
|
||||
@@ -340,6 +604,15 @@ tests["lume.rgba"] = function()
|
||||
testeq( b, 0x78 )
|
||||
end
|
||||
|
||||
-- lume.chain
|
||||
tests["lume.chain"] = function()
|
||||
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()
|
||||
testeq( t, { 2, 4 } )
|
||||
testeq( lume.chain(10):result(), 10 )
|
||||
local t = lume({1, 2}):map(function(x) return x * 2 end):result()
|
||||
testeq( t, { 2, 4 } )
|
||||
end
|
||||
|
||||
|
||||
tester.dotests(tests)
|
||||
tester.test.global()
|
Reference in New Issue
Block a user