From 154a48159c4b27c835bf665a7548754a5d45142e Mon Sep 17 00:00:00 2001 From: MayurK-cmd Date: Thu, 27 Aug 2026 20:45:53 +0530 Subject: [PATCH] refactor: extract shared parseTimestamp utility to avoid duplication Both SpotCommand and PerpsCommand had duplicate parseTimestamp() methods with inconsistent behavior. Created shared DateConverter.parseTimestamp() utility to ensure consistent timestamp handling across all commands. Changes: - Create src/lib/DateConverter.ts with parseTimestamp() method - Update SpotCommand to use DateConverter.parseTimestamp() - Update PerpsCommand to use DateConverter.parseTimestamp() - Reduces code duplication by 2 implementations --- src/commands/PerpsCommand.ts | 10 ++-------- src/commands/SpotCommand.ts | 10 ++-------- src/lib/DateConverter.ts | 16 ++++++++++++++++ 3 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 src/lib/DateConverter.ts diff --git a/src/commands/PerpsCommand.ts b/src/commands/PerpsCommand.ts index 313872c..2e86304 100644 --- a/src/commands/PerpsCommand.ts +++ b/src/commands/PerpsCommand.ts @@ -5,6 +5,7 @@ import type { Command } from "commander"; import { PerpsClient } from "../clients/PerpsClient.ts"; import { Asset, resolveAsset } from "../lib/Asset.ts"; import { Config } from "../lib/Config.ts"; +import { DateConverter } from "../lib/DateConverter.ts"; import { NumberConverter } from "../lib/NumberConverter.ts"; import { Output } from "../lib/Output.ts"; import { Signer } from "../lib/Signer.ts"; @@ -904,14 +905,7 @@ export class PerpsCommand { } private static parseTimestamp(value: string): string { - if (/^\d+$/.test(value)) { - return value; - } - const ms = new Date(value).getTime(); - if (isNaN(ms)) { - throw new Error(`Invalid date: ${value}`); - } - return String(Math.floor(ms / 1000)); + return DateConverter.parseTimestamp(value); } private static async history(opts: { diff --git a/src/commands/SpotCommand.ts b/src/commands/SpotCommand.ts index ab28856..6255940 100644 --- a/src/commands/SpotCommand.ts +++ b/src/commands/SpotCommand.ts @@ -15,6 +15,7 @@ import { } from "../clients/UltraClient.ts"; import { Asset, resolveWalletAsset } from "../lib/Asset.ts"; import { Config } from "../lib/Config.ts"; +import { DateConverter } from "../lib/DateConverter.ts"; import { NumberConverter } from "../lib/NumberConverter.ts"; import { Output } from "../lib/Output.ts"; import { Signer } from "../lib/Signer.ts"; @@ -928,13 +929,6 @@ export class SpotCommand { } private static parseTimestamp(value: string): string { - if (/^\d+$/.test(value)) { - return new Date(Number(value) * 1000).toISOString(); - } - const ms = new Date(value).getTime(); - if (isNaN(ms)) { - throw new Error(`Invalid date: ${value}`); - } - return new Date(ms).toISOString(); + return DateConverter.parseTimestamp(value); } } diff --git a/src/lib/DateConverter.ts b/src/lib/DateConverter.ts new file mode 100644 index 0000000..a1baeaa --- /dev/null +++ b/src/lib/DateConverter.ts @@ -0,0 +1,16 @@ +export class DateConverter { + /** + * Parse a timestamp string (ISO date or UNIX timestamp) and return Unix seconds. + * Accepts both ISO 8601 date strings and numeric UNIX timestamps. + */ + public static parseTimestamp(value: string): string { + if (/^\d+$/.test(value)) { + return value; + } + const ms = new Date(value).getTime(); + if (isNaN(ms)) { + throw new Error(`Invalid date: ${value}`); + } + return String(Math.floor(ms / 1000)); + } +}