Skip to content

Commit 8bf1873

Browse files
committed
added groupBy
1 parent 75b4dee commit 8bf1873

File tree

9 files changed

+106
-17
lines changed

9 files changed

+106
-17
lines changed

dev/Entity.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
namespace Linq4JS {
2-
export class Entity implements Entity {
2+
export class Entity {
33
constructor(_id: number) {
44
this.Id = _id;
55
}

dev/IArray.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
ThenBy(valueSelector: any): Array<T>;
2929
OrderByDescending(valueSelector: any): Array<T>;
3030
ThenByDescending(valueSelector: any): Array<T>;
31+
GroupBy(selector: any): Array<Array<T>>;
3132
Move(oldIndex: number, newIndex: number): Array<T>;
3233
Distinct(valueSelector: any, takelast?: boolean): Array<T>;
3334
Contains(object: T): boolean;
@@ -36,4 +37,5 @@
3637
Aggregate(method: any): string;
3738
Reverse(): Array<T>;
3839
Average(selector?: any, filter?: any): number;
40+
Sum(selector?: any, filter?: any): number;
3941
}

dev/Modules/GroupBy.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Array.prototype.GroupBy = function<T> (selector: any): Array<Array<T>> {
2+
let that: Array<T> = this;
3+
4+
let selectorFunction: Function = Linq4JS.Helper.ConvertFunction(selector);
5+
6+
let newArray: Array<Array<T>> = new Array();
7+
8+
let ordered = that.OrderBy(selectorFunction);
9+
10+
let prev;
11+
let newSub = new Array();
12+
13+
ordered.ForEach(x => {
14+
if(prev != null){
15+
if(selectorFunction(prev) != selectorFunction(x)){
16+
newArray.Add(newSub);
17+
newSub = new Array();
18+
}
19+
else{
20+
newSub.Add(x);
21+
}
22+
}
23+
else{
24+
newSub.Add(x);
25+
prev = x;
26+
}
27+
});
28+
29+
if(newSub.Count() > 0){
30+
newArray.Add(newSub);
31+
}
32+
33+
return newArray;
34+
};

dev/Modules/Sum.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Array.prototype.Sum = 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;
20+
};

dist/Linq4JS.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
declare namespace Linq4JS {
2-
class Entity implements Entity {
2+
class Entity {
33
constructor(_id: number);
44
Id: number;
55
}
@@ -40,6 +40,7 @@ interface Array<T> {
4040
ThenBy(valueSelector: any): Array<T>;
4141
OrderByDescending(valueSelector: any): Array<T>;
4242
ThenByDescending(valueSelector: any): Array<T>;
43+
GroupBy(selector: any): Array<Array<T>>;
4344
Move(oldIndex: number, newIndex: number): Array<T>;
4445
Distinct(valueSelector: any, takelast?: boolean): Array<T>;
4546
Contains(object: T): boolean;
@@ -48,6 +49,7 @@ interface Array<T> {
4849
Aggregate(method: any): string;
4950
Reverse(): Array<T>;
5051
Average(selector?: any, filter?: any): number;
52+
Sum(selector?: any, filter?: any): number;
5153
}
5254
declare namespace Linq4JS {
5355
class OrderEntry {

dist/Linq4JS.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,33 @@ Array.prototype.Get = function (index) {
296296
var that = this;
297297
return that[index];
298298
};
299+
Array.prototype.GroupBy = function (selector) {
300+
var that = this;
301+
var selectorFunction = Linq4JS.Helper.ConvertFunction(selector);
302+
var newArray = new Array();
303+
var ordered = that.OrderBy(selectorFunction);
304+
var prev;
305+
var newSub = new Array();
306+
ordered.ForEach(function (x) {
307+
if (prev != null) {
308+
if (selectorFunction(prev) != selectorFunction(x)) {
309+
newArray.Add(newSub);
310+
newSub = new Array();
311+
}
312+
else {
313+
newSub.Add(x);
314+
}
315+
}
316+
else {
317+
newSub.Add(x);
318+
prev = x;
319+
}
320+
});
321+
if (newSub.Count() > 0) {
322+
newArray.Add(newSub);
323+
}
324+
return newArray;
325+
};
299326
Array.prototype.Insert = function (object, index) {
300327
var that = this;
301328
that.splice(index, 0, object);
@@ -443,6 +470,21 @@ Array.prototype.Skip = function (count) {
443470
var that = this;
444471
return that.slice(count, that.Count());
445472
};
473+
Array.prototype.Sum = function (selector, filter) {
474+
var that = this;
475+
var result = 0;
476+
var array = that;
477+
if (filter != null) {
478+
array = array.Where(filter);
479+
}
480+
if (selector != null) {
481+
array = array.Select(selector);
482+
}
483+
array.ForEach(function (x) {
484+
result += x;
485+
});
486+
return result;
487+
};
446488
Array.prototype.Take = function (count) {
447489
var that = this;
448490
return that.slice(0, count);

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.

gulpfile.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
1-
var gulp = require('gulp');
1+
var gulp = require("gulp");
22

33
var minify = require("gulp-minify");
4-
var concat = require("gulp-concat");
5-
var rename = require("gulp-rename");
6-
var less = require("gulp-less");
7-
var cleancss = require("gulp-clean-css");
84
var typescript = require("gulp-typescript");
9-
var autoprefixer = require("gulp-autoprefixer");
105
var merge = require("merge2");
116

127
var browsersync = require("browser-sync").create();
@@ -80,7 +75,7 @@ gulp.task("server", ["browsersync"], function () {
8075
gulp.watch("test/**/*.ts", ["testts"]);
8176
gulp.watch("dev/**/*.ts", ["typescript"]);
8277

83-
gulp.watch("**/*.html").on("change", browsersync.reload);
78+
gulp.watch("demo/**/*.html").on("change", browsersync.reload);
8479
gulp.watch("dist/**/*.js").on("change", browsersync.reload);
8580
gulp.watch("demo/**/*.js").on("change", browsersync.reload);
8681
gulp.watch("test/**/*.js").on("change", browsersync.reload);

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "linq4js",
3-
"version": "1.0.8",
3+
"version": "1.0.9",
44
"description": "Linq methods for JavaScript/TypeScript for working with Arrays",
55
"main": "dist/Linq4JS.js",
66
"typings": "dist/Linq4JS.d.ts",
@@ -27,14 +27,8 @@
2727
"devDependencies": {
2828
"browser-sync": "^2.18.6",
2929
"gulp": "^3.9.1",
30-
"gulp-autoprefixer": "^3.1.1",
31-
"gulp-clean-css": "^2.3.2",
32-
"gulp-concat": "^2.6.1",
3330
"gulp-connect": "^5.0.0",
34-
"gulp-connect-php": "0.0.8",
35-
"gulp-less": "^3.3.0",
3631
"gulp-minify": "0.0.14",
37-
"gulp-rename": "^1.2.2",
3832
"gulp-typescript": "^3.1.4",
3933
"merge2": "^1.0.3",
4034
"typescript": "^2.1.5"

0 commit comments

Comments
 (0)