Skip to content

Commit

Permalink
♻ Put 0 implicit bound slice at first, remove unused code (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Dec 12, 2017
1 parent fb9c405 commit 20a1153
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 25 deletions.
25 changes: 8 additions & 17 deletions packages/immutadot/src/path/toPath.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import {
slice,
} from './consts'

import {
isIndex,
isSliceIndex,
} from './utils'

import {
isNil,
toString,
} from 'util/lang'

import {
isIndex,
} from './utils'

/**
* Strip slashes preceding occurences of <code>quote</code> from <code>str</code><br />
* Possible quotes are <code>"</code> and <code>'</code>.
Expand All @@ -37,23 +38,13 @@ const unescapeQuotes = (str, quote) => str.replace(new RegExp(`\\\\${quote}`, 'g
* Converts <code>str</code> to a slice index.
* @function
* @param {string} str The string to convert
* @param {number?} defaultValue The default value if <code>str</code> is empty
* @return {number} <code>undefined</code> if <code>str</code> is empty, otherwise an int (may be NaN)
* @memberof path
* @private
* @since 1.0.0
*/
const toSliceIndex = str => str === '' ? undefined : Number(str)

/**
* Tests whether <code>arg</code> is a valid slice index, that is <code>undefined</code> or a valid int.
* @function
* @memberof path
* @param {*} arg The value to test
* @return {boolean} True if <code>arg</code> is a valid slice index, false otherwise.
* @private
* @since 1.0.0
*/
const isSliceIndex = arg => arg === undefined || Number.isSafeInteger(arg)
const toSliceIndex = (str, defaultValue) => str === '' ? defaultValue : Number(str)

/**
* Tests whether <code>arg</code> is a valid slice index once converted to a number.
Expand Down Expand Up @@ -114,7 +105,7 @@ const sliceNotationParser = map(
regexp(/^\[([^:\]]*):([^:\]]*)\]\.?(.*)$/),
([sliceStart, sliceEnd]) => isSliceIndexString(sliceStart) && isSliceIndexString(sliceEnd),
),
([sliceStart, sliceEnd, rest]) => [[slice, [toSliceIndex(sliceStart), toSliceIndex(sliceEnd)]], ...stringToPath(rest)],
([sliceStart, sliceEnd, rest]) => [[slice, [toSliceIndex(sliceStart, 0), toSliceIndex(sliceEnd)]], ...stringToPath(rest)],
)

const pathSegmentEndedByDotParser = map(
Expand Down
4 changes: 2 additions & 2 deletions packages/immutadot/src/path/toPath.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('ToPath', () => {

it('should convert slice notation path', () => {
expect(toPath('[:][1:][:-2][3:4]')).toEqual([
[slice, [undefined, undefined]],
[slice, [0, undefined]],
[slice, [1, undefined]],
[slice, [undefined, -2]],
[slice, [0, -2]],
[slice, [3, 4]],
])
expect(toPath('[1:2:3][1:a][1:2')).toEqual([[prop, '1:2:3'], [prop, '1:a'], [prop, '1:2']])
Expand Down
7 changes: 3 additions & 4 deletions packages/immutadot/src/path/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
slice,
} from './consts'

export const getSliceBound = (value, defaultValue, length) => {
if (value === undefined) return defaultValue
export const getSliceBound = (value, length) => {
if (value < 0) return Math.max(length + value, 0)
return value
}
Expand All @@ -23,8 +22,8 @@ export const getSliceBound = (value, defaultValue, length) => {
* @since 1.0.0
*/
export const getSliceBounds = ([start, end], length) => ([
getSliceBound(start, 0, length),
getSliceBound(end, length, length),
getSliceBound(start, length),
getSliceBound(end === undefined ? length : end, length),
])

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/immutadot/src/path/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import {
describe('Path Utils', () => {
describe('GetSliceBounds', () => {
it('should return actual slice bounds', () => {
expect(getSliceBounds([undefined, undefined], 0)).toEqual([0, 0])
expect(getSliceBounds([0, undefined], 0)).toEqual([0, 0])
expect(getSliceBounds([-2, -1], 0)).toEqual([0, 0])
expect(getSliceBounds([1, 2], 0)).toEqual([1, 2])

expect(getSliceBounds([undefined, undefined], 6)).toEqual([0, 6])
expect(getSliceBounds([0, undefined], 6)).toEqual([0, 6])
expect(getSliceBounds([1, -1], 6)).toEqual([1, 5])
expect(getSliceBounds([7, 8], 6)).toEqual([7, 8])
})
Expand Down

0 comments on commit 20a1153

Please sign in to comment.