Skip to content

Commit

Permalink
Merge pull request #29 from typed-immutable/upgrade-immutable
Browse files Browse the repository at this point in the history
Add List.insert and upgrade Immutable.js
  • Loading branch information
lukesneeringer committed Jul 29, 2016
2 parents 02c1062 + 8478aa6 commit cd43268
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"prepublish": "npm run build"
},
"dependencies": {
"immutable": "3.7.0"
"immutable": ">=3.7.6"
},
"devDependencies": {
"babel": "5.6.14",
Expand Down
18 changes: 16 additions & 2 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions src/test/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit cd43268

Please sign in to comment.