@indodev/toolkit 0.5.0 → 0.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -218,29 +218,61 @@ function maskNIK(nik, options = {}) {
218
218
  }
219
219
 
220
220
  // src/nik/utils.ts
221
- function getAge(nik, referenceDate = /* @__PURE__ */ new Date()) {
221
+ function getAge(nik, options = {}) {
222
+ const { referenceDate = /* @__PURE__ */ new Date(), asString = false } = options;
222
223
  const info = parseNIK(nik);
223
224
  if (!info || !info.birthDate) {
224
225
  return null;
225
226
  }
226
227
  const birthDate = info.birthDate;
227
- let age = referenceDate.getFullYear() - birthDate.getFullYear();
228
- const m = referenceDate.getMonth() - birthDate.getMonth();
229
- if (m < 0 || m === 0 && referenceDate.getDate() < birthDate.getDate()) {
230
- age--;
231
- }
232
- return age;
233
- }
234
- function formatBirthDate(nik, options = {
235
- day: "numeric",
236
- month: "long",
237
- year: "numeric"
238
- }, locale = "id-ID") {
228
+ let years = referenceDate.getFullYear() - birthDate.getFullYear();
229
+ let months = referenceDate.getMonth() - birthDate.getMonth();
230
+ let days = referenceDate.getDate() - birthDate.getDate();
231
+ if (days < 0) {
232
+ months--;
233
+ const prevMonth = new Date(
234
+ referenceDate.getFullYear(),
235
+ referenceDate.getMonth(),
236
+ 0
237
+ );
238
+ days += prevMonth.getDate();
239
+ }
240
+ if (months < 0) {
241
+ years--;
242
+ months += 12;
243
+ }
244
+ if (asString) {
245
+ const parts = [];
246
+ if (years > 0) parts.push(`${years} Tahun`);
247
+ if (months > 0) parts.push(`${months} Bulan`);
248
+ if (days > 0 || parts.length === 0) parts.push(`${days} Hari`);
249
+ return parts.join(" ");
250
+ }
251
+ return { years, months, days };
252
+ }
253
+ function formatBirthDate(nik) {
239
254
  const info = parseNIK(nik);
240
255
  if (!info || !info.birthDate) {
241
256
  return null;
242
257
  }
243
- return new Intl.DateTimeFormat(locale, options).format(info.birthDate);
258
+ const day = info.birthDate.getDate();
259
+ const month = info.birthDate.getMonth();
260
+ const year = info.birthDate.getFullYear();
261
+ const monthNames = [
262
+ "Januari",
263
+ "Februari",
264
+ "Maret",
265
+ "April",
266
+ "Mei",
267
+ "Juni",
268
+ "Juli",
269
+ "Agustus",
270
+ "September",
271
+ "Oktober",
272
+ "November",
273
+ "Desember"
274
+ ];
275
+ return `${day} ${monthNames[month]} ${year}`;
244
276
  }
245
277
  function isValidForGender(nik, gender) {
246
278
  const info = parseNIK(nik);