2020-01-29 03:26:28 +00:00
|
|
|
--[[
|
2020-03-15 09:28:50 +00:00
|
|
|
batteries for lua
|
2020-01-29 03:26:28 +00:00
|
|
|
|
2020-05-02 09:59:02 +00:00
|
|
|
a collection of helpful code to get your project off the ground faster
|
2020-01-29 03:26:28 +00:00
|
|
|
]]
|
|
|
|
|
|
|
|
local path = ...
|
2020-03-15 09:28:50 +00:00
|
|
|
local function require_relative(p)
|
|
|
|
return require(table.concat({path, p}, "."))
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
--build the module
|
|
|
|
local _batteries = {
|
|
|
|
--
|
2020-05-19 02:03:45 +00:00
|
|
|
class = require_relative("class"),
|
2020-04-17 00:35:00 +00:00
|
|
|
--
|
2020-05-19 02:03:45 +00:00
|
|
|
assert = require_relative("assert"),
|
|
|
|
--extension libraries
|
|
|
|
mathx = require_relative("mathx"),
|
|
|
|
tablex = require_relative("tablex"),
|
|
|
|
stringx = require_relative("stringx"),
|
2020-04-07 03:49:10 +00:00
|
|
|
--sorting routines
|
2020-06-02 05:00:46 +00:00
|
|
|
sort = require_relative("sort"),
|
2020-04-07 03:49:10 +00:00
|
|
|
--
|
2020-05-19 02:03:45 +00:00
|
|
|
functional = require_relative("functional"),
|
2020-05-02 09:59:02 +00:00
|
|
|
--collections
|
2020-05-19 02:03:45 +00:00
|
|
|
sequence = require_relative("sequence"),
|
|
|
|
set = require_relative("set"),
|
2020-04-17 00:35:00 +00:00
|
|
|
--geom
|
2020-05-19 02:03:45 +00:00
|
|
|
vec2 = require_relative("vec2"),
|
|
|
|
vec3 = require_relative("vec3"),
|
|
|
|
intersect = require_relative("intersect"),
|
2020-04-07 03:49:10 +00:00
|
|
|
--
|
2021-03-12 10:23:54 +00:00
|
|
|
timer = require_relative("timer"),
|
|
|
|
pubsub = require_relative("pubsub"),
|
2020-05-19 02:03:45 +00:00
|
|
|
unique_mapping = require_relative("unique_mapping"),
|
|
|
|
state_machine = require_relative("state_machine"),
|
|
|
|
async = require_relative("async"),
|
|
|
|
manual_gc = require_relative("manual_gc"),
|
|
|
|
colour = require_relative("colour"),
|
2021-07-05 06:12:16 +00:00
|
|
|
pretty = require_relative("pretty"),
|
2021-07-15 06:09:08 +00:00
|
|
|
make_pooled = require_relative("make_pooled"),
|
2020-04-07 03:49:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-19 02:03:45 +00:00
|
|
|
--assign aliases
|
|
|
|
for _, alias in ipairs({
|
|
|
|
{"mathx", "math"},
|
|
|
|
{"tablex", "table"},
|
|
|
|
{"stringx", "string"},
|
2020-06-02 05:00:46 +00:00
|
|
|
{"sort", "stable_sort"},
|
2020-05-19 02:03:45 +00:00
|
|
|
{"colour", "color"},
|
|
|
|
}) do
|
|
|
|
_batteries[alias[2]] = _batteries[alias[1]]
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
--easy export globally if required
|
2020-05-19 02:03:45 +00:00
|
|
|
function _batteries:export()
|
2021-07-20 07:11:01 +00:00
|
|
|
--export all key strings globally, if doesn't already exist
|
2020-08-09 09:49:24 +00:00
|
|
|
for k, v in pairs(self) do
|
2021-07-20 07:11:01 +00:00
|
|
|
if _G[k] == nil then
|
2020-08-09 09:49:24 +00:00
|
|
|
_G[k] = v
|
|
|
|
end
|
|
|
|
end
|
2020-04-07 03:49:10 +00:00
|
|
|
|
|
|
|
--overlay tablex and functional and sort routines onto table
|
2020-05-19 02:03:45 +00:00
|
|
|
self.tablex.overlay(table, self.tablex)
|
|
|
|
--now we can use it through table directly
|
|
|
|
table.overlay(table, self.functional)
|
2020-06-02 05:00:46 +00:00
|
|
|
self.sort:export()
|
|
|
|
|
2020-05-19 02:03:45 +00:00
|
|
|
--overlay onto global math table
|
|
|
|
table.overlay(math, self.mathx)
|
2020-04-07 03:49:10 +00:00
|
|
|
|
2020-04-17 00:35:00 +00:00
|
|
|
--overlay onto string
|
2020-05-19 02:03:45 +00:00
|
|
|
table.overlay(string, self.stringx)
|
2020-04-17 00:35:00 +00:00
|
|
|
|
2020-11-10 09:48:40 +00:00
|
|
|
--overwrite assert wholesale (it's compatible)
|
|
|
|
assert = self.assert
|
|
|
|
|
2021-03-12 10:23:54 +00:00
|
|
|
--export the whole library to global `batteries`
|
|
|
|
batteries = self
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
return self
|
2020-03-15 09:28:50 +00:00
|
|
|
end
|
2020-02-01 08:30:36 +00:00
|
|
|
|
2021-07-20 07:11:01 +00:00
|
|
|
|
|
|
|
--convert naming, for picky eaters
|
|
|
|
--experimental, let me know how it goes
|
|
|
|
function _batteries:camelCase()
|
|
|
|
--convert something_like_this to somethingLikeThis
|
|
|
|
local function snake_to_camel(s)
|
|
|
|
local chunks = _batteries.sequence(_batteries.stringx.split(s, "_"))
|
|
|
|
local first = chunks:shift()
|
|
|
|
chunks:remap(function(v)
|
|
|
|
local head = v:sub(1,1)
|
|
|
|
local tail = v:sub(2)
|
|
|
|
return head:upper() .. tail
|
|
|
|
end)
|
|
|
|
chunks:unshift(first)
|
|
|
|
return chunks:concat("")
|
|
|
|
end
|
|
|
|
--convert all named properties
|
|
|
|
--(keep the old ones around as well)
|
|
|
|
for k, v in pairs(self) do
|
|
|
|
if
|
|
|
|
--only convert string properties
|
|
|
|
type(k) == "string"
|
|
|
|
--ignore private and metamethod properties
|
|
|
|
and not _batteries.stringx.starts_with(k, "_")
|
|
|
|
then
|
|
|
|
--convert and assign
|
|
|
|
local camel = snake_to_camel(k)
|
|
|
|
if k ~= camel and self[camel] == nil then
|
|
|
|
self[camel] = v
|
|
|
|
end
|
|
|
|
--recursively convert anything nested as well
|
|
|
|
if type(v) == "table" then
|
|
|
|
_batteries.camelCase(v)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
return _batteries
|