Skip to content

Commit

Permalink
fix(utils): remove unused utils
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Nov 13, 2024
1 parent b40e51e commit 34316ae
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 189 deletions.
6 changes: 3 additions & 3 deletions src/common/controllers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local controllers = {}

--- Set a controller.
---@param controller string The controller to set.
---@return table<string>
---@return string[]
function controllers.setController(controller)
utils.validateArweaveId(controller)

Expand All @@ -18,7 +18,7 @@ end

--- Remove a controller.
---@param controller string The controller to remove.
---@return table<string>
---@return string[]
function controllers.removeController(controller)
utils.validateArweaveId(controller)
local controllerExists = false
Expand All @@ -36,7 +36,7 @@ function controllers.removeController(controller)
end

--- Get all controllers.
---@return table<string>
---@return string[]
function controllers.getControllers()
return Controllers
end
Expand Down
2 changes: 1 addition & 1 deletion src/common/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ function ant.init()
---@alias Description string
---@description A brief description of this ANT up to 255 characters
Description = Description or "A brief description of this ANT."
---@alias Keywords table<string>
---@alias Keywords string[]
---@description A list of keywords that describe this ANT. Each keyword must be a string, unique, and less than 32 characters. There can be up to 16 keywords
Keywords = Keywords or {}
---@alias Denomination integer
Expand Down
2 changes: 1 addition & 1 deletion src/common/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
--- Logo: string,
--- Balances: table<string, integer>,
--- Owner: string,
--- Controllers: table<string>,
--- Controllers: string[],
--- Denomination: integer,
--- TotalSupply: integer,
--- Initialized: boolean,
Expand Down
186 changes: 2 additions & 184 deletions src/common/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,172 +3,6 @@ local constants = require(".common.constants")
local json = require(".common.json")
local utils = { _version = "0.0.1" }

local function isArray(table)
if type(table) == "table" then
local maxIndex = 0
for k, _ in pairs(table) do
if type(k) ~= "number" or k < 1 or math.floor(k) ~= k then
return false -- If there's a non-integer key, it's not an array
end
maxIndex = math.max(maxIndex, k)
end
-- If the highest numeric index is equal to the number of elements, it's an array
return maxIndex == #table
end
return false
end

--- @param fn function
--- @param arity number
utils.curry = function(fn, arity)
assert(type(fn) == "function", "function is required as first argument")
arity = arity or debug.getinfo(fn, "u").nparams
if arity < 2 then
return fn
end

return function(...)
local args = { ... }

if #args >= arity then
return fn(table.unpack(args))
else
return utils.curry(function(...)
return fn(table.unpack(args), ...)
end, arity - #args)
end
end
end

--- Concat two Array Tables.
--- @param a table
--- @param b table
utils.concat = utils.curry(function(a, b)
assert(type(a) == "table", "first argument should be a table that is an array")
assert(type(b) == "table", "second argument should be a table that is an array")
assert(isArray(a), "first argument should be a table")
assert(isArray(b), "second argument should be a table")

local result = {}
for i = 1, #a do
result[#result + 1] = a[i]
end
for i = 1, #b do
result[#result + 1] = b[i]
end
return result
end, 2)

--- reduce applies a function to a table
--- @param fn function
--- @param initial any
--- @param t table
utils.reduce = utils.curry(function(fn, initial, t)
assert(type(fn) == "function", "first argument should be a function that accepts (result, value, key)")
assert(type(t) == "table" and isArray(t), "third argument should be a table that is an array")
local result = initial
for k, v in pairs(t) do
if result == nil then
result = v
else
result = fn(result, v, k)
end
end
return result
end, 3)

--- @param fn function
--- @param data table
utils.map = utils.curry(function(fn, data)
assert(type(fn) == "function", "first argument should be a unary function")
assert(type(data) == "table" and isArray(data), "second argument should be an Array")

local function map(result, v, k)
result[k] = fn(v, k)
return result
end

return utils.reduce(map, {}, data)
end, 2)

--- @param fn function
--- @param data table
utils.filter = utils.curry(function(fn, data)
assert(type(fn) == "function", "first argument should be a unary function")
assert(type(data) == "table" and isArray(data), "second argument should be an Array")

local function filter(result, v)
if fn(v) then
table.insert(result, v)
end
return result
end

return utils.reduce(filter, {}, data)
end, 2)

--- @param fn function
--- @param t table
utils.find = utils.curry(function(fn, t)
assert(type(fn) == "function", "first argument should be a unary function")
assert(type(t) == "table", "second argument should be a table that is an array")
for _, v in pairs(t) do
if fn(v) then
return v
end
end
end, 2)

--- @param propName string
--- @param value string
--- @param object table
utils.propEq = utils.curry(function(propName, value, object)
assert(type(propName) == "string", "first argument should be a string")
-- assert(type(value) == "string", "second argument should be a string")
assert(type(object) == "table", "third argument should be a table<object>")

return object[propName] == value
end, 3)

--- @param data table
utils.reverse = function(data)
assert(type(data) == "table", "argument needs to be a table that is an array")
return utils.reduce(function(result, v, i)
result[#data - i + 1] = v
return result
end, {}, data)
end

--- @param ... function
utils.compose = utils.curry(function(...)
local mutations = utils.reverse({ ... })

return function(v)
local result = v
---@diagnostic disable-next-line
for _, fn in pairs(mutations) do
assert(type(fn) == "function", "each argument needs to be a function")
result = fn(result)
end
return result
end
end, 2)

--- @param propName string
--- @param object table
utils.prop = utils.curry(function(propName, object)
return object[propName]
end, 2)

--- @param val any
--- @param t table
utils.includes = utils.curry(function(val, t)
assert(type(t) == "table", "argument needs to be a table")
return utils.find(function(v)
return v == val
end, t) ~= nil
end, 2)

--- @param t table
utils.keys = function(t)
assert(type(t) == "table", "argument needs to be a table")
Expand All @@ -179,26 +13,10 @@ utils.keys = function(t)
return keys
end

--- @param t table
utils.values = function(t)
assert(type(t) == "table", "argument needs to be a table")
local values = {}
for _, value in pairs(t) do
table.insert(values, value)
end
return values
end

function utils.hasMatchingTag(tag, value)
return Handlers.utils.hasMatchingTag(tag, value)
end

---@param msg AoMessage
function utils.reply(msg)
---@diagnostic disable-next-line
Handlers.utils.reply(msg)
end

--- Deep copies a table
--- @param original table The table to copy
--- @return table|nil The deep copy of the table or nil if the original is nil
Expand Down Expand Up @@ -399,7 +217,7 @@ end

--- @param handlers Handlers
--- @description Get the names of all handlers
--- @return table<string>
--- @return string[]
function utils.getHandlerNames(handlers)
local names = {}
for _, handler in ipairs(handlers.list) do
Expand Down Expand Up @@ -485,7 +303,7 @@ function utils.createActionHandler(action, msgHandler, position)
return utils.createHandler("Action", action, msgHandler, position)
end

---@param keywords table<string>
---@param keywords string[]
---@description Validates the keywords
---Amount of keywords must be less than or equal to 16
---Each keyword must be a unique string of 32 characters or less
Expand Down

0 comments on commit 34316ae

Please sign in to comment.