linq-es5迭代数组的方法库。
linq.js
"use strict"; // 继承静态方法和原型方法,仍需调用父类构造函数获得父类的私有属性 var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); require("es6-shim"); var Iterator = require("./iterators"); var Constant = require("./utilities"); // 创建EnumerableImpl实例,TSource作为EnumerableImpl构造函数的首参 function getEnumerable(TSource) { if (TSource === void 0) { TSource = null; } return new EnumerableImpl(TSource); } exports.default = getEnumerable; exports.AsEnumerable = getEnumerable; exports.asEnumerable = getEnumerable; exports.From = getEnumerable; exports.from = getEnumerable; // 获取从start序号位开始,到start+count-1结束的迭代值 function getRange(start, count) { // Iterator.Generator(_current,_count,_increment)从_current序号位顺序获取_count个序号值(_increment为真值时) // 或者重复获取_current序号值(_increment为否值时) return new EnumerableImpl(null, function () { return new Iterator.Generator(start, count, true); }); } exports.Range = getRange; exports.range = getRange; // 重复获取start序号位的迭代值count次 function getRepeat(start, count) { return new EnumerableImpl(null, function () { return new Iterator.Generator(start, count); }); } exports.Repeat = getRepeat; exports.repeat = getRepeat; var EnumerableImpl = (function () { // 参数target目标数组或对象(通常是数组);参数factory用户构建的迭代器;参数arg传入factory的参数 // 对外不暴露接口,通过getEnumerable暴露的接口只能传递target参数;传参factory等均只在内部调用时发生 // // 示例1,针对target为数组,将target作为factory的传参 // factory=(arg=target)=>{ // return ()=>{ // let index=0; // return { // next:()=>{ // return index<arg.length ? {value:arg[index]} : {done:true}; // }, // current:arg[index], // prev:()=>{ // return index>0 ? {value:arg[--index]} : {done:true}; // } // } // } // } // 示例2,针对target为对象{name,age,...},sortedKeys完成排序的键如["age","name",...] // factory=(target,sortedKeys)=>{ // return ()=>{ // let index=0; // return { // next:()=>{ // var key=sortedKeys[index++]; // return index<sortedKeys.length ? {value:target[index]} : {done:true}; // } // } // } // } // // 补注:Set无重复项的数组,WeakSet只接受对象作为数组项的Set;Map对象的键不限任何类型 function EnumerableImpl(target, factory, arg) { this._target = target; this._factory = factory; this._factoryArg = arg; // 小写形式方法 this['aggregate'] = this.Aggregate; this['all'] = this.All; this['any'] = this.Any; this['average'] = this.Average; this['contains'] = this.Contains; this['count'] = this.Count; this['max'] = this.Max; this['min'] = this.Min; this['elementAt'] = this.ElementAt; this['elementAtOrDefault'] = this.ElementAtOrDefault; this['first'] = this.First; this['firstOrDefault'] = this.FirstOrDefault; this['last'] = this.Last; this['lastOrDefault'] = this.LastOrDefault; this['sequenceEqual'] = this.SequenceEqual; this['single'] = this.Single; this['singleOrDefault'] = this.SingleOrDefault; this['sum'] = this.Sum; this['toArray'] = this.ToArray; this['toMap'] = this.ToMap; this['toDictionary'] = this.ToDictionary; this['defaultIfEmpty'] = this.DefaultIfEmpty; this['concat'] = this.Concat; this['distinct'] = this.Distinct; this['except'] = this.Except; this['groupBy'] = this.GroupBy; this['groupJoin'] = this.GroupJoin; this['intersect'] = this.Intersect; this['ofType'] = this.OfType; this['join'] = this.Join; this['orderBy'] = this.OrderBy; this['orderByDescend'] = this.OrderByDescending; this['thenBy'] = this.ThenBy; this['thenByDescendi'] = this.ThenByDescending; this['range'] = this.Range; this['repeat'] = this.Repeat; this['reverse'] = this.Reverse; this['select'] = this.Select; this['selectMany'] = this.SelectMany; this['skip'] = this.Skip; this['skipWhile'] = this.SkipWhile; this['take'] = this.Take; this['takeWhile'] = this.TakeWhile; this['union'] = this.Union; this['where'] = this.Where; this['zip'] = this.Zip; } // 通过this._factory构建迭代器,或者由this._target[Symbol.iterator]()构建迭代器 EnumerableImpl.prototype[Symbol.iterator] = function () { return (this._factory) ? this._factory(this._factoryArg) : (null != this._target) ? this._target[Symbol.iterator]() : { next: function () { return { done: true, value: undefined }; } }; }; // 返回C#风格的迭代器 EnumerableImpl.prototype.GetEnumerator = function () { return new Iterator.CSharpEnumerator(this[Symbol.iterator]()); }; // 一般方法 // 遍历迭代值,执行func,func参数为前次执行结果及当前迭代值,首次调用func时为传参seed和当前迭代值 // func返回值传入resultSelector函数中,用以获取最终返回值 EnumerableImpl.prototype.Aggregate = function (seed, func, resultSelector) { if (func === void 0) { func = Constant.selfFn; } if (resultSelector === void 0) { resultSelector = Constant.selfFn; } var zero; var method; var selector; if ("function" === typeof seed) { method = seed; selector = func; } else { zero = seed; method = func; selector = resultSelector; } var result = zero; var res, iterator = this[Symbol.iterator](); while (!(res = iterator.next()).done) { if (!result) result = Constant.getDefaultVal(typeof (res.value)); result = method(result, res.value); } return selector(result); }; // 遍历迭代值,所有迭代值均匹配predicate,返回真值;否则返回否值 EnumerableImpl.prototype.All = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var result, iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (!predicate(result.value)) { return false; } } return true; }; // 遍历迭代值,若有迭代值匹配predicate,返回真值;否则返回否值 EnumerableImpl.prototype.Any = function (predicate) { var result, iterator = this[Symbol.iterator](); if (null == predicate) { return !iterator.next().done; } while (!(result = iterator.next()).done) { if (predicate(result.value)) { return true; } } return false; }; // 遍历迭代值,通过func函数对迭代值求和后再求平均 EnumerableImpl.prototype.Average = function (func) { if (func === void 0) { func = Constant.selfFn; } var result, sum = 0, count = 0; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { sum += func(result.value); count++; } return sum / count; }; // 遍历迭代值,若有迭代值通过equal函数和value作比较返回真值,则返回真值;否则返回否值 EnumerableImpl.prototype.Contains = function (value, equal) { if (equal === void 0) { equal = function (a, b) { return a === b; }; } var result, iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (equal(value, result.value)) { return true; } } return false; }; // 遍历迭代值,获取匹配predicate的迭代值个数 EnumerableImpl.prototype.Count = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var result, count = 0; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (predicate(result.value)) { count++; } } return count; }; // 遍历迭代值,获取经transform转化最大的迭代值 EnumerableImpl.prototype.Max = function (transform) { if (transform === void 0) { transform = Constant.selfFn; } var result, value, max, hasValue = false; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { value = transform(result.value); if (hasValue) { if (max < value) max = value; } else { max = value; hasValue = true; } } if (!hasValue) throw Constant.CONST_NO_ELEMENTS; return max; }; // 遍历迭代值,获取经transform转化最小的迭代值 EnumerableImpl.prototype.Min = function (transform) { if (transform === void 0) { transform = Constant.selfFn; } var result, value, min, hasValue = false; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { value = transform(result.value); if (hasValue) { if (min > value) min = value; } else { min = value; hasValue = true; } } if (!hasValue) throw Constant.CONST_NO_ELEMENTS; return min; }; // 遍历迭代值,获取index序号位置的迭代值;index序号位置无值时,报错 EnumerableImpl.prototype.ElementAt = function (index) { var result, count = 0; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (index === count++) { return result.value; } } throw "Argument Out Of Range"; }; // 遍历迭代值,获取index序号位置的迭代值;index序号位置无值时,返回Constant.getDefaultVal默认值 EnumerableImpl.prototype.ElementAtOrDefault = function (index) { var result, value, count = 0; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (index === count++) { return result.value; } value = result.value; } return Constant.getDefaultVal(typeof value, value); // Last good value }; // 遍历迭代值,获取首个匹配predicate的迭代值;无匹配时,报错 EnumerableImpl.prototype.First = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var result; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (predicate(result.value)) { return result.value; } } throw Constant.CONST_NOTHING_FOUND; }; // 遍历迭代值,获取首个匹配predicate的迭代值;无匹配时,返回Constant.getDefaultVal默认值 EnumerableImpl.prototype.FirstOrDefault = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var result, value; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { value = result.value; if (predicate(value)) { return result.value; } } return Constant.getDefaultVal(typeof value); // Last good value }; // 遍历迭代值,获取最终匹配predicate的迭代值;无匹配时,报错 EnumerableImpl.prototype.Last = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var result, value, found = false; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (predicate(result.value)) { value = result.value; found = true; } } if (!found) { throw Constant.CONST_NOTHING_FOUND; } return value; }; // 遍历迭代值,获取最终匹配predicate的迭代值;无匹配时,返回Constant.getDefaultVal默认值 EnumerableImpl.prototype.LastOrDefault = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var result, value, lastKnown, found = false; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (predicate(result.value)) { value = result.value; found = true; } lastKnown = result.value; } return (found) ? value : Constant.getDefaultVal(typeof lastKnown); }; // 通过比较函数equal判断当前迭代器和other迭代器的迭代内容是否相同 EnumerableImpl.prototype.SequenceEqual = function (other, equal) { if (equal === void 0) { equal = function (a, b) { return a === b; }; } var res1, res2; var it1 = this[Symbol.iterator](); var it2 = other[Symbol.iterator](); do { res1 = it1.next(); res2 = it2.next(); if (res1.done && res2.done) return true; if ((res1.done != res2.done) || !equal(res1.value, res2.value)) { return false; } } while (true); }; // 遍历迭代值,获取匹配predicate首个迭代值;匹配多次时报错;无匹配时也报错 EnumerableImpl.prototype.Single = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var value, hasValue = false; var result, iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (predicate(result.value)) { if (!hasValue) { value = result.value; hasValue = true; } else { throw Constant.CONST_TOO_MANY; } } } if (hasValue) return value; throw Constant.CONST_NOTHING_FOUND; }; // 遍历迭代值,获取匹配predicate首个迭代值;匹配多次时报错;无匹配时以Constant.getDefaultVal默认值返回 EnumerableImpl.prototype.SingleOrDefault = function (predicate) { if (predicate === void 0) { predicate = Constant.trueFn; } var value, lastKnown, hasValue = false; var result, iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { if (predicate(result.value)) { if (!hasValue) { value = result.value; hasValue = true; } else { throw Constant.CONST_TOO_MANY; } } lastKnown = result.value; } return (hasValue) ? value : Constant.getDefaultVal(typeof lastKnown); }; // 遍历迭代值,迭代值经transform函数转化后求和 EnumerableImpl.prototype.Sum = function (transform) { if (transform === void 0) { transform = Constant.selfFn; } var result, sum = 0; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { sum += transform(result.value); } return sum; }; // 遍历迭代值,将迭代值存入数组中返回,即转化为数组形式 EnumerableImpl.prototype.ToArray = function () { var result, array = []; var iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { array.push(result.value); } return array; }; // 遍历迭代值,将迭代值转化为键值对Map对象形式;keySelector用于获取键,elementSelector获取值 EnumerableImpl.prototype.ToMap = function (keySelector, elementSelector) { if (elementSelector === void 0) { elementSelector = Constant.selfFn; } var dictionary = new Map(); var result, iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { dictionary.set(keySelector(result.value), elementSelector(result.value)); } return dictionary; }; // 遍历迭代值,将迭代值转化为键值对Map对象形式;keySelector用于获取键,elementSelector获取值 EnumerableImpl.prototype.ToDictionary = function (keySelector, elementSelector) { if (elementSelector === void 0) { elementSelector = Constant.selfFn; } var dictionary = new Map(); var result, iterator = this[Symbol.iterator](); while (!(result = iterator.next()).done) { dictionary.set(keySelector(result.value), elementSelector(result.value)); } return dictionary; }; // 构建新的EnumerableImpl实例 // 构建EnumerableImpl实例,迭代到空值时返回默认值defaultValue EnumerableImpl.prototype.DefaultIfEmpty = function (defaultValue) { var _this = this; if (defaultValue === void 0) { defaultValue = undefined; } return new EnumerableImpl(this, function () { return new Iterator.DefaultIfEmpty(_this[Symbol.iterator](), defaultValue); }); }; // 获取当前EnumerableImpl实例 EnumerableImpl.prototype.Cast = function () { return this; // TODO: Remove any once TypeScript 2.0 out }; // 构建EnumerableImpl实例,拼接迭代当前迭代器及second迭代器,返回两个迭代器逐步迭代后的迭代值 EnumerableImpl.prototype.Concat = function (second) { var aggregate = [this._target, second]; return new EnumerableImpl(this, function () { // Iterator.SelectMany(outerIterator,innerInterator,transfer)由外层到内层迭代获取经transfer转化后的迭代值 return new Iterator.SelectMany(aggregate[Symbol.iterator](), Constant.selfFn); }); }; // 构建EnumerableImpl实例,迭代值value经keySelect转化后所得的key值首次出现,resultSelector转化value输出 // 重复出现的key值,将迭代值value经elementSelector转化后存入缓存中,并将该缓存作为迭代到尾稍的返回值 EnumerableImpl.prototype.ChunkBy = function (keySelect, elementSelector, resultSelector) { var _this = this; if (elementSelector === void 0) { elementSelector = Constant.selfFn; } if (resultSelector === void 0) { resultSelector = function (a, b) { return b; }; } return new EnumerableImpl(this, function () { // ChunkBy(target,keySelect,elementSelector,resultSelector) // 构建新的迭代器,迭代target获取迭代值value,该value由keySelect转化为key // 若key值首次出现,将value经resultSelector转化后作为返回值输出 // 若key值相邻重复出现时,由elementSelector转化后存入缓存this.box // 迭代到尾稍时,输出{value:this.box,done:false};最后一次迭代返回{value:undefined,done:true} return new Iterator.ChunkBy( _this[Symbol.iterator](), keySelect, elementSelector, resultSelector ); }); }; // 构建EnumerableImpl实例,以kesSelector(value)获取键key,以筛选首次出现的key,获取相关迭代值 EnumerableImpl.prototype.Distinct = function (keySelector) { var _this = this; if (keySelector === void 0) { keySelector = Constant.selfFn; } return new EnumerableImpl(this, function () { // Iterator.Distinct(iterator,keySelector)获取经keySelector转化成key后且该key值首次出现的迭代值 return new Iterator.Distinct(_this[Symbol.iterator](), keySelector); }); }; // 构建EnumerableImpl实例,以kesSelector(value)获取键key,筛选出set中不包含的value EnumerableImpl.prototype.Except = function (other, kesSelector) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Intersect(iterator,set,true,kesSelector)筛选出set不包含的值 return new Iterator.Intersect( // getKeys(iterable,keySelector)遍历迭代器iterable,获取其元素项或通过keySelector函数转化元素项 // 构建成员值唯一的set数组并返回 _this[Symbol.iterator](), Constant.getKeys(other, kesSelector), true, kesSelector ); }); }; // 构建EnumerableImpl实例,先用selKey函数将当前迭代器经selElement转化后的迭代值以键值对形式存入Map对象中 // 再通过Iterator.GroupBy遍历key键,取出Map中经selResult转化后的分组迭代值,进行下一步处理 EnumerableImpl.prototype.GroupBy = function (selKey, selElement, selResult) { if (selElement === void 0) { selElement = Constant.selfFn; } // Constant.defGrouping(a,b)存储b.key=a,且将b作为返回值 if (selResult === void 0) { selResult = Constant.defGrouping; } var result; var iterator = this[Symbol.iterator](); // 根据selKey函数将各迭代值分组存入map对象中 var _map = new Map(); while (!(result = iterator.next()).done) { var key = selKey(result.value); var group = _map.get(key); if ('undefined' === typeof group) { group = []; _map.set(key, group); } group.push(selElement(result.value)); } var factory = function () { // Iterator.GroupBy(iterator,resultSelect,map)迭代获取key键,取出map中以key键存储的数据,经result转化后作为返回值 return new Iterator.GroupBy(_map.keys(), selResult, _map); }; var tst = factory(); return new EnumerableImpl(this, function () { return new Iterator.GroupBy(_map.keys(), selResult, _map); }); }; // 构建EnumerableImpl实例,先用iKeySelect函数将当前迭代器的迭代值以键值对形式存入Map对象中 // 再通过Iterator.GroupJoin遍历当前迭代值,由该迭代值获取key键,取出Map中key键存储的分组迭代值,经resultSelector转化后作为最终迭代值 EnumerableImpl.prototype.GroupJoin = function (inner, oKeySelect, iKeySelect, resultSelector) { var _this = this; // Constant.defGrouping(a,b)存储b.key=a,且将b作为返回值 if (resultSelector === void 0) { resultSelector = Constant.defGrouping; } var _map = new Map(); var _inner = inner[Symbol.iterator](); var result; // 根据selKey函数将各迭代值分组存入map对象中 while (!(result = _inner.next()).done) { var key = iKeySelect(result.value); if ('undefined' === typeof key) throw "Inner Key selector returned undefined Key"; var group = _map.get(key); if ('undefined' === typeof group) { group = []; _map.set(key, group); } group.push(result.value); } return new EnumerableImpl(this, function () { // Iterator.GroupJoin(iterator,oKeySelect,transform,map)迭代iterator取得迭代值,再经oKeySelect获取key键 // 该key键用以获取map中的数据,并和迭代值value一同传入transform函数中,用以获取最终迭代值 return new Iterator.GroupJoin(_this[Symbol.iterator](), oKeySelect, resultSelector, _map); }); }; // 构建EnumerableImpl实例,以kesSelector(value)获取键key,筛选出set中包含的value EnumerableImpl.prototype.Intersect = function (other, kesSelector) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Intersect(iterator,set,true,kesSelector)筛选出set包含的值 return new Iterator.Intersect( _this[Symbol.iterator](), Constant.getKeys(other, kesSelector), false, kesSelector ); }); }; // 构建EnumerableImpl实例,先迭代inner并通过iSelector函数将各内层迭代器存入键值对缓存中 // 迭代当前迭代器获取经oSelector转化后的key键,通过key键查找对应的内层迭代器 // 最终通过迭代各内层子迭代器获取到经transform转化后的迭代值 EnumerableImpl.prototype.Join = function (inner, oSelector, iSelector, transform) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Join(outer,inner,oKeySelect,iKeySelect,transform)在SelectMany(this,outer,null)的基础上 // 由外层迭代器outer迭代查找内层迭代器进行迭代,经transform转化输出最终迭代值 return new Iterator.Join( _this[Symbol.iterator](), inner[Symbol.iterator](), oSelector, iSelector, transform ); }); }; // 构建EnumerableImpl实例,迭代构造函数为obj的迭代值 EnumerableImpl.prototype.OfType = function (obj) { var _this = this; var typeName; switch (obj) { case Number: return new EnumerableImpl(this, function () { // Iterator.OfValueType(target,obj,typeName)构建新的迭代器,迭代获取由obj构造函数生成且类型为typeName的迭代值 return new Iterator.OfValueType(_this[Symbol.iterator](), obj, Constant.CONST_NUMBER); }); case Boolean: return new EnumerableImpl(this, function () { return new Iterator.OfValueType(_this[Symbol.iterator](), obj, Constant.CONST_BOOLEAN); }); case String: return new EnumerableImpl(this, function () { return new Iterator.OfValueType(_this[Symbol.iterator](), obj, Constant.CONST_STRING); }); case Symbol: return new EnumerableImpl(this, function () { return new Iterator.OfValueType(_this[Symbol.iterator](), obj, Constant.CONST_SYMBOL); }); } return new EnumerableImpl(this, function () { // Iterator.OfType(target,obj)构建新的迭代器,迭代获取由obj构造函数生成的迭代值 return new Iterator.OfType(_this[Symbol.iterator](), obj); }); }; // 构建EnumerableImpl实例,当前迭代器先经过排序后再进行迭代 // keySelect为排序的比较主键, equal为比较函数(默认取Constant.defCompare) EnumerableImpl.prototype.OrderBy = function (keySelect, equal) { var comparer = equal ? equal : Constant.defCompare; var compare = !keySelect && !equal ? undefined : keySelect ? function (a, b) { return comparer(keySelect(a), keySelect(b)); } : function (a, b) { return comparer(a, b); }; // OrderedLinq(target,factory,equal),迭代目标对象经equal函数排序后再进行迭代,且继承EnumerableImpl构造函数 return new OrderedLinq(this, function (array) { // ArrayIterator(_source,_current,_done,_increment) // 从_current起始位以_increment步长遍历_source数组,_done用于判断遍历是否结束 return new Iterator.ArrayIterator(array, 0, function (i) { return i >= array.length; }); }, compare); }; // 构建EnumerableImpl实例,当前迭代器先经过排序后再进行反向迭代 // keySelect为排序的比较主键, equal为比较函数(默认取Constant.defCompare) EnumerableImpl.prototype.OrderByDescending = function (keySelect, equal) { var comparer = equal ? equal : Constant.defCompare; var compare = !keySelect && !equal ? undefined : keySelect ? function (a, b) { return comparer(keySelect(a), keySelect(b)); } : function (a, b) { return comparer(a, b); }; // OrderedLinq(target,factory,equal),迭代目标对象经equal函数排序后再进行迭代,且继承EnumerableImpl构造函数 return new OrderedLinq(this, function (array) { // ArrayIterator(_source,_current,_done,_increment) // 从_current起始位以_increment步长遍历_source数组,_done用于判断遍历是否结束 return new Iterator.ArrayIterator(array, array.length - 1, function (i) { return 0 > i; }, -1); }, compare); }; // 同OrderBy方法,只是针对OrderedLinq实例作特殊处理 // 通常构建EnumerableImpl实例,当前迭代器先经过排序后再进行迭代;当this为OrderedLinq实例时,只改变this.equal // keySelect为排序的比较主键, equal为比较函数(默认取Constant.defCompare) EnumerableImpl.prototype.ThenBy = function (keySelect, equal) { var comparer = equal ? equal : Constant.defCompare; var compare = !keySelect && !equal ? undefined : keySelect ? function (a, b) { return comparer(keySelect(a), keySelect(b)); } : function (a, b) { return comparer(a, b); }; if (this instanceof OrderedLinq) { if (!compare) return this; if (!this.equal) { this.equal = compare; }else { var superEqual_1 = this.equal; this.equal = function (a, b) { var result = superEqual_1(a, b); return (0 != result) ? result : compare(a, b); }; } return this; } else { // OrderedLinq(target,factory,equal),迭代目标对象经equal函数排序后再进行迭代,且继承EnumerableImpl构造函数 return new OrderedLinq(this, function (array) { // ArrayIterator(_source,_current,_done,_increment) // 从_current起始位以_increment步长遍历_source数组,_done用于判断遍历是否结束 return new Iterator.ArrayIterator(array, 0, function (i) { return i >= array.length; }); }, function (a, b) { return equal(keySelect(a), keySelect(b)); }); } }; // 同OrderByDescending方法,只是针对OrderedLinq实例作特殊处理 // 通常构建EnumerableImpl实例,当前迭代器先经过排序后再进行反向迭代;当this为OrderedLinq实例时,只改变this.equal // keySelect为排序的比较主键, equal为比较函数(默认取Constant.defCompare) EnumerableImpl.prototype.ThenByDescending = function (keySelect, equal) { var comparer = equal ? equal : Constant.defCompare; var compare = !keySelect && !equal ? undefined : keySelect ? function (a, b) { return comparer(keySelect(a), keySelect(b)); } : function (a, b) { return comparer(a, b); }; if (this instanceof OrderedLinq) { if (!compare) return this; if (!this.equal) { this.equal = compare; } else { var superEqual_2 = this.equal; this.equal = function (a, b) { var result = superEqual_2(a, b); return (0 != result) ? result : compare(a, b); }; } return this; } else { return new OrderedLinq(this, function (array) { return new Iterator.ArrayIterator(array, array.length - 1, function (i) { return 0 > i; }, -1); }, function (a, b) { return equal(keySelect(a), keySelect(b)); }); } }; // 构建EnumerableImpl实例,迭代获取start位起始到start+count-1位结束的序号值 EnumerableImpl.prototype.Range = function (start, count) { return new EnumerableImpl(null, function () { // Iterator.Generator(_current,_count,_increment)从_current序号位顺序获取_count个序号值(_increment为真值时) // 或者重复获取_current序号值(_increment为否值时) return new Iterator.Generator(start, count, true); }); }; // 构建EnumerableImpl实例,迭代获取element位或元素conut次 EnumerableImpl.prototype.Repeat = function (element, count) { return new EnumerableImpl(null, function () { return new Iterator.Generator(element, count); }); }; EnumerableImpl.prototype.Reverse = function () { var array = Array.isArray(this._target) ? this._target : this.ToArray(); return new EnumerableImpl(null, function () { return new Iterator.ArrayIterator(array, array.length - 1, function (i) { return 0 > i; }, -1); }); }; // 构建EnumerableImpl实例,迭代获取经transform转化后的迭代值 EnumerableImpl.prototype.Select = function (transform) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Select(iterator,transfer)迭代获取经transfer转化后的迭代值 return new Iterator.Select(_this[Symbol.iterator](), transform); }); }; // 构建EnumerableImpl实例,逐步迭代当前迭代器的子迭代器经result转化后的迭代值 EnumerableImpl.prototype.SelectMany = function (selector, result) { var _this = this; if (selector === void 0) { selector = Constant.selfFn; } if (result === void 0) { result = function (x, s) { return s; }; } return new EnumerableImpl(this, function () { // Iterator.SelectMany(outerIterator,innerInterator,transfer)由外层到内层迭代获取经transfer转化后的迭代值 return new Iterator.SelectMany(_this[Symbol.iterator](), selector, result); }); }; // 构建EnumerableImpl实例,迭代获取skip序号值后的迭代值 EnumerableImpl.prototype.Skip = function (skip) { var _this = this; return new EnumerableImpl(this, function () { var iterator = _this[Symbol.iterator](); var i = 0; while (i < skip && !(iterator.next().done)) { i++; } // Iterator.Where(iterator,match)迭代获取匹配match(value,index)函数的迭代值 return new Iterator.Where(iterator, Constant.trueFn); }); }; // 构建EnumerableImpl实例,迭代获取满足函数predicate的迭代值 EnumerableImpl.prototype.Where = function (predicate) { var _this = this; if (predicate === void 0) { predicate = Constant.trueFn; } return new EnumerableImpl(this, function () { // Iterator.Where(iterator,match)迭代获取匹配match(value,index)函数的迭代值 return new Iterator.Where(_this[Symbol.iterator](), predicate); }); }; // 构建EnumerableImpl实例,迭代获取不满足函数predicate的迭代值 EnumerableImpl.prototype.SkipWhile = function (predicate) { var _this = this; if (predicate === void 0) { predicate = function (a, n) { return false; }; } return new EnumerableImpl(this, function () { // Iterator.Skip(iterator,match)迭代获取不匹配match(value,index)的迭代值 return new Iterator.Skip(_this[Symbol.iterator](), predicate); }); }; // 构建EnumerableImpl实例,迭代获取take序号之前的迭代值,take之后返回{value:undefined,done:true} EnumerableImpl.prototype.Take = function (take) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Take(iterator,match)迭代获取匹配match(value,index)的迭代值,不匹配则输出{value:undefined,done:true} // 仍可迭代 return new Iterator.Take(_this[Symbol.iterator](), function (a, n) { return take > n; }); }); }; // 构建EnumerableImpl实例,迭代获取匹配predicate的迭代值,不匹配的返回{value:undefined,done:true} EnumerableImpl.prototype.TakeWhile = function (predicate) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Take(iterator,match)迭代获取匹配match(value,index)的迭代值,不匹配则输出{value:undefined,done:true} // 仍可迭代 return new Iterator.Take(_this[Symbol.iterator](), predicate); }); }; // 构建EnumerableImpl实例,迭代当前迭代器和second,获取迭代值;通过keySelector函数判断是否首次出现 EnumerableImpl.prototype.Union = function (second, keySelector) { if (keySelector === void 0) { keySelector = Constant.selfFn; } var aggregate = [this._target, second]; return new EnumerableImpl(this, function () { // Iterator.Union(sourceIterator,keySelector)迭代获取sourceIterator中内层迭代器的迭代值 // 该迭代值经keySelector转化后首次出现 return new Iterator.Union(aggregate[Symbol.iterator](), keySelector); }); }; // 构建EnumerableImpl实例,同步原始迭代器和second,迭代输出值经func函数转化 EnumerableImpl.prototype.Zip = function (second, func) { var _this = this; return new EnumerableImpl(this, function () { // Iterator.Zip(firstIterator,secondIterator,transfer)同步firstIterator、secondIterator,有一个终结即宣告迭代终止 // 迭代输出值经transfer转化 return new Iterator.Zip(_this[Symbol.iterator](), second[Symbol.iterator](), func); }); }; return EnumerableImpl; }()); // OrderedLinq(target,factory,equal),迭代目标对象经equal函数排序后再进行迭代,且继承EnumerableImpl构造函数 var OrderedLinq = (function (_super) { __extends(OrderedLinq, _super); function OrderedLinq(target, factory, equal) { var _this = _super.call(this, target, factory) || this; _this.equal = equal; return _this; } OrderedLinq.prototype[Symbol.iterator] = function () { if ('undefined' === typeof this._factoryArg) { this._factoryArg = this._target.ToArray(); if (this.equal) { this._factoryArg.sort(this.equal); } else { this._factoryArg.sort(); } } return this._factory(this._factoryArg); }; return OrderedLinq; }(EnumerableImpl));
iterators.js
"use strict"; // 继承静态方法和原型方法,仍需调用父类构造函数获得父类的私有属性 var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); // C#风格的迭代器 // Current方法获取当前值 // MoveNext方法移到下一项,返回真值表示遍历尚未完成 var CSharpEnumerator = (function () { function CSharpEnumerator(sourceIterator) { this._iterator = sourceIterator; } Object.defineProperty(CSharpEnumerator.prototype, "Current", { get: function () { return this._result.value; }, enumerable: true, configurable: true }); CSharpEnumerator.prototype.MoveNext = function () { this._result = this._iterator.next(); return !this._result.done; }; CSharpEnumerator.prototype.Reset = function () { throw "JavaScript iterators could not be Reset"; }; return CSharpEnumerator; }()); exports.CSharpEnumerator = CSharpEnumerator; // ArrayIterator(_source,_current,_done,_increment)从_current起始位以_increment步长遍历_source数组,_done用于判断遍历是否结束 // 参数_source为待遍历的数组,参数_current为遍历起始位 // 参数_done函数用于判断遍历是否结束,参数_increment为迭代步长 var ArrayIterator = (function () { function ArrayIterator(_source, _current, _done, _increment) { if (_increment === void 0) { _increment = 1; } this._source = _source; this._current = _current; this._done = _done; this._increment = _increment; } ArrayIterator.prototype.next = function (value) { var result = { value: this._source[this._current], done: this._done(this._current) }; this._current += this._increment; return result; }; return ArrayIterator; }()); exports.ArrayIterator = ArrayIterator; // 构建迭代器的基类 // this._iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // this._done用于判断迭代是否终止 var IteratorBase = (function () { function IteratorBase(_iterator) { this._iterator = _iterator; this._done = { value: undefined, done: true }; } return IteratorBase; }()); exports.IteratorBase = IteratorBase; // 迭代器筛选出_set不包含的值(_switch为真时),或者筛选出_set包含的值(_switch为否时) // 参数iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数_set用于筛选迭代值value;_switch为真,筛选出_set不包含的值,switch为否,筛选出_set包含的值 // 参数_switch为真值时,迭代时剔除this._set包含的key;为否值时,迭代时剔除this._set不包含的key // 参数_keySelector将迭代值value转化为key;_switch为否值时,将key存入this._set var Intersect = (function (_super) { __extends(Intersect, _super); function Intersect(iterator, _set, _switch, _keySelector) { if (_keySelector === void 0) { _keySelector = function (o) { return o; }; } var _this = _super.call(this, iterator) || this; _this._set = _set; _this._switch = _switch; _this._keySelector = _keySelector; return _this; } Intersect.prototype.next = function (value) { var result; while (!(result = this._iterator.next()).done && (this._switch == this._set.has(this._keySelector(result.value)))) { } if (!result.done && !this._switch) this._set.add(this._keySelector(result.value)); return result; }; return Intersect; }(IteratorBase)); exports.Intersect = Intersect; // Generator(_current,_count,_increment)从_current序号位顺序获取_count个序号值(_increment为真值时),或者重复获取_current序号值(_increment为否值时) // 参数_current迭代起始位 // 参数_count迭代取值个数 // 参数_increment为真值,顺序获取下一个序号值;为否值,重复获取当前序号值 var Generator = (function (_super) { __extends(Generator, _super); function Generator(_current, _count, _increment) { if (_increment === void 0) { _increment = false; } var _this = _super.call(this, null) || this; _this._current = _current; _this._count = _count; _this._increment = _increment; return _this; } Generator.prototype.next = function (value) { var result = (0 < this._count) ? { value: this._current, done: 0 >= this._count-- } : this._done; if (this._increment) this._current++; return result; }; return Generator; }(IteratorBase)); exports.Generator = Generator; // 构建新的迭代器,迭代到空值时返回默认值_default // 参数sourceIterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数_default迭代到空值时作为默认值返回 var DefaultIfEmpty = (function (_super) { __extends(DefaultIfEmpty, _super); function DefaultIfEmpty(sourceIterator, _default) { var _this = _super.call(this, sourceIterator) || this; _this._default = _default; return _this; } DefaultIfEmpty.prototype.next = function (value) { return this.check(this._iterator.next()); }; DefaultIfEmpty.prototype.check = function (result) { if (result.done) { result.value = this._default; } else { this.check = function (a) { return a; }; } return result; }; return DefaultIfEmpty; }(IteratorBase)); exports.DefaultIfEmpty = DefaultIfEmpty; // 构建新的迭代器,keySelect转化迭代值value后且key键首次出现时,输出{value,done} // 遇重复出现的key值时,elementSelector转化迭代值value后存入this.box,并作为最终迭代值 // this.box存储迭代值value经elementSelector转化后的值,迭代到尾稍时返回{value:this.box,done:false} // 参数target为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数keySelector将迭代值value转化为key;与前一个key值相同,存入this.box;不同,返回经resultSelector转化的value值 // 参数elementSelector将迭代值value转化后存入this.box,迭代终止时返回{value:this.box,done:false} // 参数resultSelector将迭代值value转化后作为迭代输出对象的value值 var ChunkBy = (function (_super) { __extends(ChunkBy, _super); function ChunkBy(target, keySelect, elementSelector, resultSelector) { var _this = _super.call(this, target) || this; _this.keySelect = keySelect; _this.elementSelector = elementSelector; _this.resultSelector = resultSelector; _this.index = 0; return _this; }; ChunkBy.prototype.next = function (value) { var result; do { result = this._iterator.next(); if (result.done) { if (this.box) { result.done = false; result.value = this.box; this.box = undefined; return result; } else return this._done; } var newKey = this.keySelect(result.value, this.index++); if (this.key !== newKey && this.box) { var ret = { done: false, value: this.resultSelector(this.key, this.box) }; this.key = newKey; this.box = new Array(); this.box.push(this.elementSelector(result.value)); return ret; } if (!this.box) { this.box = new Array(); } this.key = newKey; this.box.push(this.elementSelector(result.value)); } while (!result.done); return this._done; }; return ChunkBy; }(IteratorBase)); exports.ChunkBy = ChunkBy; // 构建新的迭代器,迭代获取由obj构造函数生成的迭代值 // 参数target为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数obj为构造函数,用于判断迭代值value是否由该构造函数生成 var OfType = (function (_super) { __extends(OfType, _super); function OfType(target, obj) { var _this = _super.call(this, target) || this; _this.obj = obj; return _this; } OfType.prototype.next = function (value) { var result; do { result = this._iterator.next(); } while (!result.done && !(result.value instanceof this.obj)); return result; }; return OfType; }(IteratorBase)); exports.OfType = OfType; // 构建新的迭代器,迭代获取由obj构造函数生成且类型为typeName的迭代值 // 参数target为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数obj为构造函数,用于判断迭代值value是否由该构造函数生成 // 参数typeName为类型"number"、"boolean"、"string"、"symbol"等,用于判断迭代值value的类型 var OfValueType = (function (_super) { __extends(OfValueType, _super); function OfValueType(target, obj, typeName) { var _this = _super.call(this, target, obj) || this; _this.typeName = typeName; return _this; } OfValueType.prototype.next = function (value) { var result; do { result = this._iterator.next(); } while (!result.done && this.typeName !== typeof (result.value) && !(result.value instanceof this.obj)); return result; }; return OfValueType; }(OfType)); exports.OfValueType = OfValueType; // MethodIteratror(iterator,_method,_index),在IteratorBase基础上,添加this._method、this._index // this._iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // this._done用于判断迭代是否终止 // this._method存储公用方法 // this._index var MethodIteratror = (function (_super) { __extends(MethodIteratror, _super); function MethodIteratror(iterator, _method, _index) { if (_method === void 0) { _method = null; } if (_index === void 0) { _index = 0; } var _this = _super.call(this, iterator) || this; _this._method = _method; _this._index = _index; return _this; } return MethodIteratror; }(IteratorBase)); exports.MethodIteratror = MethodIteratror; // Distinct(iterator,keySelector)获取经keySelector转化成key后且该key值首次出现的迭代值 // 参数iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数keySelector转化迭代值value,key已存在忽略,首次出现迭代获取 var Distinct = (function (_super) { __extends(Distinct, _super); function Distinct() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._set = new Set(); return _this; } Distinct.prototype.next = function (value) { var result, key; while (!(result = this._iterator.next()).done) { key = this._method(result.value); if (!this._set.has(key)) break; } this._set.add(key); return result; }; return Distinct; }(MethodIteratror)); exports.Distinct = Distinct; // Where(iterator,match)迭代获取匹配match(value,index)函数的迭代值 // 参数iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数match(value,index),用于校验迭代值value是否满足该匹配函数,满足则获取,不满足则获取下一个迭代值 var Where = (function (_super) { __extends(Where, _super); function Where() { return _super !== null && _super.apply(this, arguments) || this; } Where.prototype.next = function (value) { var result; do { result = this._iterator.next(); } while (!result.done && !this._method(result.value, this._index++)); return result; }; return Where; }(MethodIteratror)); exports.Where = Where; // Skip(iterator,match)迭代获取不匹配match(value,index)的迭代值 // 参数iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数match(value,index),用于校验迭代值value是否不满足该匹配函数,不满足则获取,满足则获取下一个迭代值 var Skip = (function (_super) { __extends(Skip, _super); function Skip() { var _this = _super !== null && _super.apply(this, arguments) || this; _this._hasSkipped = false; return _this; } Skip.prototype.next = function (value) { var result; if (this._hasSkipped) return this._iterator.next(); while (!(result = this._iterator.next()).done && this._method(result.value, this._index++)) { } this._hasSkipped = true; return result; }; return Skip; }(MethodIteratror)); exports.Skip = Skip; // Take(iterator,match)迭代获取匹配match(value,index)的迭代值,不匹配则输出{value:undefined,done:true};仍可迭代 // 参数iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数match(value,index),用于校验迭代值value是否满足该匹配函数,满足则获取,不满足则迭代输出{value:undefined,done:true} var Take = (function (_super) { __extends(Take, _super); function Take() { return _super !== null && _super.apply(this, arguments) || this; } Take.prototype.next = function (value) { var result = this._iterator.next(); if (result.done || !this._method(result.value, this._index++)) { return this._done; } return result; }; return Take; }(MethodIteratror)); exports.Take = Take; // Zip(firstIterator,secondIterator,transfer)同步firstIterator、secondIterator,迭代输出值经transfer转化 // 参数firstIterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数secondIterator为第二个迭代器,与firstIterator迭代器同步迭代取值,有一个终止时即终结迭代过程 // 参数transfer(firstValue,secondValue)转化迭代输出值 var Zip = (function (_super) { __extends(Zip, _super); function Zip(first, _second, func) {// func作为MethodIteratror构造函数的_method参数 var _this = _super.call(this, first, func) || this; _this._second = _second; return _this; } Zip.prototype.next = function (value) { var first = this._iterator.next(); var second = this._second.next(); if (first.done || second.done) { return this._done; } return { done: false, value: this._method(first.value, second.value) }; }; return Zip; }(MethodIteratror)); exports.Zip = Zip; // Select(iterator,transfer)迭代获取经transfer转化后的迭代值 // 参数iterator为原始迭代器,新的迭代方式通常基于this._iterator生成 // 参数transfer(value,index)转化迭代输出值 var Select = (function (_super) { __extends(Select, _super); function Select() { return _super !== null && _super.apply(this, arguments) || this; } Select.prototype.next = function (value) { var result = this._iterator.next(); if (result.done) return result; result.value = this._method(result.value, this._index++); return result; }; return Select; }(MethodIteratror)); exports.Select = Select; // SelectMany(outerIterator,innerInterator,transfer)由外层到内层迭代获取经transfer转化后的迭代值 // 参数outerIterator为外层迭代器,作为this._iterator,用于迭代获取内层迭代器 // 参数innerInterator用于构建内层迭代器 // 参数transfer(innerInterator,value)转化迭代输出值 var SelectMany = (function (_super) { __extends(SelectMany, _super); // sourceIterator作为MethodIteratror的参数iterator,selector作为MethodIteratror的参数_method function SelectMany(sourceIterator, selector, transform) { if (transform === void 0) { transform = function (t, s) { return s; }; } var _this = _super.call(this, sourceIterator, selector) || this; _this._collectionState = _this._done; _this._resultState = _this._done; _this._resultSelector = transform; return _this; } SelectMany.prototype.next = function (value) { do { if (this._resultState.done) { this._collectionState = this._iterator.next(); if (this._collectionState.done) return this._done; this._collection = this._method(this._collectionState.value)[Symbol.iterator](); } this._resultState = this._collection.next(); if (!this._resultState.done) { this._resultState.value = this._resultSelector( this._collectionState.value, this._resultState.value ); } } while (this._resultState.done); return this._resultState; }; return SelectMany; }(MethodIteratror)); exports.SelectMany = SelectMany; // Join(outer,inner,oKeySelect,iKeySelect,transform)在继承SelectMany(this,outer,null)的基础上 // 由外层迭代器outer迭代查找内层迭代器进行迭代,经transform转化输出最终迭代值 // SelectMany用于注入this._iterator,以及初始化this._collectionState、_resultState、_resultSelector属性 // 参数outer,作为this._iterator,用于迭代获取内层迭代器的key,通过key获取内层迭代器 // 参数inner,以迭代器形式存储各内层迭代器,通过iKeySelect转化为Map形式,供外层迭代器outer获取 // 参数oKeySelect由外层迭代器outer获取key值,用于查找内层迭代器 // 参数iKeySelect用于将内层迭代器转化为键值对的Map形式,供外层迭代器查询使用 // 参数transform(outerValue,innerValue)通过内外层迭代值转化获取最终迭代值 var Join = (function (_super) { __extends(Join, _super); function Join(outer, inner, oKeySelect, iKeySelect, transform) { var _this = _super.call(this, outer, null) || this; _this._method = oKeySelect; var result; _this._map = new Map(); while (!(result = inner.next()).done) { var key = iKeySelect(result.value); var group = _this._map.get(key); if ('undefined' === typeof group) { group = []; _this._map.set(key, group); } group.push(result.value); } _this._resultSelector = transform; return _this; } Join.prototype.next = function (value) { do { if (this._resultState.done) { this._collectionState = this._iterator.next(); if (this._collectionState.done) return this._done; var key = this._method(this._collectionState.value); var innerSet = this._map.get(key); if ('undefined' === typeof innerSet) continue; this._collection = innerSet[Symbol.iterator](); } this._resultState = this._collection.next(); if (!this._resultState.done) { this._resultState.value = this._resultSelector(this._collectionState.value, this._resultState.value); } } while (this._resultState.done); return this._resultState; }; return Join; }(SelectMany)); exports.Join = Join; // Union(sourceIterator,keySelector)在继承SelectMany(this,outer,null)的基础上 // 以SelectMany的next方法迭代取值,即sourceIterator作为外层迭代器,其子项作为内层迭代器 // 迭代获取内层迭代器经keySelector转化后且key键首次出现的迭代值value // 参数sourceIterator作为外层迭代器,其子项作为内层迭代器,迭代获取各内层迭代器的迭代值 // 参数keySelector用于将迭代值转化key键,以判断key键是否首次出现;若是,获取;不是,跳过 var Union = (function (_super) { __extends(Union, _super); function Union(sourceIterator, _keySelector) { var _this = _super.call(this, sourceIterator, function (o) { return o; }) || this; _this._keySelector = _keySelector; _this._set = new Set(); return _this; } Union.prototype.next = function (value) { var result, key; while (!(result = _super.prototype.next.call(this)).done) { key = this._keySelector(result.value); if (!this._set.has(key)) break; } this._set.add(key); return result; }; return Union; }(SelectMany)); exports.Union = Union; // GroupBy(iterator,resultSelect,map)迭代获取key键,取出map中以key键存储的数据,经result转化后作为返回值 // 参数iterator为原始迭代器,作为this._iterator(在linq.js中表现为迭代获取map中储值的key键) // 参数resultSelect(key,map.get(key))将key键和map中以key为键存储的数据取出作为迭代值 // 参数map,Map对象,以键值对形式存储有数据(该数据在linq.js中表现为分组的迭代值) var GroupBy = (function (_super) { __extends(GroupBy, _super); function GroupBy(iterator, resultSelect, _map) { var _this = _super.call(this, iterator, resultSelect) || this;// resultSelect作为MethodIteratror的参数_method _this._map = _map; return _this; } GroupBy.prototype.next = function (value) { var result = this._iterator.next(); if (result.done) return this._done; var iterable = this._map.get(result.value); return { value: this._method(result.value, iterable), done: false }; }; return GroupBy; }(MethodIteratror)); exports.GroupBy = GroupBy; // GroupJoin(iterator,oKeySelect,transform,map)迭代iterator取得迭代值,再经oKeySelect获取key键 // 该key键用以获取map中的数据,并和迭代值value一同传入transform函数中,用以获取最终迭代值 // 参数iterator为原始迭代器,作为this._iterator(在linq.js中表现为EnumerableImpl的相关迭代器) // 参数oKeySelect(value)通过iterator的迭代值获取Map中用来存储数据的key键 // 参数transform用于iterator的迭代值和map.get(key)存储数据转化为最终迭代值 // 参数map,Map对象,以键值对形式存储有数据(该数据在linq.js中表现为分组的迭代值) var GroupJoin = (function (_super) { __extends(GroupJoin, _super); function GroupJoin(iterator, oKeySelect, _transform, _map) { var _this = _super.call(this, iterator, oKeySelect) || this;// oKeySelect作为MethodIteratror的参数_method _this._transform = _transform; _this._map = _map; return _this; } GroupJoin.prototype.next = function (value) { var innerSet; var result; do { result = this._iterator.next(); if (result.done) return this._done; var key = this._method(result.value); innerSet = this._map.get(key); } while ('undefined' === typeof innerSet); return { value: this._transform(result.value, innerSet), done: false }; }; return GroupJoin; }(MethodIteratror)); exports.GroupJoin = GroupJoin; //# sourceMappingURL=iterators.js.map
utilities.js
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // 直接返回真值 exports.trueFn = function () { return true; }; // 直接返回首参 exports.selfFn = function (o) { return o; }; // defGrouping(a,b)存储b.key=a,且将b作为返回值 exports.defGrouping = function (a, b) { if (!b[exports.CONST_KEY]) { b[exports.CONST_KEY] = a; } return b; }; // 排序时使用的比较函数 exports.defCompare = function (a, b) { return a === b ? 0 : a > b ? 1 : -1; }; // 获取默认值 function getDefaultVal(type, value) { if (value === void 0) { value = undefined; } if (typeof type !== exports.CONST_STRING) throw new TypeError(exports.CONST_NO_STRING); switch (type) { case exports.CONST_BOOLEAN: return false; case exports.CONST_NUMBER: return 0; case exports.CONST_OBJECT: return null === value ? null : undefined; case exports.CONST_STRING: return exports.CONST_EMPTY_STRING; case exports.CONST_FUNCTION: return ("Symbol" === value['name']) ? Symbol() : void 0; } return undefined; } exports.getDefaultVal = getDefaultVal; // 将迭代值以键值对形式分组存储在Map对象中,keySelector用于获取键,selElement获取值 function getKeyedMap(iterable, keySelector, selElement) { var map = new Map(); var result, iterator = iterable[Symbol.iterator](); while (!(result = iterator.next()).done) { var key = keySelector(result.value); if (!key) throw exports.CONST_INVALID_KEY; var group = map.get(key); if (!group) { group = []; map.set(key, group); } group.push(selElement(result.value)); } return map; } exports.getKeyedMap = getKeyedMap; // 将迭代值以键值对形式分组存储在Map对象中,keySelector用于获取键 function getKeyedMapFast(iterable, keySelector) { var map = new Map(); var result, iterator = iterable[Symbol.iterator](); while (!(result = iterator.next()).done) { var key = keySelector(result.value); if (!key) throw exports.CONST_INVALID_KEY; var group = map.get(key); if (!group) { group = []; map.set(key, group); } group.push(result.value); } return map; } exports.getKeyedMapFast = getKeyedMapFast; // 遍历迭代器iterable,获取其元素项或通过keySelector函数转化元素项,构建成员值唯一的set数组并返回 function getKeys(iterable, keySelector) { var set = new Set();// 数组形式,成员值唯一,通过add方法添加元素 var otherIterator = iterable[Symbol.iterator](); var result; if (keySelector) { while (!(result = otherIterator.next()).done) { set.add(keySelector(result.value)); } }else { while (!(result = otherIterator.next()).done) { set.add(result.value); } } return set; } exports.getKeys = getKeys; exports.CONST_INVALID_KEY = "Key selector returned undefined Key"; exports.CONST_NO_STRING = "Type must be a string."; exports.CONST_DUPLICATE = "Object already has property [key]"; exports.CONST_NOTHING_FOUND = "No element satisfies the condition in predicate"; exports.CONST_NO_ELEMENTS = "The source sequence is empty."; exports.CONST_TOO_MANY = "More than one element satisfies the condition in predicate."; exports.CONST_OUTOFRANGE = "Argument Out Of Range"; exports.CONST_KEY = "key"; exports.CONST_UNDEFINED = "undefined"; exports.CONST_LENGTH = "length"; exports.CONST_FUNCTION = "function"; exports.CONST_BOOLEAN = "boolean"; exports.CONST_NUMBER = "number"; exports.CONST_OBJECT = "object"; exports.CONST_STRING = "string"; exports.CONST_SYMBOL = "symbol"; exports.CONST_EMPTY_STRING = "";
相关推荐
《LINQ-SQL与LINQ-XML:微软专家整合的最全、最强项目实战》 在.NET框架中,Language Integrated Query(LINQ)是一项革命性的技术,它将查询语句直接整合到C#和VB.NET等编程语言中,极大地提高了代码的可读性和...
在C#编程中,LINQ-to-SQL是LINQ的一个子集,专门用于处理关系数据库。本教程将深入探讨LINQ-to-SQL的基本概念、用法以及在实际开发中的应用。 1. LINQ-to-SQL的基本概念: LINQ-to-SQL是一种对象关系映射(ORM)...
这个压缩包"dynamic-linq-query-builder-master.zip"包含了该库的源代码和相关资源。 jQuery-QueryBuilder的核心功能包括: 1. **可视化构建**:用户可以通过拖拽不同的条件、操作符和字段到界面上,构建他们的...
linq-fast .NET Linq / Enumerable的TypeScript实现,用于延迟评估,着重于速度 已实现所有Enumerable方法的工作版本,并且该库可供使用 未来的更新将专注于性能增强和更大的测试范围 用法 用法很简单,要获取一个...
《PyPI与Python库:深度解析py-linq-1.2.5》 在Python的世界里,PyPI(Python Package Index)是最重要的软件仓库,它为开发者提供了丰富的Python库资源,便于他们构建、分享和使用各种功能强大的工具。本文将详细...
LINQ - 运营商 此示例显示了不同用途的联接运算符 LINQ - 分组运算符 此示例显示了不同用途的分组运算符 LINQ - 限制运营商 此示例显示了不同用途的限制运营商。 LINQ - 投影算 此示例显示了不同用途的投影算 LINQ -...
dynamic-linq-query-builder是一个小型库,允许任何.Net框架类集合在运行时动态过滤。 功能(v1.2.0) 从任何集合和过滤器组合生成IQueryable 可以根据需要对多个字段进行复杂的分组查询 通过点表示法支持嵌套...
标题“LINQ的Java示例java-linq-examples.zip”指的是一个开源项目,它将Microsoft .NET框架中的Language Integrated Query (LINQ)概念移植到了Java平台。LINQ是一种在.NET中处理数据的强大工具,它允许程序员使用...
Linq学习100例(含源代码)101 LINQ Samples Learn how to use LINQ in your applications with these code samples, covering the entire range of LINQ functionality and demonstrating LINQ with SQL, DataSets,...
《NHibernate.Linq-1.0.0.GA-bin.zip:NHibernate与LINQ在.NET开发中的深度探索》 NHibernate.Linq-1.0.0.GA-bin.zip 是一个包含NHibernate.Linq组件的压缩包,该组件是NHibernate框架的一个扩展,使得在.NET环境中...
python库。资源全名:python_linq-1.1.0-py3-none-any.whl
**LINQ-to-SQL:SQL 增删改查指南** LINQ(Language Integrated Query,语言集成查询)是.NET框架中的一项创新技术,它允许开发者使用C#或VB.NET等编程语言的语法来处理各种数据源,包括SQL数据库。本文将深入探讨...
标题"Linq.rar_ linq to odbc_Linq-CSharp.part_linq"提到了几个关键概念:Linq(Language Integrated Query,语言集成查询),Linq to ODBC,以及Linq-CSharp.part。这表明内容可能涉及使用C#编程语言中的Linq特性...
理解LINQ-to-SQL.pdf
Angular-LINQ.zip,语言集成查询的typescript实现(linq)(ecmascript 2015)该库是使用typescript和javascript语言的最新特性实现linq的持续努力(对于es5兼容库,请查看linq-es5分支)。这个库是用typescript实现...
资源分类:Python库 所属语言:Python 资源全名:python-linq-2.0.4.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
《LINQ-GuestBook:基于ASP.NET C#的优雅留言本实现》 在现代Web开发领域,高效的数据处理和友好的用户界面是不可或缺的组成部分。本文将深入探讨一个名为"Linq-GuestBook"的项目,这是一个利用LINQ(Language ...
- **数据类型支持**:LINQ适用于各种数据类型,每种类型都有对应的LINQ类型,如LINQ to Objects用于处理内存中的数据结构,LINQ to XML用于XML数据,LINQ to SQL用于SQL数据库,LINQ to ADO.NET则支持更广泛的ADO...