Skip to content

Commit

Permalink
add shareable printHeader func
Browse files Browse the repository at this point in the history
  • Loading branch information
g-viet committed Jun 26, 2019
1 parent ea07033 commit 0dd72e3
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/stock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ export class Stock {
this.time = new Date();
}

static printfHeader() {
console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
}

printf () {
const volume = this.f(this.volume);
console.log(`${this.code}\t${this.f(this.currentPrice)}\t\t${volume}${volume.length >= 9 ? '\t' : '\t\t' }${this.f(this.openPrice)}\t\t${this.f(this.highestPrice)}\t\t${this.f(this.lowestPrice)}\t\t${this.time.toLocaleTimeString()}`);
Expand Down
5 changes: 3 additions & 2 deletions src/stock_api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Helper } from './helper';
import { Stock } from './stock';

const gets = (stockCodes: string[]) => {
console.log(`Stock\tPrice\t\tVolume\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
Stock.printfHeader();
try {
return Promise.all(stockCodes.map((code) => {
return Helper.fetchStock(code).then(stock => stock && stock.printf());
Expand All @@ -12,7 +13,7 @@ const gets = (stockCodes: string[]) => {
}

const stream = (stockCode: string) => {
console.log(`Stock\tPrice\t\tVolume\t\t\tOpenPrice\tHighestPrice\tLowestPrice\tTime`);
Stock.printfHeader();
setInterval(() => {
Helper.fetchStock(stockCode).then(stock => stock && stock.printf());
}, 2000);
Expand Down

0 comments on commit 0dd72e3

Please sign in to comment.