-
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
5 changed files
with
44 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
import { isNil } from 'util/lang' | ||
|
||
export const copy = value => isNil(value) ? {} : { ...value } | ||
export const onCopy = (cb, force = false) => value => { | ||
const nil = isNil(value) | ||
if (nil && !force) return value | ||
const newValue = nil ? {} : { ...value } | ||
cb(newValue, value) | ||
return newValue | ||
} |
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,20 +1,35 @@ | ||
import { copy } from './objectNav' | ||
import { isNil } from 'util/lang' | ||
import { onCopy } from './objectNav' | ||
|
||
const update = (key, next) => updater => { | ||
const nextUpdater = next(updater) | ||
return value => { | ||
const newValue = copy(value) | ||
return onCopy((newValue, value) => { | ||
newValue[key] = nextUpdater(isNil(value) ? undefined : value[key]) | ||
return newValue | ||
}) | ||
} | ||
|
||
const get = (key, next) => () => { | ||
const nextGetter = next() | ||
return value => nextGetter(isNil(value) ? undefined : value[key]) | ||
} | ||
|
||
const unset = (key, next) => () => { | ||
const nextUnsetter = next() | ||
if (nextUnsetter) { | ||
return onCopy((newValue, value) => { | ||
newValue[key] = nextUnsetter(isNil(value) ? undefined : value[key]) | ||
}) | ||
} | ||
return onCopy(newValue => { | ||
delete newValue[key] | ||
}) | ||
} | ||
|
||
export const propNav = key => next => operation => { | ||
switch (operation) { | ||
case 'update': return update(key, next(operation)) | ||
case 'get': return null | ||
case 'unset': return null | ||
case 'get': return get(key, next(operation)) | ||
case 'unset': return unset(key, next(operation)) | ||
} | ||
throw TypeError(`Unknown navigator operation ${operation}`) | ||
} |