-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
109 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1 @@ | ||
import { NavType } from 'immutadot-parser' | ||
import { finalNav } from './finalNav' | ||
import { indexNav } from './indexNav' | ||
import { propNav } from './propNav' | ||
import { propsNav } from './propsNav' | ||
import { sliceNav } from './sliceNav' | ||
|
||
export function nav(path) { | ||
if (path.length === 0) throw new TypeError('path should not be empty') | ||
|
||
return path.reduceRight((next, [type, value]) => toNav(type)(value, next), finalNav) | ||
} | ||
|
||
function toNav(type) { | ||
switch (type) { | ||
case NavType.allProps: | ||
case NavType.list: | ||
return propsNav | ||
case NavType.index: return indexNav | ||
case NavType.prop: return propNav | ||
case NavType.slice: return sliceNav | ||
default: throw TypeError(type) | ||
} | ||
} | ||
export { nav } from '../nav2/nav' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { length as getLength, isNil } from 'util/lang' | ||
import { makeNav } from './makeNav' | ||
import { onCopy } from './arrayNav' | ||
|
||
const resolveIndex = (index, length) => index >= 0 ? index : Math.max(length + index, 0) | ||
|
||
const resolveStart = (pStart, length, step) => { | ||
if (length === 0) return 0 | ||
if (pStart === undefined) return step > 0 ? 0 : length - 1 | ||
return resolveIndex(pStart, length) | ||
} | ||
|
||
const resolveEnd = (pEnd, length, step) => { | ||
if (length === 0) return 0 | ||
if (pEnd === undefined) return step > 0 ? length : -1 | ||
return resolveIndex(pEnd, length) | ||
} | ||
|
||
const update = ([pStart, pEnd, pStep], next) => updater => { | ||
const nextUpdater = next(updater) | ||
const step = pStep === undefined ? 1 : pStep | ||
return onCopy((newValue, value) => { | ||
const length = getLength(value) | ||
const start = resolveStart(pStart, length, step) | ||
const end = resolveEnd(pEnd, length, step) | ||
if (step > 0) { | ||
if (end <= start) return // TODO avoid useless copy | ||
if (isNil(value)) | ||
for (let i = start; i < end; i += step) newValue[i] = nextUpdater(undefined) | ||
else | ||
for (let i = start; i < end; i += step) newValue[i] = nextUpdater(value[i]) | ||
} | ||
if (end >= start) return // TODO avoid useless copy | ||
if (isNil(value)) | ||
for (let i = start; i > end; i += step) newValue[i] = nextUpdater(undefined) | ||
else | ||
for (let i = start; i > end; i += step) newValue[i] = nextUpdater(value[i]) | ||
}, true) | ||
} | ||
|
||
const get = ([pStart, pEnd, pStep], next) => () => { | ||
const nextGetter = next() | ||
const step = pStep === undefined ? 1 : pStep | ||
return value => { | ||
const length = getLength(value) | ||
const start = resolveStart(pStart, length, step) | ||
const end = resolveEnd(pEnd, length, step) | ||
if (isNil(value)) return [] | ||
let range | ||
if (step > 0) { | ||
if (end <= start) return [] | ||
range = (function* () { | ||
for (let i = start; i < end; i += step) yield i | ||
}()) | ||
} else { | ||
if (end >= start) return [] | ||
range = (function* () { | ||
for (let i = start; i > end; i += step) yield i | ||
}()) | ||
} | ||
return Array.from(range, i => nextGetter(value[i])) | ||
} | ||
} | ||
|
||
export const sliceNav = makeNav({ | ||
update, | ||
get, | ||
}) |