From 901cb381b421f2c6984ab6360c4f5898b25e5354 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Fri, 12 Nov 2021 10:16:16 +1100 Subject: [PATCH] feat(table): add functions to the standard table lib Can overwrite the default table functions with `table = import 'table'`, adding functions to check if value(s) exist or match a table. --- imports/mysql/server.lua | 4 +++ imports/table/shared.lua | 53 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 imports/table/shared.lua diff --git a/imports/mysql/server.lua b/imports/mysql/server.lua index 91bc083..0dc236b 100644 --- a/imports/mysql/server.lua +++ b/imports/mysql/server.lua @@ -1,3 +1,7 @@ +--------------------------------------------------------------------------- +--- MySQL-Async like library for OxMySQL, without the function name aliases +--------------------------------------------------------------------------- + local MySQL = {} MySQL.ready = function(cb) diff --git a/imports/table/shared.lua b/imports/table/shared.lua new file mode 100644 index 0000000..b70792a --- /dev/null +++ b/imports/table/shared.lua @@ -0,0 +1,53 @@ +---------------------------------------------------------- +--- Add additional functions to the standard table library +---------------------------------------------------------- + +local table = table +local pairs = pairs + +local function contains(tbl, value) + if type(value) == 'string' then + for _, v in pairs(tbl) do + if v == value then return true end + end + else + local matched_values = 0 + local values = 0 + for _, v1 in pairs(value) do + values += 1 + + for _, v2 in pairs(tbl) do + if v1 == v2 then matched_values += 1 end + end + end + if matched_values == values then return true end + end + return false +end +table.contains = contains + +local function match(t1, t2) + if t1 == t2 then return true end + if type(t1) ~= 'table' then return false end + + local matched_values = 0 + local values = 0 + + for _, v in pairs(t1) do + values += 1 + if type(t2) == 'table' then + for _, v2 in pairs(t2) do + if v == v2 then + matched_values += 1 + elseif match(v, v2) then + values += 1 + matched_values += #v + end + end + elseif v == t2 then matched_values += 1 end + end + return matched_values == values +end +table.match = match + +return table \ No newline at end of file