diff --git a/declarations/wu.js b/declarations/wu.js index 0f7e6e1..8d5b6f5 100644 --- a/declarations/wu.js +++ b/declarations/wu.js @@ -6,6 +6,8 @@ declare module 'wu' { declare export default class Wu { static (Iterable): Wu; + static values(T): Wu<$Values>; + tap(T => mixed): Wu; map(T => U): Wu; @@ -24,6 +26,8 @@ declare module 'wu' { some(): boolean; some(T => boolean): boolean; + forEach(T => mixed): void; + toArray(): T[]; } } diff --git a/tests/run.js b/tests/run.js index 748a7e0..9c2dafe 100644 --- a/tests/run.js +++ b/tests/run.js @@ -2,11 +2,12 @@ import * as assert from 'assert'; import * as fs from 'fs'; import * as path from 'path'; import * as yaml from 'yaml-js'; +import wu from 'wu'; import collect from '../src'; function run(title) { - let actual, expected; + let actual, expected: any; // Run the collector only if the suite will be checked. before(() => { @@ -14,9 +15,33 @@ function run(title) { expected = yaml.load(fs.readFileSync(title + '.yaml', 'utf8')); }); - it('should provide expected types', () => { - assert.deepEqual((actual: any).types, (expected: any).types); + it('should not include cycles', () => { + assert.deepEqual(detectCycles(actual.types), new Set); }); + + it('should provide expected types', () => { + assert.deepEqual(actual.types, expected.types); + }); +} + +function detectCycles(obj: mixed, cycles: Set = new Set, objs: Set = new Set) { + if (obj == null || typeof obj !== 'object') { + return cycles; + } + + if (objs.has(obj)) { + cycles.add(obj); + } + + objs.add(obj); + + if (obj instanceof Array) { + wu(obj).forEach(item => detectCycles(item, cycles, objs)); + } else { + wu.values(obj).forEach(item => detectCycles(item, cycles, objs)); + } + + return cycles; } function main() {