Skip to content

Commit 1125995

Browse files
committed
definition improvements, updated functions, added some
1 parent 2f20d98 commit 1125995

Some content is hidden

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

44 files changed

+599
-173
lines changed

README.md

Lines changed: 145 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,17 @@ var array = ["item1", "item2", "item3", "item4", "no"];
9797
array.Get(2);
9898
```
9999

100+
### Repeat
101+
102+
Repeats an object in the array
103+
104+
```javascript
105+
var array = ["item1", "item2", "item3", "item4"];
106+
107+
//["item1", "item2", "item3", "item4", "example", "example", "example"]
108+
array.Repeat("example", 3);
109+
```
110+
100111
### ForEach
101112

102113
Executes a method for each item in the array
@@ -225,6 +236,22 @@ array.Count();
225236
array.Count("i => i.match(/item/gi)");
226237
```
227238

239+
### SequenceEqual
240+
241+
Compares to sequences of objects
242+
243+
```javascript
244+
var array = ["item1", "item2", "item3"];
245+
var array2 = ["item1", "item2", "item3"];
246+
var array3 = ["item", "item2", "item3"];
247+
248+
//true
249+
array.SequenceEqual(array2);
250+
251+
//false
252+
array.SequenceEqual(array3);
253+
```
254+
228255
### Any
229256

230257
Tests if any item is in the array and if set matches the filter
@@ -276,6 +303,30 @@ var array2 = ["item4", "no"];
276303
array.Concat(array2);
277304
```
278305

306+
### Intersect
307+
308+
Combines two arrays but only applies values that are in both arrays
309+
310+
```javascript
311+
var array = ["item1", "item2", "item3"];
312+
var array2 = ["item1", "unique", "item2", "item3"];
313+
314+
//["item1", "item2", "item3"]
315+
array.Intersect(array2);
316+
```
317+
318+
### Union
319+
320+
Combines two arrays without duplicates
321+
322+
```javascript
323+
var array = ["item1", "item2", "item3"];
324+
var array2 = ["item1", "unique", "item2", "item3"];
325+
326+
//["item1", "item2", "item3", "unique"]
327+
array.Union(array2);
328+
```
329+
279330
### Join
280331

281332
Joins the entries by the given char
@@ -292,7 +343,7 @@ array.Join("-", "x => x.length > 2");
292343

293344
### Aggregate
294345

295-
Combines the entries by your definition
346+
Combines the entries using a custom function
296347

297348
```javascript
298349
var array = ["item1", "item2", "item3", "item4", "no"];
@@ -301,6 +352,32 @@ var array = ["item1", "item2", "item3", "item4", "no"];
301352
array.Aggregate("(str, item) => item + '-' + item");
302353
```
303354

355+
### ToDictionary
356+
357+
Converts the array to a dictionary
358+
359+
```javascript
360+
var array = [{OtherId: 1, Value: "item1"}, {OtherId: 2, Value: "item2"}];
361+
362+
//{1: {OtherId: 1, Value: "item1"}, 2: {OtherId: 2, Value: "item2"}}
363+
array.ToDictionary("x => x.OtherId");
364+
365+
//{1: "item1", 2: "item2"}
366+
array.ToDictionary("x => x.OtherId", "x => x.Value");
367+
```
368+
369+
### Zip
370+
371+
Combines the entries of two arrays using a custom function
372+
373+
```javascript
374+
var array = [0, 1, 2, 3, 4];
375+
var array2 = ["zero", "one", "two", "three"];
376+
377+
//["0 zero", "1 one", "2 two", "3 three"]
378+
array.Zip(array2, "(x, y) => x + ' ' + y");
379+
```
380+
304381
### Reverse
305382

306383
Reverses the array
@@ -386,6 +463,22 @@ array.FirstOrDefault("i => i.match(/item/gi)");
386463
array.First("i => i == 'notgiven'");
387464
```
388465

