Skip to content

Commit

Permalink
SERVICES-2541: fixes after review
Browse files Browse the repository at this point in the history
- use regex for numeric string validation
- replace magic numbers with constants
  • Loading branch information
mad2sm0key committed Sep 10, 2024
1 parent 77320ad commit d46a377
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { BigNumber } from 'bignumber.js';
import { BinaryUtils } from '@multiversx/sdk-nestjs-common';
import moment from 'moment';

export const SECONDS_TIMESTAMP_LENGTH = 10;
export const MILLISECONDS_TIMESTAMP_LENGTH = 13;

export function encodeTransactionData(data: string): string {
const delimiter = '@';

Expand Down Expand Up @@ -63,18 +66,17 @@ export function delay(ms: number) {
}

export function isValidUnixTimestamp(value: string) {
const timestamp = Number(value);
if (isNaN(timestamp)) {
if (/^\d+$/.test(value) === false) {
return false;
}

// If the timestamp is in seconds (10 digits)
if (value.length === 10) {
const timestamp = Number(value);

if (value.length === SECONDS_TIMESTAMP_LENGTH) {
return moment.unix(timestamp).isValid();
}

// If the timestamp is in milliseconds (13 digits)
if (value.length === 13) {
if (value.length === MILLISECONDS_TIMESTAMP_LENGTH) {
return moment(timestamp).isValid();
}

Expand Down

0 comments on commit d46a377

Please sign in to comment.