2
2
3
3
A TypeScript/JavaScript class for general array manipulation.
4
4
5
+ The main reason you would use PublicArray is if you hate JavaScript's built-in Array methods,
6
+ like ` .slice() ` , ` .splice() ` , ` .push() ` , and ` .shift() ` . PublicArray has much clearer and expressive
7
+ method names, and a lot more of them. Examples:
8
+ ```
9
+ let arr = getPublicArray([1,2,3,4,5,6]);
10
+ arr.remove.tail(2); // arr.data is now [1,2,3,4]
11
+ arr.remove.head(1); // arr.data is now [2,3,4]
12
+
13
+ if (arr.notEmpty) arr.prepend([10,11]); // arr.data is now [10,11,2,3,4]
14
+ arr.append([100,200,300]); // arr.data is now [10,11,2,3,4,100,200,300]
15
+ ```
16
+ To actually see or get the array itself, you must access PublicArray's ` data ` property:
17
+
18
+ ` console.log(arr.data); // logs '[10,11,2,3,4,100,200,300]' `
19
+
5
20
## Installation
6
21
7
22
You must have npm installed first. Then, in the command line:
@@ -34,36 +49,64 @@ getPublicArray(array = []): PublicArray
34
49
35
50
### Properties
36
51
37
- ```
38
- className: string (read-only)
52
+ #### className: string (read-only)
53
+
54
+ #### copy: PublicArray (read-only)
55
+ ###### an independent copy of the PublicArray instance
56
+
57
+ #### data: any[ ] (read-writable)
58
+ ###### This is the array to be operated on.
59
+
60
+ #### length: number (read-writable)
61
+ ###### length of array
62
+
63
+ #### isEmpty: boolean (read-only)
64
+ ###### true if this.data is empty
65
+
66
+ #### notEmpty: boolean (read-only)
67
+
68
+ #### filter: PublicArrayFilter (read-only)
69
+ Filter has methods that narrow down the content of the array it contains
70
+ and return the class instance.
71
+
72
+ To instantiate, pass the actual array it will contain into its constructor:
39
73
40
- copy: PublicArray (read-only) // an independent copy of the PublicArray instance
74
+ let filter = new PublicArrayFilter( [item1, item2, item3,...] );
41
75
42
- data: any[] (read-writable) // This is the array to be operated on.
76
+ You can also reset the array by accessing the class 'data' property:
43
77
44
- length: number (read-writable) // length of array
78
+ filter.data = [1,2,3,4,...];
45
79
46
- isEmpty: boolean (read-only) // true if this.data is empty
80
+ These are all its methods:
47
81
48
- notEmpty: boolean (read- only)
82
+ // Narrows down the array to only the values that pass test:
49
83
50
- filter: PublicArrayFilter (read-only)
84
+ byTest(testFunction): this
85
+ // testFunction = function(currentValue, currentIndex?, theArray?){...}
86
+ // testFunction must return boolean.
51
87
52
- getConverted: PublicArrayGetterConverter (read-only)
88
+
89
+ // Narrows down the array to only values that are the specified type:
90
+
91
+ byType(
92
+ type: 'number' | 'boolean' | 'string' | 'array' | 'object' | 'function' | 'undefined'
93
+ ): this
94
+
95
+
96
+ #### getConverted: PublicArrayGetterConverter (read-only)
53
97
54
- get: PublicArrayGetter (read-only)
98
+ #### get: PublicArrayGetter (read-only)
55
99
56
- getAndRemove: PublicArrayGetterRemover (read-only)
100
+ #### getAndRemove: PublicArrayGetterRemover (read-only)
57
101
58
- insert: PublicArrayInserter (read-only)
102
+ #### insert: PublicArrayInserter (read-only)
59
103
60
- remove: PublicArrayRemover (read-only)
104
+ #### remove: PublicArrayRemover (read-only)
61
105
62
- replace: PublicArrayReplacer (read-only)
106
+ #### replace: PublicArrayReplacer (read-only)
63
107
64
- sort: PublicArraySorter (read-only)
108
+ #### sort: PublicArraySorter (read-only)
65
109
66
- ```
67
110
68
111
### Methods
69
112
@@ -92,35 +135,28 @@ endsWith(values: any[]): boolean
92
135
matches(array): boolean
93
136
// returns false if array contains object.
94
137
95
-
96
138
// For the next 3 methods:
97
139
// testFunction is a callback with same signature as callback passed to
98
140
// Array.filter() :
99
141
// testFunction(value, index?, theArray?): checks if value passes test. If yes, it returns true.
100
142
101
-
102
- // returns true if all items pass test.
103
143
allPass(testFunction): boolean
144
+ // returns true if all items pass test.
104
145
105
-
106
- // returns true if only 1 value passes.
107
146
anyPass(testFunction): boolean
147
+ // returns true if only 1 value passes.
108
148
149
+ indexesThatPass(testFunction): number[]
150
+ // returns all indexes of items that pass test.
109
151
110
- // returns all indexes of items that pass test.
111
- indexesThatPass(testFunction): number[]
112
-
113
-
114
- // Does not work if value is object.
115
- firstIndexOf(value): number
152
+ firstIndexOf(value): number
153
+ // Does not work if value is object.
116
154
155
+ lastIndexOf(value): number
156
+ // Does not work if value is object.
117
157
118
- // Does not work if value is object.
119
- lastIndexOf(value): number
120
-
121
-
122
- // Does not work if value is object.
123
- indexesOf(value): number[]
158
+ indexesOf(value): number[]
159
+ // Does not work if value is object.
124
160
125
161
append(values: any[]): this
126
162
@@ -135,6 +171,7 @@ protected _createGetterAndOrSetterForEach(
135
171
) : void
136
172
137
173
returnThis_after(voidExpression: any) : this
174
+ // Executes voidExpression and returns this.
138
175
// Even if voidExpression returns something, the returned data isn't used.
139
176
140
177
runMethod_and_returnThis(
@@ -150,17 +187,16 @@ runMethod_and_returnThis(
150
187
## Usage
151
188
152
189
```
153
-
154
190
// changing the array content:
155
191
arr.data = [1,2,3,4,5,6,7];
156
192
157
193
```
158
194
159
195
## Performance
160
196
161
- PublicArray has a large number of dependencies. You should keep this in mind when optimizing the
162
- performance of your app. If your code only uses one property of PublicArray, like for
163
- instance .replace, you will get a slight performance boost if you just use an instance of
197
+ PublicArray has a large number of dependencies. You should keep this in mind when optimizing
198
+ the performance of your app. If your code only uses one property of PublicArray, like for
199
+ instance ` .replace ` , you will get a slight performance boost if you just use an instance of
164
200
PublicArrayReplacer instead:
165
201
```
166
202
let replace = new PublicArrayReplacer(arr);
0 commit comments