Skip to content

Commit 8fd6a6d

Browse files
replaced moment with Date object (#5)
* replaced moment with Date object * some refactor * remove filesize --------- Co-authored-by: Erfanium <[email protected]>
1 parent 3eb02d5 commit 8fd6a6d

File tree

5 files changed

+63
-61
lines changed

5 files changed

+63
-61
lines changed

package-lock.json

Lines changed: 0 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
"fs-extra": "^11.2.0",
1616
"inquirer": "^9.2.23",
1717
"jwt-decode": "^4.0.0",
18-
"moment": "^2.30.1",
1918
"open": "^10.1.0"
2019
},
2120
"devDependencies": {

src/commands/offers/index.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1-
import CliTable from "cli-table3";
2-
31
import { AuthorizedCommand } from "../../command.js";
4-
import { TableCommon } from "../../table.js";
2+
import { makeTable } from "../../table.js";
53

64
export default class Offers extends AuthorizedCommand {
75
static description = "List all available offers";
86

97
async run(): Promise<void> {
108
const data = await this.api.getOffers();
119

12-
const orderByDuration = data
13-
.filter((item) => item.category !== "smart-bundle")
14-
15-
.sort((a, b) => {
16-
const aDuration = Number(a.duration);
17-
const bDuration = Number(b.duration);
18-
return aDuration - bDuration;
19-
});
20-
21-
const table = new CliTable({
22-
...TableCommon,
10+
const table = makeTable({
2311
head: ["Offer Code", "Title", "Price", "Per Gig Price"],
2412
colWidths: [15, 30, 15, 15],
13+
rows: data
14+
.filter((item) => item.category !== "smart-bundle")
15+
.sort((a, b) => {
16+
const aDuration = Number(a.duration);
17+
const bDuration = Number(b.duration);
18+
return aDuration - bDuration;
19+
})
20+
.map((offer) => [
21+
offer.offer_id,
22+
offer.title,
23+
Math.round(offer.price / 10_000).toLocaleString() + "T",
24+
offer.volume
25+
? (offer.price / 10_000 / (offer.volume / 1024)).toFixed(1) + "T"
26+
: "-",
27+
]),
2528
});
2629

27-
for (const offer of orderByDuration) {
28-
table.push([
29-
offer.offer_id,
30-
offer.title,
31-
Math.round(offer.price / 10_000).toLocaleString() + "T",
32-
offer.volume
33-
? (offer.price / 10_000 / (offer.volume / 1024)).toFixed(1) + "T"
34-
: "-",
35-
]);
36-
}
37-
3830
this.log(table.toString());
3931
}
4032
}

src/commands/status/index.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,37 @@
1-
import CliTable from "cli-table3";
2-
import moment from "moment";
3-
41
import { AuthorizedCommand } from "../../command.js";
5-
import { TableCommon } from "../../table.js";
2+
import { makeTable } from "../../table.js";
3+
4+
export const formatSize = (sizeInMegaBytes: number): string =>
5+
`${(sizeInMegaBytes / 1024).toFixed(2)} GB`;
66

77
export default class Status extends AuthorizedCommand {
88
static description = "Show account status";
99

1010
async run(): Promise<void> {
11-
const data = await this.api.getAccount();
12-
13-
const table = new CliTable({
14-
...TableCommon,
15-
head: ["Offer Code", "Name", "Expiry", "Data Used", "Data Remaining"],
16-
colWidths: [15, 30, 15, 15, 15],
11+
const rtf = new Intl.RelativeTimeFormat("en", {
12+
style: "long",
13+
numeric: "auto",
1714
});
1815

19-
for (const offer of data.active_offers) {
20-
const expiryDate = moment(offer.expiry_date);
21-
const now = moment();
22-
const daysRemaining = expiryDate.diff(now, "days");
23-
24-
const globalDataUsedGB = (offer.global_data_used / 1024).toFixed(2);
25-
const globalDataRemainingGB = (
26-
offer.global_data_remaining / 1024
27-
).toFixed(2);
16+
const data = await this.api.getAccount();
2817

29-
table.push([
18+
const table = makeTable({
19+
head: ["Offer Code", "Name", "Expiry", "Data Used / Remaining"],
20+
colWidths: [15, 30, 15, 30],
21+
rows: data.active_offers.map((offer) => [
3022
offer.offer_code,
3123
offer.name,
32-
`in ${daysRemaining} days`,
33-
globalDataUsedGB + " GB",
34-
globalDataRemainingGB + " GB",
35-
]);
36-
}
24+
rtf.format(
25+
Math.round(
26+
(Date.parse(offer.expiry_date) - Date.now()) / (1000 * 60 * 60 * 24)
27+
),
28+
"day"
29+
),
30+
`${formatSize(
31+
offer.total_amount - offer.remained_amount
32+
)} / ${formatSize(offer.total_amount)}`,
33+
]),
34+
});
3735

3836
this.log(table.toString());
3937
}

src/table.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
export const TableCommon = {
1+
import CliTable from "cli-table3";
2+
3+
const TableCommon = {
24
chars: {
35
top: "",
46
"top-mid": "",
@@ -18,3 +20,23 @@ export const TableCommon = {
1820
},
1921
style: { "padding-left": 0, "padding-right": 0 },
2022
};
23+
24+
export const makeTable = ({
25+
head,
26+
rows,
27+
colWidths,
28+
}: {
29+
head: string[];
30+
rows: string[][];
31+
colWidths: number[];
32+
}) => {
33+
const t = new CliTable({
34+
...TableCommon,
35+
head,
36+
colWidths,
37+
});
38+
39+
t.push(...rows);
40+
41+
return t;
42+
};

0 commit comments

Comments
 (0)