Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
emonkak committed Oct 5, 2016
1 parent d51735c commit 3f7a31b
Show file tree
Hide file tree
Showing 74 changed files with 146 additions and 164 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: node_js
node_js:
- '6'
- '5'
- '4'
before_script:
- $(npm bin)/tsc --version
script:
Expand Down
3 changes: 2 additions & 1 deletion src/count.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export default function count<TSource>(this: Iterable<TSource>, predicate?: (ite
return count;
} else {
if (Array.isArray(this)) return (this as Array<TSource>).length;
const iterator = this[Symbol.iterator]();
let count = 0;
for (const element of this) {
for (let result = iterator.next(); !result.done; result = iterator.next()) {
count++;
}
return count;
Expand Down
3 changes: 0 additions & 3 deletions src/internal/IComparer.ts

This file was deleted.

6 changes: 2 additions & 4 deletions src/internal/OrderedEnumerable.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import IComparer from './IComparer';
import OrderedPartition from './OrderedPartition';
import Partition from './Partition';
import partialQuickSort from './partialQuickSort';
import quickSelect from './quickSelect';
import { Enumerable } from './Enumerable';
Expand Down Expand Up @@ -306,7 +304,7 @@ export default class OrderedEnumerable<TElement, TKey> extends Enumerable<TEleme
return array.sort(comparer);
}

toArrayPartition(minIndex: number, maxIndex: number): TElement[] {
toArrayInPartition(minIndex: number, maxIndex: number): TElement[] {
const array = Array.from(this._source);
const count = array.length;

Expand All @@ -325,7 +323,7 @@ export default class OrderedEnumerable<TElement, TKey> extends Enumerable<TEleme
return [];
}

private _getComparer(next?: IComparer<TElement>): IComparer<TElement> {
private _getComparer(next?: (first: TElement, second: TElement) => number): (first: TElement, second: TElement) => number {
if (!next) next = () => 0;
const comparer = (first: TElement, second: TElement): number => {
const firstKey = this._keySelector(first);
Expand Down
2 changes: 1 addition & 1 deletion src/internal/OrderedPartition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export default class OrderedPartition<TElement> extends Partition<TElement> {
}

[Symbol.iterator](): Iterator<TElement> {
return this._source.toArrayPartition(this._minIndex, this._maxIndex)[Symbol.iterator]();
return this._source.toArrayInPartition(this._minIndex, this._maxIndex)[Symbol.iterator]();
}

skip(count: number): Partition<TElement> {
Expand Down
3 changes: 1 addition & 2 deletions src/internal/partialQuickSort.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import IComparer from './IComparer';
import pivotPosition from './pivotPosition';

export default function partialQuickSort<T>(array: Array<T>, comparer: IComparer<T>, left: number, right: number, minIndex: number, maxIndex: number): void {
export default function partialQuickSort<T>(array: Array<T>, comparer: (first: T, second: T) => number, left: number, right: number, minIndex: number, maxIndex: number): void {
if (left < right) {
const pivotIndex = left + ((right - left) >> 1);
const pivotNewIndex = pivotPosition(array, comparer, left, right, pivotIndex);
Expand Down
4 changes: 1 addition & 3 deletions src/internal/pivotPosition.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import IComparer from './IComparer';

export default function pivotPosition<T>(array: Array<T>, comparer: IComparer<T>, left: number, right: number, pivotIndex: number): number {
export default function pivotPosition<T>(array: Array<T>, comparer: (first: T, second: T) => number, left: number, right: number, pivotIndex: number): number {
const pivotValue = array[pivotIndex];

swap(array, right, pivotIndex);
Expand Down
3 changes: 1 addition & 2 deletions src/internal/quickSelect.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import IComparer from './IComparer';
import pivotPosition from './pivotPosition';

export default function quickSelect<T>(array: Array<T>, comparer: IComparer<T>, left: number, right: number, n: number): T {
export default function quickSelect<T>(array: Array<T>, comparer: (first: T, second: T) => number, left: number, right: number, n: number): T {
while (true) {
if (left === right) return array[left];

Expand Down
2 changes: 1 addition & 1 deletion test/aggregateTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Enumerable from '../src';
import * as assert from 'assert';
import Enumerable from '../src/';

describe('aggregate()', () => {
it('should applies an accumulator function over a sequence', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/allTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Enumerable from '../src';
import * as assert from 'assert';
import Enumerable from '../src/';

describe('all()', () => {
it('should determines whether all elements of a sequence satisfy a condition', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/anyTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Enumerable from '../src';
import * as assert from 'assert';
import Enumerable from '../src/';

describe('any()', () => {
it('should determines whether a sequence contains any elements', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/averageTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('average()', () => {
it('should computes the average of a sequence of numbers', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/catchTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('catch()', () => {
it('should creates a sequence that corresponds to the source sequence', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/countTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('count()', () => {
it('should returns the number of elements in a sequence', () => {
Expand Down
6 changes: 3 additions & 3 deletions test/defaultIfEmptyTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import defaultIfEmpty from '../src/defaultIfEmpty';
import Enumerable from '../src/';

describe('defaultIfEmpty()', () => {
it('should returns the elements of the specified sequence', () => {
assert.deepEqual(Array.from(defaultIfEmpty.call([1, 2, 3], 123)), [1, 2, 3]);
assert.deepEqual(new Enumerable([1, 2, 3]).defaultIfEmpty(123).toArray(), [1, 2, 3]);
});

it('should returns the specified value in a singleton collection if the sequence is empty', () => {
assert.deepEqual(Array.from(defaultIfEmpty.call([], 123)), [123]);
assert.deepEqual(new Enumerable([]).defaultIfEmpty(123).toArray(), [123]);
});
});
2 changes: 1 addition & 1 deletion test/distinctTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('distinct()', () => {
it('should returns distinct elements from a sequence', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/distinctUntilChangedTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('distinctUntilChanged()', () => {
it('should returns consecutive distinct elements', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/doTest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('do()', () => {
it('should lazily invokes an action for each value in the sequence, and executes an action upon exceptional termination', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/doWhileTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('doWhile()', () => {
it('should generates an enumerable sequence by repeating a source sequence as long as the given loop postcondition holds', () => {
Expand Down
45 changes: 21 additions & 24 deletions test/elementAtOrDefaultTest.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,40 @@
import * as assert from 'assert';
import elementAtOrDefault from '../src/elementAtOrDefault';
import orderBy from '../src/orderBy';
import skip from '../src/skip';
import take from '../src/take';
import Enumerable from '../src/';

describe('elementAtOrDefault()', () => {
it('should returns the element at a specified index in a sequence', () => {
const xs = [1, 2, 3, 4];

assert.strictEqual(elementAtOrDefault.call(xs, 0), 1);
assert.strictEqual(elementAtOrDefault.call(xs, 1), 2);
assert.strictEqual(elementAtOrDefault.call(xs, 2), 3);
assert.strictEqual(elementAtOrDefault.call(xs, 3), 4);
assert.strictEqual(new Enumerable(xs).elementAtOrDefault(0), 1);
assert.strictEqual(new Enumerable(xs).elementAtOrDefault(1), 2);
assert.strictEqual(new Enumerable(xs).elementAtOrDefault(2), 3);
assert.strictEqual(new Enumerable(xs).elementAtOrDefault(3), 4);
});

it('should returns a default value if the index is out of range', () => {
const xs = [1, 2, 3, 4];

assert.strictEqual(elementAtOrDefault.call(xs, 4, 123), 123);
assert.strictEqual(elementAtOrDefault.call(xs, 4), null);
assert.strictEqual(new Enumerable(xs).elementAtOrDefault(4, 123), 123);
assert.strictEqual(new Enumerable(xs).elementAtOrDefault(4), null);
});

it('should works with orderBy()', () => {
const xs = [3, 2, 4, 1];

assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 0), 1);
assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 1), 2);
assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 2), 3);
assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 3), 4);
assert.strictEqual(elementAtOrDefault.call(take.call(orderBy.call(xs), 2), 0), 1);
assert.strictEqual(elementAtOrDefault.call(take.call(orderBy.call(xs), 2), 1), 2);
assert.strictEqual(elementAtOrDefault.call(skip.call(orderBy.call(xs), 2), 0), 3);
assert.strictEqual(elementAtOrDefault.call(skip.call(orderBy.call(xs), 2), 1), 4);
assert.strictEqual(elementAtOrDefault.call(skip.call(take.call(orderBy.call(xs), 2), 1), 0), 2);
assert.strictEqual(elementAtOrDefault.call(take.call(skip.call(orderBy.call(xs), 2), 1), 0), 3);
assert.strictEqual(new Enumerable(xs).orderBy().elementAtOrDefault(0), 1);
assert.strictEqual(new Enumerable(xs).orderBy().elementAtOrDefault(1), 2);
assert.strictEqual(new Enumerable(xs).orderBy().elementAtOrDefault(2), 3);
assert.strictEqual(new Enumerable(xs).orderBy().elementAtOrDefault(3), 4);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).elementAtOrDefault(0), 1);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).elementAtOrDefault(1), 2);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).elementAtOrDefault(0), 3);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).elementAtOrDefault(1), 4);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).elementAtOrDefault(0), 2);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).elementAtOrDefault(0), 3);

assert.strictEqual(elementAtOrDefault.call(orderBy.call(xs), 4, 123), 123);
assert.strictEqual(elementAtOrDefault.call(take.call(orderBy.call(xs), 2), 3, 123), 123);
assert.strictEqual(elementAtOrDefault.call(skip.call(take.call(orderBy.call(xs), 2), 1), 1, 123), 123);
assert.strictEqual(elementAtOrDefault.call(take.call(skip.call(orderBy.call(xs), 2), 1), 1, 123), 123);
assert.strictEqual(new Enumerable(xs).orderBy().elementAtOrDefault(4, 123), 123);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).elementAtOrDefault(3, 123), 123);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).elementAtOrDefault(1, 123), 123);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).elementAtOrDefault(1, 123), 123);
});
});
43 changes: 20 additions & 23 deletions test/elementAtTest.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,39 @@
import * as assert from 'assert';
import elementAt from '../src/elementAt';
import orderBy from '../src/orderBy';
import skip from '../src/skip';
import take from '../src/take';
import Enumerable from '../src/';

describe('elementAt()', () => {
it('should returns the element at a specified index in a sequence', () => {
const xs = [1, 2, 3, 4];

assert.strictEqual(elementAt.call(xs, 0), 1);
assert.strictEqual(elementAt.call(xs, 1), 2);
assert.strictEqual(elementAt.call(xs, 2), 3);
assert.strictEqual(elementAt.call(xs, 3), 4);
assert.strictEqual(new Enumerable(xs).elementAt(0), 1);
assert.strictEqual(new Enumerable(xs).elementAt(1), 2);
assert.strictEqual(new Enumerable(xs).elementAt(2), 3);
assert.strictEqual(new Enumerable(xs).elementAt(3), 4);
});

it('should throws the exception if index is out of range', () => {
const xs = [1, 2, 3, 4];

assert.throws(() => elementAt.call(xs, 4));
assert.throws(() => new Enumerable(xs).elementAt(4));
});

it('should works with orderBy()', () => {
const xs = [3, 2, 4, 1];

assert.strictEqual(elementAt.call(orderBy.call(xs), 0), 1);
assert.strictEqual(elementAt.call(orderBy.call(xs), 1), 2);
assert.strictEqual(elementAt.call(orderBy.call(xs), 2), 3);
assert.strictEqual(elementAt.call(orderBy.call(xs), 3), 4);
assert.strictEqual(elementAt.call(take.call(orderBy.call(xs), 2), 0), 1);
assert.strictEqual(elementAt.call(take.call(orderBy.call(xs), 2), 1), 2);
assert.strictEqual(elementAt.call(skip.call(orderBy.call(xs), 2), 0), 3);
assert.strictEqual(elementAt.call(skip.call(orderBy.call(xs), 2), 1), 4);
assert.strictEqual(elementAt.call(skip.call(take.call(orderBy.call(xs), 2), 1), 0), 2);
assert.strictEqual(elementAt.call(take.call(skip.call(orderBy.call(xs), 2), 1), 0), 3);
assert.strictEqual(new Enumerable(xs).orderBy().elementAt(0), 1);
assert.strictEqual(new Enumerable(xs).orderBy().elementAt(1), 2);
assert.strictEqual(new Enumerable(xs).orderBy().elementAt(2), 3);
assert.strictEqual(new Enumerable(xs).orderBy().elementAt(3), 4);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).elementAt(0), 1);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).elementAt(1), 2);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).elementAt(0), 3);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).elementAt(1), 4);
assert.strictEqual(new Enumerable(xs).orderBy().take(2).skip(1).elementAt(0), 2);
assert.strictEqual(new Enumerable(xs).orderBy().skip(2).take(1).elementAt(0), 3);

assert.throws(() => elementAt.call(orderBy.call(xs), 4));
assert.throws(() => elementAt.call(take.call(orderBy.call(xs), 2), 3));
assert.throws(() => elementAt.call(skip.call(take.call(orderBy.call(xs), 2), 1), 1));
assert.throws(() => elementAt.call(take.call(skip.call(orderBy.call(xs), 2), 1), 1));
assert.throws(() => new Enumerable(xs).orderBy().elementAt(4));
assert.throws(() => new Enumerable(xs).orderBy().take(2).elementAt(3));
assert.throws(() => new Enumerable(xs).orderBy().take(2).skip(1).elementAt(1));
assert.throws(() => new Enumerable(xs).orderBy().skip(2).take(1).elementAt(1));
});
});
10 changes: 5 additions & 5 deletions test/exceptTest.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as assert from 'assert';
import except from '../src/except';
import Enumerable from '../src/';

describe('except()', () => {
it('should produces the set difference of two sequences', () => {
assert.deepEqual(Array.from(except.call([], [])), []);
assert.deepEqual(Array.from(except.call([1, 2, 3, 4, 5, 6], [2, 3, 4])), [1, 5, 6]);
assert.deepEqual(Array.from(except.call([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6], [2, 3, 4])), [1, 5, 6]);
assert.deepEqual(Array.from(except.call([2, 3, 4], [1, 2, 3, 4, 5, 6])), []);
assert.deepEqual(new Enumerable([]).except([]).toArray(), []);
assert.deepEqual(new Enumerable([1, 2, 3, 4, 5, 6]).except([2, 3, 4]).toArray(), [1, 5, 6]);
assert.deepEqual(new Enumerable([1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]).except([2, 3, 4]).toArray(), [1, 5, 6]);
assert.deepEqual(new Enumerable([2, 3, 4]).except([1, 2, 3, 4, 5, 6]).toArray(), []);
});
});
6 changes: 3 additions & 3 deletions test/finallyTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as assert from 'assert';
import * as sinon from 'sinon';
import _finally from '../src/finally';
import Enumerable from '../src/';

describe('finally()', () => {
it('should creates a sequence whose termination or disposal of an enumerator causes a finally action to be executed', () => {
const source1 = [1, 2, 3];
const finallyAction1 = sinon.spy();
assert.deepEqual(Array.from(_finally.call(source1, finallyAction1)), [1, 2, 3]);
assert.deepEqual(new Enumerable(source1).finally(finallyAction1).toArray(), [1, 2, 3]);
sinon.assert.called(finallyAction1);

const source2 = {
Expand All @@ -18,7 +18,7 @@ describe('finally()', () => {
}
};
const finallyAction2 = sinon.spy();
assert.throws(() => Array.from(_finally.call(source2, finallyAction2)));
assert.throws(() => new Enumerable(source2).finally(finallyAction2).toArray());
sinon.assert.called(finallyAction2);
});
});
2 changes: 1 addition & 1 deletion test/firstOrDefaultTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('firstOrDefault()', () => {
it('should returns the first element of a sequence', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/firstTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('first()', () => {
it('should returns the first element of a sequence', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/forEachTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as sinon from 'sinon';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('forEach()', () => {
it('should enumerates the sequence and invokes the given action for each value in the sequence', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/groupByTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('groupBy()', () => {
it('should groups the elements of a sequence according to a specified key selector function', () => {
Expand Down
2 changes: 1 addition & 1 deletion test/groupJoinTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as assert from 'assert';
import Enumerable from '../src';
import Enumerable from '../src/';

describe('groupJoin()', () => {
it('should correlates the elements of two sequences based on equality of keys and groups the results', () => {
Expand Down
6 changes: 2 additions & 4 deletions test/intersectTest.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import * as assert from 'assert';
import intersect from '../src/intersect';
import Enumerable from '../src/';

describe('intersect()', () => {
it('should Produces the set intersection of two sequences by using the default equality comparer to compare values', () => {
const xs = [44, 26, 92, 30, 71, 38];
const ys = [39, 59, 83, 47, 26, 4, 30];
const result = intersect.call(xs, ys);

assert.deepEqual(Array.from(result), [26, 30]);
assert.deepEqual(new Enumerable(xs).intersect(ys).toArray(), [26, 30]);
});
});
Loading

0 comments on commit 3f7a31b

Please sign in to comment.