466+
### Min
467+
468+
Returns the smallest element in array
469+
470+
```javascript
471+
var array = [0, 8, 1, 5, -3];
472+
473+
//-3
474+
array.Min();
475+
476+
var array = [{name: "test", age: 3}, {name: "test2", age: 18}];
477+
478+
//{name: "test", age: 3}
479+
array.Min("x => x.age");
480+
```
481+
389482
### Last
390483

391484
Returns the Last item of the array and if a filter was set the last item that matches the filter - Throws an Exception if no item was found
@@ -420,6 +513,22 @@ array.LastOrDefault("i => i.match(/item/gi)");
420513
array.LastOrDefault("i => i == 'notgiven'");
421514
```
422515

516+
### Max
517+
518+
Returns the greates element in array
519+
520+
```javascript
521+
var array = [0, 8, 1, 5, -3];
522+
523+
//8
524+
array.Max();
525+
526+
var array = [{name: "test", age: 3}, {name: "test2", age: 18}];
527+
528+
//{name: "test2", age: 18}
529+
array.Max("x => x.age");
530+
```
531+
423532
### Select
424533

425534
Select the properties for a new array
@@ -445,6 +554,41 @@ var array = ["item1", "item2", "item3", "item4"];
445554
array.Take(2);
446555
```
447556

557+
### TakeWhile
558+
559+
Takes entries as long as a condition is true
560+
561+
```javascript
562+
var array = ["item1", "item2", "item3", "item2", "item4"];
563+
var item2count = 0;
564+
565+
//["item1", "item2", "item3"]
566+
array.TakeWhile(function(x){
567+
if(x == "item2"){
568+
item2count++;
569+
}
570+
571+
return item2count < 2;
572+
});
573+
```
574+
575+
This is the basic usage. But if you want conditional executes for e.g. with counting this can get a little bit messy.
576+
577+
```javascript
578+
var array = ["item1", "item2", "item3", "item2", "item4"];
579+
580+
//["item1", "item2", "item3"]
581+
array.TakeWhile(function(item, storage){
582+
return item != "item2" || storage.count < 1; //Condition
583+
}, function(storage){
584+
storage.count = 0; //Init the Storage
585+
}, function(item, storage){
586+
if(item == "item2"){
587+
storage.count++; //After executing the condition
588+
}
589+
});
590+
```
591+
448592
### Skip
449593

450594
Skips entries

demo/demo.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ window.onload = function () {
3131
Helper.users = Helper.users.Clone();
3232
Helper.draw();
3333
};
34+
var test = [new User(1, "Max", "Mustermann", 18), new User(2, "John", "Doe", 89), new User(2, "John", "Doe", 18)];
35+
console.log(test.Where(function (x) { return x.Age > 10; }));
36+
var result = test.Aggregate(function (x, y) {
37+
return x += y.Age;
38+
});
39+
console.log(result);
40+
var array = ["item1", "item2", "item3", "item2", "item4"];
41+
var res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0', '(x, s) => x == "item2" && s.c++');
42+
console.log(res2);
3443
//let test: Array<Linq4JS.Entity> = [new testClass("test", 5, 1), new testClass("test5", 3, 2)];
3544
//console.log(test);
3645
//console.log("Foreach");

demo/js/app.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ window.onload = function () {
4545
Helper.draw();
4646
};
4747

48+
let test: Array<User> = [new User(1, "Max", "Mustermann", 18), new User(2, "John", "Doe", 89), new User(2, "John", "Doe", 18)];
49+
console.log(test.Where(x => x.Age > 10));
50+
51+
let result = test.Aggregate((x, y) => {
52+
return x += y.Age;
53+
});
54+
55+
console.log(result);
56+
57+
58+
let array = ["item1", "item2", "item3", "item2", "item4"];
59+
60+
let res2 = array.TakeWhile('(x, s) => x != "item2" || s.c < 1', 's => s.c = 0', '(x, s) => x == "item2" && s.c++');
61+
62+
console.log(res2);
63+
4864
//let test: Array<Linq4JS.Entity> = [new testClass("test", 5, 1), new testClass("test5", 3, 2)];
4965
//console.log(test);
5066

