1
- --- @class List
1
+ local class = require (' react.util.class' )
2
+
3
+ --- @class react.List
2
4
--- @field protected list any[]
3
- local M = {}
5
+ local List = class ()
4
6
5
7
--- @param list any[] | nil
6
- function M :new (list )
7
- local o = {
8
- list = list or {},
9
- }
10
-
11
- setmetatable (o , self )
12
- self .__index = self
13
-
14
- return o
8
+ function List :_init (list )
9
+ self .list = list or {}
15
10
end
16
11
17
12
--- Returns the value at given index
18
13
--- @param index number index of the param
19
14
--- @return s any
20
- function M :get (index )
15
+ function List :get (index )
21
16
return self .list [index ]
22
17
end
23
18
24
19
--- Sets the value at given index to given value
25
20
--- @param index number index to set the value at
26
21
--- @param value any value to set
27
- function M :set (index , value )
22
+ function List :set (index , value )
28
23
self .list [index ] = value
29
24
return 0
30
25
end
31
26
32
27
--- Returns an iterator of the list
33
28
--- @return s function
34
- function M :iter ()
29
+ function List :iter ()
35
30
return ipairs (self .list )
36
31
end
37
32
38
33
--- Append new value to the list
39
34
--- @param value any value to append
40
- function M :add (value )
35
+ function List :add (value )
41
36
table.insert (self .list , value )
42
37
end
43
38
44
39
--- Remove existing value from the list by index
45
40
--- @param index number
46
41
--- @return s any value that got removed from the list
47
- function M :remove (index )
42
+ function List :remove (index )
48
43
return table.remove (self .list , index )
49
44
end
50
45
51
46
--- Remove existing value from the list by the value
52
47
--- @param value any value to remove from the list
53
48
--- @return s any value that got removed from the list
54
- function M :remove_by_value (value )
49
+ function List :remove_by_value (value )
55
50
local index = self :find_index (value )
56
51
57
52
if index < 0 then
@@ -64,14 +59,14 @@ function M:remove_by_value(value)
64
59
end
65
60
66
61
--- Remove all the values from the list
67
- function M :remove_all ()
62
+ function List :remove_all ()
68
63
self .list = {}
69
64
end
70
65
71
66
--- Returns the index of a given value if exists
72
67
--- Returns -1 if the value does not exist
73
68
--- @return number
74
- function M :find_index (value )
69
+ function List :find_index (value )
75
70
for index , ele in ipairs (self .list ) do
76
71
if ele == value then
77
72
return index
84
79
--- Returns true if the given value exists in the list
85
80
--- @param value any value to check the existence
86
81
--- @return s boolean
87
- function M :has (value )
82
+ function List :has (value )
88
83
for _ , ele in ipairs (self .list ) do
89
84
if ele == value then
90
85
return true
96
91
97
92
--- Returns the size of the list
98
93
--- @return s number size of the list
99
- function M :length ()
94
+ function List :length ()
100
95
return # self .list
101
96
end
102
97
103
98
--- Concatenate the given list to this list
104
- --- @param list List | any[]
105
- function M :concat (list )
99
+ --- @param list react. List | any[]
100
+ function List :concat (list )
106
101
if list .iter then
107
102
for _ , v in list :iter () do
108
103
self :add (v )
@@ -117,20 +112,20 @@ end
117
112
--- Returns a string after joining the list by given string separator
118
113
--- @param separator string separator to join the list with
119
114
--- @return s string joined string
120
- function M :join (separator )
115
+ function List :join (separator )
121
116
return table.concat (self .list , separator )
122
117
end
123
118
124
119
--- Returns a clone of the current list
125
120
--- @return s List clone of the current list
126
- function M :clone ()
127
- return M : new ( M .deepcopy (self .list ))
121
+ function List :clone ()
122
+ return List ( List .deepcopy (self .list ))
128
123
end
129
124
130
125
--- @private
131
126
--- @param orig table original table to copy
132
127
--- @return s table clone of the table
133
- function M .deepcopy (orig , copies )
128
+ function List .deepcopy (orig , copies )
134
129
copies = copies or {}
135
130
local orig_type = type (orig )
136
131
local copy
@@ -141,7 +136,8 @@ function M.deepcopy(orig, copies)
141
136
copy = {}
142
137
copies [orig ] = copy
143
138
for orig_key , orig_value in next , orig , nil do
144
- copy [M .deepcopy (orig_key , copies )] = M .deepcopy (orig_value , copies )
139
+ copy [List .deepcopy (orig_key , copies )] =
140
+ List .deepcopy (orig_value , copies )
145
141
end
146
142
-- setmetatable(copy, M.deepcopy(getmetatable(orig), copies))
147
143
end
@@ -152,4 +148,4 @@ function M.deepcopy(orig, copies)
152
148
return copy
153
149
end
154
150
155
- return M
151
+ return List
0 commit comments