Skip to content

Commit

Permalink
Update README and Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
jinhduong committed Apr 18, 2018
1 parent c6ade5a commit 7b7d8a0
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 27 deletions.
44 changes: 29 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,39 @@
import { Queryable } from "./implements/queryable";

const queryable = new Queryable<{ name, overall }>();
let source = [
{ name: 'Ronaldo', overall: 96 },
{ name: 'Messi', overall: 98 },
{ name: 'Mbappe', overall: 86 },
]

let query = queryable
.from(source)
.where(x => x.overall > 90);
let promiseApi = new Promise((resolve, reject) => {
// Fake data from api
setTimeout(() => {
console.log('...get data');
resolve([
{ name: 'Ronaldo', overall: 96 },
{ name: 'Messi', overall: 98 },
{ name: 'Mbappe', overall: 86 },
]);
}, 3000);
})

console.log(query.toList())
// [{name:'Ronaldo', overall: 96}, {name:'Messi', overall: 98}]
async function main() {
// Just query not execute query
let query = queryable
.from(promiseApi)
.where(x => x.overall > 90);

let query1 = query
.where(x => x.overall > 96)
.select(x => `Best player is ${x.name}`);
const count = await query.count();
console.log(count)
// 2

console.log(query1.toList())
// ['Best player is Messi']
let query1 = query
.where(x => x.overall > 96)
.select(x => `Best player is ${x.name}`);

const num1 = await query1.toList();
console.log(num1)

}

main();
```

### Process
Expand Down
8 changes: 7 additions & 1 deletion src/implements/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,13 @@ export class IteratorMethods<T> implements Methods<T> {
}

toList<S>(): Promise<S[]> {
if (this._data) return Promise.resolve(this._data as S[]);
if (this._data) {
let _result = Object.assign([], this._data as any[] | T[]);
this._iteratorCollection.forEach((ite: IIterator<T>) => {
_result = ite.execute(_result);
});
return Promise.resolve(_result as S[]);
}
if (Utils.isPromise(this._source)) {
return (this._source as Promise<T[]>).then(data => {
let _result = Object.assign([], data as any[] | T[]);
Expand Down
30 changes: 19 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,25 @@ let promiseApi = new Promise((resolve, reject) => {
}, 3000);
})

// Just query not execute query
let query = queryable
.from(promiseApi)
.where(x => x.overall > 90);
async function main() {
// Just query not execute query
let query = queryable
.from(promiseApi)
.where(x => x.overall > 90);

query.count().then(x => console.log(x))
// [{name:'Ronaldo', overall: 96}, {name:'Messi', overall: 98}]
const count = await query.count();
console.log(count)
// 2

let query1 = query
.where(x => x.overall > 96)
.select(x => `Best player is ${x.name}`);

const num1 = await query1.toList();
console.log(num1)

}

main();

let query1 = query
.where(x => x.overall > 96)
.select(x => `Best player is ${x.name}`);

query1.toList().then(res => console.log(res));
// ['Best player is Messi']

0 comments on commit 7b7d8a0

Please sign in to comment.