From 6a0c3b5ec21fb57cc0eff06c8ba1748060e9addb Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Wed, 27 Jul 2016 14:37:24 -0500 Subject: [PATCH 1/2] Add List.insert and upgrade Immutable.js This commit upgrades Immutable to 3.8.1, and sets it to `>=` in `package.json`. It also adds a `List.insert()` method to typed-immutable lists. FIxes #20. --- package.json | 2 +- src/list.js | 18 ++++++++++++++++-- src/test/list.js | 11 +++++++++++ 3 files changed, 28 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index da51178..00123a1 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "prepublish": "npm run build" }, "dependencies": { - "immutable": "3.7.0" + "immutable": ">=3.8.1" }, "devDependencies": { "babel": "5.6.14", diff --git a/src/list.js b/src/list.js index 2449375..fdb1d33 100644 --- a/src/list.js +++ b/src/list.js @@ -130,7 +130,7 @@ class TypeInferedList extends BaseImmutableList { } get(index, notSetValue) { - return this[$store] ? this[$store].get(index, notSetValue) : + return this[$store] ? this[$store].get(parseInt(index), notSetValue) : notSetValue } @@ -142,13 +142,27 @@ class TypeInferedList extends BaseImmutableList { return this[$empty] || this[$read]() } + insert(index, value) { + if (index > this.size) { + throw TypeError(`Index "${index}" is out of bounds.`) + } + + const result = this[$type][$read](value) + + if (result instanceof TypeError) { + throw TypeError(`Invalid value: ${result.message}`) + } + + return change(this, store => store.insert(index, value)) + } + remove(index) { return change(this, store => store && store.remove(index)) } set(index, value) { if (index > this.size) { - throw TypeError(`Index "${index}" is out of bound`) + throw TypeError(`Index "${index}" is out of bounds.`) } const result = this[$type][$read](value) diff --git a/src/test/list.js b/src/test/list.js index f623fbc..b9feece 100644 --- a/src/test/list.js +++ b/src/test/list.js @@ -545,6 +545,17 @@ test('push inserts at highest index', assert => { assert.deepEqual(v1.toArray(), [1, 2, 3, 4, 5, 6]) }) +test('insert inserts where told', assert => { + const v0 = NumberList.of(1, 2, 3, 4, 5) + const v1 = v0.insert(2, 50) + + assert.ok(v0 instanceof NumberList) + assert.ok(v1 instanceof NumberList) + + assert.deepEqual(v0.toArray(), [1, 2, 3, 4, 5]) + assert.deepEqual(v1.toArray(), [1, 2, 50, 3, 4, 5]) +}) + test('pop removes the highest index, decrementing size', assert => { const v0 = NumberList.of(1, 2, 3) const v1 = v0.pop() From 8c935592c571fc2e264288d04805a954a6e1b0c4 Mon Sep 17 00:00:00 2001 From: Luke Sneeringer Date: Wed, 27 Jul 2016 18:38:48 -0500 Subject: [PATCH 2/2] We only need Immutable.js to be 3.7.6 or above. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 00123a1..1cb55ac 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "prepublish": "npm run build" }, "dependencies": { - "immutable": ">=3.8.1" + "immutable": ">=3.7.6" }, "devDependencies": { "babel": "5.6.14",