Skip to content

Commit 6618f05

Browse files
committed
updated readme, fixed groupby
1 parent 8bf1873 commit 6618f05

File tree

7 files changed

+52
-12
lines changed

7 files changed

+52
-12
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,26 @@ var array2 = [3, 4, 5];
332332
array2.Average();
333333
```
334334

335+
### Sum
336+
337+
Computes the sum of the elements
338+
339+
```javascript
340+
var array = [{val: 5}, {val: 3}, {val: 1}];
341+
342+
//9
343+
array.Sum("x => x.val");
344+
345+
//8
346+
array.Sum("x => x.val", "x => x.val > 1");
347+
348+
349+
var array2 = [3, 4, 5];
350+
351+
//12
352+
array2.Sum();
353+
```
354+
335355
### First
336356

337357
Returns the First item of the array and if a filter was set the first item that matches the filter - Throws an Exception if no item was found
@@ -436,6 +456,25 @@ var array = ["item1", "item2", "item3", "item4"];
436456
array.Skip(2);
437457
```
438458

459+
### GroupBy
460+
461+
Group Array by Property
462+
463+
```javascript
464+
var array = [
465+
{name: "Max", age: 17},
466+
{name: "Emily", age: 54},
467+
{name: "max", age: 32},
468+
{name: "emily", age: 12}
469+
];
470+
471+
//[
472+
// [{name: "Emily", age: 54},{name: "emily", age: 12}],
473+
// [{name: "Max", age: 17},{name: "max", age: 32}]
474+
//]
475+
array.GroupBy("i => i.name.toLowerCase()");
476+
```
477+
439478
### OrderBy & OrderByDescending
440479

441480
Order Array by Property or Value

dev/IArray.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interface Array<T> {
22
Order: Array<Linq4JS.OrderEntry>;
3+
GroupValue: any;
34

45
Clone(): Array<T>;
56
FindIndex(filter: any): number;

dev/Modules/GroupBy.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@
1515
if(selectorFunction(prev) != selectorFunction(x)){
1616
newArray.Add(newSub);
1717
newSub = new Array();
18-
}
19-
else{
20-
newSub.Add(x);
18+
newSub.GroupValue = selectorFunction(x);
2119
}
2220
}
2321
else{
24-
newSub.Add(x);
25-
prev = x;
22+
newSub.GroupValue = selectorFunction(x);
2623
}
24+
25+
newSub.Add(x);
26+
prev = x;
2727
});
2828

2929
if(newSub.Count() > 0){

dist/Linq4JS.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ declare namespace Linq4JS {
1313
}
1414
interface Array<T> {
1515
Order: Array<Linq4JS.OrderEntry>;
16+
GroupValue: any;
1617
Clone(): Array<T>;
1718
FindIndex(filter: any): number;
1819
Get(index: number): T;

dist/Linq4JS.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,14 @@ Array.prototype.GroupBy = function (selector) {
308308
if (selectorFunction(prev) != selectorFunction(x)) {
309309
newArray.Add(newSub);
310310
newSub = new Array();
311-
}
312-
else {
313-
newSub.Add(x);
311+
newSub.GroupValue = selectorFunction(x);
314312
}
315313
}
316314
else {
317-
newSub.Add(x);
318-
prev = x;
315+
newSub.GroupValue = selectorFunction(x);
319316
}
317+
newSub.Add(x);
318+
prev = x;
320319
});
321320
if (newSub.Count() > 0) {
322321
newArray.Add(newSub);

dist/Linq4JS.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linq4js",
3-
"version": "1.0.9",
3+
"version": "1.1.0",
44
"description": "Linq methods for JavaScript/TypeScript for working with Arrays",
55
"main": "dist/Linq4JS.js",
66
"typings": "dist/Linq4JS.d.ts",

0 commit comments

Comments
 (0)