mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-14 04:01:48 +00:00
[modified] refactored init.lua extensively; used new assert module in stringx and tablex
This commit is contained in:
parent
cf7354d75f
commit
284856d2c3
127
init.lua
127
init.lua
@ -9,109 +9,90 @@ local function require_relative(p)
|
|||||||
return require(table.concat({path, p}, "."))
|
return require(table.concat({path, p}, "."))
|
||||||
end
|
end
|
||||||
|
|
||||||
local _class = require_relative("class")
|
|
||||||
|
|
||||||
local _mathx = require_relative("mathx")
|
|
||||||
|
|
||||||
local _tablex = require_relative("tablex")
|
|
||||||
local _stable_sort = require_relative("stable_sort")
|
|
||||||
|
|
||||||
local _functional = require_relative("functional")
|
|
||||||
|
|
||||||
local _sequence = require_relative("sequence")
|
|
||||||
local _set = require_relative("set")
|
|
||||||
|
|
||||||
local _stringx = require_relative("stringx")
|
|
||||||
|
|
||||||
local _vec2 = require_relative("vec2")
|
|
||||||
local _vec3 = require_relative("vec3")
|
|
||||||
local _intersect = require_relative("intersect")
|
|
||||||
|
|
||||||
local _unique_mapping = require_relative("unique_mapping")
|
|
||||||
local _state_machine = require_relative("state_machine")
|
|
||||||
|
|
||||||
local _async = require_relative("async")
|
|
||||||
|
|
||||||
local _manual_gc = require_relative("manual_gc")
|
|
||||||
|
|
||||||
local _colour = require_relative("colour")
|
|
||||||
|
|
||||||
--build the module
|
--build the module
|
||||||
local _batteries = {
|
local _batteries = {
|
||||||
--fire and forget mode function
|
|
||||||
export = export,
|
|
||||||
--
|
--
|
||||||
class = _class,
|
class = require_relative("class"),
|
||||||
--support x and non-x naming
|
|
||||||
math = _mathx,
|
|
||||||
mathx = _mathx,
|
|
||||||
--
|
--
|
||||||
table = _tablex,
|
assert = require_relative("assert"),
|
||||||
tablex = _tablex,
|
--extension libraries
|
||||||
--
|
mathx = require_relative("mathx"),
|
||||||
string = _stringx,
|
tablex = require_relative("tablex"),
|
||||||
stringx = _stringx,
|
stringx = require_relative("stringx"),
|
||||||
--sorting routines
|
--sorting routines
|
||||||
stable_sort = _stable_sort,
|
stable_sort = require_relative("stable_sort"),
|
||||||
sort = _stable_sort,
|
|
||||||
--
|
--
|
||||||
functional = _functional,
|
functional = require_relative("functional"),
|
||||||
--collections
|
--collections
|
||||||
sequence = _sequence,
|
sequence = require_relative("sequence"),
|
||||||
set = _set,
|
set = require_relative("set"),
|
||||||
--geom
|
--geom
|
||||||
vec2 = _vec2,
|
vec2 = require_relative("vec2"),
|
||||||
vec3 = _vec3,
|
vec3 = require_relative("vec3"),
|
||||||
intersect = _intersect,
|
intersect = require_relative("intersect"),
|
||||||
--
|
--
|
||||||
unique_mapping = _unique_mapping,
|
unique_mapping = require_relative("unique_mapping"),
|
||||||
state_machine = _state_machine,
|
state_machine = require_relative("state_machine"),
|
||||||
async = _async,
|
async = require_relative("async"),
|
||||||
manual_gc = _manual_gc,
|
manual_gc = require_relative("manual_gc"),
|
||||||
colour = _colour,
|
colour = require_relative("colour"),
|
||||||
color = _colour,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
--assign aliases
|
||||||
|
for _, alias in ipairs({
|
||||||
|
{"mathx", "math"},
|
||||||
|
{"tablex", "table"},
|
||||||
|
{"stringx", "string"},
|
||||||
|
{"stable_sort", "sort"},
|
||||||
|
{"colour", "color"},
|
||||||
|
}) do
|
||||||
|
_batteries[alias[2]] = _batteries[alias[1]]
|
||||||
|
end
|
||||||
|
|
||||||
--easy export globally if required
|
--easy export globally if required
|
||||||
function _batteries:export(self)
|
function _batteries:export()
|
||||||
--export oo
|
--export oo
|
||||||
class = _class
|
class = self.class
|
||||||
|
|
||||||
|
--export assert
|
||||||
|
assert = self.assert
|
||||||
|
|
||||||
--overlay tablex and functional and sort routines onto table
|
--overlay tablex and functional and sort routines onto table
|
||||||
_tablex.overlay(table, _tablex)
|
self.tablex.overlay(table, self.tablex)
|
||||||
_tablex.overlay(table, _functional)
|
--now we can use it through table directly
|
||||||
_stable_sort:export()
|
table.overlay(table, self.functional)
|
||||||
|
self.stable_sort:export()
|
||||||
|
|
||||||
--functional module also available separate from table
|
--functional module also available separate from table
|
||||||
functional = _functional
|
functional = self.functional
|
||||||
|
|
||||||
--export collections
|
--export collections
|
||||||
sequence = _sequence
|
sequence = self.sequence
|
||||||
set = _set
|
set = self.set
|
||||||
|
|
||||||
--overlay onto math
|
--overlay onto global math table
|
||||||
_tablex.overlay(math, _mathx)
|
table.overlay(math, self.mathx)
|
||||||
|
|
||||||
--overlay onto string
|
--overlay onto string
|
||||||
_tablex.overlay(string, _stringx)
|
table.overlay(string, self.stringx)
|
||||||
|
|
||||||
--export geom
|
--export geom
|
||||||
vec2 = _vec2
|
vec2 = self.vec2
|
||||||
vec3 = _vec3
|
vec3 = self.vec3
|
||||||
intersect = _intersect
|
intersect = self.intersect
|
||||||
|
|
||||||
--"misc" :)
|
--"misc" :)
|
||||||
unique_mapping = _unique_mapping
|
unique_mapping = self.unique_mapping
|
||||||
state_machine = _state_machine
|
state_machine = self.state_machine
|
||||||
async = _async
|
async = self.async
|
||||||
manual_gc = _manual_gc
|
manual_gc = self.manual_gc
|
||||||
|
|
||||||
--support both spellings
|
--support both spellings
|
||||||
colour = _colour
|
colour = self.colour
|
||||||
color = _colour
|
color = self.colour
|
||||||
|
|
||||||
--export top level module as well for ease of migration for code
|
--export top level module as well for ease of migration for code
|
||||||
batteries = _batteries
|
batteries = self
|
||||||
|
|
||||||
return self
|
return self
|
||||||
end
|
end
|
||||||
|
12
stringx.lua
12
stringx.lua
@ -2,12 +2,18 @@
|
|||||||
extra string routines
|
extra string routines
|
||||||
]]
|
]]
|
||||||
|
|
||||||
|
local path = (...):gsub(".stringx", ".")
|
||||||
|
local assert = require(path .. "assert")
|
||||||
|
|
||||||
local stringx = setmetatable({}, {
|
local stringx = setmetatable({}, {
|
||||||
__index = string
|
__index = string
|
||||||
})
|
})
|
||||||
|
|
||||||
--split a string on a delimiter into an ordered table
|
--split a string on a delimiter into an ordered table
|
||||||
function stringx:split(delim)
|
function stringx.split(self, delim)
|
||||||
|
assert:type(self, "string", "stringx.split - self", 1)
|
||||||
|
assert:type(delim, "string", "stringx.split - delim", 1)
|
||||||
|
|
||||||
--we try to create as little garbage as possible!
|
--we try to create as little garbage as possible!
|
||||||
--only one table to contain the result, plus the split strings.
|
--only one table to contain the result, plus the split strings.
|
||||||
--so we do two passes, and work with the bytes underlying the string
|
--so we do two passes, and work with the bytes underlying the string
|
||||||
@ -63,8 +69,8 @@ end
|
|||||||
|
|
||||||
--turn input into a vaguely easy to read string
|
--turn input into a vaguely easy to read string
|
||||||
--(which is also able to be parsed by lua in many cases)
|
--(which is also able to be parsed by lua in many cases)
|
||||||
--todo: multi-line for big tables
|
--todo: multi-line/indent for big tables
|
||||||
--todo: support self-referential tables at least without crashing :)
|
--todo: support cyclic references without crashing :)
|
||||||
function stringx.pretty(input)
|
function stringx.pretty(input)
|
||||||
--if the input is not a table, or it has a tostring metamethod
|
--if the input is not a table, or it has a tostring metamethod
|
||||||
--then we can just use tostring
|
--then we can just use tostring
|
||||||
|
13
tablex.lua
13
tablex.lua
@ -6,6 +6,9 @@
|
|||||||
--so it works "as if" it was the global table api
|
--so it works "as if" it was the global table api
|
||||||
--upgraded with these routines
|
--upgraded with these routines
|
||||||
|
|
||||||
|
local path = (...):gsub(".tablex", ".")
|
||||||
|
local assert = require(path .. "assert")
|
||||||
|
|
||||||
local tablex = setmetatable({}, {
|
local tablex = setmetatable({}, {
|
||||||
__index = table,
|
__index = table,
|
||||||
})
|
})
|
||||||
@ -209,7 +212,7 @@ else
|
|||||||
--useful when multiple references are being held
|
--useful when multiple references are being held
|
||||||
--so you cannot just create a new table
|
--so you cannot just create a new table
|
||||||
function tablex.clear(t)
|
function tablex.clear(t)
|
||||||
assert(type(t) == "table", "tablex.clear - argument 't' must be a table")
|
assert:type(t, "table", "tablex.clear - t", 1)
|
||||||
local k = next(t)
|
local k = next(t)
|
||||||
while k ~= nil do
|
while k ~= nil do
|
||||||
t[k] = nil
|
t[k] = nil
|
||||||
@ -240,7 +243,7 @@ end
|
|||||||
-- but doesn't clear anything out
|
-- but doesn't clear anything out
|
||||||
-- (useful for deep overlays and avoiding garbage)
|
-- (useful for deep overlays and avoiding garbage)
|
||||||
function tablex.copy(t, deep_or_into)
|
function tablex.copy(t, deep_or_into)
|
||||||
assert(type(t) == "table", "tablex.copy - argument 't' must be a table")
|
assert:type(t, "table", "tablex.copy - t", 1)
|
||||||
local is_bool = type(deep_or_into) == "boolean"
|
local is_bool = type(deep_or_into) == "boolean"
|
||||||
local is_table = type(deep_or_into) == "table"
|
local is_table = type(deep_or_into) == "table"
|
||||||
|
|
||||||
@ -264,8 +267,8 @@ end
|
|||||||
--overlays them in passed order onto the first,
|
--overlays them in passed order onto the first,
|
||||||
--and returns the first table with the overlay(s) applied
|
--and returns the first table with the overlay(s) applied
|
||||||
function tablex.overlay(a, b, ...)
|
function tablex.overlay(a, b, ...)
|
||||||
assert(type(a) == "table", "tablex.overlay - argument 'a' must be a table")
|
assert:type(a, "table", "tablex.overlay - a", 1)
|
||||||
assert(type(b) == "table", "tablex.overlay - argument 'b' must be a table")
|
assert:type(b, "table", "tablex.overlay - b", 1)
|
||||||
for k,v in pairs(b) do
|
for k,v in pairs(b) do
|
||||||
a[k] = v
|
a[k] = v
|
||||||
end
|
end
|
||||||
@ -284,7 +287,7 @@ end
|
|||||||
-- so they can't exist in the base level
|
-- so they can't exist in the base level
|
||||||
-- (... or at least, their non-ipairs members won't survive the collapse)
|
-- (... or at least, their non-ipairs members won't survive the collapse)
|
||||||
function tablex.collapse(t)
|
function tablex.collapse(t)
|
||||||
assert(type(t) == "table", "tablex.collapse - argument 't' must be a table")
|
assert:type(t, "table", "tablex.collapse - t", 1)
|
||||||
local r = {}
|
local r = {}
|
||||||
for _, v in ipairs(t) do
|
for _, v in ipairs(t) do
|
||||||
if type(v) == "table" then
|
if type(v) == "table" then
|
||||||
|
Loading…
Reference in New Issue
Block a user