Skip to content

Commit

Permalink
Fixes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Jul 21, 2016
1 parent e046b78 commit 704eb89
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 8 deletions.
8 changes: 8 additions & 0 deletions lib/enumerable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,14 @@ export interface Enumerable<T> extends Iterable<T>, IEnumerable<T> {
* var e = asEnumerable([0, 1, 2, 3, 4, 5, 6, 7]).Min()
*/
Min(transform?: (x: T) => number): number;

/**
* Filters the elements of an IEnumerable based on a specified type.
* @param Object to check InstanceOf against.
* @example
* var e = asEnumerable([0, 1, 2, 3, 4, 5, 6, 7]).OfType(myObject)
*/
OfType(type: any): Enumerable<T>;

/**
* Sorts the elements of a sequence in ascending order by using a specified
Expand Down
46 changes: 46 additions & 0 deletions lib/generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
// under the License.


import * as Constant from "./utilities";


export function* Forward<T>(target: Array<T>) {
yield* target;
}
Expand Down Expand Up @@ -65,6 +68,49 @@ export function* DistinctFast<T>(target: Iterable<T>) {
}


export function* OfType<T>(target: Iterable<T>, obj: any) {
if (Number === obj) {
for (let value of target) {
if (Constant.CONST_NUMBER == typeof(value)) {
yield value;
} else if (value instanceof obj) {
yield value;
}
}
} else if (Boolean === obj) {
for (let value of target) {
if (Constant.CONST_BOOLEAN == typeof(value)) {
yield value;
} else if (value instanceof obj) {
yield value;
}
}
} else if (String === obj) {
for (let value of target) {
if (Constant.CONST_STRING == typeof(value)) {
yield value;
} else if (value instanceof obj) {
yield value;
}
}
} else if (Symbol === obj) {
for (let value of target) {
if (Constant.CONST_SYMBOL == typeof(value)) {
yield value;
} else if (value instanceof obj) {
yield value;
}
}
} else {
for (let value of target) {
if (value instanceof obj) {
yield value;
}
}
}
}


export function* Where<T>(target: Iterable<T>, predicate: (x: T, i: number) => Boolean) {
let index = 0;
for (let value of target) {
Expand Down
7 changes: 7 additions & 0 deletions lib/linq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ class EnumerableImpl<T> implements Enumerable<T>, Iterable<T>, IEnumerable<T> {
(this as any)['groupJoin'] = this.GroupJoin;
(this as any)['intersect'] = this.Intersect;
(this as any)['join'] = this.Join;
(this as any)['ofType'] = this.OfType;
(this as any)['orderBy'] = this.OrderBy;
(this as any)['orderByDescend'] = this.OrderByDescending;
(this as any)['thenBy'] = this.ThenBy;
Expand Down Expand Up @@ -571,6 +572,12 @@ class EnumerableImpl<T> implements Enumerable<T>, Iterable<T>, IEnumerable<T> {
return this as any as Enumerable<R>;
}


public OfType(obj: any): Enumerable<T> {
this._target = Generator.OfType(this._target, obj);
return this;
}


public OrderBy<K>(keySelect: (x: T) => K = Constant.selfFn,
equal: (a: K, b: K) => number = (a, b) => <any>a - <any>b):
Expand Down
22 changes: 22 additions & 0 deletions test/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,26 @@ var daisy = { Name: "Daisy", Age: 4, Owner: magnus };
export var people = [magnus, terry, charlotte];
export var pets = [barley, boots, whiskers, daisy];

export var mix = [
0,
new Number(1),
"This is a simple string",
new String(),
new String("String created with constructor"),
new Date(),
true,
Symbol.iterator,
2,
false,
{ Name: "asd"},
simpleArray,
un1,
new Boolean(true),
pets,
new Boolean(false),
3,
function(){}
];


/** Copyright (c) ENikS. All rights reserved. */
95 changes: 87 additions & 8 deletions test/deferred.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// License for the specific language governing permissions and limitations
// under the License.

import {simpleArray, oddArray, jsn, un1, un2, people, pets} from "./data";
import {simpleArray, oddArray, jsn, un1, un2, people, pets, mix} from "./data";
import {assert} from "chai";
import Linq from "../lib/linq";

Expand Down Expand Up @@ -239,24 +239,103 @@ describe('Deferred Execution -', function () {



// Union
// Except

it('Union()', function () {
var iterable = Linq([0, 1, 2, 3, 4, 5, 6, 7]).Union([5, 6, 7, 8, 9]);
it('Except()', function () {
var iterable = Linq(simpleArray).Except([0, 2, 4, 6, 11]);
var iterator = iterable[Symbol.iterator]()
assert.equal(0, iterator.next().value);
assert.equal(1, iterator.next().value);
assert.equal(2, iterator.next().value);
assert.equal(3, iterator.next().value);
assert.equal(4, iterator.next().value);
assert.equal(5, iterator.next().value);
assert.equal(6, iterator.next().value);
assert.equal(7, iterator.next().value);
assert.equal(8, iterator.next().value);
assert.equal(9, iterator.next().value);
assert.equal(10, iterator.next().value);
assert.isTrue(iterator.next().done);
});



// OfType

it('OfType() - Number', function () {

var iterable = Linq(mix).OfType(Number);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, 0);
assert.equal(iterator.next().value, 1);
assert.equal(iterator.next().value, 2);
assert.equal(iterator.next().value, 3);
assert.isTrue(iterator.next().done);
});

it('OfType() - Boolean', function () {

var iterable = Linq(mix).OfType(Boolean);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, true);
assert.equal(iterator.next().value, false);
assert.equal(iterator.next().value, true);
assert.equal(iterator.next().value, false);
assert.isTrue(iterator.next().done);
});

it('OfType() - String', function () {

var iterable = Linq(mix).OfType(String);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, mix[2]);
assert.equal(iterator.next().value, mix[3]);
assert.equal(iterator.next().value, mix[4]);
assert.isTrue(iterator.next().done);
});

it('OfType() - Date', function () {

var iterable = Linq(mix).OfType(Date);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, mix[5]);
assert.isTrue(iterator.next().done);
});

it('OfType() - Symbol', function () {

var iterable = Linq(mix).OfType(Symbol);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, mix[7]);
assert.isTrue(iterator.next().done);
});

it('OfType() - Function', function () {

var iterable = Linq(mix).OfType(Function);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, mix[17]);
assert.isTrue(iterator.next().done);
});

it('OfType() - Object', function () {

var iterable = Linq(mix).OfType(Object);
var iterator = iterable[Symbol.iterator]();
assert.equal(iterator.next().value, 1);
assert.equal(iterator.next().value, mix[3]);
assert.equal(iterator.next().value, mix[4]);
assert.equal(iterator.next().value, mix[5]);
assert.equal(iterator.next().value, mix[10]);
assert.equal(iterator.next().value, mix[11]);
assert.equal(iterator.next().value, mix[12]);
assert.equal(iterator.next().value, mix[13]);
assert.equal(iterator.next().value, mix[14]);
assert.equal(iterator.next().value, mix[15]);
assert.equal(iterator.next().value, mix[17]);
assert.isTrue(iterator.next().done);
});




// Union

it('Union() - Keyed', function () {
var iterable = Linq(un1).Union(un2, (o) => o.id);
Expand Down

0 comments on commit 704eb89

Please sign in to comment.