-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.js
More file actions
168 lines (144 loc) · 4.36 KB
/
Copy pathutils.js
File metadata and controls
168 lines (144 loc) · 4.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
const crypto = require('crypto');
/**
* Generate unique invoice number
* Format: INV-YYYYMMDD-XXXXXX
*/
function generateInvoiceNumber() {
const date = new Date();
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
const random = Math.floor(Math.random() * 1000000).toString().padStart(6, '0');
return `INV-${year}${month}${day}-${random}`;
}
/**
* Generate unique code (1-999) for payment verification
*/
function generateUniqueCode() {
return Math.floor(Math.random() * 999) + 1;
}
/**
* Generate secure download token
*/
function generateDownloadToken() {
return crypto.randomBytes(32).toString('hex');
}
/**
* Generate product slug from name
*/
function generateSlug(text) {
return text
.toLowerCase()
.trim()
.replace(/[^\w\s-]/g, '')
.replace(/[\s_-]+/g, '-')
.replace(/^-+|-+$/g, '');
}
/**
* Format currency to IDR
*/
function formatCurrency(amount) {
return new Intl.NumberFormat('id-ID', {
style: 'currency',
currency: 'IDR',
minimumFractionDigits: 0
}).format(amount);
}
/**
* Format date to Indonesian format with WIB timezone
*/
function formatDate(dateString) {
const date = new Date(dateString);
return new Intl.DateTimeFormat('id-ID', {
day: '2-digit',
month: 'long',
year: 'numeric',
hour: '2-digit',
minute: '2-digit',
timeZone: 'Asia/Jakarta',
hour12: false
}).format(date);
}
/**
* Generate random product ID
*/
function generateProductId() {
return `PRD-${Date.now()}-${Math.random().toString(36).substr(2, 9).toUpperCase()}`;
}
/**
* Validate email format
*/
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
/**
* Sanitize phone number (remove non-numeric characters)
*/
function sanitizePhoneNumber(phone) {
if (!phone) return null;
let cleaned = phone.replace(/\D/g, '');
// Convert 08xx to 628xx
if (cleaned.startsWith('0')) {
cleaned = '62' + cleaned.substring(1);
}
// Ensure it starts with 62
if (!cleaned.startsWith('62')) {
cleaned = '62' + cleaned;
}
return cleaned;
}
/**
* Generate random string for various purposes
*/
function randomString(length = 8) {
return crypto.randomBytes(length).toString('hex').substring(0, length);
}
/**
* Format date to WIB timezone for views
* Usage in EJS: <%= formatDateWIB(date, options) %>
* IMPORTANT: Uses manual UTC+7 conversion for reliability across all browsers
*/
function formatDateWIB(dateString, options = {}) {
const date = new Date(dateString);
// Manual UTC to WIB conversion (UTC +7 hours)
const wibTime = new Date(date.getTime() + (7 * 60 * 60 * 1000));
const day = String(wibTime.getUTCDate()).padStart(2, '0');
const month = wibTime.getUTCMonth() + 1;
const year = wibTime.getUTCFullYear();
const hour = String(wibTime.getUTCHours()).padStart(2, '0');
const minute = String(wibTime.getUTCMinutes()).padStart(2, '0');
const monthNames = {
long: ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'],
short: ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun',
'Jul', 'Agu', 'Sep', 'Okt', 'Nov', 'Des']
};
// Build format based on options
let result = '';
if (options.day) result += day;
if (options.month === 'long') result += ` ${monthNames.long[month - 1]}`;
else if (options.month === 'short') result += ` ${monthNames.short[month - 1]}`;
else if (options.month === '2-digit') result += `.${String(month).padStart(2, '0')}`;
if (options.year === 'numeric') result += ` ${year}`;
else if (options.year === '2-digit') result += ` ${String(year).slice(-2)}`;
if (options.hour && options.minute) {
result += ` pukul ${hour}.${minute}`;
} else if (options.hour) {
result += ` ${hour}`;
}
return result.trim();
}
module.exports = {
generateInvoiceNumber,
generateUniqueCode,
generateDownloadToken,
generateSlug,
formatCurrency,
formatDate,
formatDateWIB,
generateProductId,
isValidEmail,
sanitizePhoneNumber,
randomString
};