diff options
Diffstat (limited to 'js/mixins')
-rw-r--r-- | js/mixins/dom_iterable.ts | 70 | ||||
-rw-r--r-- | js/mixins/dom_iterable_test.ts | 78 |
2 files changed, 148 insertions, 0 deletions
diff --git a/js/mixins/dom_iterable.ts b/js/mixins/dom_iterable.ts new file mode 100644 index 000000000..015226589 --- /dev/null +++ b/js/mixins/dom_iterable.ts @@ -0,0 +1,70 @@ +import { DomIterable } from "../dom_types"; +import { globalEval } from "../global_eval"; + +// if we import it directly from "globals" it will break the unit tests so we +// have to grab a reference to the global scope a different way +const window = globalEval("this"); + +// tslint:disable:no-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]`. + */ +export function DomIterableMixin<K, V, TBase extends Constructor>( + // tslint:disable-next-line:variable-name + 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. + + // tslint:disable-next-line:variable-name + const DomIterable = class extends Base { + *entries(): IterableIterator<[K, V]> { + for (const entry of (this as any)[dataSymbol].entries()) { + yield entry; + } + } + + *keys(): IterableIterator<K> { + for (const key of (this as any)[dataSymbol].keys()) { + yield key; + } + } + + *values(): IterableIterator<V> { + for (const value of (this as any)[dataSymbol].values()) { + yield value; + } + } + + forEach( + callbackfn: (value: V, key: K, parent: this) => void, + // tslint:disable-next-line:no-any + thisArg?: any + ): void { + callbackfn = callbackfn.bind(thisArg == null ? window : Object(thisArg)); + for (const [key, value] of (this as any)[dataSymbol].entries()) { + callbackfn(value, key, this); + } + } + + *[Symbol.iterator](): IterableIterator<[K, V]> { + 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; +} +// tslint:enable:no-any diff --git a/js/mixins/dom_iterable_test.ts b/js/mixins/dom_iterable_test.ts new file mode 100644 index 000000000..a4791c1b8 --- /dev/null +++ b/js/mixins/dom_iterable_test.ts @@ -0,0 +1,78 @@ +// Copyright 2018 the Deno authors. All rights reserved. MIT license. +import { test, assert, assertEqual } from "../test_util.ts"; +import { DomIterableMixin } from "./dom_iterable.ts"; + +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, + DomIterable: DomIterableMixin<string, number, typeof Base>(Base, dataSymbol) + }; +} + +test(function testDomIterable() { + // tslint:disable-next-line:variable-name + const { DomIterable, Base } = setup(); + + const fixture: Array<[string, number]> = [["foo", 1], ["bar", 2]]; + + const domIterable = new DomIterable(fixture); + + assertEqual(Array.from(domIterable.entries()), fixture); + assertEqual(Array.from(domIterable.values()), [1, 2]); + assertEqual(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]); + } + assertEqual(fixture, result); + + result = []; + const scope = {}; + function callback(value, key, parent) { + assertEqual(parent, domIterable); + assert(key != null); + assert(value != null); + assert(this === scope); + result.push([key, value]); + } + domIterable.forEach(callback, scope); + assertEqual(fixture, result); + + assertEqual(DomIterable.name, Base.name); +}); + +test(function testDomIterableScope() { + // tslint:disable-next-line:variable-name + const { DomIterable } = setup(); + + const domIterable = new DomIterable([["foo", 1]]); + + // tslint:disable-next-line:no-any + function checkScope(thisArg: any, expected: any) { + function callback() { + assertEqual(this, expected); + } + domIterable.forEach(callback, thisArg); + } + + checkScope(0, Object(0)); + checkScope("", Object("")); + checkScope(null, window); + checkScope(undefined, window); +}); |