You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-6Lines changed: 30 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -14,14 +14,14 @@ Arrays are an extremely popular data structure in most JavaScript programs. But
14
14
15
15
Some libraries exist that provide random access data structures similar to the Array but that are immutable, such as [Mori](http://swannodette.github.io/mori/) or [Immutable](https://facebook.github.io/immutable-js/) - but they are large opinionated libraries that expose a completely different API.
16
16
17
-
**IArray** provides an Immutable array only, and is very light.
17
+
**IArray** provides an Immutable array only, and is very light.
18
18
19
19
```bash
20
-
wc -l IArray.js
21
-
119 IArray.js
20
+
wc -l IArray.js
21
+
146 IArray.js
22
22
```
23
23
24
-
119 lines in the *source* file, much of which is comments and the universal module definition.
24
+
146 lines in the *source* file, much of which is comments and the universal module definition.
25
25
26
26
## How
27
27
@@ -41,7 +41,7 @@ In cases where a method mutated the underlying array and also returned a value (
41
41
42
42
## API
43
43
44
-
Since this extends the standard JavaScript `Array`, I will only document the methods that have *changed* from the standard Array API:
44
+
Since this extends the standard JavaScript `Array`, I will only document the methods that have *changed* from the standard Array API. These include **3** new methods, `rm`, `rmAt` and `set` along with several methods from the `Array` API whose behavior is slightly changed to reflect the immutable nature of `IArray`.
45
45
46
46
47
47
### `concat(value1[, value2 ...]) => IArray`
@@ -122,6 +122,30 @@ var a2 = a1.pop()
122
122
// a2.ret = 9 - last element stored in ret property
123
123
```
124
124
125
+
### `rm(value) => IArray`
126
+
127
+
Returns a new `IArray` with the `value` specified removed. If multiple occurrences of the value exists, the first one is removed. The removed value appears on the `ret` property of the newly created `IArray`. If the value does not exist in the array, no error is thrown, but the `ret` property contains `undefined`.
128
+
129
+
```javascript
130
+
var a1 =IArray([1, 4, 9]) // a1 is [ 1, 4, 9 ]
131
+
var a2 =a1.rm(4) // a2 is [ 1, 9 ]
132
+
log(a2.ret) // 4
133
+
var a3 =a2.rm(6) // this doesn't exist, so a3 is [ 1, 9 ]
134
+
log(a3.ret) // undefined
135
+
```
136
+
137
+
### `rmAt(index) => IArray`
138
+
139
+
Returns a new `IArray` with the value at position `index` removed. The removed value appears on the `ret` property of the newly created `IArray`. If the `index` position specified is out of the range of the `IArray`, no error is thrown, but the `ret` property contains `undefined` and the values in the returned `IArray` are unchanged.
140
+
141
+
```javascript
142
+
var a1 =IArray([1, 4, 9]) // a1 is [ 1, 4, 9 ]
143
+
var a2 =a1.rmAt(1) // a2 is [ 1, 9 ]
144
+
log(a2.ret) // 4
145
+
var a3 =a2.rmAt(6) // position out of range, so a3 is [ 1, 9 ]
146
+
log(a3.ret) // undefined
147
+
```
148
+
125
149
### `set(index, value) => IArray`
126
150
127
151
Used to assign a value to a specific index within the `IArray`. This is used as a substitute to the `array[index] = value` notation, which mutates the array. The returned `IArray` is a copy of the original array with value assigned at the index specified.
@@ -200,4 +224,4 @@ var a2 = a1.unshift("hello", "world")
200
224
201
225
### License
202
226
203
-
See the LICENSE file for license rights and limitations (MIT).
227
+
See the LICENSE file for license rights and limitations (MIT).
0 commit comments