Skip to content

Commit 3faaf45

Browse files
committed
document all new array methods
1 parent c9e55b6 commit 3faaf45

File tree

1 file changed

+144
-26
lines changed

1 file changed

+144
-26
lines changed

pages/ox_lib/Modules/Array/Shared.mdx

+144-26
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,53 @@ A table used as a prototype for the Array class. Functions can be used as class
88

99
Constructs instance of Array containing the given elements.
1010

11+
- ...: `any`
12+
1113
```lua
1214
lib.array:new(...)
1315
```
1416

15-
- ...: `any`
16-
1717
**Returns:**
1818

1919
- arr: `Array`
2020

21-
### lib.isArray
21+
### lib.array.from
2222

23-
Determines if the given table is an instance of Array or an array-like table.
23+
Creates a new array from an iterable value.
24+
25+
- iter: `function` | `table` | `string`
26+
- Iterator functions such as string.gmatch can be used to construct arrays.
2427

2528
```lua
26-
lib.array.isArray(tbl)
29+
lib.array.from(iter)
2730
```
2831

29-
- tbl: `table`
32+
**Returns:**
33+
34+
- arr: `Array`
35+
36+
### lib.array.at
37+
38+
Returns the element at the given index, with negative numbers counting backwards from the end of the array.
39+
40+
- index: `integer`
41+
42+
```lua
43+
lib.array.at(index)
44+
```
3045

3146
**Returns:**
3247

33-
- isArray: `boolean`
48+
- element: `unknown`
3449

3550
### lib.array.merge
3651

37-
Combines the elements of two arrays into a new array.
52+
Combines the elements of two or more arrays into a new array.
3853

39-
- a: `Array`
40-
- b: `Array`
54+
- ...: `Array`
4155

4256
```lua
43-
lib.array.merge(a, b)
57+
lib.array.merge(...)
4458
```
4559

4660
**Returns:**
@@ -52,7 +66,7 @@ lib.array.merge(a, b)
5266
Tests if all elements in an array succeed in passing the provided test function.
5367

5468
- arr: `Array`
55-
- testFn: `function`
69+
- testFn: `function(element: unknown): boolean`
5670

