diff --git a/lib/generators.ts b/lib/generators.ts index 02bdece..8043dff 100644 --- a/lib/generators.ts +++ b/lib/generators.ts @@ -68,34 +68,10 @@ export function* DistinctFast(target: Iterable) { } -export function* OfType(target: Iterable, obj: any) { - if (Number === obj) { +export function* OfType(target: Iterable, obj: any, typeName: string) { + if (typeName){ 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)) { + if (typeName == typeof(value)) { yield value; } else if (value instanceof obj) { yield value; @@ -111,7 +87,8 @@ export function* OfType(target: Iterable, obj: any) { } -export function* Where(target: Iterable, predicate: (x: T, i: number) => Boolean) { +export function* Where(target: Iterable, + predicate: (x: T, i: number) => Boolean) { let index = 0; for (let value of target) { if (!predicate(value, index++)) continue; diff --git a/lib/linq.ts b/lib/linq.ts index 46bb28f..c42b0cf 100644 --- a/lib/linq.ts +++ b/lib/linq.ts @@ -574,7 +574,29 @@ class EnumerableImpl implements Enumerable, Iterable, IEnumerable { public OfType(obj: any): Enumerable { - this._target = Generator.OfType(this._target, obj); + let typeName: string; + switch (obj) { + case Number: + typeName = Constant.CONST_NUMBER; + break; + + case Boolean: + typeName = Constant.CONST_BOOLEAN; + break; + + case String: + typeName = Constant.CONST_STRING; + break; + + case Symbol: + typeName = Constant.CONST_SYMBOL; + break; + + default: + typeName = undefined; + } + + this._target = Generator.OfType(this._target, obj, typeName); return this; }