-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}); | ||
} | ||
} |