Skip to content

Commit

Permalink
🐛 Fix slice explicit 0 index (#346)
Browse files Browse the repository at this point in the history
* ✅ Add a test

* 🐛 fix #345 slice nav explicit 0 index

* ♻️ Rename resolveNegativeIndex to resolveIndex
  • Loading branch information
nlepage committed Jun 5, 2019
1 parent 2482461 commit 04b0f1f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/immutadot/src/nav/sliceNav.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ArrayNav } from './arrayNav'
import { isNil } from 'util/lang'

const resolveNegativeIndex = (index, length) => index > 0 ? index : Math.max(length + index, 0)
const resolveIndex = (index, length) => index >= 0 ? index : Math.max(length + index, 0)

class SliceNav extends ArrayNav {
constructor(value, params, next) {
Expand All @@ -12,14 +12,14 @@ class SliceNav extends ArrayNav {
get start() {
const { length, params: [start] } = this
if (length === 0) return 0
if (start !== undefined) return resolveNegativeIndex(start, length)
if (start !== undefined) return resolveIndex(start, length)
return this.step > 0 ? 0 : length - 1
}

get end() {
const { length, params: [, end] } = this
if (length === 0) return 0
if (end !== undefined) return resolveNegativeIndex(end, length)
if (end !== undefined) return resolveIndex(end, length)
return this.step > 0 ? length : -1
}

Expand Down
23 changes: 23 additions & 0 deletions packages/immutadot/src/nav/sliceNav.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* eslint-env jest */
import { sliceNav } from './sliceNav'

describe('SliceNav', () => {

it('should update a slice', () => {
const input = [0, 1]
const next = value => ({
update: updater => updater(value),
})
const updater = value => value + 1

const test = (params, expected) => {
const output = sliceNav(params, next)(input).update(updater)

expect(input).toEqual([0, 1])
expect(output).not.toBe(input)
expect(output).toEqual(expected)
}

test([0, undefined], [1, 2])
})
})

0 comments on commit 04b0f1f

Please sign in to comment.