@@ -44,7 +44,6 @@ getPublicArray(array = []): PublicArray
44
44
// let arr = getPublicArray([1,2,3,4,5]);
45
45
// Or, instantiate with an empty array:
46
46
// let arr = getPublicArray();
47
-
48
47
```
49
48
50
49
### Properties
@@ -94,10 +93,10 @@ get.copy(): any[]
94
93
// Returns independent copy of the array.
95
94
96
95
get.byIndex(index): any
97
- // Returns 1 item. index can be negative or positive.
96
+ // Returns item identified by passed index . index can be negative or positive.
98
97
99
98
get.byIndexes(indexes): any[]
100
- // indexes can be negative or positive.
99
+ // Returns items identified by passed indexes. indexes can be negative or positive.
101
100
102
101
get.head(numItems): any[]
103
102
// returns numItems from beginning
@@ -108,7 +107,7 @@ get.tail(numItems): any[]
108
107
get.between(numItemsToIgnoreAtEachEnd): any[]
109
108
// Returns middle of array, between numItemsToIgnoreAtEachEnd
110
109
111
- get.adjacentAt(startingIndex, numItems ): any[]
110
+ get.adjacentAt(startingIndex, numItemsToGet ): any[]
112
111
// Returns adjacent items. startingIndex can be negative or positive.
113
112
114
113
get.adjacentToValue(info: IAdjacentToValueInfo): any[]
@@ -123,22 +122,24 @@ get.adjacentToValue(info: IAdjacentToValueInfo): any[]
123
122
howMany: integer greater than zero (it's how many adjacent items to return)
124
123
}
125
124
Example:
126
- // arr .data is [1,2,3,4,5,6,7,8,9,10]
127
- let numbers = arr .get.adjacentToValue({value:5, offset: -2, howMany:3});
125
+ // this .data is [1,2,3,4,5,6,7,8,9,10]
126
+ let numbers = this .get.adjacentToValue({value:5, offset: -2, howMany:3});
128
127
// numbers is now [3,4,5]
129
128
***************/
130
129
130
+ // For all the functions below, the parameter 'value' cannot be object.
131
+
131
132
get.allAfterFirst(value): any[]
132
- // value cannot be object
133
+ // returns all items after the first instance of value.
133
134
134
135
get.allBeforeFirst(value): any[]
135
- // value cannot be object
136
+ // returns all items before the first instance of value.
136
137
137
138
get.allAfterLast(value): any[]
138
- // value cannot be object
139
+ // returns all items after the last instance of value.
139
140
140
141
get.allBeforeLast(value): any[]
141
- // value cannot be object
142
+ // returns all items before the last instance of value.
142
143
143
144
get.uniqueItems(): any[]
144
145
// returns no duplicates.
@@ -147,7 +148,7 @@ get.duplicates(): any[]
147
148
// returns every instance of a duplicate, so you may get multiple instances.
148
149
149
150
get.shuffled(): any[]
150
- // returns new, shuffled version of the array
151
+ // returns new version of the array with the order of items randomized.
151
152
152
153
get.byTest(testFunction: ((currentValue, currentIndex?, array?) => boolean)): IValueIndexPair[]
153
154
/***************
@@ -159,27 +160,29 @@ get.byTest(testFunction: ((currentValue, currentIndex?, array?) => boolean)): IV
159
160
get.byType(
160
161
type: 'object' | 'array' | 'number' | 'string' | 'boolean' | 'function' | 'undefined'
161
162
): IValueIndexPair[]
162
- // For explanation of IValueIndexPair, see explanation of byTest().
163
+ // For explanation of IValueIndexPair, see explanation of get. byTest().
163
164
```
164
165
165
166
#### getAndRemove: PublicArrayGetterRemover (read-only)
166
167
###### Has methods that both remove and return items from the array:
167
168
```
168
169
getAndRemove.byIndex(index): any
169
- // index can be negative or positive.
170
+ // removes and returns item identified by passed index. index can be negative or positive.
170
171
171
172
getAndRemove.byIndexes(indexes): any[]
172
- // indexes can be negative or positive.
173
+ // removes and returns items identified by passed indexes. indexes can be negative or positive.
173
174
174
175
getAndRemove.head(numItemsToRemove): any[]
176
+ // removes and returns numItemsToRemove from beginning
175
177
176
178
getAndRemove.tail(numItemsToRemove): any[]
179
+ // removes and returns numItemsToRemove from end
177
180
178
181
getAndRemove.between(numItemsToKeepAtEachEnd): any[]
179
- // Returns middle of array, between numItemsToIgnoreAtEachEnd
182
+ // removes and returns middle of array, between numItemsToKeepAtEachEnd
180
183
181
184
getAndRemove.adjacentAt(startingIndex, numItemsToRemove): any[]
182
- // startingIndex can be negative or positive.
185
+ // removes and returns adjacent items. startingIndex can be negative or positive.
183
186
184
187
getAndRemove.adjacentToValue(info: IAdjacentToValueInfo): any[]
185
188
/********
@@ -193,38 +196,39 @@ getAndRemove.adjacentToValue(info: IAdjacentToValueInfo): any[]
193
196
howMany: integer greater than zero (it's how many adjacent items to remove/return)
194
197
}
195
198
Example:
196
- // arr .data is [1,2,3,4,5,6,7,8,9,10]
197
- let numbers = arr .getAndRemove.adjacentToValue({value:5, offset: -2, howMany:3});
198
- // numbers is now [3,4,5]. getAndRemove .data is now [1,2,6,7,8,9,10]
199
+ // this .data is [1,2,3,4,5,6,7,8,9,10]
200
+ let numbers = this .getAndRemove.adjacentToValue({value:5, offset: -2, howMany:3});
201
+ // numbers is now [3,4,5]. this .data is now [1,2,6,7,8,9,10]
199
202
*********/
200
203
201
204
// For all the functions below, the parameter 'value' cannot be object.
202
205
203
206
getAndRemove.allAfterFirst(value): any[]
204
- // Removes and returns everything after first instance of value
207
+ // removes and returns all items after first instance of value
205
208
206
209
getAndRemove.allBeforeFirst(value): any[]
207
- // Removes and returns everything before first instance of value
210
+ // removes and returns all items before first instance of value
208
211
209
212
getAndRemove.allAfterLast(value): any[]
210
- // Removes and returns everything after last instance of value
213
+ // removes and returns all items after last instance of value
211
214
212
215
getAndRemove.allBeforeLast(value): any[]
213
- // Removes and returns everything before last instance of value
216
+ // removes and returns all items before last instance of value
214
217
215
218
getAndRemove.duplicates(): any[]
216
219
// removes and returns every instance of a duplicate, so you may receive multiple instances.
217
220
218
221
/************
219
222
These last 2 methods both return an array of IValueIndexPairs. A IValueIndexPair looks like this:
220
-
221
223
{value: any, index: number}
222
224
223
225
Each one represents a removed array item.
224
226
************/
225
227
226
- getAndRemove.byTest(testFunction: (currentValue, currentIndex?, array?) => boolean): IValueIndexPair[]
227
- // Gets and removes any value that passes test.
228
+ getAndRemove.byTest(
229
+ testFunction: (currentValue, currentIndex?, array?) => boolean
230
+ ): IValueIndexPair[]
231
+ // removes and returns any value that passes test.
228
232
229
233
getAndRemove.byType(
230
234
type: 'object' | 'array' | 'number' | 'string' | 'boolean' | 'function' | 'undefined'
@@ -241,8 +245,8 @@ insert.at(index, values: any[]): PublicArrayInserter
241
245
insert.middle(values: any[], offset = 0): PublicArrayInserter
242
246
// inserts values in middle of the array.
243
247
// By default, if the array has odd number of items, values will be inserted just before the
244
- // middle item. If you want to change the insert position, set the optional offset parameter to +
245
- // or - whatever integer you want.
248
+ // middle item. If you want to change the insert position, set the optional offset parameter
249
+ // to + or - whatever integer you want.
246
250
```
247
251
248
252
#### remove: PublicArrayRemover (read-only)
@@ -316,8 +320,8 @@ remove.allBeforeLast(value): PublicArrayRemover
316
320
317
321
remove.duplicates(): PublicArrayRemover
318
322
319
- remove.byTest(testFunction: (currentItem , currentIndex?, array?) => boolean): PublicArrayRemover
320
- // if currentItem passes test, it is removed.
323
+ remove.byTest(testFunction: (currentValue , currentIndex?, array?) => boolean): PublicArrayRemover
324
+ // if currentValue passes test, it is removed.
321
325
322
326
remove.byType(
323
327
type: 'object' | 'array' | 'number' | 'string' | 'boolean' | 'function' | 'undefined'
@@ -358,6 +362,12 @@ replace.between(numItemsToKeepAtEachEnd, newValues: any[]): PublicArrayReplacer
358
362
// Example: if this.data is [1,2,3,4,5,6,7] , and you call .between(2, [9,10])
359
363
// this.data will be [1,2,9,10,6,7] . It preserves the first 2 items and the last 2.
360
364
365
+ /************
366
+ For all the functions below:
367
+ Any parameter called 'value' cannot be an object.
368
+ Any parameter called 'values' cannot contain an object.
369
+ ************/
370
+
361
371
replace.firstOf(value, newValue): PublicArrayReplacer
362
372
// Replaces first instance of value with newValue.
363
373
@@ -370,14 +380,13 @@ replace.allOf(value, newValue): PublicArrayReplacer
370
380
replace.allOfEach(values: any[], newValues: any[]): PublicArrayReplacer
371
381
// All instances of values[i] found in array get replaced with newValues[i].
372
382
373
- replace.each(replacementFunction: (item, index ?, array?) => any): PublicArrayReplacer
383
+ replace.each(replacementFunction: (currentValue, currentIndex ?, array?) => any): PublicArrayReplacer
374
384
/**********
375
385
Loops thru array, passing each item into replacementFunction.
376
- replacementFunction signature: function(item, index?, array?): any
377
386
replacementFunction must return the new value you want to give to that index in the array.
378
387
Example:
379
388
// this.data is [1,2,3,4,5,6] .
380
- // this.each((item) => {
389
+ // this.replace. each((item) => {
381
390
// if (item === 2 || item === 6) return item * 3;
382
391
// else return item;
383
392
// });
@@ -390,6 +399,22 @@ replace.allWithOne(values: any[], newValue): PublicArrayReplacer
390
399
```
391
400
392
401
#### sort: PublicArraySorter (read-only)
402
+ ###### Has methods that change the order of the items and return the PublicArraySorter instance:
403
+ ```
404
+ sort.alphabetize(): PublicArraySorter;
405
+ // No item in the array gets modified, but each is treated as a string during the sorting.
406
+
407
+ sort.numbersAscending(): PublicArraySorter;
408
+ // If not all items in array are of type 'number', it triggers error.
409
+
410
+ sort.numbersDescending(): PublicArraySorter;
411
+ // If not all items in array are of type 'number', it triggers error.
412
+
413
+ sort.reverse(): PublicArraySorter;
414
+
415
+ sort.shuffle(): PublicArraySorter;
416
+ // randomizes the order of items.
417
+ ```
393
418
394
419
#### className: string (read-only)
395
420
@@ -399,31 +424,41 @@ replace.allWithOne(values: any[], newValue): PublicArrayReplacer
399
424
```
400
425
asString(glue = ', '): string
401
426
// Does same thing as Array.join()
427
+
428
+ /************
429
+ For all the functions below:
430
+ Any parameter called 'value' cannot be an object.
431
+ Any parameter called 'values' cannot contain an object.
432
+ ************/
402
433
403
434
has(value): boolean
404
- // returns false if value is object .
435
+ // returns true if array contains value .
405
436
406
437
hasAll(values: any[]): boolean
407
- // returns false if values contains object .
438
+ // returns true if array contains every value in values .
408
439
409
440
hasAny(values: any[]): boolean
441
+ // returns true if array contains at least 1 value in values.
410
442
411
443
hasAdjacent(values: any[]): boolean
412
- // returns false if values contains object.
444
+ // returns true if array contains exact sequence of values.
445
+ // Example: if this.data is [10,1,2,3,11], then this.hasAdjacent([1,2,3]) returns true.
413
446
414
447
startsWith(values: any[]): boolean
415
- // returns false if values contains object .
448
+ // returns true if array starts with exact sequence of values .
416
449
417
450
endsWith(values: any[]): boolean
418
- // returns false if values contains object .
451
+ // returns true if array ends with exact sequence of values .
419
452
420
453
matches(array): boolean
421
- // returns false if array contains object.
454
+ // returns true if entire array matches passed array exactly.
455
+ // returns false if passed array contains object.
422
456
423
457
// For the next 3 methods:
424
458
// testFunction is a callback with same signature as callback passed to
425
459
// Array.filter() :
426
- // testFunction(value, index?, theArray?): checks if value passes test. If yes, it returns true.
460
+ // testFunction(currentValue, currentIndex?, entireArray?): boolean
461
+ // checks if currentValue passes test. If yes, it returns true.
427
462
428
463
allPass(testFunction): boolean
429
464
// returns true if all items pass test.
@@ -435,20 +470,23 @@ indexesThatPass(testFunction): number[]
435
470
// returns all indexes of items that pass test.
436
471
437
472
firstIndexOf(value): number
438
- // Does not work if value is object .
473
+ // returns index of first instance of value in array. If not found, returns -1 .
439
474
440
475
lastIndexOf(value): number
441
- // Does not work if value is object .
476
+ // returns index of last instance of value in array. If not found, returns -1 .
442
477
443
478
indexesOf(value): number[]
444
- // Does not work if value is object .
479
+ // returns all indexes of value in array. If not found, returns empty array .
445
480
446
481
append(values: any[]): this
482
+ // attaches values to end of array.
447
483
448
484
prepend(values: any[]): this
485
+ // attaches values to beginning of array.
449
486
450
487
forEach(iterationFunction): this
451
- // iterationFunction = function(currentItem, currentIndex?, entireArray?){...}
488
+ // Behaves same as Array.forEach()
489
+ // iterationFunction = function(currentValue, currentIndex?, entireArray?){...}
452
490
453
491
protected _createGetterAndOrSetterForEach(
454
492
propertyNames: string[],
@@ -469,12 +507,11 @@ runMethod_and_returnThis(
469
507
470
508
471
509
472
- ## Usage
510
+ ## Usage Examples
473
511
474
512
```
475
513
// changing the array content:
476
514
arr.data = [1,2,3,4,5,6,7];
477
-
478
515
```
479
516
480
517
## Performance
0 commit comments