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-10-22 01:40:23 +00:00
|
|
|
measure = require_relative("measure"),
|
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()
|
2021-07-21 09:44:31 +00:00
|
|
|
--not part of stringx for now, because it's not necessarily utf8 safe
|
|
|
|
local function capitalise(s)
|
|
|
|
local head = s:sub(1,1)
|
|
|
|
local tail = s:sub(2)
|
|
|
|
return head:upper() .. tail
|
|
|
|
end
|
|
|
|
|
|
|
|
--any acronyms to fully capitalise to avoid "Rgb" and the like
|
2021-07-23 04:26:26 +00:00
|
|
|
local acronyms = _batteries.set{"rgb", "rgba", "argb", "hsl", "xy", "gc", "aabb",}
|
2021-07-21 09:44:31 +00:00
|
|
|
local function caps_acronym(s)
|
|
|
|
if acronyms:has(s) then
|
|
|
|
s = s:upper()
|
|
|
|
end
|
|
|
|
return s
|
|
|
|
end
|
|
|
|
|
2021-07-20 07:11:01 +00:00
|
|
|
--convert something_like_this to somethingLikeThis
|
|
|
|
local function snake_to_camel(s)
|
|
|
|
local chunks = _batteries.sequence(_batteries.stringx.split(s, "_"))
|
2021-07-21 09:44:31 +00:00
|
|
|
chunks:remap(caps_acronym)
|
2021-07-20 07:11:01 +00:00
|
|
|
local first = chunks:shift()
|
2021-07-21 09:44:31 +00:00
|
|
|
chunks:remap(capitalise)
|
2021-07-20 07:11:01 +00:00
|
|
|
chunks:unshift(first)
|
|
|
|
return chunks:concat("")
|
|
|
|
end
|
|
|
|
--convert all named properties
|
|
|
|
--(keep the old ones around as well)
|
2021-07-21 09:44:31 +00:00
|
|
|
--(we take a copy of the keys here cause we're going to be inserting new keys as we go)
|
|
|
|
for _, k in ipairs(_batteries.tablex.keys(self)) do
|
|
|
|
local v = self[k]
|
2021-07-20 07:11:01 +00:00
|
|
|
if
|
|
|
|
--only convert string properties
|
|
|
|
type(k) == "string"
|
|
|
|
--ignore private and metamethod properties
|
|
|
|
and not _batteries.stringx.starts_with(k, "_")
|
|
|
|
then
|
2021-07-21 09:44:31 +00:00
|
|
|
--convert
|
2021-07-20 07:11:01 +00:00
|
|
|
local camel = snake_to_camel(k)
|
|
|
|
if type(v) == "table" then
|
2021-07-21 09:44:31 +00:00
|
|
|
--capitalise classes
|
|
|
|
if v.__index == v then
|
|
|
|
camel = capitalise(camel)
|
|
|
|
--modify the internal name for :type()
|
|
|
|
--might be a problem for serialisation etc,
|
|
|
|
--but i imagine converting to/from camelCase mid-project is rare
|
|
|
|
v.__name = camel
|
|
|
|
end
|
|
|
|
--recursively convert anything nested as well
|
2021-07-20 07:11:01 +00:00
|
|
|
_batteries.camelCase(v)
|
|
|
|
end
|
2021-07-21 09:44:31 +00:00
|
|
|
--assign if the key changed and there isn't a matching key
|
|
|
|
if k ~= camel and self[camel] == nil then
|
|
|
|
self[camel] = v
|
|
|
|
end
|
2021-07-20 07:11:01 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return self
|
|
|
|
end
|
|
|
|
|
2020-04-07 03:49:10 +00:00
|
|
|
return _batteries
|