2014-03-03 12:11:57 +00:00
|
|
|
# Lume
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-03-01 15:34:40 +00:00
|
|
|
A collection of functions for Lua, geared towards game development.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
|
|
|
|
## Installation
|
|
|
|
|
2014-03-02 17:13:18 +00:00
|
|
|
The [lume.lua](lume.lua?raw=1) file should be dropped into an existing project
|
|
|
|
and required by it:
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
```lua
|
|
|
|
lume = require "lume"
|
|
|
|
```
|
|
|
|
|
|
|
|
|
2014-03-03 12:11:57 +00:00
|
|
|
## Function Reference
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
### lume.clamp(x, min, max)
|
2014-03-03 12:11:57 +00:00
|
|
|
Returns the number `x` clamped between the numbers `min` and `max`
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-03-02 21:29:23 +00:00
|
|
|
### lume.round(x [, increment])
|
|
|
|
Rounds `x` to the nearest integer; rounds away from zero if we're midway
|
|
|
|
between two integers. If `increment` is set then the number is rounded to the
|
|
|
|
nearest increment.
|
|
|
|
```lua
|
|
|
|
lume.round(2.3) -- Returns 2
|
|
|
|
lume.round(123.4567, .1) -- Returns 123.5
|
|
|
|
```
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
### lume.sign(x)
|
|
|
|
Returns `1` if `x` is 0 or above, returns `-1` when `x` is negative.
|
|
|
|
|
|
|
|
### lume.lerp(a, b, amount)
|
2014-03-03 12:11:57 +00:00
|
|
|
Returns the linearly interpolated number between `a` and `b`, `amount` should
|
|
|
|
be in the range of 0 - 1; if `amount` is outside of this range it is clamped.
|
2014-03-02 20:59:32 +00:00
|
|
|
```lua
|
|
|
|
lume.lerp(100, 200, .5) -- Returns 150
|
|
|
|
```
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
### lume.smooth(a, b, amount)
|
2014-02-27 21:11:31 +00:00
|
|
|
Similar to `lume.lerp()` but uses cosine interpolation instead of linear
|
|
|
|
interpolation.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-02-27 23:35:07 +00:00
|
|
|
### lume.pingpong(x)
|
2014-03-03 12:11:57 +00:00
|
|
|
Ping-pongs the number `x` between 0 and 1.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-03-04 22:53:31 +00:00
|
|
|
### lume.distance(x1, y1, x2, y2 [, squared])
|
|
|
|
Returns the distance between the two points. If `squared` is true then the
|
|
|
|
squared distance is returned -- this is faster to calculate and can still be
|
|
|
|
used when comparing distances.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
2014-03-02 01:55:27 +00:00
|
|
|
### lume.angle(x1, y1, x2, y2)
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns the angle between the two points.
|
|
|
|
|
|
|
|
### lume.random([a [, b]])
|
2014-02-27 23:40:40 +00:00
|
|
|
Returns a random number between `a` and `b`. If only `a` is supplied a number
|
2014-02-27 20:19:10 +00:00
|
|
|
between `0` and `a` is returned. If no arguments are supplied a random number
|
|
|
|
between `0` and `1` is returned.
|
|
|
|
|
|
|
|
### lume.randomchoice(t)
|
2014-03-04 21:21:50 +00:00
|
|
|
Returns a random value from array `t`. If the array is empty an error is
|
|
|
|
raised.
|
2014-03-02 20:59:32 +00:00
|
|
|
```lua
|
|
|
|
lume.randomchoice({true, false}) -- Returns either true or false
|
|
|
|
```
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
### lume.shuffle(t)
|
2014-02-27 21:11:31 +00:00
|
|
|
Shuffles the values of array `t` in place, returns the array.
|
2014-02-27 20:19:10 +00:00
|
|
|
|
|
|
|
### lume.array(...)
|
2014-02-27 21:11:31 +00:00
|
|
|
Iterates the supplied iterator and returns an array filled with the values.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-28 13:05:34 +00:00
|
|
|
lume.array(pairs({a = 1, b = 2})) -- Returns {"a", "b"}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2014-03-03 12:08:11 +00:00
|
|
|
### lume.each(t, fn, ...)
|
|
|
|
Iterates the table `t` and calls the function `fn` on each value followed by
|
|
|
|
the supplied additional arguments; if `fn` is a string the method of that name
|
|
|
|
is called for each value. The function returns `t` unmodified.
|
|
|
|
```lua
|
|
|
|
lume.each({1, 2, 3}, print) -- Prints "1", "2", "3" on separate lines
|
|
|
|
lume.each({a, b, c}, "move", 10, 20) -- Does x:move(10, 20) on each value
|
|
|
|
```
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
### lume.map(t, fn)
|
2014-02-27 21:11:31 +00:00
|
|
|
Applies the function `fn` to each value in table `t` and returns a new table
|
|
|
|
with the resulting values.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.map({1, 2, 3}, function(x) return x * 2 end) -- Returns {2, 4, 6}
|
|
|
|
```
|
|
|
|
|
2014-02-28 12:51:50 +00:00
|
|
|
### lume.all(t [, fn])
|
|
|
|
Returns true if all the values in `t` table are true. If a `fn` function is
|
|
|
|
supplied it is called on each value, true is returned if all of the calls to
|
|
|
|
`fn` return true.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.all({1, 2, 1}, function(x) return x == 1 end) -- Returns false
|
|
|
|
```
|
|
|
|
|
2014-02-28 12:51:50 +00:00
|
|
|
### lume.any(t [, fn])
|
|
|
|
Returns true if any of the values in `t` table are true. If a `fn` function is
|
|
|
|
supplied it is called on each value, true is returned if any of the calls to
|
|
|
|
`fn` return true.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-28 12:51:50 +00:00
|
|
|
lume.any({1, 2, 1}, function(x) return x == 1 end) -- Returns true
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2014-03-06 19:33:13 +00:00
|
|
|
### lume.reduce(t, fn [, first])
|
2014-02-27 20:19:10 +00:00
|
|
|
Applies `fn` on two arguments cumulative to the items of the array `t`, from
|
2014-03-06 19:33:13 +00:00
|
|
|
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,
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-03-06 19:33:13 +00:00
|
|
|
lume.reduce({1, 2, 3}, function(a, b) return a + b end) -- Returns 6
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2014-03-08 14:45:08 +00:00
|
|
|
### lume.invert(t)
|
|
|
|
Returns a copy of the table where the keys have become the values and the
|
|
|
|
values the keys.
|
|
|
|
```lua
|
|
|
|
lume.invert({a = "x", b = "y"}) -- returns {x = "a", y = "b"}
|
|
|
|
```
|
|
|
|
|
2014-02-28 13:05:34 +00:00
|
|
|
### lume.set(t [, retainkeys])
|
2014-02-27 22:17:36 +00:00
|
|
|
Returns a copy of the `t` table with all the duplicate values removed. If
|
2014-02-27 22:39:09 +00:00
|
|
|
`retainkeys` is true the table is not treated as an array and retains its
|
|
|
|
original keys.
|
2014-02-27 22:17:36 +00:00
|
|
|
```lua
|
|
|
|
lume.set({2, 1, 2, "cat", "cat"}) -- Returns {1, 2, cat}
|
|
|
|
```
|
|
|
|
|
2014-02-27 22:39:09 +00:00
|
|
|
### lume.filter(t, fn [, retainkeys])
|
|
|
|
Calls `fn` on each value of `t` table. Returns a new table with only the values
|
|
|
|
where `fn` returned true. If `retainkeys` is true the table is not treated as
|
|
|
|
an array and retains its original keys.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-27 23:23:07 +00:00
|
|
|
lume.filter({1, 2, 3, 4}, function(x) return x % 2 == 0 end) -- Returns {2, 4}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2014-02-27 22:39:09 +00:00
|
|
|
### 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.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-27 23:20:38 +00:00
|
|
|
lume.merge({2, 3}, {4, 5}) -- Returns {2, 3, 4, 5}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### lume.find(t, value)
|
|
|
|
Returns the index/key of `value` in `t`. Returns `nil` if that value does not
|
|
|
|
exist in the table.
|
|
|
|
```lua
|
|
|
|
lume.find({"a", "b", "c"}, "b") -- Returns 2
|
|
|
|
```
|
|
|
|
|
|
|
|
### 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.
|
|
|
|
```lua
|
|
|
|
lume.slice({"a", "b", "c", "d", "e"}, 2, 4) -- Returns {"b", "c", "d"}
|
|
|
|
```
|
|
|
|
|
|
|
|
### lume.clone(t)
|
|
|
|
Returns a shallow copy of the table `t`.
|
|
|
|
|
|
|
|
### lume.fn(fn, ...)
|
|
|
|
Creates a wrapper function around function `fn`, automatically inserting the
|
2014-03-03 13:34:27 +00:00
|
|
|
arguments into `fn` which will persist every time the wrapper is called. Any
|
|
|
|
arguments which are passed to the returned function will be inserted after the
|
|
|
|
already existing arguments passed to `fn`.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
local f = lume.fn(print, "Hello")
|
2014-03-03 13:34:27 +00:00
|
|
|
f("world") -- Prints "Hello world"
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2014-03-02 00:09:24 +00:00
|
|
|
### lume.once(fn, ...)
|
|
|
|
Returns a wrapper function to `fn` which takes the supplied arguments. The
|
|
|
|
wrapper function will call `fn` on the first call and do nothing on any
|
|
|
|
subsequent calls.
|
|
|
|
```lua
|
|
|
|
local f = lume.once(print, "Hello")
|
|
|
|
f() -- Prints "Hello"
|
|
|
|
f() -- Does nothing
|
|
|
|
```
|
|
|
|
|
2014-02-28 19:39:29 +00:00
|
|
|
### 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.
|
|
|
|
```lua
|
|
|
|
lume.time(function(x) return x end, "hello") -- Returns 0, "hello"
|
|
|
|
```
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
### lume.serialize(x)
|
2014-02-27 21:11:31 +00:00
|
|
|
Serializes the argument `x` into a string which can be loaded again using
|
2014-02-28 13:05:34 +00:00
|
|
|
`lume.deserialize()`. Only booleans, numbers, tables and strings can be
|
|
|
|
serialized. Circular references are not handled; all nested tables are
|
|
|
|
serialized as unique tables.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.serialize({a = "test", b = {1, 2, 3}, false})
|
|
|
|
-- Returns "{[1]=false,["a"]="test",["b"]={[1]=1,[2]=2,[3]=3,},}"
|
|
|
|
```
|
|
|
|
|
|
|
|
### lume.deserialize(str)
|
|
|
|
Deserializes a string created by `lume.serialize()` and returns the resulting
|
|
|
|
value. This function should not be run on an untrusted string.
|
|
|
|
```lua
|
|
|
|
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`
|
2014-02-27 21:11:31 +00:00
|
|
|
instead of on whitespace.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-02-27 22:51:22 +00:00
|
|
|
lume.split("One two three") -- Returns {"One", "two", "three"}
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### lume.trim(str [, chars])
|
|
|
|
Trims the whitespace from the start and end of the string `str` and returns the
|
|
|
|
new string. If a `chars` value is set the characters in `chars` are trimmed
|
2014-02-27 21:11:31 +00:00
|
|
|
instead of whitespace.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.trim(" Hello ") -- Returns "Hello"
|
|
|
|
```
|
|
|
|
|
2014-03-03 20:36:42 +00:00
|
|
|
### lume.format(str [, vars])
|
2014-02-27 20:19:10 +00:00
|
|
|
Returns a formatted string. The values of keys in the table `vars` can be
|
2014-03-04 12:18:50 +00:00
|
|
|
inserted into the string by using the form `"{key}"` in `str`; numerical keys
|
|
|
|
can also be used.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
2014-03-04 12:18:50 +00:00
|
|
|
lume.format("{b} hi {a}", {a = "mark", b = "Oh"}) -- Returns "Oh hi mark"
|
|
|
|
lume.format("Hello {1}!", {"world"}) -- Returns "Hello world!"
|
2014-02-27 20:19:10 +00:00
|
|
|
```
|
|
|
|
|
2014-03-02 20:48:21 +00:00
|
|
|
### lume.trace(...)
|
|
|
|
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"
|
|
|
|
```
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
### lume.dostring(str)
|
|
|
|
Executes the lua code inside `str`.
|
|
|
|
```lua
|
|
|
|
lume.dostring("print('Hello!')") -- Prints "Hello!"
|
|
|
|
```
|
|
|
|
|
2014-03-01 15:05:51 +00:00
|
|
|
### lume.hotswap(modname)
|
2014-03-03 12:11:57 +00:00
|
|
|
Reloads an already loaded module in place, allowing you to immediately see the
|
2014-03-02 17:13:18 +00:00
|
|
|
effects of code changes without having to restart the program. `modname` should
|
|
|
|
be the same string used when loading the module with require(). In the case of
|
|
|
|
an error the global environment is restored and `nil` plus an error message is
|
|
|
|
returned.
|
2014-03-01 15:05:51 +00:00
|
|
|
```lua
|
|
|
|
lume.hotswap("lume") -- Reloads the lume module
|
2014-03-02 17:13:18 +00:00
|
|
|
assert(lume.hotswap("inexistant_module")) -- Raises an error
|
2014-03-01 15:05:51 +00:00
|
|
|
```
|
|
|
|
|
2014-02-27 20:19:10 +00:00
|
|
|
### lume.rgba(color)
|
|
|
|
Takes the 32bit integer `color` argument and returns 4 numbers, one for each
|
2014-03-01 15:34:40 +00:00
|
|
|
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.
|
2014-02-27 20:19:10 +00:00
|
|
|
```lua
|
|
|
|
lume.rgba(0xFF304050) -- Returns 48, 64, 80, 255
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
## License
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or modify it under
|
|
|
|
the terms of the MIT license. See [LICENSE](LICENSE) for details.
|
|
|
|
|