Skip to content

Commit c79b1fb

Browse files
committed
FEAT: Add rm and rmAt methods
1 parent c4e66c4 commit c79b1fb

File tree

5 files changed

+78
-8
lines changed

5 files changed

+78
-8
lines changed

IArray.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@
7474
return deepFreeze(a2)
7575
}
7676

77+
function rmAt(index)
78+
{
79+
var a2 = createIArray(this),
80+
ret = Array.prototype.splice.call(a2, index, 1)
81+
a2.ret = ret[0]
82+
return deepFreeze(a2)
83+
}
84+
85+
function rm(value)
86+
{
87+
var index = this.indexOf(value)
88+
if(index >= 0)
89+
return this.rmAt(index)
90+
var a2 = createIArray(this)
91+
a2.ret = undefined
92+
return deepFreeze(a2) // return a clone
93+
}
94+
7795
// Create our custom IArray prototype, with Array.prototype at the base
7896
var IAProto = Object.assign(
7997
Object.create(Array.prototype),
@@ -83,6 +101,8 @@
83101
concat: concat,
84102
isIArray: true,
85103
set: set,
104+
rm: rm,
105+
rmAt: rmAt,
86106
toJSON: function() { return this.toArray() },
87107
toArray: function() { return Array.prototype.slice.call(this) }
88108
}

IArray.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ Arrays are an extremely popular data structure in most JavaScript programs. But
1414

1515
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.
1616

17-
**IArray** provides an Immutable array only, and is very light.
17+
**IArray** provides an Immutable array only, and is very light.
1818

1919
```bash
20-
wc -l IArray.js
21-
119 IArray.js
20+
wc -l IArray.js
21+
146 IArray.js
2222
```
2323

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.
2525

2626
## How
2727

@@ -41,7 +41,7 @@ In cases where a method mutated the underlying array and also returned a value (
4141

4242
## API
4343

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`.
4545

4646

4747
### `concat(value1[, value2 ...]) => IArray`
@@ -122,6 +122,30 @@ var a2 = a1.pop()
122122
// a2.ret = 9 - last element stored in ret property
123123
```
124124
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+
125149
### `set(index, value) => IArray`
126150
127151
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")
200224
201225
### License
202226
203-
See the LICENSE file for license rights and limitations (MIT).
227+
See the LICENSE file for license rights and limitations (MIT).

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "IArray",
3-
"version": "1.1.5",
3+
"version": "1.2.0",
44
"description": "Immutable Array",
55
"main": "IArray.js",
66
"scripts": {

test/test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,32 @@
469469
assert.deepEqual(a4.toArray(), [ "hello", "world", 5, 10, 20]) // prepended to start
470470

471471
})
472+
473+
test("rm", function() {
474+
475+
var a1 = IArray([5, 6, 7, 8, 9, 10])
476+
var a2 = a1.rm(8)
477+
var a3 = a1.rm(23) // test removal of non-existant entry
478+
479+
assert.deepEqual(a1.toArray(), [5, 6, 7, 8, 9, 10]) // unchanged
480+
assert.deepEqual(a2.toArray(), [5, 6, 7, 9, 10]) // a1 with 8 value removed
481+
assert.equal(a2.ret, 8) // here is the removed value
482+
assert.deepEqual(a3.toArray(), [5, 6, 7, 8, 9, 10]) // unchanged since 23 was not present
483+
assert.equal(a3.ret, undefined) // ensure return value was set as undefined
484+
})
485+
486+
test("rmAt", function() {
487+
488+
var a1 = IArray([5, 6, 7, 8, 9, 10])
489+
var a2 = a1.rmAt(2) // removes the 7 at position 2
490+
var a3 = a1.rmAt(7) // test removal of non-existant entry
491+
492+
assert.deepEqual(a1.toArray(), [5, 6, 7, 8, 9, 10]) // unchanged
493+
assert.deepEqual(a2.toArray(), [5, 6, 8, 9, 10]) // a1 with 7 value at position 2 removed
494+
assert.equal(a2.ret, 7) // here is the removed value
495+
assert.deepEqual(a3.toArray(), [5, 6, 7, 8, 9, 10]) // unchanged since no value at position 7
496+
assert.equal(a3.ret, undefined) // ensure return value was set as undefined
497+
})
472498
}
473499

474500
function testImmutableIndexChanges()

0 commit comments

Comments
 (0)