summaryrefslogtreecommitdiff
path: root/cli/js/mixins
diff options
context:
space:
mode:
Diffstat (limited to 'cli/js/mixins')
-rw-r--r--cli/js/mixins/dom_iterable.ts82
-rw-r--r--cli/js/mixins/dom_iterable_test.ts79
2 files changed, 161 insertions, 0 deletions
diff --git a/cli/js/mixins/dom_iterable.ts b/cli/js/mixins/dom_iterable.ts
new file mode 100644
index 000000000..bbd1905ce
--- /dev/null
+++ b/cli/js/mixins/dom_iterable.ts
@@ -0,0 +1,82 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+// eslint-disable-next-line @typescript-eslint/no-unused-vars
+import { DomIterable } from "../dom_types.ts";
+import { window } from "../window.ts";
+import { requiredArguments } from "../util.ts";
+
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+type Constructor<T = {}> = new (...args: any[]) => T;
+
+/** Mixes in a DOM iterable methods into a base class, assumes that there is
+ * a private data iterable that is part of the base class, located at
+ * `[dataSymbol]`.
+ * TODO Don't expose DomIterableMixin from "deno" namespace.
+ */
+export function DomIterableMixin<K, V, TBase extends Constructor>(
+ Base: TBase,
+ dataSymbol: symbol
+): TBase & Constructor<DomIterable<K, V>> {
+ // we have to cast `this` as `any` because there is no way to describe the
+ // Base class in a way where the Symbol `dataSymbol` is defined. So the
+ // runtime code works, but we do lose a little bit of type safety.
+
+ // Additionally, we have to not use .keys() nor .values() since the internal
+ // slot differs in type - some have a Map, which yields [K, V] in
+ // Symbol.iterator, and some have an Array, which yields V, in this case
+ // [K, V] too as they are arrays of tuples.
+
+ const DomIterable = class extends Base {
+ *entries(): IterableIterator<[K, V]> {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ for (const entry of (this as any)[dataSymbol]) {
+ yield entry;
+ }
+ }
+
+ *keys(): IterableIterator<K> {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ for (const [key] of (this as any)[dataSymbol]) {
+ yield key;
+ }
+ }
+
+ *values(): IterableIterator<V> {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ for (const [, value] of (this as any)[dataSymbol]) {
+ yield value;
+ }
+ }
+
+ forEach(
+ callbackfn: (value: V, key: K, parent: this) => void,
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ thisArg?: any
+ ): void {
+ requiredArguments(
+ `${this.constructor.name}.forEach`,
+ arguments.length,
+ 1
+ );
+ callbackfn = callbackfn.bind(thisArg == null ? window : Object(thisArg));
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ for (const [key, value] of (this as any)[dataSymbol]) {
+ callbackfn(value, key, this);
+ }
+ }
+
+ *[Symbol.iterator](): IterableIterator<[K, V]> {
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ for (const entry of (this as any)[dataSymbol]) {
+ yield entry;
+ }
+ }
+ };
+
+ // we want the Base class name to be the name of the class.
+ Object.defineProperty(DomIterable, "name", {
+ value: Base.name,
+ configurable: true
+ });
+
+ return DomIterable;
+}
diff --git a/cli/js/mixins/dom_iterable_test.ts b/cli/js/mixins/dom_iterable_test.ts
new file mode 100644
index 000000000..4c84fa68e
--- /dev/null
+++ b/cli/js/mixins/dom_iterable_test.ts
@@ -0,0 +1,79 @@
+// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
+import { test, assert, assertEquals } from "../test_util.ts";
+
+// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
+function setup() {
+ const dataSymbol = Symbol("data symbol");
+ class Base {
+ private [dataSymbol] = new Map<string, number>();
+
+ constructor(
+ data: Array<[string, number]> | IterableIterator<[string, number]>
+ ) {
+ for (const [key, value] of data) {
+ this[dataSymbol].set(key, value);
+ }
+ }
+ }
+
+ return {
+ Base,
+ // This is using an internal API we don't want published as types, so having
+ // to cast to any to "trick" TypeScript
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ DomIterable: (Deno as any).DomIterableMixin(Base, dataSymbol)
+ };
+}
+
+test(function testDomIterable(): void {
+ const { DomIterable, Base } = setup();
+
+ const fixture: Array<[string, number]> = [["foo", 1], ["bar", 2]];
+
+ const domIterable = new DomIterable(fixture);
+
+ assertEquals(Array.from(domIterable.entries()), fixture);
+ assertEquals(Array.from(domIterable.values()), [1, 2]);
+ assertEquals(Array.from(domIterable.keys()), ["foo", "bar"]);
+
+ let result: Array<[string, number]> = [];
+ for (const [key, value] of domIterable) {
+ assert(key != null);
+ assert(value != null);
+ result.push([key, value]);
+ }
+ assertEquals(fixture, result);
+
+ result = [];
+ const scope = {};
+ function callback(value, key, parent): void {
+ assertEquals(parent, domIterable);
+ assert(key != null);
+ assert(value != null);
+ assert(this === scope);
+ result.push([key, value]);
+ }
+ domIterable.forEach(callback, scope);
+ assertEquals(fixture, result);
+
+ assertEquals(DomIterable.name, Base.name);
+});
+
+test(function testDomIterableScope(): void {
+ const { DomIterable } = setup();
+
+ const domIterable = new DomIterable([["foo", 1]]);
+
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ function checkScope(thisArg: any, expected: any): void {
+ function callback(): void {
+ assertEquals(this, expected);
+ }
+ domIterable.forEach(callback, thisArg);
+ }
+
+ checkScope(0, Object(0));
+ checkScope("", Object(""));
+ checkScope(null, window);
+ checkScope(undefined, window);
+});