5771
```lua
5872
lib.array.every(arr, testFn)
@@ -62,32 +76,49 @@ lib.array.every(arr, testFn)
6276

6377
- success: `boolean`
6478

79+
### lib.array.fill
80+
81+
Sets all elements within a range to the given value and returns the modified array.
82+
83+
- arr: `Array`
84+
- value: `any`
85+
- start?: `integer`
86+
- endIndex?: `integer`
87+
88+
```lua
89+
lib.array.fill(arr, value, start, endIndex)
90+
```
91+
92+
**Returns:**
93+
94+
- arr: `Array`
95+
6596
### lib.array.filter
6697

6798
Creates a new array containing the elements from an array that pass the provided test function.
6899

69100
- arr: `Array`
70-
- testFn: `function`
101+
- testFn: `function(element: unknown): boolean`
71102

72103
```lua
73104
lib.array.filter(arr, testFn)
74105
```
75106

76107
**Returns:**
77108

78-
- arr: `Array`
109+
- newArr: `Array`
79110

80111
### lib.array.find
81112

82113
Returns the first element of an array the passes the provided test function.
83114

84115
- arr: `Array`
85-
- testFn: `function`
116+
- testFn: `function(element: unknown): boolean`
86117
- reverse?: `boolean`
87118
- Iterate over the array in reverse order.
88119

89120
```lua
90-
lib.array.find(arr, function(element) end, reverse)
121+
lib.array.find(arr, testFn, reverse)
91122
```
92123

93124
**Returns:**
@@ -99,17 +130,17 @@ lib.array.find(arr, function(element) end, reverse)
99130
Returns the index of the first element of an array the passes the provided test function.
100131

101132
- arr: `Array`
102-
- testFn: `function`
133+
- testFn: `function(element: unknown): boolean`
103134
- reverse?: `boolean`
104135
- Iterate over the array in reverse order.
105136

106137
```lua
107-
lib.array.findIndex(arr, function(element) end, reverse)
138+
lib.array.findIndex(arr, testFn, reverse)
108139
```
109140

110141
**Returns:**
111142

112-
- index: `number`
143+
- index: `integer`
113144

114145
### lib.array.indexOf
115146

@@ -126,22 +157,22 @@ lib.array.indexOf(arr, value, reverse)
126157

127158
**Returns:**
128159

129-
- index: `number`
160+
- index: `integer`
130161

131162
### lib.array.forEach
132163

133164
Executes the provided function for each element in an array.
134165

135166
- arr: `Array`
136-
- cb: `function`
167+
- cb: `function(element: unknown)`
137168

138169
```lua
139-
lib.array.forEach(arr, function(element) end, reverse)
170+
lib.array.forEach(arr, cb, reverse)
140171
```
141172

142173
**Returns:**
143174

144-
- index: `number`
175+
- index: `integer`
145176

146177
### lib.array.join
147178

@@ -158,6 +189,20 @@ lib.array.join(arr, seperator)
158189

159190
- str: `string`
160191

192+
### lib.array.map
193+
194+
Create a new array containing the results from calling the provided function on each element in an array.
195+
196+
- fn: `function(element: unknown, index: integer, arr: Array): unknown`
197+
198+
```lua
199+
lib.array.map(arr, fn)
200+
```
201+
202+
**Returns:**
203+
204+
- newArr: `Array`
205+
161206
### lib.array.pop
162207

163208
Removes the last element from an array and returns the value.
@@ -185,7 +230,7 @@ lib.array.push(arr, ...)
185230

186231
**Returns:**
187232

188-
- length: `number`
233+
- length: `integer`
189234

190235
### lib.array.shift
191236

@@ -201,19 +246,92 @@ lib.array.shift(arr)
201246

202247
- element: `unknown`
203248

249+
### lib.array.slice
250+
251+
Creates a shallow copy of a portion of an array as a new array.
252+
253+
- arr: `Array`
254+
- start?: `integer`
255+
- finish?: `integer`
256+
257+
```lua
258+
lib.array.slice(arr, start, finish)
259+
```
260+
261+
**Returns:**
262+
263+
- newArr: `Array`
264+
265+
### lib.array.toReversed
266+
267+
Creates a new array with the order of its elements reversed from the given array.
268+
269+
- arr: `Array`
270+
271+
```lua
272+
lib.array.toReversed(arr)
273+
```
274+
275+
**Returns:**
276+
277+
- newArr: `Array`
278+
279+
### lib.array.unshift
280+
281+
Inserts new elements at the start of an array and returns the new array length.
282+
283+
- arr: `Array`
284+
- ...: `any`
285+
286+
```lua
287+
lib.array.unshift(arr, ...)
288+
```
289+
290+
**Returns:**
291+
292+
- length: `integer`
293+
204294
### lib.array.reduce
205295

206296
The "reducer" function is applied to every element in an array, with the previous result serving as the accumulator.
207297
If an initial value is provided it's used as the accumulator for the first index; otherwise iteration starts at the second index, with the first index as the accumulator.
208298

209299
- arr: `Array`
210-
- reducer: `function`
300+
- reducer: `function(accumulator: unknown, element: unknown, index?: integer)`
211301
- initialValue?: `any`
212302

213303
```lua
214-
lib.array.reduce(arr, function(accumulator, element, index) end)
304+
lib.array.reduce(arr, reducer)
215305
```
216306

217307
**Returns:**
218308

219309
- accumulator: `unknown`
310+
311+
### lib.array.reverse
312+
313+
Reverses the order of elements inside an array.
314+
315+
- arr: `Array`
316+
317+
```lua
318+
lib.array.reverse(arr)
319+
```
320+
321+
**Returns:**
322+
323+
- arr: `Array`
324+
325+
### lib.isArray
326+
327+
Determines if the given table is an instance of Array or an array-like table.
328+
329+
- tbl: `table`
330+
331+
```lua
332+
lib.array.isArray(tbl)
333+
```
334+
335+
**Returns:**
336+
337+
- isArray: `boolean`

0 commit comments

Comments
 (0)