-
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.
* 🚧 PoC nav * 🚧 Add index navigator * 🚧 Add slice navigator * ♻️ Reorganize code * ♻️ Move apart get and update logic and use ES6 classes * 🔥 Remove unused currification level * ⚡ Use nav in set and validate performance improvement with benchmark * ✨ Put back negative array index support * ✅ Report apply tests to nav * 🚚 Move each navigator in its own file as suggested by @frinyvonnick * :refactor: Optional currying on set * ✨ Add all props navigator * ✨ Prop list navigator * ♻️ Put TypeError for empty path in nav * 👌 @frinyvonnick's review
- Loading branch information
Showing
15 changed files
with
663 additions
and
97 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { isNil, length } from 'util/lang' | ||
import { BaseNav } from './baseNav' | ||
|
||
export class ArrayNav extends BaseNav { | ||
get length() { | ||
return length(this.value) | ||
} | ||
|
||
copy() { | ||
const { value } = this | ||
if (isNil(value)) return [] | ||
return Array.isArray(value) ? [...value] : { ...value } | ||
} | ||
} |
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,6 @@ | ||
export class BaseNav { | ||
constructor(value, next) { | ||
this.value = value | ||
this._next = next | ||
} | ||
} |
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,35 @@ | ||
import { ArrayNav } from './arrayNav' | ||
import { isNil } from 'util/lang' | ||
|
||
class IndexNav extends ArrayNav { | ||
constructor(value, index, next) { | ||
super(value, next) | ||
this._index = index | ||
} | ||
|
||
get index() { | ||
const { _index, length } = this | ||
if (_index >= 0) return _index | ||
if (-_index > length) return undefined | ||
return Math.max(length + _index, 0) | ||
} | ||
|
||
get next() { | ||
const { _next, index, value } = this | ||
return (isNil(value) || index === undefined) ? _next(undefined) : _next(value[index]) | ||
} | ||
|
||
get() { | ||
return this.next.get() | ||
} | ||
|
||
update(updater) { | ||
const copy = this.copy() | ||
copy[this.index] = this.next.update(updater) | ||
return copy | ||
} | ||
} | ||
|
||
export function indexNav(index, next) { | ||
return value => new IndexNav(value, index, next) | ||
} |
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,41 @@ | ||
import { allProps, index, list, prop, slice } from '@immutadot/parser/consts' | ||
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 allProps: | ||
case list: | ||
return propsNav | ||
case index: return indexNav | ||
case prop: return propNav | ||
case slice: return sliceNav | ||
default: throw TypeError(type) | ||
} | ||
} | ||
|
||
class FinalNav { | ||
constructor(value) { | ||
this.value = value | ||
} | ||
|
||
get() { | ||
return this.value | ||
} | ||
|
||
update(updater) { | ||
return updater(this.value) | ||
} | ||
} | ||
|
||
function finalNav(value) { | ||
return new FinalNav(value) | ||
} |
Oops, something went wrong.