Skip to content

Commit

Permalink
Groupby
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhduong committed Apr 23, 2018
1 parent ac22c73 commit 9c39b92
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
7 changes: 6 additions & 1 deletion src/implements/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import { OrderByClause } from "../methods/orderBy";
import { OrderByDescendingClause } from "../methods/orderByDescending";

export class IteratorMethods<T> implements Methods<T> {



// Contains all iterators
_iteratorCollection: Array<IIterator<T>> = [];

Expand Down Expand Up @@ -72,6 +73,10 @@ export class IteratorMethods<T> implements Methods<T> {
return this;
}

groupBy(iterator: (entity: T) => any): Methods<{ key: any; items: T[]; }> {
throw new Error("Method not implemented.");
}

toList<S>(): Promise<S[]> {

// From cache
Expand Down
1 change: 1 addition & 0 deletions src/intefaces/methods.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface Methods<T> {
orderByDescending(iterator: (entity: T) => T): Methods<T>;
join<S>(source: S[] | Promise<S[]>, iterator: (aEntity: T, bEntity: S) => boolean): Methods<{ x: T, y: S }>;
leftJoin<S, U extends T & S>(source: S[] | Promise<S[]>, iterator: (aEntity: T, bEntity: S) => boolean): Methods<U>;
groupBy(iterator: (entity: T) => any): Methods<{ key: any, items: T[] }>;

// Execute methods
toList<S extends T>(): Promise<S[]>;
Expand Down
2 changes: 1 addition & 1 deletion src/methods/any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export class AnyClause<T> implements IIterator<T> {
_iterator: (item: T) => boolean;

execute(source: T[] | any[]): T[] | T | any {
if (!source) return source.length;
if (!source) return false;
else {
return (source as T[]).every((x) => {
return this._iterator(x);
Expand Down
52 changes: 52 additions & 0 deletions src/methods/groupBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { IIterator } from "../intefaces/iterator.interface";
import { SelectClause } from "./select";

export class GroupByClause<T> implements IIterator<T> {

_iterator: (item: T) => any;

execute(source: T[]): T[] | any[] {

if (!source) return null;

// Object contains all data in array follow by [index] : {object}
let _tmp = {};

source.forEach((x: T, i: number) => {
_tmp[i] = x;
});

// Make lookup list base on input iterator function
let _result = (source as T[]).map((x: T, index: number) => {
return {
value: (this._iterator(x)),
index: index
}
});

let _distinctGroupByValues =
this.distinct(new SelectClause(this._iterator).execute(source));

// Prepare
let _keyLookup = {};
_distinctGroupByValues.forEach(key => {
_keyLookup[key] = [];
});

if (!_result) return null;

_result.forEach(lk => {
_keyLookup[lk.value] = _tmp[lk.index];
});
}

constructor(func: (item: T) => any) {
this._iterator = func;
}

private distinct(array: any[]) {
return array.filter((val, index, self) => {
return self.indexOf(val) === index;
});
}
}

0 comments on commit 9c39b92

Please sign in to comment.