-
Notifications
You must be signed in to change notification settings - Fork 1
/
AsEnumerable.js
176 lines (162 loc) · 4.38 KB
/
AsEnumerable.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
Object.defineProperty(Array.prototype, 'AsEnumerable', { value: function(){
return new Enumerable(this);
}});
var Enumerable = function (set) {
this.Set = set;
this.Path = [];
this.Paths = [];
};
(function(fn,undefined){
function Break(){}
var compares = {};
compares["[object Number]"] = function (x, y, desc) {
return !desc? x - y : y - x;
};
compares["[object String]"] = function (x, y, desc) {
return !desc? x.localeCompare(y) : y.localeCompare(x);
};
compares["[object Date]"] = function (x, y, desc) {
var r = x.split('/');
var s = y.split('/');
var g = (100 * r[0]) + (r[1] | 0) + (10000 * r[2]);
var h = (100 * s[0]) + (s[1] | 0) + (10000 * s[2]);
return !desc? g - h : h - g;
};
fn.Any = function(func)
{
if(func == undefined && this.Set.length > 0) return true;
for(var i = 0; i < this.Set.length; i++){
if(func(this.Set[i],i))return true;
}
return false;
};
fn.All = function(func)
{
if(func == undefined) throw new Error("No expression");
for(var i = 0; i < this.Set.length; i++){
if(!func(this.Set[i],i))return false;
}
return true;
};
fn.Count = function(func)
{
if(func == undefined) return this.Set.length;
var count = 0;
for(var i = 0; i < this.Set.length; i++){
if(func(this.Set[i],i))count++;
}
return count;
};
fn.Sum = function(func)
{
if(func == undefined) return this.Set.length;
var sum = 0;
for(var i = 0; i < this.Set.length; i++){
if(func == undefined){
sum += this.Set[i];
}else{
sum += func(this.Set[i],i);
}
}
return sum;
};
fn.Where = function(func)
{
this.Path.push(
function(item,i){
if(func(item,i))return item;
}
);
return this;
};
fn.Select = function(func)
{
this.Path.push(
function(item,i){
return func(item,i);
}
);
return this;
};
fn.Lookup = function(func){
return this;
};
function same(x){ return x; }
fn.OrderBy = function(func){
if(this.Path.length > 0 ){
this.Paths.push(this.Path);
this.Path = [];
}
func = func ? func : same;
var f = function(array){
var type = toString.call(array[0]);
array.sort(function(a,b){
var x = func(a);
var y = func(b);
return compares[type](x, y, false);
});
};
f.type = "order";
this.Paths.push([f]);
return this;
};
fn.OrderByDescending = function(func){
if(this.Path.length > 0 ){
this.Paths.push(this.Path);
this.Path = [];
}
func = func ? func : same;
var f = function(array){
var type = toString.call(array[0]);
array.sort(function(a,b){
var x = func(a);
var y = func(b);
return compares[type](x, y, true);
});
};
f.type = "order";
this.Paths.push([f]);
return this;
};
fn.ThenBy = function(func){
return this;
};
fn.Take = function(count){
this.Path.push(
function(item,i){
if( i >= count ) return Break;
return item;
}
);
return this;
};
fn.ToArray = function(){
if(this.Path.length > 0 )this.Paths.push(this.Path);
var array = [];
var set = this.Set;
for(var a = 0; a < this.Paths.length; a++)
{
var path = this.Paths[a];
if(path[0].type == "order"){
for(var i = 0; i < path.length; i++){
path[i](set);
}
}else{
for(var i = 0; i < set.length; i++){
var result = (function walk(item,n){
if(n == path.length
|| item == undefined
|| item == Break
) return item;
return walk(path[n](item,i),n+1);
})(set[i],0);
if(result == Break)break;
if(result != undefined)array.push(result);
}
set = array;
array = [];
}
}
return set;
};
})(Enumerable.prototype)