-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
streamline repeat with more data type support and docs
- Loading branch information
1 parent
8a1501d
commit 4b2abde
Showing
8 changed files
with
171 additions
and
117 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,3 +17,4 @@ playground | |
docs | ||
docs-src | ||
playground.html | ||
website |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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,94 +1,55 @@ | ||
import { val } from './val' | ||
import { DoubleLinkedList } from '../DoubleLinkedList' | ||
import { HtmlTemplate } from '../html' | ||
import { syncNodes } from '../utils/sync-nodes' | ||
import { getNodeOrTemplate } from '../utils/get-node-or-template' | ||
import { ObjectLiteral, StateGetter } from '../types' | ||
|
||
type DataGetter<T> = () => number | Array<T> | ||
type repeatData<T> = number | ObjectLiteral<T> | Iterable<T> | ||
|
||
const getList = (data: unknown) => { | ||
data = val(data) | ||
if (data) { | ||
if (typeof data === 'number') { | ||
return Array.from({ length: data }, (_, i) => i + 1) | ||
} | ||
|
||
if (Array.isArray(data)) { | ||
return data | ||
} | ||
if ( | ||
typeof (data as Iterable<unknown>)[Symbol.iterator] === 'function' | ||
) { | ||
return Array.from(data as Iterable<unknown>) | ||
} | ||
|
||
if (typeof data === 'number') { | ||
return Array.from({ length: data }, (_, i) => i + 1) | ||
if (data instanceof Object) { | ||
return Object.entries(data) | ||
} | ||
} | ||
|
||
return [] | ||
} | ||
|
||
/** | ||
* renders things repeatedly based on first argument list or number | ||
* renders things repeatedly based on first argument iterable or number | ||
* @param data | ||
* @param cb | ||
* @param whenEmpty | ||
*/ | ||
export const repeat = <T>( | ||
data: number | Array<T> | DataGetter<T>, | ||
data: repeatData<T> | StateGetter<repeatData<T>>, | ||
cb: (data: T, index: number) => unknown, | ||
whenEmpty?: () => unknown | ||
) => { | ||
const cache: Map<T, Node | HtmlTemplate> = new Map() | ||
let currentRenderedNodes = new DoubleLinkedList<Node | HtmlTemplate>() | ||
let prevList: T[] = [] | ||
|
||
const each = (d: T, i: number) => { | ||
if (!cache.has(d)) { | ||
cache.set(d, getNodeOrTemplate(cb(d, i))) | ||
} | ||
|
||
return cache.get(d) as Node | HtmlTemplate | ||
} | ||
let map = new Map() | ||
|
||
return (anchor: Node, temp: HtmlTemplate) => { | ||
const list = getList(data) as T[] | ||
return () => { | ||
const list = getList(val(data)) as T[] | ||
|
||
if (list.length === 0) { | ||
const res = whenEmpty?.() ?? [] | ||
map = new Map() | ||
|
||
currentRenderedNodes = syncNodes( | ||
currentRenderedNodes, | ||
Array.isArray(res) ? res : [res], | ||
anchor, | ||
temp | ||
) | ||
|
||
prevList = [] | ||
cache.clear() | ||
} else { | ||
const prevListSet = DoubleLinkedList.fromArray(prevList) | ||
|
||
currentRenderedNodes = syncNodes( | ||
currentRenderedNodes, | ||
new Proxy(list, { | ||
get(_, prop) { | ||
if (typeof prop === 'string') { | ||
const idx = Number(prop) | ||
|
||
if (!isNaN(idx)) { | ||
const item = list[idx] | ||
prevListSet.remove(item) | ||
return each(item, idx) | ||
} | ||
} | ||
|
||
return Reflect.get(_, prop) | ||
}, | ||
}) as Array<Node | HtmlTemplate>, | ||
anchor, | ||
temp | ||
) | ||
|
||
for (const d of prevListSet) { | ||
cache.delete(d) | ||
} | ||
|
||
prevList = list | ||
return whenEmpty?.() ?? [] | ||
} | ||
|
||
return currentRenderedNodes | ||
map = list.reduce((acc, item, idx) => { | ||
acc.set(item, map.get(item) ?? cb(item, idx)) | ||
return acc | ||
}, new Map()) | ||
|
||
return Array.from(map.values()) | ||
} | ||
} |
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