-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
♻ PoC navigators with set #294
Changes from 14 commits
1d07280
6203a82
9592a00
52f80e8
2e40686
0960f54
4a8578a
b8e0c6e
719fd70
410cec9
c74587b
481f3d5
53fc488
3fa0bd2
77f9857
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isNil, length } from 'util/lang' | ||
import { BaseNav } from './baseNav' | ||
|
||
export class ArrayNav extends BaseNav { | ||
get length() { | ||
if (this._length === undefined) this._length = length(this.value) | ||
return this._length | ||
} | ||
|
||
copy() { | ||
const { value } = this | ||
if (isNil(value)) return [] | ||
return Array.isArray(value) ? [...value] : { ...value } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you testing if this is an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because users might use slice or array index notation on an object, this is to avoid replacing the object by an array. This conforms to normal JavaScript behavior: const obj = {}
obj[0] = 'val'
console.log(obj) // Outputs: {0: "val"} |
||
} | ||
} |
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 | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we could use private syntax https://github.com/tc39/proposal-private-methods#private-methods-and-fields ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is appropriate, let's close our eyes on the ugly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't find the babel plugin corresponding to the proposal. I think it's time to rewrite in TS... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OK it seems to be in babel 7 beta 48, it might be risky to upgrade to babel 7 but I'll give it a try... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
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) | ||
} |
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) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this optimization really efficient ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're right, I'll remove this