mirror of
https://github.com/voltrevo/ValueScript.git
synced 2026-04-18 03:00:27 -04:00
Update tree shaking example
This commit is contained in:
@@ -157,8 +157,28 @@ export default class Range<T = never> implements Iterable<T> {
|
||||
return new Range(res());
|
||||
}
|
||||
|
||||
// TODO: Negative indexes
|
||||
at(n: number) {
|
||||
if (n < 0) {
|
||||
if (n === -1) {
|
||||
return this.last();
|
||||
}
|
||||
|
||||
let buf: T[] = [];
|
||||
let len = -n;
|
||||
let i = 0;
|
||||
|
||||
for (const x of this.iterable) {
|
||||
buf[i % len] = x;
|
||||
i++;
|
||||
}
|
||||
|
||||
if (buf.length < len) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return buf[i % len];
|
||||
}
|
||||
|
||||
let i = 0;
|
||||
|
||||
for (const x of this.iterable) {
|
||||
@@ -345,12 +365,11 @@ export default class Range<T = never> implements Iterable<T> {
|
||||
return new Range(iter());
|
||||
}
|
||||
|
||||
// TODO: `in` operator
|
||||
if (hasKey(iter, Symbol.iterator)) {
|
||||
if (Symbol.iterator in iter) {
|
||||
return new Range(iter);
|
||||
}
|
||||
|
||||
if (hasKey(iter, "next")) {
|
||||
if ("next" in iter) {
|
||||
return Range.fromIterator(iter);
|
||||
}
|
||||
|
||||
@@ -388,13 +407,6 @@ export default class Range<T = never> implements Iterable<T> {
|
||||
}
|
||||
}
|
||||
|
||||
function hasKey<Obj, K extends string | symbol>(
|
||||
obj: unknown,
|
||||
key: K,
|
||||
): obj is Obj & Record<K, unknown> {
|
||||
return (obj as Record<K, unknown>)[key] !== undefined;
|
||||
}
|
||||
|
||||
function never(x: never): never {
|
||||
throw new Error(`Unexpected value: ${x}`);
|
||||
}
|
||||
|
||||
@@ -8,11 +8,11 @@
|
||||
// - `factorizeAsPowers`
|
||||
// - `BinaryTree`
|
||||
// - `NotNullish`
|
||||
// - `range`
|
||||
// - `Range`
|
||||
//
|
||||
// These definitions are not included, because the definitions exported by this
|
||||
// module do not need them. Omitting those unused definitions reduces the
|
||||
// bytecode for this module from 3,593 to 413 bytes.
|
||||
// bytecode for this module from 3,606 to 337 bytes.
|
||||
|
||||
import { factorize } from "../lib/mod.ts";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user