mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-29 11:32:20 +00:00
Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
89931c1ad8 | ||
|
9ff4637201 | ||
|
617729e261 | ||
|
ad64d7af05 | ||
|
d69f419f5a | ||
|
ad34f6ce33 | ||
|
5882ca1303 | ||
|
828d23e6f6 | ||
|
13a8edb2e7 | ||
|
425a52b99f |
11
README.md
11
README.md
@@ -130,7 +130,7 @@ 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
|
`retainkeys` is true the table is not treated as an array and retains its
|
||||||
original keys.
|
original keys.
|
||||||
```lua
|
```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])
|
### lume.filter(t, fn [, retainkeys])
|
||||||
@@ -202,7 +202,7 @@ lume.time(function(x) return x end, "hello") -- Returns 0, "hello"
|
|||||||
|
|
||||||
### lume.lambda(str)
|
### lume.lambda(str)
|
||||||
Takes a string lambda and returns a function. `str` should be a list of
|
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.
|
will be evaluated and returned.
|
||||||
```lua
|
```lua
|
||||||
local f = lume.lambda "x,y -> 2*x+y"
|
local f = lume.lambda "x,y -> 2*x+y"
|
||||||
@@ -227,11 +227,12 @@ lume.deserialize("{1, 2, 3}") -- Returns {1, 2, 3}
|
|||||||
```
|
```
|
||||||
|
|
||||||
### lume.split(str [, sep])
|
### lume.split(str [, sep])
|
||||||
Splits the string `str` into words and returns a table of the sub strings. If
|
Returns an array of the words in the string `str`. If `sep` is provided it is
|
||||||
`sep` is provided the string will be split at any of the characters in `sep`
|
used as the delimiter, consecutive delimiters are not grouped together and will
|
||||||
instead of on whitespace.
|
delimit empty strings.
|
||||||
```lua
|
```lua
|
||||||
lume.split("One two three") -- Returns {"One", "two", "three"}
|
lume.split("One two three") -- Returns {"One", "two", "three"}
|
||||||
|
lume.split("a,b,,c", ",") -- Returns {"a", "b", "", "c"}
|
||||||
```
|
```
|
||||||
|
|
||||||
### lume.trim(str [, chars])
|
### lume.trim(str [, chars])
|
||||||
|
17
lume.lua
17
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.2.0" }
|
local lume = { _version = "1.2.1" }
|
||||||
|
|
||||||
local pairs, ipairs = pairs, ipairs
|
local pairs, ipairs = pairs, ipairs
|
||||||
local type, assert, unpack = type, assert, unpack
|
local type, assert, unpack = type, assert, unpack
|
||||||
@@ -23,6 +23,10 @@ local math_min = math.min
|
|||||||
local math_max = math.max
|
local math_max = math.max
|
||||||
local math_pi = math.pi
|
local math_pi = math.pi
|
||||||
|
|
||||||
|
local patternescape = function(str)
|
||||||
|
return str:gsub("[%(%)%.%%%+%-%*%?%[%]%^%$]", "%%%1")
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.clamp(x, min, max)
|
function lume.clamp(x, min, max)
|
||||||
return x < min and min or (x > max and max or x)
|
return x < min and min or (x > max and max or x)
|
||||||
@@ -277,12 +281,19 @@ end
|
|||||||
|
|
||||||
|
|
||||||
function lume.split(str, sep)
|
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
|
end
|
||||||
|
|
||||||
|
|
||||||
function lume.trim(str, chars)
|
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 .. "]*$")
|
return str:match("^[" .. chars .. "]*(.-)[" .. chars .. "]*$")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@@ -281,8 +281,14 @@ end
|
|||||||
-- lume.split
|
-- lume.split
|
||||||
tests["lume.split"] = function()
|
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
|
end
|
||||||
|
|
||||||
-- lume.trim
|
-- lume.trim
|
||||||
@@ -290,6 +296,8 @@ 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*-" )
|
testeq( lume.trim("***hello world*-*", "*"), "hello world*-" )
|
||||||
|
testeq( lume.trim("...hello world.", "."), "hello world" )
|
||||||
|
testeq( lume.trim("^.hello world]^", "^.]"), "hello world" )
|
||||||
end
|
end
|
||||||
|
|
||||||
-- lume.format
|
-- lume.format
|
||||||
|
Reference in New Issue
Block a user