mirror of
https://github.com/TangentFoxy/lume.git
synced 2025-07-28 02:52:21 +00:00
Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e2a9e009af | ||
|
98847e7812 | ||
|
6389f859c6 | ||
|
d8c2eddc10 | ||
|
9e0f56e3e9 | ||
|
c3089b0eb0 | ||
|
fb7b826942 | ||
|
0eccde530a | ||
|
cea8f76bb3 | ||
|
78805a5e42 | ||
|
0980d07eaa | ||
|
09035882f1 | ||
|
2b10ce1f98 | ||
|
64aae8d473 | ||
|
758067dd33 |
2
LICENSE
2
LICENSE
@@ -1,4 +1,4 @@
|
||||
Copyright (c) 2017 rxi
|
||||
Copyright (c) 2020 rxi
|
||||
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
|
23
README.md
23
README.md
@@ -2,6 +2,10 @@
|
||||
|
||||
A collection of functions for Lua, geared towards game development.
|
||||
|
||||
This fork incorporates randomization from the LOVE engine's math library if
|
||||
available, uses a local for random calls, and adds a function for setting the
|
||||
seed for its randomizer.
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
@@ -58,6 +62,11 @@ Given an `angle` and `magnitude`, returns a vector.
|
||||
local x, y = lume.vector(0, 10) -- Returns 10, 0
|
||||
```
|
||||
|
||||
#### lume.seed([low [, high]])
|
||||
Sets random seed. If using LOVE engine's math library, both `low` and `high`
|
||||
arguments are used, else, only one argument is accepted.
|
||||
|
||||
|
||||
#### lume.random([a [, b]])
|
||||
Returns a random number between `a` and `b`. If only `a` is supplied a number
|
||||
between `0` and `a` is returned. If no arguments are supplied a random number
|
||||
@@ -175,15 +184,15 @@ Applies `fn` on two arguments cumulative to the items of the array `t`, from
|
||||
left to right, so as to reduce the array to a single value. If a `first` value
|
||||
is specified the accumulator is initialised to this, otherwise the first value
|
||||
in the array is used. If the array is empty and no `first` value is specified
|
||||
an error is raised,
|
||||
an error is raised.
|
||||
```lua
|
||||
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
|
||||
```
|
||||
|
||||
#### lume.set(t)
|
||||
#### lume.unique(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.unique({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, "cat"}
|
||||
```
|
||||
|
||||
#### lume.filter(t, fn [, retainkeys])
|
||||
@@ -442,14 +451,6 @@ 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
|
||||
arguments to [LÖVE](http://love2d.org)'s setColor() function.
|
||||
```lua
|
||||
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
|
||||
|
57
lume.lua
57
lume.lua
@@ -1,13 +1,28 @@
|
||||
--
|
||||
-- lume
|
||||
--
|
||||
-- Copyright (c) 2017 rxi
|
||||
-- Copyright (c) 2020 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.
|
||||
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
-- this software and associated documentation files (the "Software"), to deal in
|
||||
-- the Software without restriction, including without limitation the rights to
|
||||
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
-- of the Software, and to permit persons to whom the Software is furnished to do
|
||||
-- so, subject to the following conditions:
|
||||
--
|
||||
-- The above copyright notice and this permission notice shall be included in all
|
||||
-- copies or substantial portions of the Software.
|
||||
--
|
||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
-- SOFTWARE.
|
||||
--
|
||||
|
||||
local lume = { _version = "2.3.0" }
|
||||
local lume = { _version = "2.4.0-Guard13007-fork" }
|
||||
|
||||
local pairs, ipairs = pairs, ipairs
|
||||
local type, assert, unpack = type, assert, unpack or table.unpack
|
||||
@@ -17,6 +32,8 @@ local math_ceil = math.ceil
|
||||
local math_atan2 = math.atan2 or math.atan
|
||||
local math_sqrt = math.sqrt
|
||||
local math_abs = math.abs
|
||||
local math_random = love and love.math and love.math.random or math.random
|
||||
local math_randomseed = love and love.math and love.math.setRandomSeed or math.randomseed
|
||||
|
||||
local noop = function()
|
||||
end
|
||||
@@ -115,15 +132,20 @@ function lume.vector(angle, magnitude)
|
||||
end
|
||||
|
||||
|
||||
function lume.seed(low, high)
|
||||
return math_randomseed(low, high)
|
||||
end
|
||||
|
||||
|
||||
function lume.random(a, b)
|
||||
if not a then a, b = 0, 1 end
|
||||
if not b then b = 0 end
|
||||
return a + math.random() * (b - a)
|
||||
return a + math_random() * (b - a)
|
||||
end
|
||||
|
||||
|
||||
function lume.randomchoice(t)
|
||||
return t[math.random(#t)]
|
||||
return t[math_random(#t)]
|
||||
end
|
||||
|
||||
|
||||
@@ -143,7 +165,7 @@ end
|
||||
|
||||
|
||||
function lume.isarray(x)
|
||||
return (type(x) == "table" and x[1] ~= nil) and true or false
|
||||
return type(x) == "table" and x[1] ~= nil
|
||||
end
|
||||
|
||||
|
||||
@@ -198,7 +220,7 @@ end
|
||||
function lume.shuffle(t)
|
||||
local rtn = {}
|
||||
for i = 1, #t do
|
||||
local r = math.random(i)
|
||||
local r = math_random(i)
|
||||
if r ~= i then
|
||||
rtn[i] = rtn[r]
|
||||
end
|
||||
@@ -271,8 +293,8 @@ end
|
||||
|
||||
|
||||
function lume.reduce(t, fn, first)
|
||||
local started = first ~= nil
|
||||
local acc = first
|
||||
local started = first and true or false
|
||||
local iter = getiter(t)
|
||||
for _, v in iter(t) do
|
||||
if started then
|
||||
@@ -287,7 +309,7 @@ function lume.reduce(t, fn, first)
|
||||
end
|
||||
|
||||
|
||||
function lume.set(t)
|
||||
function lume.unique(t)
|
||||
local rtn = {}
|
||||
for k in pairs(lume.invert(t)) do
|
||||
rtn[#rtn + 1] = k
|
||||
@@ -662,7 +684,7 @@ end
|
||||
|
||||
function lume.uuid()
|
||||
local fn = function(x)
|
||||
local r = math.random(16) - 1
|
||||
local r = math_random(16) - 1
|
||||
r = (x == "x") and (r + 1) or (r % 4) + 9
|
||||
return ("0123456789abcdef"):sub(r, r)
|
||||
end
|
||||
@@ -709,7 +731,9 @@ end
|
||||
local ripairs_iter = function(t, i)
|
||||
i = i - 1
|
||||
local v = t[i]
|
||||
if v then return i, v end
|
||||
if v ~= nil then
|
||||
return i, v
|
||||
end
|
||||
end
|
||||
|
||||
function lume.ripairs(t)
|
||||
@@ -739,15 +763,6 @@ function lume.color(str, mul)
|
||||
end
|
||||
|
||||
|
||||
function lume.rgba(color)
|
||||
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)
|
||||
|
@@ -141,7 +141,6 @@ tests["lume.remove"] = function()
|
||||
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
|
||||
@@ -245,6 +244,7 @@ end
|
||||
tests["lume.reduce"] = function()
|
||||
local concat = function(a, b) return a .. b end
|
||||
local add = function(a, b) return a + b end
|
||||
local any = function(a, b) return a or b end
|
||||
testeq( lume.reduce({"cat", "dog"}, concat, ""), "catdog" )
|
||||
testeq( lume.reduce({"cat", "dog"}, concat, "pig"), "pigcatdog" )
|
||||
testeq( lume.reduce({"me", "ow"}, concat), "meow" )
|
||||
@@ -254,16 +254,18 @@ tests["lume.reduce"] = function()
|
||||
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 )
|
||||
testeq( lume.reduce({false, false, false}, any), false )
|
||||
testeq( lume.reduce({false, true, false}, any), true )
|
||||
tester.test.error(lume.reduce, {}, add)
|
||||
end
|
||||
|
||||
-- lume.set
|
||||
tests["lume.set"] = function()
|
||||
testeq( lume.set({}), {} )
|
||||
local t = lume.set({1, 2, 3, 2, 5, 6, 6})
|
||||
-- lume.unique
|
||||
tests["lume.unique"] = function()
|
||||
testeq( lume.unique({}), {} )
|
||||
local t = lume.unique({1, 2, 3, 2, 5, 6, 6})
|
||||
table.sort(t)
|
||||
testeq( t, {1, 2, 3, 5, 6} )
|
||||
local t = lume.set({"a", "b", "c", "b", "d"})
|
||||
local t = lume.unique({"a", "b", "c", "b", "d"})
|
||||
table.sort(t)
|
||||
testeq( t, {"a", "b", "c", "d"} )
|
||||
end
|
||||
@@ -581,12 +583,12 @@ end
|
||||
|
||||
-- lume.ripairs
|
||||
tests["lume.ripairs"] = function()
|
||||
local t = { "a", "b", "c" }
|
||||
local t = { "a", "b", false, "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" } })
|
||||
testeq( r, { { 4, "c" }, { 3, false }, { 2, "b" }, { 1, "a" } })
|
||||
tester.test.error(lume.ripairs, nil)
|
||||
end
|
||||
|
||||
@@ -607,15 +609,6 @@ tests["lume.color"] = function()
|
||||
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)
|
||||
testeq( a, 0x12 )
|
||||
testeq( r, 0x34 )
|
||||
testeq( g, 0x56 )
|
||||
testeq( b, 0x78 )
|
||||
end
|
||||
|
||||
-- lume.chain
|
||||
tests["lume.chain"] = function()
|
||||
local t = lume.chain({1, 2}):map(function(x) return x * 2 end):result()
|
||||
|
Reference in New Issue
Block a user