@thigasdevelopment/luam 0.19.12 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lua/string.lua CHANGED
@@ -1,82 +1,98 @@
1
- local function trim(value)
2
- if type(value) ~= 'string' then
3
- return tostring(value)
4
- end
5
-
6
- return value:match('^%s*(.-)%s*$') or ''
1
+ ---@param value any
2
+ ---@return string
3
+ local function trim (value)
4
+ if (type (value) ~= 'string') then
5
+ return tostring (value);
6
+ end
7
+
8
+ return value:match ('^%s*(.-)%s*$') or '';
7
9
  end
8
10
 
9
- local function resolve(context, path)
10
- local current = context
11
-
12
- for key in path:gmatch('[^%.]+') do
13
- if type(current) ~= 'table' then
14
- return nil
15
- end
11
+ ---@param context table
12
+ ---@param path string
13
+ ---@return any
14
+ local function resolve (context, path)
15
+ local current = context;
16
16
 
17
- current = current[key]
17
+ for key in path:gmatch ('[^%.]+') do
18
+ if (type (current) ~= 'table') then
19
+ return nil;
20
+ end
18
21
 
19
- if current == nil then
20
- return nil
21
- end
22
- end
22
+ current = current[key];
23
+ if (current == nil) then
24
+ return nil;
25
+ end
26
+ end
23
27
 
24
- return current
28
+ return current;
25
29
  end
26
30
 
27
- local function parse(raw)
28
- local key, rest = raw:match('^(.-):(.+)$')
31
+ ---@param raw string
32
+ ---@return string, string?
33
+ local function parse (raw)
34
+ local key, rest = raw:match ('^(.-):(.+)$');
35
+ if (not key) then
36
+ return raw, nil;
37
+ end
29
38
 
30
- if not key then
31
- return raw, nil
32
- end
33
-
34
- return key, trim(rest:match('^([^:]+)'))
39
+ return key, trim (rest:match ('^([^:]+)'));
35
40
  end
36
41
 
37
- function string.trim(value)
38
- return trim(value)
42
+ ---@param value any
43
+ ---@return string
44
+ function string.trim (value)
45
+ return trim (value);
39
46
  end
40
47
 
41
- function string.startsWith(value, prefix)
42
- if type(value) ~= 'string' or type(prefix) ~= 'string' then
43
- return false
44
- end
48
+ ---@param value string
49
+ ---@param prefix string
50
+ ---@return boolean
51
+ function string.startsWith (value, prefix)
52
+ if (type (value) ~= 'string') or (type (prefix) ~= 'string') then
53
+ return false;
54
+ end
45
55
 
46
- return value:sub(1, prefix:len()) == prefix
56
+ return value:sub (1, prefix:len ()) == prefix;
47
57
  end
48
58
 
49
- function string.endsWith(value, suffix)
50
- if type(value) ~= 'string' or type(suffix) ~= 'string' then
51
- return false
52
- end
59
+ ---@param value string
60
+ ---@param suffix string
61
+ ---@return boolean
62
+ function string.endsWith (value, suffix)
63
+ if (type (value) ~= 'string') or (type (suffix) ~= 'string') then
64
+ return false;
65
+ end
53
66
 
54
- if suffix == '' then
55
- return true
56
- end
67
+ if (suffix == '') then
68
+ return true;
69
+ end
57
70
 
58
- return value:sub(-suffix:len()) == suffix
71
+ return value:sub (-suffix:len ()) == suffix;
59
72
  end
60
73
 
61
- function string.template(text, context)
62
- if type(text) ~= 'string' then
63
- return tostring(text)
64
- end
65
-
66
- if type(context) ~= 'table' or next(context) == nil then
67
- return text
68
- end
69
-
70
- return (
71
- text:gsub('%${(.-)}', function(raw)
72
- local key, default = parse(trim(raw))
73
- local value = resolve(context, trim(key))
74
-
75
- if value == nil then
76
- return default ~= nil and default or ''
77
- end
78
-
79
- return tostring(value)
80
- end)
81
- )
74
+ ---@param text string
75
+ ---@param context table
76
+ ---@return string
77
+ function string.template (text, context)
78
+ if (type (text) ~= 'string') then
79
+ return tostring (text);
80
+ end
81
+
82
+ if (type (context) ~= 'table') or (next (context) == nil) then
83
+ return text;
84
+ end
85
+
86
+ return (
87
+ text:gsub ('%${(.-)}', function (raw)
88
+ local key, default = parse (trim (raw));
89
+ local value = resolve (context, trim (key));
90
+
91
+ if (value == nil) then
92
+ return default ~= nil and default or '';
93
+ end
94
+
95
+ return tostring (value);
96
+ end)
97
+ );
82
98
  end
package/lua/table.lua CHANGED
@@ -1,63 +1,71 @@
1
- function table.size(value)
2
- local size = 0
3
-
4
- if type(value) ~= 'table' then
5
- return size
6
- end
7
-
8
- for _ in pairs(value) do
9
- size = size + 1
10
- end
11
-
12
- return size
1
+ ---@param value table
2
+ ---@return number
3
+ function table.size (value)
4
+ local size = 0;
5
+ if (type (value) ~= 'table') then
6
+ return size;
7
+ end
8
+
9
+ for _ in pairs (value) do
10
+ size = size + 1;
11
+ end
12
+
13
+ return size;
13
14
  end
14
15
 
15
- function table.isEmpty(value)
16
- if type(value) ~= 'table' then
17
- return true
18
- end
16
+ ---@param value table
17
+ ---@return boolean
18
+ function table.isEmpty (value)
19
+ if (type (value) ~= 'table') then
20
+ return true;
21
+ end
19
22
 
20
- return next(value) == nil
23
+ return next (value) == nil;
21
24
  end
22
25
 
23
- function table.keys(value)
24
- local keys = {}
25
-
26
- if type(value) ~= 'table' then
27
- return keys
28
- end
26
+ ---@param value table
27
+ ---@return table
28
+ function table.keys (value)
29
+ local keys = { };
30
+ if (type (value) ~= 'table') then
31
+ return keys;
32
+ end
29
33
 
30
- for key in pairs(value) do
31
- keys[#keys + 1] = key
32
- end
34
+ for key in pairs (value) do
35
+ keys[#keys + 1] = key;
36
+ end
33
37
 
34
- return keys
38
+ return keys;
35
39
  end
36
40
 
37
- function table.values(value)
38
- local values = {}
41
+ ---@param value table
42
+ ---@return table
43
+ function table.values (value)
44
+ local values = { };
45
+ if (type (value) ~= 'table') then
46
+ return values;
47
+ end
39
48
 
40
- if type(value) ~= 'table' then
41
- return values
42
- end
49
+ for _, item in pairs (value) do
50
+ values[#values + 1] = item;
51
+ end
43
52
 
44
- for _, item in pairs(value) do
45
- values[#values + 1] = item
46
- end
47
-
48
- return values
53
+ return values;
49
54
  end
50
55
 
51
- function table.includes(value, item)
52
- if type(value) ~= 'table' then
53
- return false
54
- end
55
-
56
- for _, current in pairs(value) do
57
- if current == item then
58
- return true
59
- end
60
- end
61
-
62
- return false
56
+ ---@param value table
57
+ ---@param item any
58
+ ---@return boolean
59
+ function table.includes (value, item)
60
+ if (type (value) ~= 'table') then
61
+ return false;
62
+ end
63
+
64
+ for _, current in pairs (value) do
65
+ if (current == item) then
66
+ return true;
67
+ end
68
+ end
69
+
70
+ return false;
63
71
  end