Skip to content

Commit

Permalink
Implementing OfType(...)
Browse files Browse the repository at this point in the history
  • Loading branch information
ENikS committed Jul 21, 2016
1 parent 6faaa72 commit 051060e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
34 changes: 34 additions & 0 deletions lib/iterators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,40 @@ export class DefaultIfEmptyIteratror<T> extends IteratorBase<T> {
}


export class OfTypeIteratror<T> extends IteratorBase<T> {

constructor(target: Iterator<T>, protected obj: any) {
super(target);
}

public next(value?: any): IteratorResult<T> {
var result: any;
do {
result = this._iterator.next();
} while (!result.done && !(result.value instanceof this.obj));
return result;
}
}


export class OfValueTypeIteratror<T> extends OfTypeIteratror<T> {

constructor(target: Iterator<T>, obj: any, protected typeName: string) {
super(target, obj);
}

public next(value?: any): IteratorResult<T> {
var result: any;
do {
result = this._iterator.next();
} while (!result.done &&
this.typeName !== typeof(result.value) &&
!(result.value instanceof this.obj));
return result;
}
}


export class MethodIteratror<T> extends IteratorBase<T> {

constructor(iterator: Iterator<T>, protected _method: Function = null, protected _index = 0) {
Expand Down
32 changes: 32 additions & 0 deletions lib/linq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ class EnumerableImpl<T> implements Enumerable<T>, Iterable<T>, IEnumerable<T> {
(this as any)['groupBy'] = this.GroupBy;
(this as any)['groupJoin'] = this.GroupJoin;
(this as any)['intersect'] = this.Intersect;
(this as any)['ofType'] = this.OfType;
(this as any)['join'] = this.Join;
(this as any)['orderBy'] = this.OrderBy;
(this as any)['orderByDescend'] = this.OrderByDescending;
Expand Down Expand Up @@ -550,6 +551,37 @@ class EnumerableImpl<T> implements Enumerable<T>, Iterable<T>, IEnumerable<T> {
}


public OfType(obj: any): Enumerable<T> {
let typeName: string;
switch (obj) {
case Number:
return new EnumerableImpl<T>(this,
() => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](),
obj,
Constant.CONST_NUMBER));
case Boolean:
return new EnumerableImpl<T>(this,
() => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](),
obj,
Constant.CONST_BOOLEAN));
case String:
return new EnumerableImpl<T>(this,
() => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](),
obj,
Constant.CONST_STRING));
case Symbol:
return new EnumerableImpl<T>(this,
() => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](),
obj,
Constant.CONST_SYMBOL));
}

return new EnumerableImpl<T>(this,
() => new Iterator.OfTypeIteratror(this[Symbol.iterator](),
obj));
}


public OrderBy<K>(keySelect: (x: T) => K = Constant.selfFn, equal: (a: K, b: K) => number = (a, b) => <any>a - <any>b): Enumerable<T> {
return new OrderedLinq<T>(this,
(array: Array<T>) => new Iterator.ArrayIterator(array, 0, (i: number) => i >= array.length),
Expand Down
8 changes: 6 additions & 2 deletions lib/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ export const nothingFound = "No element satisfies the condition in predicate";
export const noElements = "The source sequence is empty.";
export const tooMany = "More than one element satisfies the condition in predicate.";
export const outOfRange = "Argument Out Of Range";


export const CONST_FUNCTION = "function";
export const CONST_BOOLEAN = "boolean";
export const CONST_NUMBER = "number";
export const CONST_OBJECT = "object";
export const CONST_STRING = "string";
export const CONST_SYMBOL = "symbol";

0 comments on commit 051060e

Please sign in to comment.