Skip to content

Commit e48820f

Browse files
committed
fixed some issues, applied improvements, added SelectMany
1 parent ae2cf06 commit e48820f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+2118
-1184
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ array.Where("x => x.Name == 'Max'");
3232

3333
Also a complete procedure as an sql-like string is supported
3434

35+
```
36+
array.Evaluate("select x => x.Id sum");
37+
```
38+
3539
```
3640
clone
3741
reverse
@@ -638,6 +642,17 @@ array.Select("i => {C: i.Value}");
638642
array.Select("i => {C = i.Value}");
639643
```
640644

645+
### SelectMany
646+
647+
Select the properties with an array as value and concats them
648+
649+
```javascript
650+
var array = [["item1", "item2"], ["item1", "item2"]];
651+
652+
//["item1", "item2", "item1", "item2"]
653+
array.SelectMany("i => i");
654+
```
655+
641656
### Take
642657

643658
Limits the number of entries taken

demo/demo.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"use strict";
22
var User = (function () {
3-
function User(_id, _firstName, _name, _age) {
3+
function User(_id, _firstName, _name, _age, _pets) {
44
this.Id = _id;
55
this.FirstName = _firstName;
66
this.Name = _name;
77
this.Age = _age;
8+
this.Pets = _pets;
89
}
910
return User;
1011
}());
@@ -77,22 +78,22 @@ console.log(array.FindLastIndex(x => x == "item2"));
7778
//console.log(test);
7879
//console.log(test.Count(x => x.Id > 3));
7980
var userArray = [
80-
new User(1, "Brenda", "Thompson", 49),
81-
new User(2, "Kelly", "Grady", 62),
82-
new User(3, "Lavina", "Baskin", 34),
83-
new User(4, "Corey", "Medina", 53),
84-
new User(5, "Walter", "Pankey", 61),
85-
new User(6, "Virginia", "Ayala", 54),
86-
new User(7, "Allison", "Israel", 38),
87-
new User(8, "Christine", "Starkey", 19),
88-
new User(9, "Robert", "Humphreys", 22),
89-
new User(10, "Daniel", "Stanley", 85),
90-
new User(11, "Frank", "Brown", 73),
91-
new User(12, "Juan", "Barnhart", 56),
92-
new User(13, "Timothy", "Olson", 29),
93-
new User(14, "Christina", "Holland", 81),
94-
new User(15, "Albert", "Dunn", 58),
95-
new User(16, "Kelly", "Grady", 48)
81+
new User(1, "Brenda", "Thompson", 49, ["Dog", "Cat"]),
82+
new User(2, "Kelly", "Grady", 62, ["Dog", "Cat"]),
83+
new User(3, "Lavina", "Baskin", 34, ["Dog", "Cat"]),
84+
new User(4, "Corey", "Medina", 53, ["Dog", "Cat"]),
85+
new User(5, "Walter", "Pankey", 61, ["Dog", "Cat"]),
86+
new User(6, "Virginia", "Ayala", 54, ["Dog", "Cat"]),
87+
new User(7, "Allison", "Israel", 38, ["Dog", "Cat"]),
88+
new User(8, "Christine", "Starkey", 19, ["Dog", "Cat"]),
89+
new User(9, "Robert", "Humphreys", 22, ["Dog", "Cat"]),
90+
new User(10, "Daniel", "Stanley", 85, ["Dog", "Cat"]),
91+
new User(11, "Frank", "Brown", 73, ["Dog", "Cat"]),
92+
new User(12, "Juan", "Barnhart", 56, ["Dog", "Cat"]),
93+
new User(13, "Timothy", "Olson", 29, ["Dog", "Cat"]),
94+
new User(14, "Christina", "Holland", 81, ["Dog", "Cat"]),
95+
new User(15, "Albert", "Dunn", 58, ["Dog", "Cat"]),
96+
new User(16, "Kelly", "Grady", 48, ["Dog", "Cat"])
9697
];
9798
window.onload = function () {
9899
arrayPreview.innerHTML = JSON.stringify(userArray);

dev/Helper.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103

104104
for (let cmd of this.Commands) {
105105
for (let split of cmd.SplitRegex) {
106-
while (true) {
106+
while (true) {
107107
let result = split.exec(command);
108108
if (result != null) {
109109
splitIndexes.push(result.index);
@@ -150,13 +150,19 @@
150150
public static Commands: EvaluateCommand[] = [
151151
new EvaluateCommand("Clone", "clone"),
152152
new EvaluateCommand("Reverse", "reverse"),
153+
new EvaluateCommand("Contains", "contains {x}"),
154+
new EvaluateCommand("Join", "join {x}"),
155+
new EvaluateCommand("Sum", "sum {x}", "sum"),
156+
new EvaluateCommand("Average", "average {x}", "average"),
153157
new EvaluateCommand("Where", "where {x}"),
158+
new EvaluateCommand("SelectMany", "selectmany {x}", "select many {x}", "select {x} many"),
154159
new EvaluateCommand("Select", "select {x}"),
155160
new EvaluateCommand("Get", "get {x}"),
156161
new EvaluateCommand("ForEach", "foreach {x}", "for each {x}"),
157162
new EvaluateCommand("Count", "count", "count {x}"),
158163
new EvaluateCommand("All", "all {x}"),
159164
new EvaluateCommand("Any", "any {x}", "any"),
165+
new EvaluateCommand("TakeWhile", "take while {x}", "take {x} while", "takewhile {x}"),
160166
new EvaluateCommand("Take", "take {x}"),
161167
new EvaluateCommand("Skip", "skip {x}"),
162168
new EvaluateCommand("Min", "min {x}", "min"),
@@ -176,5 +182,17 @@
176182
new EvaluateCommand("ThenByDescending", "thenby {x} descending", "then by {x} descending", "thenbydescending {x}", "then by descending {x}"),
177183
new EvaluateCommand("ThenBy", "thenby {x} ascending", "then by {x} ascending", "thenbyascending {x}", "then by ascending {x}", "thenby {x}", "then by {x}")
178184
];
185+
186+
public static NonEnumerable(name: string, value: Function) {
187+
Object.defineProperty(Array.prototype, name, {
188+
value: value,
189+
enumerable: false
190+
});
191+
}
179192
}
180-
}
193+
}
194+
195+
Object.defineProperty(Array.prototype, "_linq4js_", {
196+
value: { Order: [] },
197+
enumerable: false
198+
});

dev/IArray.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
interface Array<T> {
2-
Order: Linq4JS.OrderEntry[];
3-
GroupValue: any;
2+
_linq4js_: { Order: Linq4JS.OrderEntry[], GroupValue: any };
43

54
/**
65
* Executes actions defined in the command-string
@@ -165,6 +164,12 @@
165164
*/
166165
Select(selector: ((item: T) => any) | string): any[];
167166

167+
/**
168+
* Select the properties with an array as value and concats them
169+
* @param selector A function (or a function-string) that returns the array with elements to select
170+
*/
171+
SelectMany(selector: ((item: T) => any) | string): any[];
172+
168173
/**
169174
* Limits the number of entries taken
170175
* @param count The count of elements taken

dev/Modules/Add.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
Array.prototype.Add = function<T> (this: T[], object: T, generateId?: boolean): T[] {
2-
let that: T[] = this;
3-
1+
Linq4JS.Helper.NonEnumerable("Add", function<T> (this: T[], object: T, generateId?: boolean): T[] {
42
if (object != null) {
53
if (generateId === true) {
64
let newIndex: number;
75

86
let castedObject: Linq4JS.GeneratedEntity = object as any;
9-
let last: Linq4JS.GeneratedEntity = that.Where((x: any) => x._GeneratedId_ != null).OrderBy((x: any) => x._GeneratedId_).LastOrDefault() as any;
7+
let last: Linq4JS.GeneratedEntity = this.Where((x: any) => x._GeneratedId_ != null).OrderBy((x: any) => x._GeneratedId_).LastOrDefault() as any;
108
if (last != null) {
119
newIndex = last._GeneratedId_ != null ? last._GeneratedId_ : 1;
1210

13-
while (that.Any(function(x: any) {
11+
while (this.Any(function(x: any) {
1412
return (x as Linq4JS.GeneratedEntity)._GeneratedId_ === newIndex;
1513
})) {
1614
newIndex++;
@@ -22,8 +20,8 @@
2220
}
2321
}
2422

25-
that.push(object);
23+
this.push(object);
2624
}
2725

28-
return that;
29-
};
26+
return this;
27+
});

dev/Modules/AddRange.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
Array.prototype.AddRange = function<T> (this: T[], objects: T[], generateId: boolean): T[] {
1+
Linq4JS.Helper.NonEnumerable("AddRange", function<T> (this: T[], objects: T[], generateId: boolean): T[] {
22
let that: T[] = this;
33

44
objects.ForEach(function (x: T) {
55
that.Add(x, generateId);
66
});
77

88
return that;
9-
};
9+
});

dev/Modules/Aggregate.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
Array.prototype.Aggregate = function<T> (this: T[], method: ((result: any, item: T) => any) | string, startVal?: any): string {
2-
let that: T[] = this;
3-
1+
Linq4JS.Helper.NonEnumerable("Aggregate", function<T> (this: T[], method: ((result: any, item: T) => any) | string, startVal?: any): string {
42
let result: any;
53

64
if (startVal != null) {
@@ -11,9 +9,9 @@
119

1210
let methodFunction = Linq4JS.Helper.ConvertFunction<(result: any, item: T) => any>(method);
1311

14-
that.ForEach(function(x){
12+
this.ForEach(function(x){
1513
result = methodFunction(result, x);
1614
});
1715

1816
return result;
19-
};
17+
});

dev/Modules/All.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Array.prototype.All = function<T> (this: T[], filter: ((item: T) => boolean) | string): boolean {
2-
let that: T[] = this;
3-
4-
return that.Count(filter) === that.Count();
5-
};
1+
Linq4JS.Helper.NonEnumerable("All", function<T> (this: T[], filter: ((item: T) => boolean) | string): boolean {
2+
return this.Count(filter) === this.Count();
3+
});

dev/Modules/Any.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
Array.prototype.Any = function<T> (this: T[], filter?: ((item: T) => boolean) | string): boolean {
2-
let that: T[] = this;
3-
4-
return that.Count(filter) > 0;
5-
};
1+
Linq4JS.Helper.NonEnumerable("Any", function<T> (this: T[], filter?: ((item: T) => boolean) | string): boolean {
2+
return this.Count(filter) > 0;
3+
});

dev/Modules/Average.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
Array.prototype.Average = function <T>(this: T[], selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number {
2-
let that: T[] = this;
3-
1+
Linq4JS.Helper.NonEnumerable("Average", function <T>(this: T[], selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number {
42
let result: number = 0;
5-
let array: any[] = that;
3+
let array: any[] = this;
64

75
if (filter != null) {
86
array = array.Where(filter);
@@ -17,4 +15,4 @@
1715
});
1816

1917
return result / array.Count();
20-
};
18+
});

0 commit comments

Comments
 (0)