Skip to content

Commit

Permalink
✨ Finish functionnal navs...
Browse files Browse the repository at this point in the history
  • Loading branch information
nlepage committed Jul 29, 2019
1 parent 41f4858 commit ea59c79
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 67 deletions.
12 changes: 6 additions & 6 deletions packages/immutadot/src/nav2/indexNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ const get = (pIndex, next) => () => {
const unset = (pIndex, next) => () => {
const nextUnsetter = next()
if (nextUnsetter) {
return onCopy(newValue => {
const index = resolveIndex(pIndex, newValue)
return onCopy(value => {
const index = resolveIndex(pIndex, value)
if (index === undefined) return // TODO avoid useless copy ?
newValue[index] = nextUnsetter(newValue[index])
value[index] = nextUnsetter(value[index])
})
}
return onCopy(newValue => {
const index = resolveIndex(pIndex, newValue)
return onCopy(value => {
const index = resolveIndex(pIndex, value)
if (index === undefined) return // TODO avoid useless copy ?
delete newValue[index]
delete value[index]
})
}

Expand Down
19 changes: 10 additions & 9 deletions packages/immutadot/src/nav2/propNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ const get = (key, next) => () => {
}
}

const unset = (key, next) => () => {
const unsetProp = (key, nextUnsetter) => () => onCopy(newValue => {
newValue[key] = nextUnsetter(newValue[key])
})

const deleteProp = key => () => onCopy(newValue => {
delete newValue[key]
})

const unset = (key, next) => {
const nextUnsetter = next()
if (nextUnsetter) {
return onCopy(newValue => {
newValue[key] = nextUnsetter(newValue[key])
})
}
return onCopy(newValue => {
delete newValue[key]
})
return nextUnsetter ? unsetProp(key, nextUnsetter) : deleteProp(key)
}

export const propNav = makeNav({
Expand Down
42 changes: 20 additions & 22 deletions packages/immutadot/src/nav2/propsNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,27 @@ const get = (keys, next) => () => {
return value => isNil(value) ? undefined : Object.keys(value).map(key => nextGetter(value[key]))
}

const unset = (keys, next) => () => {
const unsetKeys = (keys, nextUnsetter) => () => onCopy(value => {
for (const key of keys) value[key] = nextUnsetter(value[key])
})

const unsetAll = nextUnsetter => () => onCopy(value => {
for (const key of Object.keys(value)) value[key] = nextUnsetter(value[key])
})

const deleteKeys = keys => () => onCopy(value => {
for (const key of keys) delete value[key]
})

const deleteAll = () => onCopy(value => {
for (const key of Object.keys(value)) delete value[key]
})

const unset = (keys, next) => {
const nextUnsetter = next()
if (nextUnsetter) {
if (keys) {
return onCopy((newValue, value) => {
if (isNil(value))
for (const key of keys) newValue[key] = nextUnsetter(undefined)
else
for (const key of keys) newValue[key] = nextUnsetter(value[key])
}, true)
}
return onCopy((newValue, value) => {
for (const key of Object.keys(value)) newValue[key] = nextUnsetter(value[key])
})
}
if (keys) {
return onCopy(newValue => {
for (const key of keys) delete newValue[key]
})
}
return onCopy((newValue, value) => {
for (const key of Object.keys(value)) delete newValue[key]
})
if (nextUnsetter)
return keys ? unsetKeys(keys, nextUnsetter) : unsetAll(nextUnsetter)
return keys ? deleteKeys(keys) : deleteAll
}

export const propsNav = makeNav({
Expand Down
101 changes: 71 additions & 30 deletions packages/immutadot/src/nav2/sliceNav.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,53 +16,94 @@ const resolveEnd = (pEnd, length, step) => {
return resolveIndex(pEnd, length)
}

const getRange = (pStart, pEnd, pStep, value) => {
const step = pStep === undefined ? 1 : pStep
const length = getLength(value)
const start = resolveStart(pStart, length, step)
const end = resolveEnd(pEnd, length, step)
if (step > 0) {
if (end <= start) return null
return {
[Symbol.iterator]: () => {
let i = start
return {
next: () => {
if (i < end) {
const res = {
value: i,
done: false,
}
i += step
return res
}
return { done: true }
},
}
},
}
}
if (end >= start) return null
return {
[Symbol.iterator]: () => {
let i = start
return {
next: () => {
if (i > end) {
const res = {
value: i,
done: false,
}
i += step
return res
}
return { done: true }
},
}
},
}
}

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
const range = getRange(pStart, pEnd, pStep, value)
if (!range) return // TODO avoid useless copy
if (isNil(value))
for (let i = start; i > end; i += step) newValue[i] = nextUpdater(undefined)
for (const i of range) newValue[i] = nextUpdater(undefined)
else
for (let i = start; i > end; i += step) newValue[i] = nextUpdater(value[i])
for (const i of range) 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
}())
}
const range = getRange(pStart, pEnd, pStep, value)
if (!range) return []
return Array.from(range, i => nextGetter(value[i]))
}
}

const unsetSlice = (pStart, pEnd, pStep, nextUnsetter) => () => onCopy(value => {
const range = getRange(pStart, pEnd, pStep, value)
if (!range) return // TODO avoid useless copy
for (const i of range) value[i] = nextUnsetter(value[i])
})

const deleteSlice = (pStart, pEnd, pStep) => () => onCopy(value => {
const range = getRange(pStart, pEnd, pStep, value)
if (!range) return // TODO avoid useless copy
for (const i of range) delete value[i]
})

const unset = ([pStart, pEnd, pStep], next) => {
const nextUnsetter = next()
return nextUnsetter ? unsetSlice(pStart, pEnd, pStep, nextUnsetter) : deleteSlice(pStart, pEnd, pStep)
}

export const sliceNav = makeNav({
update,
get,
unset,
})

0 comments on commit ea59c79

Please sign in to comment.