Skip to content

Commit

Permalink
fix(tests): Get closure working on tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mpodwysocki authored and trxcllnt committed Sep 1, 2020
1 parent 8692d0c commit 4c0705f
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 161 deletions.
21 changes: 2 additions & 19 deletions spec/asynciterable-operators/share-spec.ts
@@ -1,6 +1,6 @@
import { hasNext, noNext } from '../asynciterablehelpers';
import { share, take, tap } from 'ix/asynciterable/operators';
import { range, toArray, zip } from 'ix/asynciterable';
import { share, take } from 'ix/asynciterable/operators';
import { range, toArray } from 'ix/asynciterable';
import { sequenceEqual } from 'ix/iterable';

test('AsyncIterable#share single', async () => {
Expand Down Expand Up @@ -54,20 +54,3 @@ test('AsyncIterable#share shared does not interrupt', async () => {
const res2 = await toArray(rng);
expect(sequenceEqual(res2, [3, 4])).toBe(true);
});

test('AsyncIterable#share with selector', async () => {
let n = 0;
const res = await toArray(
range(0, 10).pipe(
tap({
next: async () => {
n++;
}
}),
share(xs => zip(([l, r]) => l + r, xs, xs).pipe(take(4)))
)
);

expect(sequenceEqual(res, [0 + 1, 2 + 3, 4 + 5, 6 + 7])).toBeTruthy();
expect(8).toBe(n);
});
14 changes: 10 additions & 4 deletions spec/asynciterable/tomap-spec.ts
Expand Up @@ -3,28 +3,34 @@ import { of, toMap } from 'ix/asynciterable';

test('AsyncIterable#toMap stores values', async () => {
const xs = of(1, 4);
const res = await toMap(xs, async (x) => x % 2);
const res = await toMap(xs, { keySelector: async (x) => x % 2 });
expect(res.get(0)).toBe(4);
expect(res.get(1)).toBe(1);
});

test('AsyncIterable#toMap overwrites duplicates', async () => {
const xs = of(1, 4, 2);
const res = await toMap(xs, async (x) => x % 2);
const res = await toMap(xs, { keySelector: async (x) => x % 2 });
expect(res.get(0)).toBe(2);
expect(res.get(1)).toBe(1);
});

test('AsyncIterable#toMap with element selector', async () => {
const xs = of(1, 4);
const res = await toMap(xs, async (x) => x % 2, { elementSelector: async (x) => x + 1 });
const res = await toMap(xs, {
keySelector: async (x) => x % 2,
elementSelector: async (x) => x + 1,
});
expect(res.get(0)).toBe(5);
expect(res.get(1)).toBe(2);
});

test('AsyncIterable#toMap with element selector overwrites duplicates', async () => {
const xs = of(1, 4, 2);
const res = await toMap(xs, async (x) => x % 2, { elementSelector: async (x) => x + 1 });
const res = await toMap(xs, {
keySelector: async (x) => x % 2,
elementSelector: async (x) => x + 1,
});
expect(res.get(0)).toBe(3);
expect(res.get(1)).toBe(2);
});
15 changes: 2 additions & 13 deletions spec/iterable-operators/share-spec.ts
@@ -1,6 +1,6 @@
import { hasNext, noNext } from '../iterablehelpers';
import { share, take, tap } from 'ix/iterable/operators';
import { range, sequenceEqual, toArray, zip } from 'ix/iterable';
import { share } from 'ix/iterable/operators';
import { range } from 'ix/iterable';

test('Iterable#share single', () => {
const rng = range(0, 5).pipe(share());
Expand Down Expand Up @@ -43,14 +43,3 @@ test('Iterable#share shared exhausts any time', () => {
noNext(it1);
noNext(it2);
});

test('Iterable#share with selector', () => {
let n = 0;
const res = range(0, 10)
.pipe(tap({ next: () => n++ }))
.pipe(share(xs => zip(([l, r]) => l + r, xs, xs).pipe(take(4))))
.pipe(toArray);

expect(sequenceEqual(res, [0 + 1, 2 + 3, 4 + 5, 6 + 7])).toBeTruthy();
expect(8).toBe(n);
});
File renamed without changes.
Expand Up @@ -11,9 +11,9 @@ import { map, toDOMStream } from 'ix/iterable/operators';
});
}

const stringsItr = () => from([1, 2, 3]).pipe(map(i => `${i}`));
const buffersItr = () => stringsItr().pipe(map(val => Buffer.from(val)));
const objectsItr = () => stringsItr().pipe(map(val => ({ val })));
const stringsItr = () => from([1, 2, 3]).pipe(map((i) => `${i}`));
const buffersItr = () => stringsItr().pipe(map((val) => Buffer.from(val)));
const objectsItr = () => stringsItr().pipe(map((val) => ({ val })));
const compare = <T>(a: T, b: T) => {
const aVal = ArrayBuffer.isView(a) ? `${Buffer.from(a.buffer, a.byteOffset, a.byteLength)}` : a;
const bVal = ArrayBuffer.isView(b) ? `${Buffer.from(b.buffer, b.byteOffset, b.byteLength)}` : b;
Expand All @@ -29,8 +29,8 @@ import { map, toDOMStream } from 'ix/iterable/operators';
describe('Iterable#toDOMStream', () => {
describe('DefaultController', () => {
const expectedStrings = ['1', '2', '3'];
const expectedObjects = expectedStrings.map(val => ({ val }));
const expectedBuffers = expectedStrings.map(x => Buffer.from(x));
const expectedObjects = expectedStrings.map((val) => ({ val }));
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
const expected = from(expectedStrings);
const actual = stringsItr().pipe(toDOMStream());
Expand All @@ -50,11 +50,11 @@ import { map, toDOMStream } from 'ix/iterable/operators';

describe('ReadableByteStreamController (byobRequest)', () => {
const expectedStrings = ['123'];
const expectedBuffers = expectedStrings.map(x => Buffer.from(x));
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
const expected = from(expectedBuffers);
const actual = stringsItr()
.pipe(map(x => Buffer.from(x)))
.pipe(map((x) => Buffer.from(x)))
.pipe(toDOMStream({ type: 'bytes' }));
await expect(actual).toEqualStream(expected, compare);
});
Expand All @@ -67,11 +67,11 @@ import { map, toDOMStream } from 'ix/iterable/operators';

describe('ReadableByteStreamController (autoAllocateChunkSize)', () => {
const expectedStrings = ['123'];
const expectedBuffers = expectedStrings.map(x => Buffer.from(x));
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
const expected = from(expectedBuffers);
const actual = stringsItr()
.pipe(map(x => Buffer.from(x)))
.pipe(map((x) => Buffer.from(x)))
.pipe(toDOMStream({ type: 'bytes', autoAllocateChunkSize: 1024 }));
await expect(actual).toEqualStream(expected, compare);
});
Expand Down
Expand Up @@ -2,25 +2,25 @@ import '../iterablehelpers';
import { toMap } from 'ix/iterable';

test('Iterable#toMap stores values', () => {
const res = toMap([1, 4], x => x % 2);
const res = toMap([1, 4], { keySelector: (x) => x % 2 });
expect(res.get(0)).toBe(4);
expect(res.get(1)).toBe(1);
});

test('Iterable#toMap overwrites duplicates', () => {
const res = toMap([1, 4, 2], x => x % 2);
const res = toMap([1, 4, 2], { keySelector: (x) => x % 2 });
expect(res.get(0)).toBe(2);
expect(res.get(1)).toBe(1);
});

test('Iterable#toMap with element selector', () => {
const res = toMap([1, 4], x => x % 2, x => x + 1);
const res = toMap([1, 4], { keySelector: (x) => x % 2, elementSelector: (x) => x + 1 });
expect(res.get(0)).toBe(5);
expect(res.get(1)).toBe(2);
});

test('Iterable#toMap with element selector overwrites duplicates', () => {
const res = toMap([1, 4, 2], x => x % 2, x => x + 1);
const res = toMap([1, 4, 2], { keySelector: (x) => x % 2, elementSelector: (x) => x + 1 });
expect(res.get(0)).toBe(3);
expect(res.get(1)).toBe(2);
});
Expand Up @@ -13,9 +13,9 @@ import { IterableReadable } from 'ix/Ix.node';
});
}

const stringsItr = () => fromIterable([1, 2, 3]).pipe(map(i => `${i}`));
const buffersItr = () => stringsItr().pipe(map(val => Buffer.from(val)));
const objectsItr = () => stringsItr().pipe(map(val => ({ val })));
const stringsItr = () => fromIterable([1, 2, 3]).pipe(map((i) => `${i}`));
const buffersItr = () => stringsItr().pipe(map((val) => Buffer.from(val)));
const objectsItr = () => stringsItr().pipe(map((val) => ({ val })));
const compare = <T>(a: T, b: T) => {
const aVal = ArrayBuffer.isView(a) ? `${Buffer.from(a.buffer, a.byteOffset, a.byteLength)}` : a;
const bVal = ArrayBuffer.isView(b) ? `${Buffer.from(b.buffer, b.byteOffset, b.byteLength)}` : b;
Expand All @@ -31,8 +31,8 @@ import { IterableReadable } from 'ix/Ix.node';
describe('Iterable#toNodeStream', () => {
describe('objectMode: true', () => {
const expectedStrings = ['1', '2', '3'];
const expectedObjects = expectedStrings.map(val => ({ val }));
const expectedBuffers = expectedStrings.map(x => Buffer.from(x));
const expectedObjects = expectedStrings.map((val) => ({ val }));
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
await expect(stringsItr().pipe(toNodeStream({ objectMode: true }))).toEqualStream(
fromAsyncIterable(expectedStrings),
Expand All @@ -55,7 +55,7 @@ import { IterableReadable } from 'ix/Ix.node';

describe('objectMode: false', () => {
const expectedStrings = ['123'];
const expectedBuffers = expectedStrings.map(x => Buffer.from(x));
const expectedBuffers = expectedStrings.map((x) => Buffer.from(x));
test('yields Strings', async () => {
await expect(stringsItr().pipe(toNodeStream({ objectMode: false }))).toEqualStream(
fromAsyncIterable(expectedStrings),
Expand Down
File renamed without changes.
14 changes: 12 additions & 2 deletions spec/iterable/while-spec.ts → spec/iterable/whiledo-spec.ts
Expand Up @@ -4,14 +4,24 @@ import { defer, sequenceEqual, toArray, whileDo } from 'ix/iterable';

test('Iterable#while some', () => {
let x = 5;
const res = toArray(whileDo(() => x > 0, defer(() => tap({ next: () => x-- })([x]))));
const res = toArray(
whileDo(
defer(() => tap({ next: () => x-- })([x])),
() => x > 0
)
);

expect(sequenceEqual(res, [5, 4, 3, 2, 1])).toBeTruthy();
});

test('Iterable#while none', () => {
let x = 0;
const res = toArray(whileDo(() => x > 0, defer(() => tap({ next: () => x-- })([x]))));
const res = toArray(
whileDo(
defer(() => tap({ next: () => x-- })([x])),
() => x > 0
)
);

expect(sequenceEqual(res, [])).toBeTruthy();
});
5 changes: 2 additions & 3 deletions src/add/asynciterable-operators/tomap.ts
Expand Up @@ -3,10 +3,9 @@ import { toMap, ToMapOptions } from '../../asynciterable/tomap';

export async function toMapProto<TSource, TKey, TElement = TSource>(
this: AsyncIterable<TSource>,
keySelector: (item: TSource, signal?: AbortSignal) => TKey | Promise<TKey>,
options?: ToMapOptions<TSource, TElement>
options: ToMapOptions<TSource, TElement>
): Promise<Map<TKey, TElement | TSource>> {
return toMap(this, keySelector, options);
return toMap(this, options);
}

AsyncIterableX.prototype.toMap = toMapProto;
Expand Down
5 changes: 2 additions & 3 deletions src/add/iterable-operators/tomap.ts
Expand Up @@ -3,10 +3,9 @@ import { toMap, ToMapOptions } from '../../iterable/tomap';

export function toMapProto<TSource, TKey, TElement = TSource>(
this: IterableX<TSource>,
keySelector: (item: TSource) => TKey,
options?: ToMapOptions<TSource, TElement>
options: ToMapOptions<TSource, TElement>
): Map<TKey, TElement | TSource> {
return toMap(this, keySelector, options);
return toMap(this, options);
}

IterableX.prototype.toMap = toMapProto;
Expand Down
12 changes: 6 additions & 6 deletions src/asynciterable/_extremaby.ts
@@ -1,26 +1,26 @@
import { wrapWithAbort } from './operators/withabort';
import { ExtremaOptions } from './extremaoptions';

export async function extremaBy<TSource, TKey>(
source: AsyncIterable<TSource>,
options: ExtremaOptions<TSource, TKey>
selector: (item: TSource, signal?: AbortSignal) => TKey | Promise<TKey>,
comparer: (left: TKey, right: TKey) => number | Promise<number>,
signal?: AbortSignal
): Promise<TSource[]> {
const { ['comparer']: comparer, ['signal']: signal, ['selector']: selector } = options;
let result = [];
const it = wrapWithAbort(source, signal)[Symbol.asyncIterator]();
const { value, done } = await it.next();
if (done) {
throw new Error('Sequence contains no elements');
}

let resKey = await selector!(value, signal);
let resKey = await selector(value, signal);
result.push(value);

let next: IteratorResult<TSource>;
while (!(next = await it.next()).done) {
const { value: current } = next;
const key = await selector!(current, signal);
const cmp = comparer!(resKey, key);
const key = await selector(current, signal);
const cmp = await comparer(resKey, key);

if (cmp === 0) {
result.push(current);
Expand Down
2 changes: 1 addition & 1 deletion src/asynciterable/count.ts
Expand Up @@ -23,7 +23,7 @@ export async function count<T>(
let i = 0;

for await (const item of wrapWithAbort(source, signal)) {
if (await predicate!.call(thisArg, item, i, signal)) {
if (await predicate.call(thisArg, item, i, signal)) {
i++;
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/asynciterable/maxby.ts
Expand Up @@ -17,12 +17,10 @@ export async function maxBy<TSource, TKey>(
source: AsyncIterable<TSource>,
options?: ExtremaOptions<TSource, TKey>
): Promise<TSource[]> {
const opts = options || ({} as ExtremaOptions<TSource, TKey>);
if (!opts.comparer) {
opts.comparer = equalityComparerAsync;
}
if (!opts.selector) {
opts.selector = identityAsync;
}
return extremaBy(source, opts);
const {
['comparer']: comparer = equalityComparerAsync,
['selector']: selector = identityAsync,
['signal']: signal,
} = options || {};
return extremaBy(source, selector, comparer, signal);
}
18 changes: 8 additions & 10 deletions src/asynciterable/minby.ts
Expand Up @@ -10,20 +10,18 @@ import { identityAsync } from '../util/identity';
* @template TSource The type of the elements in the source sequence.
* @template TKey The type of the key computed for each element in the source sequence.
* @param {AsyncIterable<TSource>} source An async-iterable sequence to get the minimum elements for.
* @param {ExtremaOptions<TSource, TKey>} [options] The options which include an optional comparer and abort signal.
* @param {ExtremaOptions<TSource, TKey>} options The options which include an optional comparer and abort signal.
* @returns {Promise<TSource[]>} A promise containing a list of zero or more elements that have a minimum key value.
*/
export async function minBy<TSource, TKey>(
source: AsyncIterable<TSource>,
options?: ExtremaOptions<TSource, TKey>
): Promise<TSource[]> {
const opts = options || ({} as ExtremaOptions<TSource, TKey>);
if (!opts.comparer) {
opts.comparer = equalityComparerAsync;
}
if (!opts.selector) {
opts.selector = identityAsync;
}
opts.comparer = (key, minValue) => -opts.comparer!(key, minValue);
return extremaBy(source, opts);
const {
['comparer']: comparer = equalityComparerAsync,
['selector']: selector = identityAsync,
['signal']: signal,
} = options || {};
const newComparer = (key: TKey, minValue: TKey) => -comparer!(key, minValue);
return extremaBy(source, selector, newComparer, signal);
}
2 changes: 1 addition & 1 deletion src/asynciterable/operators/distinct.ts
Expand Up @@ -54,6 +54,6 @@ export function distinct<TSource, TKey = TSource>(
): AsyncIterableX<TSource> {
const { ['keySelector']: keySelector = identityAsync, ['comparer']: comparer = comparerAsync } =
options || {};
return new DistinctAsyncIterable<TSource, TKey>(source, keySelector!, comparer!);
return new DistinctAsyncIterable<TSource, TKey>(source, keySelector, comparer);
};
}
2 changes: 1 addition & 1 deletion src/asynciterable/operators/distinctuntilchanged.ts
Expand Up @@ -60,6 +60,6 @@ export function distinctUntilChanged<TSource, TKey = TSource>(
): AsyncIterableX<TSource> {
const { ['keySelector']: keySelector = identityAsync, ['comparer']: comparer = comparerAsync } =
options || {};
return new DistinctUntilChangedAsyncIterable<TSource, TKey>(source, keySelector!, comparer!);
return new DistinctUntilChangedAsyncIterable<TSource, TKey>(source, keySelector, comparer);
};
}
12 changes: 6 additions & 6 deletions src/asynciterable/sum.ts
Expand Up @@ -34,15 +34,15 @@ export async function sum<T>(source: AsyncIterable<T>, options?: MathOptions<T>)
* @returns {Promise<number>} A promise containing the sum of the sequence of values.
*/
export async function sum(source: AsyncIterable<any>, options?: MathOptions<any>): Promise<number> {
const opts = options || ({} as MathOptions<any>);
if (!opts.selector) {
opts.selector = identityAsync;
}
const { ['selector']: selector, ['signal']: signal, ['thisArg']: thisArg } = opts;
const {
['selector']: selector = identityAsync as any,
['signal']: signal,
['thisArg']: thisArg,
} = options || {};
throwIfAborted(signal);
let value = 0;
for await (const item of wrapWithAbort(source, signal)) {
value += await selector!.call(thisArg, item, signal);
value += await selector.call(thisArg, item, signal);
}

return value;
Expand Down

0 comments on commit 4c0705f

Please sign in to comment.