Skip to content

Commit 1e43f55

Browse files
authored
fix: Add function validateVariadicArgs (#116)
Signed-off-by: Edmond <edomondja@gmail.com>
1 parent ac8e060 commit 1e43f55

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/util/BuiltInFunctions.lua

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,20 @@ local posix = require("posix.fnmatch")
1717

1818
local BuiltInFunctions = {}
1919

20+
function BuiltInFunctions.validateVariadicArgs(expectedLen, args)
21+
if #args ~= expectedLen then
22+
return error("Expected"..expectedLen.." arguments, but got "..#args)
23+
end
24+
for i=1,expectedLen do
25+
if type(args[i])~="string" then
26+
return error("Argument must be a string")
27+
end
28+
end
29+
end
30+
2031
-- Wrapper for keyMatch
2132
function BuiltInFunctions.keyMatchFunc(args)
22-
if #args<2 then
23-
error("BuiltInFunctions should have atleast 2 arguments")
24-
end
33+
BuiltInFunctions.validateVariadicArgs(2, args)
2534
return BuiltInFunctions.keyMatch(args[1], args[2])
2635
end
2736

@@ -42,9 +51,7 @@ end
4251

4352
-- Wrapper for keyGet
4453
function BuiltInFunctions.keyGetFunc(args)
45-
if #args<2 then
46-
error("BuiltInFunctions should have atleast 2 arguments")
47-
end
54+
BuiltInFunctions.validateVariadicArgs(2, args)
4855
return BuiltInFunctions.keyGet(args[1], args[2])
4956
end
5057

@@ -67,9 +74,7 @@ end
6774

6875
-- Wrapper for keyMatch2
6976
function BuiltInFunctions.keyMatch2Func(args)
70-
if #args<2 then
71-
error("BuiltInFunctions should have atleast 2 arguments")
72-
end
77+
BuiltInFunctions.validateVariadicArgs(2, args)
7378
return BuiltInFunctions.keyMatch2(args[1], args[2])
7479
end
7580

@@ -84,9 +89,7 @@ end
8489

8590
-- Wrapper for keyMatch3
8691
function BuiltInFunctions.keyMatch3Func(args)
87-
if #args<2 then
88-
error("BuiltInFunctions should have atleast 2 arguments")
89-
end
92+
BuiltInFunctions.validateVariadicArgs(2, args)
9093
return BuiltInFunctions.keyMatch3(args[1], args[2])
9194
end
9295

@@ -100,9 +103,7 @@ end
100103

101104
-- Wrapper for regexMatch
102105
function BuiltInFunctions.regexMatchFunc(args)
103-
if #args<2 then
104-
error("BuiltInFunctions should have atleast 2 arguments")
105-
end
106+
BuiltInFunctions.validateVariadicArgs(2, args)
106107
return BuiltInFunctions.regexMatch(args[1], args[2])
107108
end
108109

@@ -118,9 +119,7 @@ end
118119

119120
-- Wrapper for globMatch
120121
function BuiltInFunctions.globMatchFunc(args)
121-
if #args<2 then
122-
error("BuiltInFunctions should have atleast 2 arguments")
123-
end
122+
BuiltInFunctions.validateVariadicArgs(2, args)
124123
return BuiltInFunctions.globMatch(args[1], args[2])
125124
end
126125

0 commit comments

Comments
 (0)