Skip to content

Commit

Permalink
replaced moment with Date object (#5)
Browse files Browse the repository at this point in the history
* replaced moment with Date object

* some refactor

* remove filesize

---------

Co-authored-by: Erfanium <[email protected]>
  • Loading branch information
MadGanGithub and erfanium authored Jun 18, 2024
1 parent 3eb02d5 commit 8fd6a6d
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 61 deletions.
9 changes: 0 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
"fs-extra": "^11.2.0",
"inquirer": "^9.2.23",
"jwt-decode": "^4.0.0",
"moment": "^2.30.1",
"open": "^10.1.0"
},
"devDependencies": {
Expand Down
42 changes: 17 additions & 25 deletions src/commands/offers/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,32 @@
import CliTable from "cli-table3";

import { AuthorizedCommand } from "../../command.js";
import { TableCommon } from "../../table.js";
import { makeTable } from "../../table.js";

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

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

const orderByDuration = data
.filter((item) => item.category !== "smart-bundle")

.sort((a, b) => {
const aDuration = Number(a.duration);
const bDuration = Number(b.duration);
return aDuration - bDuration;
});

const table = new CliTable({
...TableCommon,
const table = makeTable({
head: ["Offer Code", "Title", "Price", "Per Gig Price"],
colWidths: [15, 30, 15, 15],
rows: data
.filter((item) => item.category !== "smart-bundle")
.sort((a, b) => {
const aDuration = Number(a.duration);
const bDuration = Number(b.duration);
return aDuration - bDuration;
})
.map((offer) => [
offer.offer_id,
offer.title,
Math.round(offer.price / 10_000).toLocaleString() + "T",
offer.volume
? (offer.price / 10_000 / (offer.volume / 1024)).toFixed(1) + "T"
: "-",
]),
});

for (const offer of orderByDuration) {
table.push([
offer.offer_id,
offer.title,
Math.round(offer.price / 10_000).toLocaleString() + "T",
offer.volume
? (offer.price / 10_000 / (offer.volume / 1024)).toFixed(1) + "T"
: "-",
]);
}

this.log(table.toString());
}
}
48 changes: 23 additions & 25 deletions src/commands/status/index.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,37 @@
import CliTable from "cli-table3";
import moment from "moment";

import { AuthorizedCommand } from "../../command.js";
import { TableCommon } from "../../table.js";
import { makeTable } from "../../table.js";

export const formatSize = (sizeInMegaBytes: number): string =>
`${(sizeInMegaBytes / 1024).toFixed(2)} GB`;

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

async run(): Promise<void> {
const data = await this.api.getAccount();

const table = new CliTable({
...TableCommon,
head: ["Offer Code", "Name", "Expiry", "Data Used", "Data Remaining"],
colWidths: [15, 30, 15, 15, 15],
const rtf = new Intl.RelativeTimeFormat("en", {
style: "long",
numeric: "auto",
});

for (const offer of data.active_offers) {
const expiryDate = moment(offer.expiry_date);
const now = moment();
const daysRemaining = expiryDate.diff(now, "days");

const globalDataUsedGB = (offer.global_data_used / 1024).toFixed(2);
const globalDataRemainingGB = (
offer.global_data_remaining / 1024
).toFixed(2);
const data = await this.api.getAccount();

table.push([
const table = makeTable({
head: ["Offer Code", "Name", "Expiry", "Data Used / Remaining"],
colWidths: [15, 30, 15, 30],
rows: data.active_offers.map((offer) => [
offer.offer_code,
offer.name,
`in ${daysRemaining} days`,
globalDataUsedGB + " GB",
globalDataRemainingGB + " GB",
]);
}
rtf.format(
Math.round(
(Date.parse(offer.expiry_date) - Date.now()) / (1000 * 60 * 60 * 24)
),
"day"
),
`${formatSize(
offer.total_amount - offer.remained_amount
)} / ${formatSize(offer.total_amount)}`,
]),
});

this.log(table.toString());
}
Expand Down
24 changes: 23 additions & 1 deletion src/table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const TableCommon = {
import CliTable from "cli-table3";

const TableCommon = {
chars: {
top: "",
"top-mid": "",
Expand All @@ -18,3 +20,23 @@ export const TableCommon = {
},
style: { "padding-left": 0, "padding-right": 0 },
};

export const makeTable = ({
head,
rows,
colWidths,
}: {
head: string[];
rows: string[][];
colWidths: number[];
}) => {
const t = new CliTable({
...TableCommon,
head,
colWidths,
});

t.push(...rows);

return t;
};

0 comments on commit 8fd6a6d

Please sign in to comment.