pos-be/src/modules/queue/domain/helpers/time.helper.ts

48 lines
1.2 KiB
TypeScript

import * as moment from 'moment';
export function toTime(timestamp: number): string {
const date = moment.unix(timestamp / 1000).add(7, 'hours');
const hours = date.hours();
const minutes = date.minutes();
return `${hours < 10 ? '0' : ''}${hours}:${
minutes < 10 ? '0' : ''
}${minutes}`;
}
export function phoneNumberOnly(phone: string): string {
return phone.replace(/[^0-9]/g, '');
}
export function sanitizePhoneNumber(phone: string): string {
if (!phone) return phone;
// Remove all non-numeric characters first
const cleanPhone = phone.replace(/[^0-9]/g, '');
// If the number starts with '0', replace it with '+62'
if (cleanPhone.startsWith('0')) {
return '+62' + cleanPhone.slice(1);
}
// If the number starts with '62', add '+' prefix
if (cleanPhone.startsWith('62')) {
return '+' + cleanPhone;
}
// If the number doesn't start with '62' and is not empty, assume it's a local number and add '+62'
if (cleanPhone.length > 0) {
return '+62' + cleanPhone;
}
// Return original if empty or invalid
return phone;
}
export function timeIsBefore(
currentTime: number,
time: number,
diffInMilliseconds: number,
): boolean {
return time < currentTime + diffInMilliseconds;
}