-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollection.lua
More file actions
212 lines (167 loc) · 4.64 KB
/
Collection.lua
File metadata and controls
212 lines (167 loc) · 4.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
local Collection = {};
Collection.__index = Collection;
-- Class Content
function Collection.new(...)
local res = {};
setmetatable(res, Collection);
res:push(...);
return res;
end
-- Instance Content
---
-- @description The first items in the table. If the magnitude of the amount
-- of equal to 1, returns the value at the index else returns a table of values.
-- By default, the amount is 1
-- @param {number} amount The amount of items to return
-- @returns {*[]}
function Collection:first(amount)
if (type(amount) ~= "number") then
amount = 1;
end
if (amount < 0) then
return self:last(-amount);
else
return self:slice(1, amount);
end
end
---
-- @description The last items in the table. If the magnitude of the amount
-- of equal to 1, returns the value at the index else returns a table of values.
-- By default, the amount is 1
-- @param {number} amount The amount of items to return
-- @returns {*[]}
function Collection:last(amount)
if (type(amount) ~= "number") then
amount = 1;
end
if (amount < 0) then
return self:first(-amount);
else
return self:slice(#self - amount + 1, #self):reverse();
end
end
---
-- @description Retrives a slice of the table, creating a new
-- "Collection" and returning it
-- @param {number} indexInitial The initial index to begin the slice
-- @param {number} amount The maximum amount of entries to return
-- @returns {Collection}
function Collection:slice(indexInitial, amount)
if (type(amount) ~= "number") then
amount = 1;
end
return Collection.new(unpack(self, indexInitial, indexInitial + amount - 1));
end
---
-- @description Empties the table
-- @returns {@self}
function Collection:clear()
self:splice(1, #self);
return self;
end
---
-- @description Removes an item from the collection
-- @param {...number} The indexes to remove
-- @returns {@self}
function Collection:remove(...)
local removed = 0;
for _, index in ipairs({...}) do
if (index > 0 and index <= #self) then
table.remove(self, index - removed);
removed += 1;
end
end
return self;
end
---
-- @description Reverses the order of items
-- @returns {@self}
function Collection:reverse()
table.foreach({ table.unpack(self, 1, math.floor(#self / 2)) }, function(i, v)
self[i], self[#self - i + 1] = self[#self - i + 1], self[i];
end)
return self;
end
---
-- @description Inserts items starting at a specified index
-- @param {number} index The index to start at
-- @param {*...} The items to add
-- @returns {@self}
function Collection:insert(index, ...)
table.foreach({ ... }, function(i, v)
table.insert(self, index + i - 1, v);
end)
return self;
end
---
-- @description Removes items starting at specified index then inserts items starting at a specified index
-- @param {number} index The index to start at
-- @param {number} index The amount of items to remove
-- @param {*...} The items to add
-- @returns {@self}
function Collection:splice(index, amount, ...)
for i = 1, amount, 1 do
table.remove(self, index);
end
self:insert(index, ...);
return self;
end
---
-- @description Adds items to the end of the table
-- @param {*...} The items to add
-- @returns {@self}
function Collection:push(...)
table.foreach({ ... }, function(i, v)
self:insert(1 + #self, v);
end)
return self;
end
---
-- @description Adds items to the beginning of the table
-- @param {*...} The items to add
-- @returns {@self}
function Collection:unshift(...)
table.foreach({ ... }, function(i, v)
self:insert(1, v);
end)
return self;
end
---
-- @description Checks whether all of the entered items can be found
-- in the table. If no items are entered, returns false
-- @param {*...} The items to check for
-- @returns {boolean} Whether all of the items were found in the table
function Collection:includes(...)
if (table.getn({ ... }) == 0) then
return false;
end
local bool = true;
for _, v in ipairs({ ... }) do
if (table.find(self, v) == nil) then
bool = false;
break;
end
end
return bool;
end
---
-- @description Retrieves the index for specified item or returns 0
-- @param {number} indexInitial The index to start at
-- @param {*} value The value to look for
-- @returns {number}
function Collection:findIndex(indexInitial, value)
return table.find(self, value, indexInitial) or 0;
end
---
-- @description Retrieves the indexes for the specified items or returns an empty Collection
-- @param {number} indexInitial The index to start at
-- @param {*...} The values to look for
-- @returns {Collection}
function Collection:findIndexMultiple(indexInitial, ...)
local indexes = Collection.new();
for _, v in ipairs({...}) do
indexes:push(self:findIndex(indexInitial, v));
end
return indexes;
end
return Collection;