Skip to content

Commit

Permalink
Temp
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhduong committed Apr 24, 2018
1 parent 1767fb4 commit 6ad29e4
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\dist\\out-tsc\\demo\\test.js",
"program": "${workspaceFolder}\\dist\\demo\\test.js",
"outFiles": [
"${workspaceFolder}/**/*.js"
]
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,12 @@ main();
- [ ] elementAtOrDefault
- [ ] orderByDescending
- [ ] defaultIfEmpty
- [ ] contains
- [x] contains
- [ ] reserve
- [ ] sequenceEqual
- [x] any : `Promise<boolean>`
- [ ] all
- [x] count : `Promise<number>`
- [ ] longCount
- [x] min : `Promise<number>`
- [x] max : `Promise<number>`
- [x] sum : `Promise<number>`
Expand Down
18 changes: 9 additions & 9 deletions src/demo/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ let promiseApi = new Promise((resolve, reject) => {
{ name: 'Ronaldo', overall: 96, nationId: 1, skills: [96, 85, 87, 91] },
{ name: 'Messi', overall: 98, nationId: 2, skills: [97, 85, 91, 93] },
{ name: 'Mbappe', overall: 86, nationId: 3, skills: [89, 81, 95, 83] },
{ name: 'Matial', overall: 81, nationId: 3, skills: [81, 80, 89, 81] },
{ name: 'Salah', overall: 89, nationId: 4, skills: [88, 82, 97, 86] }
]);
}, 1000);
Expand All @@ -32,18 +33,17 @@ function main() {
console.time('querytime');
let query = queryable
.from(promiseApi)
.join(nations, (pl, na) => pl.nationId === na.id)
.leftJoin(continents, (plNation, co) => plNation.y.areaId === co.id)
.select(o => {
.groupJoin(nations, (x, y) => x.nationId === y.id, x => x.y.name)
.select(x => {
return {
playerName: o.x.name,
overall: o.x.overall,
nation: o.y.name,
area: o.areaName
area: x.key,
players: new Queryable().from(x.items).select((pl: { x, y }) => {
return {
playerName: pl.x.name
}
}).toList()
}
})
.groupBy(x => x.area)
.orderByDescending(x => x.key);

console.timeEnd('querytime');

Expand Down
15 changes: 14 additions & 1 deletion src/implements/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,17 @@ import { AvarageClause } from "../methods/average";
import { OrderByClause } from "../methods/orderBy";
import { OrderByDescendingClause } from "../methods/orderByDescending";
import { GroupByClause } from "../methods/groupBy";
import { ContainsClause } from "../methods/contains";
import { GroupJoinClause } from "../methods/groupJoin";

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


groupJoin<S>(source: S[],
joinIterator: (aEntity: T, bEntity: S) => boolean,
groupIterator: (entity: { x: T; y: S; }) => any): Methods<{ key: any; items: T[]; }> {
this._iteratorCollection.push(new GroupJoinClause(source, joinIterator, groupIterator));
return this as any;
}
// Contains all iterators
_iteratorCollection: Array<IIterator<T>> = [];

Expand Down Expand Up @@ -196,6 +203,12 @@ export class IteratorMethods<T> implements Methods<T> {
});
}

contains(entity: T): Promise<boolean> {
return this.toList().then((data: T[]) => {
return new ContainsClause(entity).execute(data);
});
}

// Private funstions

private runIterators(syncSource: T[]) {
Expand Down
2 changes: 2 additions & 0 deletions src/intefaces/methods.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface 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[] }>;
groupJoin<S>(source: S[] | Promise<S[]>, joinIterator: (aEntity: T, bEntity: S) => boolean, groupIterator: (entity: { x: T, y: S }) => any): Methods<{ key: any, items: T[] }>;

// Execute methods
toList<S extends T>(): Promise<S[]>;
Expand All @@ -27,4 +28,5 @@ export interface Methods<T> {
max<S>(iterator: (entity: T) => S): Promise<number>;
avarage<S>(iterator: (entity: T) => S): Promise<number>;
any<T>(iterator: (entity: T) => boolean): Promise<boolean>;
contains(entity: T): Promise<boolean>;
}
25 changes: 25 additions & 0 deletions src/methods/contains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { IIterator } from "../intefaces/iterator.interface";

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

_entity: T;

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

if (!source) return false;

let _flag = false;
for (let i = 0, li = source.length; i < li; i++) {
if (source[i] === this._entity) {
_flag = true;
break;
}
}

return _flag;
}

constructor(entity: T) {
this._entity = entity;
}
}
39 changes: 39 additions & 0 deletions src/methods/groupJoin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { IIterator } from "../intefaces/iterator.interface";
import { SelectClause } from "./select";
import { JoinClause } from "./join";
import { GroupByClause } from "./groupBy";

export class GroupJoinClause<T, S> implements IIterator<T> {

_anotherSource: S[];
_joinIterator: (aEntity: T, bEntity: S) => boolean;
_groupIterator: (entity: { x: T; y: S; }) => any;

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

if (!source) return null;

let _result = new JoinClause(this._anotherSource, this._joinIterator).execute(source);

_result = new GroupByClause(this._groupIterator).execute(_result as { x: T; y: S; }[]);

return _result;

}

constructor(
anotherSource: S[],
joinIterator: (aEntity: T, bEntity: S) => boolean,
groupIterator: (entity: { x: T; y: S; }) => any) {

this._joinIterator = joinIterator;
this._groupIterator = groupIterator;
this._anotherSource = anotherSource;
}

private distinct(array: any[]) {
return array.filter((val, index, self) => {
return self.indexOf(val) === index;
}).filter(x => x != undefined);
}
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"outDir": "./dist/out-tsc",
"outDir": "./dist",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
Expand Down

0 comments on commit 6ad29e4

Please sign in to comment.