-
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.
- Distinct - Concat - All - Update Any - Add first test
- Loading branch information
Showing
14 changed files
with
209 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { Queryable } from '../implements'; | ||
const cTable = require('console.table'); | ||
|
||
let players = new Promise<{ name, overall, nationId, skills }[]>((resolve, reject) => { | ||
// skills: attack, stamia, speed, shoot | ||
console.log('get players...'); | ||
setTimeout(() => { | ||
resolve([ | ||
{ 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, 81, 89, 81] }, | ||
{ name: 'Salah', overall: 89, nationId: 4, skills: [88, 82, 97, 86] } | ||
]); | ||
}, 1000); | ||
}) | ||
|
||
let nations = new Promise<{ id, name, areaId }[]>(resolve => { | ||
console.log('get nations...'); | ||
setTimeout(() => { | ||
resolve([ | ||
{ id: 1, name: 'Portugal', areaId: 1 }, | ||
{ id: 2, name: 'Argentina', areaId: 2 }, | ||
{ id: 3, name: 'France', areaId: 1 }, | ||
{ id: 4, name: 'Egypt', areaId: 3 } | ||
]); | ||
}, 2000); | ||
}) | ||
|
||
let continents = new Promise<{ id, areaName }[]>(resolve => { | ||
console.log('get continents...'); | ||
setTimeout(() => { | ||
resolve([ | ||
{ id: 1, areaName: 'Euro' }, | ||
{ id: 2, areaName: 'South America' }, | ||
]); | ||
}, 2300); | ||
}) | ||
|
||
function main() { | ||
let query10 = Queryable | ||
.from(players) | ||
.selectMany(x => x.skills) | ||
.distinct(); | ||
|
||
query10.toList().then(d => console.table(d)); | ||
|
||
let query9 = Queryable | ||
.from(players) | ||
.concat(players); | ||
|
||
query9.toList().then(d => console.table(d)); | ||
|
||
let query = Queryable | ||
.from(players) | ||
.where(x => x.overall > 85) | ||
.select(x => { | ||
return { | ||
name: x.name, | ||
stars: (() => { x.overall > 90 ? 5 : 4 })(), | ||
realOverall: Queryable.fromSync(x.skills).avarage(), | ||
nationId: x.nationId | ||
} | ||
}) | ||
.join(nations, (x, y) => x.nationId == y.id) | ||
.leftJoin(continents, (plNation, con) => plNation.y.areaId == con.id) | ||
.select(x => { | ||
return { | ||
name: x.x.name, | ||
stars: x.x.stars, | ||
realOverall: x.x.realOverall, | ||
nation: x.y.name, | ||
area: x.areaName | ||
} | ||
}) | ||
.groupBy(x => x.area) | ||
.select(x => { | ||
return { | ||
area: x.key, | ||
avg: (Queryable.fromSync(x.items).avarage(x => x.realOverall)) | ||
} | ||
}); | ||
|
||
query.toList().then(data => { | ||
console.table(data) | ||
}); | ||
} | ||
|
||
main(); | ||
|
||
|
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,21 @@ | ||
import { IIterator } from "../intefaces/iterator.interface"; | ||
import { BaseIterator } from "../implements/baseIterator"; | ||
|
||
export class AllClause<T> extends BaseIterator<T> implements IIterator<T> { | ||
|
||
_iterator: (item: T) => boolean; | ||
|
||
execute(source: T[] | any[]): T[] | T | any { | ||
if (!source) return false; | ||
else { | ||
return (source as T[]).every((x) => { | ||
return this._iterator(x); | ||
}); | ||
} | ||
} | ||
|
||
constructor(func: (item: T) => boolean) { | ||
super(); | ||
this._iterator = func; | ||
} | ||
} |
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,15 @@ | ||
import { BaseIterator } from '../implements'; | ||
import { IIterator } from '../intefaces'; | ||
|
||
export class ConcatClause<T> extends BaseIterator<T> implements IIterator<T>{ | ||
nextSource; | ||
|
||
execute(source: T[]): T[] { | ||
return source.concat(this.nextSource); | ||
} | ||
|
||
constructor(another: T[] | Promise<T[]>) { | ||
super(); | ||
this.nextSource = another; | ||
} | ||
} |
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,17 @@ | ||
import { IIterator } from '../intefaces'; | ||
import { BaseIterator } from '../implements'; | ||
import { Utils } from '../utils'; | ||
|
||
export class DistinctClause<T> extends BaseIterator<T> implements IIterator<T> { | ||
_comparer: (aEntity: T, bEntity: T) => boolean; | ||
|
||
execute(source: T[]): T[] { | ||
if (!this._comparer) | ||
return Utils.distinct(source); | ||
} | ||
|
||
constructor(comparer?: (aEntity: T, bEntity: T) => boolean) { | ||
super(); | ||
this._comparer = comparer; | ||
} | ||
} |
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 @@ | ||
export * from './object'; |
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,14 @@ | ||
import { players, continents, nations } from './source'; | ||
import { expect } from 'chai'; | ||
import { Queryable } from '../dist'; | ||
|
||
describe('distinct', () => { | ||
const query = Queryable.from(nations) | ||
.join(continents, (x, y) => x.areaId === y.id) | ||
.select(x => x.y.areaName); | ||
|
||
it('can run', async () => { | ||
const first = await query.distinct(); | ||
expect(first).to.not.equal(undefined); | ||
}) | ||
}); |
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,13 @@ | ||
import { players, continents, nations } from './source'; | ||
import { expect } from 'chai'; | ||
import { Queryable } from '../dist'; | ||
|
||
describe('first', () => { | ||
const query = Queryable.from(players); | ||
|
||
it('can run', async () => { | ||
const first = await query.first(); | ||
expect(first).to.not.equal(undefined); | ||
expect(first).to.not.equal(null); | ||
}) | ||
}); |