@indodev/toolkit 0.4.2 → 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);
@@ -754,24 +786,12 @@ function normalizePhoneNumber(phone) {
754
786
  }
755
787
  return "";
756
788
  }
757
- function normalizeToNational(phone) {
758
- if (phone.startsWith("+62")) {
759
- return "0" + phone.substring(3);
760
- }
761
- if (phone.startsWith("62") && !phone.startsWith("620")) {
762
- return "0" + phone.substring(2);
763
- }
764
- if (phone.startsWith("0")) {
765
- return phone;
766
- }
767
- return "";
768
- }
769
789
  function getLandlineRegion(phone) {
770
790
  if (!phone || typeof phone !== "string") {
771
791
  return null;
772
792
  }
773
793
  const cleaned = phone.replace(/[^\d+]/g, "");
774
- const normalized = normalizeToNational(cleaned);
794
+ const normalized = normalizePhoneNumber(cleaned);
775
795
  if (!normalized || !normalized.startsWith("0")) {
776
796
  return null;
777
797
  }
@@ -942,7 +962,7 @@ function maskPhoneNumber(phone, options = {}) {
942
962
 
943
963
  // src/phone/links.ts
944
964
  function generateWALink(phone, message) {
945
- if (!validatePhoneNumber(phone)) {
965
+ if (!validatePhoneNumber(phone) || !isMobileNumber(phone)) {
946
966
  return "";
947
967
  }
948
968
  const e164 = toE164(phone);
@@ -953,7 +973,7 @@ function generateWALink(phone, message) {
953
973
  return link;
954
974
  }
955
975
  function generateSmsLink(phone, body) {
956
- if (!validatePhoneNumber(phone)) {
976
+ if (!validatePhoneNumber(phone) || !isMobileNumber(phone)) {
957
977
  return "";
958
978
  }
959
979
  const e164 = toE164(phone);