diff --git a/lib/iterators.ts b/lib/iterators.ts index f57ef24..986e7be 100644 --- a/lib/iterators.ts +++ b/lib/iterators.ts @@ -132,6 +132,40 @@ export class DefaultIfEmptyIteratror extends IteratorBase { } +export class OfTypeIteratror extends IteratorBase { + + constructor(target: Iterator, protected obj: any) { + super(target); + } + + public next(value?: any): IteratorResult { + var result: any; + do { + result = this._iterator.next(); + } while (!result.done && !(result.value instanceof this.obj)); + return result; + } +} + + +export class OfValueTypeIteratror extends OfTypeIteratror { + + constructor(target: Iterator, obj: any, protected typeName: string) { + super(target, obj); + } + + public next(value?: any): IteratorResult { + 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 extends IteratorBase { constructor(iterator: Iterator, protected _method: Function = null, protected _index = 0) { diff --git a/lib/linq.ts b/lib/linq.ts index 07fdb3c..4021b26 100644 --- a/lib/linq.ts +++ b/lib/linq.ts @@ -128,6 +128,7 @@ class EnumerableImpl implements Enumerable, Iterable, IEnumerable { (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; @@ -550,6 +551,37 @@ class EnumerableImpl implements Enumerable, Iterable, IEnumerable { } + public OfType(obj: any): Enumerable { + let typeName: string; + switch (obj) { + case Number: + return new EnumerableImpl(this, + () => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](), + obj, + Constant.CONST_NUMBER)); + case Boolean: + return new EnumerableImpl(this, + () => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](), + obj, + Constant.CONST_BOOLEAN)); + case String: + return new EnumerableImpl(this, + () => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](), + obj, + Constant.CONST_STRING)); + case Symbol: + return new EnumerableImpl(this, + () => new Iterator.OfValueTypeIteratror(this[Symbol.iterator](), + obj, + Constant.CONST_SYMBOL)); + } + + return new EnumerableImpl(this, + () => new Iterator.OfTypeIteratror(this[Symbol.iterator](), + obj)); + } + + public OrderBy(keySelect: (x: T) => K = Constant.selfFn, equal: (a: K, b: K) => number = (a, b) => a - b): Enumerable { return new OrderedLinq(this, (array: Array) => new Iterator.ArrayIterator(array, 0, (i: number) => i >= array.length), diff --git a/lib/utilities.ts b/lib/utilities.ts index 3a641fd..dc42771 100644 --- a/lib/utilities.ts +++ b/lib/utilities.ts @@ -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";