mirror of
https://github.com/1bardesign/batteries.git
synced 2024-11-10 02:31:48 +00:00
modified :camelCase to capitalise class names and fully capitalise known acronyms. __name changed to __type, used for :type() calls
fixes #29
This commit is contained in:
parent
444df42569
commit
9f65e1150b
14
class.lua
14
class.lua
@ -68,18 +68,18 @@ local function class(config)
|
|||||||
|
|
||||||
local c = {}
|
local c = {}
|
||||||
|
|
||||||
--unique generated id per-class
|
|
||||||
c.__id = class_id
|
|
||||||
|
|
||||||
--the class name
|
|
||||||
c.__name = name
|
|
||||||
|
|
||||||
--prototype
|
--prototype
|
||||||
c.__index = c
|
c.__index = c
|
||||||
|
|
||||||
|
--unique generated id per-class
|
||||||
|
c.__id = class_id
|
||||||
|
|
||||||
|
--the class name for type calls
|
||||||
|
c.__type = name
|
||||||
|
|
||||||
--return the name of the class
|
--return the name of the class
|
||||||
function c:type()
|
function c:type()
|
||||||
return name
|
return self.__type
|
||||||
end
|
end
|
||||||
|
|
||||||
if config.default_tostring then
|
if config.default_tostring then
|
||||||
|
47
init.lua
47
init.lua
@ -87,36 +87,61 @@ end
|
|||||||
--convert naming, for picky eaters
|
--convert naming, for picky eaters
|
||||||
--experimental, let me know how it goes
|
--experimental, let me know how it goes
|
||||||
function _batteries:camelCase()
|
function _batteries:camelCase()
|
||||||
|
--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
|
||||||
|
local acronyms = _batteries.set{"rgb", "rgba", "argb", "hsl", "xy", "gc",}
|
||||||
|
local function caps_acronym(s)
|
||||||
|
if acronyms:has(s) then
|
||||||
|
s = s:upper()
|
||||||
|
end
|
||||||
|
return s
|
||||||
|
end
|
||||||
|
|
||||||
--convert something_like_this to somethingLikeThis
|
--convert something_like_this to somethingLikeThis
|
||||||
local function snake_to_camel(s)
|
local function snake_to_camel(s)
|
||||||
local chunks = _batteries.sequence(_batteries.stringx.split(s, "_"))
|
local chunks = _batteries.sequence(_batteries.stringx.split(s, "_"))
|
||||||
|
chunks:remap(caps_acronym)
|
||||||
local first = chunks:shift()
|
local first = chunks:shift()
|
||||||
chunks:remap(function(v)
|
chunks:remap(capitalise)
|
||||||
local head = v:sub(1,1)
|
|
||||||
local tail = v:sub(2)
|
|
||||||
return head:upper() .. tail
|
|
||||||
end)
|
|
||||||
chunks:unshift(first)
|
chunks:unshift(first)
|
||||||
return chunks:concat("")
|
return chunks:concat("")
|
||||||
end
|
end
|
||||||
--convert all named properties
|
--convert all named properties
|
||||||
--(keep the old ones around as well)
|
--(keep the old ones around as well)
|
||||||
for k, v in pairs(self) do
|
--(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]
|
||||||
if
|
if
|
||||||
--only convert string properties
|
--only convert string properties
|
||||||
type(k) == "string"
|
type(k) == "string"
|
||||||
--ignore private and metamethod properties
|
--ignore private and metamethod properties
|
||||||
and not _batteries.stringx.starts_with(k, "_")
|
and not _batteries.stringx.starts_with(k, "_")
|
||||||
then
|
then
|
||||||
--convert and assign
|
--convert
|
||||||
local camel = snake_to_camel(k)
|
local camel = snake_to_camel(k)
|
||||||
|
if type(v) == "table" then
|
||||||
|
--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
|
||||||
|
_batteries.camelCase(v)
|
||||||
|
end
|
||||||
|
print(camel)
|
||||||
|
--assign if the key changed and there isn't a matching key
|
||||||
if k ~= camel and self[camel] == nil then
|
if k ~= camel and self[camel] == nil then
|
||||||
self[camel] = v
|
self[camel] = v
|
||||||
end
|
end
|
||||||
--recursively convert anything nested as well
|
|
||||||
if type(v) == "table" then
|
|
||||||
_batteries.camelCase(v)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user