@koloseum/utils 0.2.4 → 0.2.6
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/utils.d.ts +5 -5
- package/dist/utils.js +50 -17
- package/package.json +1 -1
package/dist/utils.d.ts
CHANGED
|
@@ -85,18 +85,18 @@ export declare const Utility: {
|
|
|
85
85
|
formatSocialMediaPlatform: (platform: SocialMediaPlatform) => string;
|
|
86
86
|
/**
|
|
87
87
|
* Returns an age in years given a birth date.
|
|
88
|
-
* @param {string} dateOfBirth - The date of birth
|
|
88
|
+
* @param {Date | string | number | null} dateOfBirth - The date of birth
|
|
89
89
|
* @returns The age in years, or `NaN` if the input is invalid
|
|
90
90
|
*/
|
|
91
|
-
getAge: (dateOfBirth: string) => number;
|
|
91
|
+
getAge: (dateOfBirth: Date | string | number | null) => number;
|
|
92
92
|
/**
|
|
93
93
|
* Formats a date as a locale-specific string.
|
|
94
|
-
* @param {Date | string} date - Date to be formatted
|
|
94
|
+
* @param {Date | string | number | null} date - Date to be formatted
|
|
95
95
|
* @param {boolean} withTime - Specify whether the date should include time; defaults to `false`
|
|
96
96
|
* @param {string} timeZone - The time zone to use for formatting; defaults to `Africa/Nairobi`
|
|
97
|
-
* @returns The formatted date string, or
|
|
97
|
+
* @returns The formatted date string, or an empty string if invalid input
|
|
98
98
|
*/
|
|
99
|
-
getDateString: (date: Date | string | null, withTime?: boolean, timeZone?: string) => string
|
|
99
|
+
getDateString: (date: Date | string | number | null, withTime?: boolean, timeZone?: string) => string;
|
|
100
100
|
/**
|
|
101
101
|
* Formats an identity type for display.
|
|
102
102
|
* @param {IdentityType} identityType - The identity type to be formatted
|
package/dist/utils.js
CHANGED
|
@@ -230,44 +230,77 @@ export const Utility = {
|
|
|
230
230
|
},
|
|
231
231
|
/**
|
|
232
232
|
* Returns an age in years given a birth date.
|
|
233
|
-
* @param {string} dateOfBirth - The date of birth
|
|
233
|
+
* @param {Date | string | number | null} dateOfBirth - The date of birth
|
|
234
234
|
* @returns The age in years, or `NaN` if the input is invalid
|
|
235
235
|
*/
|
|
236
236
|
getAge: (dateOfBirth) => {
|
|
237
|
-
|
|
237
|
+
// Return NaN if no date is provided
|
|
238
|
+
if (!dateOfBirth)
|
|
238
239
|
return NaN;
|
|
239
|
-
|
|
240
|
-
|
|
240
|
+
// Validate and process date of birth
|
|
241
|
+
let birthDate;
|
|
242
|
+
if (dateOfBirth instanceof Date) {
|
|
243
|
+
if (isNaN(dateOfBirth.getTime()))
|
|
244
|
+
return NaN;
|
|
245
|
+
birthDate = dateOfBirth;
|
|
246
|
+
}
|
|
247
|
+
else if (typeof dateOfBirth === "string") {
|
|
248
|
+
if (dateOfBirth === "" || isNaN(Date.parse(dateOfBirth)))
|
|
249
|
+
return NaN;
|
|
250
|
+
// Format date of birth if it's a string
|
|
251
|
+
const formattedDate = Utility.formatDate(dateOfBirth, true);
|
|
252
|
+
if (!formattedDate)
|
|
253
|
+
return NaN;
|
|
254
|
+
birthDate = new Date(formattedDate);
|
|
255
|
+
}
|
|
256
|
+
else if (typeof dateOfBirth === "number") {
|
|
257
|
+
if (isNaN(dateOfBirth))
|
|
258
|
+
return NaN;
|
|
259
|
+
birthDate = new Date(dateOfBirth);
|
|
260
|
+
}
|
|
261
|
+
else {
|
|
262
|
+
return NaN;
|
|
263
|
+
}
|
|
264
|
+
// Validate the final date object
|
|
265
|
+
if (isNaN(birthDate.getTime()))
|
|
266
|
+
return NaN;
|
|
267
|
+
// Calculate age
|
|
241
268
|
const currentDate = new Date();
|
|
242
|
-
let age = currentDate.getFullYear() - birthDate.getFullYear();
|
|
243
269
|
const monthDiff = currentDate.getMonth() - birthDate.getMonth();
|
|
270
|
+
let age = currentDate.getFullYear() - birthDate.getFullYear();
|
|
271
|
+
// Return age
|
|
244
272
|
return monthDiff < 0 || (monthDiff === 0 && currentDate.getDate() < birthDate.getDate()) ? --age : age;
|
|
245
273
|
},
|
|
246
274
|
/**
|
|
247
275
|
* Formats a date as a locale-specific string.
|
|
248
|
-
* @param {Date | string} date - Date to be formatted
|
|
276
|
+
* @param {Date | string | number | null} date - Date to be formatted
|
|
249
277
|
* @param {boolean} withTime - Specify whether the date should include time; defaults to `false`
|
|
250
278
|
* @param {string} timeZone - The time zone to use for formatting; defaults to `Africa/Nairobi`
|
|
251
|
-
* @returns The formatted date string, or
|
|
279
|
+
* @returns The formatted date string, or an empty string if invalid input
|
|
252
280
|
*/
|
|
253
281
|
getDateString: (date, withTime = false, timeZone = "Africa/Nairobi") => {
|
|
254
|
-
// Return
|
|
282
|
+
// Return empty string if no date is provided
|
|
255
283
|
if (!date)
|
|
256
|
-
return
|
|
284
|
+
return "";
|
|
257
285
|
// Validate date to format
|
|
258
286
|
let dateToFormat;
|
|
259
|
-
if (
|
|
260
|
-
if (date === "" || isNaN(Date.parse(date)))
|
|
261
|
-
return null;
|
|
262
|
-
dateToFormat = new Date(date);
|
|
263
|
-
}
|
|
264
|
-
else if (date instanceof Date) {
|
|
287
|
+
if (date instanceof Date) {
|
|
265
288
|
if (isNaN(date.getTime()))
|
|
266
|
-
return
|
|
289
|
+
return "";
|
|
267
290
|
dateToFormat = date;
|
|
268
291
|
}
|
|
292
|
+
else if (typeof date === "string" || typeof date === "number") {
|
|
293
|
+
if (typeof date === "string" && (date === "" || isNaN(Date.parse(date))))
|
|
294
|
+
return "";
|
|
295
|
+
if (typeof date === "number" && isNaN(date))
|
|
296
|
+
return "";
|
|
297
|
+
dateToFormat = new Date(date);
|
|
298
|
+
}
|
|
269
299
|
else
|
|
270
|
-
return
|
|
300
|
+
return "";
|
|
301
|
+
// Validate the final date object
|
|
302
|
+
if (isNaN(dateToFormat.getTime()))
|
|
303
|
+
return "";
|
|
271
304
|
// Format date
|
|
272
305
|
return dateToFormat.toLocaleString("en-KE", {
|
|
273
306
|
timeZone,
|