Skip to content

Commit 8053b54

Browse files
committed
added new functions
1 parent 513547b commit 8053b54

File tree

12 files changed

+146
-13
lines changed

12 files changed

+146
-13
lines changed

dev/Helper.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,21 @@
66
throw "Linq4JS: Cannot convert empty string to function";
77
}
88

9-
let varname: string = functionString.substring(0, functionString.indexOf("=>")).replace(" ", "");
9+
let varnameString: string = functionString.substring(0, functionString.indexOf("=>")).replace(" ", "").replace("(", "").replace(")", "");
1010

11-
if (varname.indexOf(",") != -1) {
12-
throw `Linq4JS: Cannot use '${varname}' as variable`;
13-
}
11+
let varnames: Array<string> = varnameString.split(",");
1412

1513
let func: string = functionString
1614
.substring(functionString.indexOf("=>") + 2)
15+
.replace("{", "").replace("}", "")
1716
.split(".match(//gi)").join("");
1817

19-
func = "return " + func;
18+
/*No return outside of quotations*/
19+
if(func.match(/return(?=([^\"']*[\"'][^\"']*[\"'])*[^\"']*$)/g) == null){
20+
func = "return " + func;
21+
}
2022

21-
return Function(varname, func);
23+
return Function(...varnames, func);
2224
}
2325

2426
static ConvertFunction: Function = function (testFunction: any): Function {

dev/IArray.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Where(filter: any): Array<T>;
1616
Range(start: number, length: number): Array<T>;
1717
Count(filter?: any): number;
18+
All(filter: any): boolean;
1819
Any(filter?: any): boolean;
1920
First(filter?: any): T;
2021
FirstOrDefault(filter?: any): T;
@@ -29,4 +30,10 @@
2930
ThenByDescending(valueSelector: any): Array<T>;
3031
Move(oldIndex: number, newIndex: number): Array<T>;
3132
Distinct(valueSelector: any, takelast?: boolean): Array<T>;
33+
Contains(object: T): boolean;
34+
Concat(array: Array<T>): Array<T>;
35+
Join(character: string, selector?: any): string;
36+
Aggregate(method: any): string;
37+
Reverse(): Array<T>;
38+
Average(selector?: any, filter?: any): number;
3239
}

dev/Modules/Aggregate.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
Array.prototype.Aggregate = function<T> (method: any): string {
2+
let that: Array<T> = this;
3+
4+
let result: string = "";
5+
let methodFunction: Function = Linq4JS.Helper.ConvertFunction(method);
6+
7+
that.ForEach(function(x){
8+
result = methodFunction(result, x);
9+
});
10+
11+
return result;
12+
};

dev/Modules/All.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Array.prototype.All = function<T> (filter: any): boolean {
2+
let that: Array<T> = this;
3+
4+
return that.Count(filter) == that.Count();
5+
};

dev/Modules/Average.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Array.prototype.Average = function <T>(selector?: any, filter?: any): number {
2+
let that: Array<T> = this;
3+
4+
let result: number = 0;
5+
let array: Array<any> = that;
6+
7+
if (filter != null) {
8+
array = array.Where(filter);
9+
}
10+
11+
if(selector != null){
12+
array = array.Select(selector);
13+
}
14+
15+
array.ForEach(function(x){
16+
result += x;
17+
});
18+
19+
return result / array.Count();
20+
};

dev/Modules/Concat.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Array.prototype.Concat = function<T> (array: Array<T>): Array<T> {
2+
let that: Array<T> = this;
3+
that = that.concat(array);
4+
return that;
5+
};

dev/Modules/Contains.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Array.prototype.Contains = function<T> (object: T): boolean {
2+
let that: Array<T> = this;
3+
4+
return that.Any(function(x){
5+
return x == object;
6+
});
7+
};

dev/Modules/Join.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Array.prototype.Join = function <T>(char: string, selector?: any): string {
2+
let that: Array<T> = this;
3+
4+
let array: Array<any> = that;
5+
6+
if(selector != null){
7+
array = array.Select(selector);
8+
}
9+
10+
return array.join(char);
11+
};

dev/Modules/Reverse.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Array.prototype.Reverse = function <T>(): Array<T> {
2+
let that: Array<T> = this;
3+
return that.reverse();
4+
};

dist/Linq4JS.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ interface Array<T> {
2727
Where(filter: any): Array<T>;
2828
Range(start: number, length: number): Array<T>;
2929
Count(filter?: any): number;
30+
All(filter: any): boolean;
3031
Any(filter?: any): boolean;
3132
First(filter?: any): T;
3233
FirstOrDefault(filter?: any): T;
@@ -41,6 +42,12 @@ interface Array<T> {
4142
ThenByDescending(valueSelector: any): Array<T>;
4243
Move(oldIndex: number, newIndex: number): Array<T>;
4344
Distinct(valueSelector: any, takelast?: boolean): Array<T>;
45+
Contains(object: T): boolean;
46+
Concat(array: Array<T>): Array<T>;
47+
Join(character: string, selector?: any): string;
48+
Aggregate(method: any): string;
49+
Reverse(): Array<T>;
50+
Average(selector?: any, filter?: any): number;
4451
}
4552
declare namespace Linq4JS {
4653
class OrderEntry {

0 commit comments

Comments
 (0)