Skip to content

Commit

Permalink
feat(endWith): adds endWith operator
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpodwysocki committed Oct 5, 2017
1 parent 740d401 commit f967e3b
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 2 deletions.
12 changes: 12 additions & 0 deletions spec/asynciterable-operators/endwith-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Ix from '../Ix';
import * as test from 'tape';
const { range } = Ix.asynciterable;
const { sequenceEqual } = Ix.asynciterable;
const { endWith } = Ix.asynciterable;

test('AsyncIterable#endWith adds to end', async t => {
const e = range(0, 5);
const r = endWith(e, 6, 7);
t.true(await sequenceEqual(r, range(0, 7)));
t.end();
});
12 changes: 12 additions & 0 deletions spec/iterable-operators/endwith-spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as Ix from '../Ix';
import * as test from 'tape';
const { range } = Ix.iterable;
const { sequenceEqual } = Ix.iterable;
const { endWith } = Ix.iterable;

test('Iterable#endWith adds to end', t => {
const e = range(0, 5);
const r = endWith(e, 6, 7);
t.true(sequenceEqual(r, range(0, 7)));
t.end();
});
19 changes: 19 additions & 0 deletions src/add/asynciterable-operators/endwith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AsyncIterableX } from '../../asynciterable';
import { endWith } from '../../asynciterable/endwith';

/**
* @ignore
*/
export function endWithProto<T>(
this: AsyncIterableX<T>,
...args: T[]) {
return endWith(this, ...args);
}

AsyncIterableX.prototype.endWith = endWithProto;

declare module '../../asynciterable' {
interface AsyncIterableX<T> {
endWith: typeof endWithProto;
}
}
19 changes: 19 additions & 0 deletions src/add/iterable-operators/endwith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { IterableX } from '../../iterable';
import { endWith } from '../../iterable/endwith';

/**
* @ignore
*/
export function endWithProto<T>(
this: IterableX<T>,
...args: T[]) {
return endWith(this, ...args);
}

IterableX.prototype.endWith = endWithProto;

declare module '../../iterable' {
interface IterableX<T> {
endWith: typeof endWithProto;
}
}
4 changes: 3 additions & 1 deletion src/asynciterable/__modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { distinctUntilChanged } from './distinctuntilchanged';
import { doWhile } from './dowhile';
import { elementAt } from './elementat';
import { empty } from './empty';
import { endWith } from './endwith';
import { every } from './every';
import { except } from './except';
import { expand } from './expand';
Expand Down Expand Up @@ -139,6 +140,7 @@ export default {
doWhile,
elementAt,
empty,
endWith,
every,
except,
expand,
Expand Down Expand Up @@ -220,4 +222,4 @@ export default {
union,
_while,
zip
};
};
23 changes: 23 additions & 0 deletions src/asynciterable/endwith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { AsyncIterableX } from '../asynciterable';

class EndWithAsyncIterable<TSource> extends AsyncIterableX<TSource> {
private _source: AsyncIterable<TSource>;
private _args: TSource[];

constructor(source: AsyncIterable<TSource>, args: TSource[]) {
super();
this._source = source;
this._args = args;
}

async *[Symbol.asyncIterator]() {
for await (let item of this._source) { yield item; }
for (let x of this._args) { yield x; }
}
}

export function endWith<TSource>(
source: AsyncIterable<TSource>,
...args: TSource[]): AsyncIterableX<TSource> {
return new EndWithAsyncIterable<TSource>(source, args);
}
4 changes: 3 additions & 1 deletion src/iterable/__modules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { distinctUntilChanged } from './distinctuntilchanged';
import { doWhile } from './dowhile';
import { elementAt } from './elementat';
import { empty } from './empty';
import { endWith } from './endwith';
import { every } from './every';
import { except } from './except';
import { expand } from './expand';
Expand Down Expand Up @@ -128,6 +129,7 @@ export default {
doWhile,
elementAt,
empty,
endWith,
every,
except,
expand,
Expand Down Expand Up @@ -200,4 +202,4 @@ export default {
union,
_while,
zip
};
};
23 changes: 23 additions & 0 deletions src/iterable/endwith.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { IterableX } from '../iterable';

class EndWithIterable<TSource> extends IterableX<TSource> {
private _source: Iterable<TSource>;
private _args: TSource[];

constructor(source: Iterable<TSource>, args: TSource[]) {
super();
this._source = source;
this._args = args;
}

*[Symbol.iterator]() {
for (let item of this._source) { yield item; }
for (let x of this._args) { yield x; }
}
}

export function endWith<TSource>(
source: Iterable<TSource>,
...args: TSource[]): IterableX<TSource> {
return new EndWithIterable<TSource>(source, args);
}

0 comments on commit f967e3b

Please sign in to comment.