dev/Helper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace Linq4JS {
22
export class Helper {
3-
static ConvertStringFunction: Function = function (functionString: string): Function {
3+
static ConvertStringFunction = function (functionString: string): any {
44

55
if (functionString.length == 0) {
66
throw "Linq4JS: Cannot convert empty string to function";
@@ -23,9 +23,9 @@
2323
return Function(...varnames, func);
2424
}
2525

26-
static ConvertFunction: Function = function (testFunction: any): Function {
26+
static ConvertFunction = function<T> (testFunction: any): T {
2727

28-
let result: Function;
28+
let result: T;
2929

3030
if (typeof testFunction == "function") {
3131
result = testFunction;
@@ -40,9 +40,9 @@
4040
return result;
4141
}
4242

43-
static OrderCompareFunction: Function = function (valueSelector: Function, a: Linq4JS.Entity, b: Linq4JS.Entity, invert: boolean): number {
44-
let value_a: any = valueSelector(a);
45-
let value_b: any = valueSelector(b);
43+
static OrderCompareFunction = function<T> (valueSelector: (item: T) => any, a: T, b: T, invert: boolean): number {
44+
let value_a = valueSelector(a);
45+
let value_b = valueSelector(b);
4646

4747
let type = typeof value_a;
4848

dev/IArray.ts

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,40 +3,49 @@
33
GroupValue: any;
44

55
Clone(): Array<T>;
6-
FindIndex(filter: any): number;
6+
FindIndex(filter: ((item: T) => boolean) | string): number;
77
Get(index: number): T;
8-
ForEach(action: any): Array<T>;
9-
Update(object: T, primaryKeySelector?: any): Array<T>;
10-
UpdateRange(objects: Array<T>, primaryKeySelector?: any): Array<T>;
11-
Remove(object: T, primaryKeySelector?: any): Array<T>;
12-
RemoveRange(objects: Array<T>, primaryKeySelector?: any): Array<T>;
8+
ForEach(action: ((item: T) => boolean | any) | string): Array<T>;
9+
Update(object: T, primaryKeySelector?: ((item: T) => any) | string): Array<T>;
10+
UpdateRange(objects: Array<T>, primaryKeySelector?: ((item: T) => any) | string): Array<T>;
11+
Remove(object: T, primaryKeySelector?: ((item: T) => any) | string): Array<T>;
12+
RemoveRange(objects: Array<T>, primaryKeySelector?: ((item: T) => any) | string): Array<T>;
1313
Add(object: T, generateId?: boolean): Array<T>;
1414
AddRange(objects: Array<T>): Array<T>;
1515
Insert(object: T, index: number): Array<T>;
16-
Where(filter: any): Array<T>;
16+
Where(filter: ((item: T) => boolean) | string): Array<T>;
1717
Range(start: number, length: number): Array<T>;
18-
Count(filter?: any): number;
19-
All(filter: any): boolean;
20-
Any(filter?: any): boolean;
21-
First(filter?: any): T;
22-
FirstOrDefault(filter?: any): T;
23-
Last(filter?: any): T;
24-
LastOrDefault(filter?: any): T;
25-
Select(selector: any): any[];
18+
Repeat(object: T, count: number): Array<T>;
19+
Count(filter?: ((item: T) => boolean) | string): number;
20+
All(filter: ((item: T) => boolean) | string): boolean;
21+
Any(filter?: ((item: T) => boolean) | string): boolean;
22+
First(filter?: ((item: T) => boolean) | string): T;
23+
FirstOrDefault(filter?: ((item: T) => boolean) | string): T;
24+
Last(filter?: ((item: T) => boolean) | string): T;
25+
LastOrDefault(filter?: ((item: T) => boolean) | string): T;
26+
Select(selector: ((item: T) => any) | string): any[];
2627
Take(count: number): Array<T>;
28+
TakeWhile(condition: ((item: T, storage?: any) => boolean) | string, initial?: ((storage: any) => void) | string, after?: ((item: T, storage: any) => void) | string): Array<T>;
2729
Skip(count: number): Array<T>;
28-
OrderBy(valueSelector: any): Array<T>;
29-
ThenBy(valueSelector: any): Array<T>;
30-
OrderByDescending(valueSelector: any): Array<T>;
31-
ThenByDescending(valueSelector: any): Array<T>;
32-
GroupBy(selector: any): Array<Array<T>>;
30+
OrderBy(valueSelector: ((item: T) => any) | string): Array<T>;
31+
ThenBy(valueSelector: ((item: T) => any) | string): Array<T>;
32+
OrderByDescending(valueSelector: ((item: T) => any) | string): Array<T>;
33+
ThenByDescending(valueSelector: ((item: T) => any) | string): Array<T>;
34+
Min(valueSelector?: ((item: T) => any) | string): T;
35+
Max(valueSelector?: ((item: T) => any) | string): T;
36+
GroupBy(selector: ((item: T) => any) | string): Array<Array<T>>;
3337
Move(oldIndex: number, newIndex: number): Array<T>;
34-
Distinct(valueSelector: any, takelast?: boolean): Array<T>;
38+
Distinct(valueSelector?: ((item: T) => any) | string): Array<T>;
3539
Contains(object: T): boolean;
3640
Concat(array: Array<T>): Array<T>;
37-
Join(character: string, selector?: any): string;
38-
Aggregate(method: any): string;
41+
Intersect(array: Array<T>): Array<T>;
42+
Join(character: string, selector?: ((item: T) => any) | string): string;
43+
Aggregate(method: ((result: any, item: T) => any) | string, startVal?: any): string;
3944
Reverse(): Array<T>;
40-
Average(selector?: any, filter?: any): number;
41-
Sum(selector?: any, filter?: any): number;
45+
Average(selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number;
46+
Sum(selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number;
47+
SequenceEqual(array: Array<T>): boolean;
48+
Zip<T, X>(array: Array<X>, result: ((first: T, second: X) => any) | string): Array<any>;
49+
Union(array: Array<T>): Array<T>;
50+
ToDictionary(keySelector: ((item: T) => any) | string, valueSelector?: ((item: T) => any) | string): any;
4251
}

dev/Modules/Aggregate.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
Array.prototype.Aggregate = function<T> (method: any): string {
1+
Array.prototype.Aggregate = function<T> (method: ((result: any, item: T) => any) | string, startVal?: any): string {
22
let that: Array<T> = this;
33

4-
let result: string = "";
5-
let methodFunction: Function = Linq4JS.Helper.ConvertFunction(method);
4+
let result;
5+
6+
if(startVal != null){
7+
result = startVal;
8+
}
9+
else{
10+
result = "";
11+
}
12+
13+
let methodFunction = Linq4JS.Helper.ConvertFunction<(result: any, item: T) => any>(method);
614

715
that.ForEach(function(x){
816
result = methodFunction(result, x);

dev/Modules/All.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Array.prototype.All = function<T> (filter: any): boolean {
1+
Array.prototype.All = function<T> (filter: ((item: T) => boolean) | string): boolean {
22
let that: Array<T> = this;
33

44
return that.Count(filter) == that.Count();

dev/Modules/Any.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Array.prototype.Any = function<T> (filter?: any): boolean {
1+
Array.prototype.Any = function<T> (filter?: ((item: T) => boolean) | string): boolean {
22
let that: Array<T> = this;
33

44
return that.Count(filter) > 0;

dev/Modules/Average.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Array.prototype.Average = function <T>(selector?: any, filter?: any): number {
1+
Array.prototype.Average = function <T>(selector?: ((item: T) => any) | string, filter?: ((item: T) => boolean) | string): number {
22
let that: Array<T> = this;
33

44
let result: number = 0;

dev/Modules/Count.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Array.prototype.Count = function <T>(filter?: any): number {
1+
Array.prototype.Count = function <T>(filter?: ((item: T) => boolean) | string): number {
22
let that: Array<T> = this;
33

44
if (filter != null) {

0 commit comments

Comments
 (0)