Skip to content

Commit 574fbb4

Browse files
author
Oren (electricessence)
committed
Checkpoint.
1 parent 59c40fd commit 574fbb4

11 files changed

+88
-98
lines changed

src/iterables/concat.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* @author electricessence / https://github.com/electricessence/
3+
* Licensing: MIT
4+
*/
5+
6+
import concatThese from './concatThese';
7+
8+
/**
9+
* Concatenates the provided sequences.
10+
*/
11+
export default function concat<T> (...sequences: Iterable<T>[]): Iterable<T> {
12+
return concatThese(sequences);
13+
}

src/iterables/concatThese.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @author electricessence / https://github.com/electricessence/
3+
* Licensing: MIT
4+
*/
5+
6+
/**
7+
* Concatenates the sequences.
8+
*/
9+
export default function* concatThese<T> (sequences: Iterable<Iterable<T>>): Iterable<T> {
10+
for(const s of sequences)
11+
{
12+
for(const e of s)
13+
{
14+
yield e;
15+
}
16+
}
17+
}

src/linq.ts

+33-21
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,51 @@
33
* Licensing: MIT
44
*/
55

6-
import {IterableTransform} from './pipe';
6+
import {IterableFilter, IterableTransform} from './IterableTransform';
77

8-
export type LinqTransform<TIn, TOut> = (source: Iterable<TIn>) => TOut;
9-
10-
export interface Linqable<T>
11-
extends Iterable<T>
12-
{
13-
linq<TResult> (filter: LinqTransform<T, TResult>): TResult;
14-
}
15-
16-
class Linq<TIn, TOut>
17-
implements Linqable<TOut>
8+
export class Linq<T>
9+
implements Iterable<T>
1810
{
1911
constructor (
20-
private readonly _source: Iterable<TIn>,
21-
private readonly _transform?: IterableTransform<TIn, TOut>)
12+
private readonly _source: Iterable<T>)
2213
{
2314
}
2415

25-
[Symbol.iterator] (): Iterator<TOut>
16+
[Symbol.iterator] (): Iterator<T>
2617
{
27-
return this._transform(this._source)[Symbol.iterator]();
18+
return this._source[Symbol.iterator]();
2819
}
2920

21+
filter (): this
22+
filter (...filters: IterableFilter<T>[]): Linq<T>
23+
filter (...filters: IterableFilter<T>[]): Linq<T> | this
24+
{
25+
if(!filters?.length) return this;
26+
let iterable = this._source;
27+
for(const filter of filters)
28+
{
29+
iterable = filter(iterable);
30+
}
31+
return new Linq<T>(iterable);
32+
}
3033

31-
pipe<TResult> (filter: IterableTransform<TOut, TResult>): TResult
34+
toArray (): T[]
3235
{
33-
return new Linq<TOut, TResult>(this, filter);
36+
const a: T[] = [];
37+
for(const e of this._source)
38+
{
39+
a.push(e);
40+
}
41+
return a;
3442
}
3543

44+
resolve<TResolution> (resolution: IterableTransform<T, TResolution>): TResolution
45+
{
46+
return resolution(this._source);
47+
}
3648
}
3749

38-
export default function linq<T, TResult> (
39-
source: Iterable<T>): Pipe<TResult> {
40-
return new PipeSource(source, filter);
41-
}
50+
// export default function linq<T, TResult> (
51+
// source: Iterable<T>): Pipe<TResult> {
52+
// return new PipeSource(source, filter);
53+
// }

src/pipe.ts

-68
This file was deleted.

src/resolutions/any.ts

+3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55

66
/* eslint-disable @typescript-eslint/no-unused-vars */
77

8+
import ArgumentNullException from '@tsdotnet/exceptions/dist/ArgumentNullException';
9+
810
/**
911
* Returns true if sequence is not empty.
1012
*/
1113
export default function any<T> (sequence: Iterable<T>): boolean {
14+
if(!sequence) throw new ArgumentNullException('sequence');
1215
// noinspection LoopStatementThatDoesntLoopJS
1316
for(const _ of sequence)
1417
{

src/resolutions/elementAt.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensing: MIT
44
*/
55

6-
import IterableTransform from '../IterableTransform';
6+
import {IterableTransform} from '../IterableTransform';
77
import ArgumentOutOfRangeException from '@tsdotnet/exceptions/dist/ArgumentOutOfRangeException';
88
import integer from '@tsdotnet/integer';
99

src/resolutions/elementAtOrDefault.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* Licensing: MIT
44
*/
55

6-
import IterableTransform from '../IterableTransform';
6+
import {IterableTransform} from '../IterableTransform';
77
import integer from '@tsdotnet/integer';
88

99
/**

src/resolutions/first.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,18 @@
33
* Licensing: MIT
44
*/
55

6+
import ArgumentNullException from '@tsdotnet/exceptions/dist/ArgumentNullException';
7+
import InvalidOperationException from '@tsdotnet/exceptions/dist/InvalidOperationException';
8+
69
/**
710
* Returns the first element of a sequence.
811
*/
9-
export default function first<T> (elements: Iterable<T>): T {
12+
export default function first<T> (sequence: Iterable<T>): T {
13+
if(!sequence) throw new ArgumentNullException('sequence');
1014
// noinspection LoopStatementThatDoesntLoopJS
11-
for(const e of elements)
15+
for(const e of sequence)
1216
{
1317
return e;
1418
}
15-
throw new Error('The sequence is empty.');
19+
throw new InvalidOperationException('The sequence is empty.');
1620
}

src/resolutions/firstOrDefault.ts

+3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* Licensing: MIT
44
*/
55

6+
import ArgumentNullException from '@tsdotnet/exceptions/dist/ArgumentNullException';
7+
68
/**
79
* Returns the first element of a sequence, or a default value if no element is found.
810
*/
911
export default function firstOrDefault<T> (sequence: Iterable<T>): T | undefined {
12+
if(!sequence) throw new ArgumentNullException('sequence');
1013
// noinspection LoopStatementThatDoesntLoopJS
1114
for(const e of sequence)
1215
{

src/resolutions/single.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
* Licensing: MIT
44
*/
55

6+
import ArgumentNullException from '@tsdotnet/exceptions/dist/ArgumentNullException';
7+
68
/**
79
* Returns a single, specific element of a sequence.
810
*/
9-
export default function single<T> (elements: Iterable<T>): T {
11+
export default function single<T> (sequence: Iterable<T>): T {
12+
if(!sequence) throw new ArgumentNullException('sequence');
1013
// noinspection LoopStatementThatDoesntLoopJS
1114
let i = 0;
1215
let value: T;
13-
for(const e of elements)
16+
for(const e of sequence)
1417
{
1518
if(i++) throw new Error('Sequence contains more than one element.');
1619
value = e;

src/resolutions/singleOrDefault.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
* Licensing: MIT
44
*/
55

6+
import ArgumentNullException from '@tsdotnet/exceptions/dist/ArgumentNullException';
7+
68
/**
79
* Returns a single, specific element of a sequence, or a default value if that element is not found.
810
*/
9-
export default function singleOrDefault<T> (elements: Iterable<T>): T | undefined {
11+
export default function singleOrDefault<T> (sequence: Iterable<T>): T | undefined {
12+
if(!sequence) throw new ArgumentNullException('sequence');
1013
// noinspection LoopStatementThatDoesntLoopJS
1114
let i = 0;
1215
let value: T;
13-
for(const e of elements)
16+
for(const e of sequence)
1417
{
1518
if(i++) throw new Error('Sequence contains more than one element.');
1619
value = e;

0 commit comments

Comments
 (0)