-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinq.js
127 lines (94 loc) · 3.26 KB
/
linq.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
/*---------------------------------------------------------------------------------------------------------
Linq for javascript
Enables quries on objects for javascript arrays.
For loops can be avoided using this library for filtering, transforming, check exists etc.
It has no dependency with any other javascript library.
It is independent and can be included in any of your javascript programs
Complete sample is available in plunker. http://plnkr.co/edit/oBZnD5ZBJinyYGgiwghV?p=preview
-------------------------------------------------------------------------------------------------------------
*/
function Enumerable(data) {
this.enumerable = data;
//Transforms the items using the transformation function
this.Select = function (transformFunction) {
if (!validate(this.enumerable))
return null;
var result = [];
for (var i = 0; i < this.enumerable.length; i++) {
result.push(transformFunction(this.enumerable[i]));
}
return new Enumerable(result);
}
//Filters the items using the predicate function
this.Where = function(predicate) {
if (!validate(this.enumerable))
return null;
var result = [];
for (var i = 0; i < this.enumerable.length; i++) {
if (predicate(this.enumerable[i])) {
result.push(this.enumerable[i]);
}
}
return new Enumerable(result);
}
//Returns true if atleast one of the item satiesfies the predicate
this.Any = function(predicate) {
if (!validate(this.enumerable))
return null;
if (arguments.length == 0) {
return this.enumerable.length > 0;
}
var result = [];
for (var i = 0; i < this.enumerable.length; i++) {
if (predicate(this.enumerable[i])) {
return true;
}
}
return false;
}
//Returns true if atleast all the items satiesfies the predicate
this.All = function(predicate) {
if (!validate(this.enumerable))
return null;
var result = [];
for (var i = 0; i < this.enumerable.length; i++) {
if (!predicate(this.enumerable[i])) {
return false;
}
}
return true;
}
this.ForEach = function(action) {
for (var i = 0; i < this.enumerable.length; i++) {
action(this.enumerable[i]);
}
}
//Returns the first element that satisfies the predicate
this.First = function (predicate) {
if (!validate(this.enumerable))
return null;
if (arguments.length == 0) {
return this.enumerable[0];
}
for (var i = 0; i < this.enumerable.length; i++) {
if (predicate(this.enumerable[i])) {
return this.enumerable[i];
}
}
return null;
}
//Returns the array
this.ToList = function() { return this.enumerable; }
}
//Converts the array to enumerable to enable object queries
function ToEnumerable(data) {
return new Enumerable(data);
}
function validate(enumerable) {
if (enumerable == null) {
return false;
}
if (!Array.isArray(enumerable))
throw "array is required";
return true;
}