@koloseum/utils 0.1.8 → 0.1.9

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.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { error as svelteError } from "@sveltejs/kit";
2
- import parser from "any-date-parser";
3
2
  import sanitize from "sanitize-html";
4
3
  import validator from "validator";
5
4
  /* Helper functions */
@@ -52,18 +51,28 @@ export const Utility = {
52
51
  * @returns The formatted date string, or `null` if invalid input or in case of an error
53
52
  */
54
53
  formatDate: (date, forClient = false) => {
55
- if (date === "")
54
+ // Return null if no date is provided
55
+ if (date === "" || typeof date !== "string")
56
56
  return null;
57
+ // Handle input
57
58
  try {
59
+ // Parse date string
60
+ const parsedDate = Date.parse(date);
61
+ // Return null if date string is not parseable
62
+ if (isNaN(parsedDate))
63
+ return null;
64
+ // Use the Intl.DateTimeFormat with explicit options to ensure correct formatting
58
65
  const formattedDate = new Intl.DateTimeFormat(forClient ? "fr-CA" : "en-KE", {
59
66
  day: "2-digit",
60
67
  month: "2-digit",
61
- year: "numeric"
62
- // @ts-ignore: .fromString method exists but is not typed in `any-date-parser` package
63
- }).format(parser.fromString(date));
68
+ year: "numeric",
69
+ timeZone: "Africa/Nairobi"
70
+ }).format(parsedDate);
71
+ // Return formatted date string
64
72
  return formattedDate;
65
73
  }
66
74
  catch (error) {
75
+ console.log(error);
67
76
  return null;
68
77
  }
69
78
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.1.8",
3
+ "version": "0.1.9",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",
@@ -33,7 +33,6 @@
33
33
  "@koloseum/types": "^0.1.3",
34
34
  "@supabase/supabase-js": "^2.48.1",
35
35
  "@sveltejs/kit": "^2.17.1",
36
- "any-date-parser": "^2.0.3",
37
36
  "sanitize-html": "^2.14.0",
38
37
  "validator": "^13.12.0"
39
38
  },
package/src/utils.test.ts CHANGED
@@ -46,9 +46,9 @@ describe("Utility helper", () => {
46
46
  });
47
47
 
48
48
  it("formats dates", () => {
49
- expect(formatDate("31st December 2021")).toBe("31/12/2021");
50
- expect(formatDate("31st December 2021", true)).toBe("2021-12-31");
51
- expect(formatDate("31st December 2021", false)).toBe("31/12/2021");
49
+ expect(formatDate("31 December 2021")).toBe("31/12/2021");
50
+ expect(formatDate("31 December 2021", true)).toBe("2021-12-31");
51
+ expect(formatDate("31 December 2021", false)).toBe("31/12/2021");
52
52
  });
53
53
 
54
54
  it("formats social media platforms", () => {
package/src/utils.ts CHANGED
@@ -4,7 +4,6 @@ import type { MobilePhoneLocale } from "validator";
4
4
 
5
5
  import { error as svelteError } from "@sveltejs/kit";
6
6
 
7
- import parser from "any-date-parser";
8
7
  import sanitize from "sanitize-html";
9
8
  import validator from "validator";
10
9
 
@@ -62,17 +61,29 @@ export const Utility = {
62
61
  * @returns The formatted date string, or `null` if invalid input or in case of an error
63
62
  */
64
63
  formatDate: (date: string, forClient: boolean = false) => {
65
- if (date === "") return null;
64
+ // Return null if no date is provided
65
+ if (date === "" || typeof date !== "string") return null;
66
+
67
+ // Handle input
66
68
  try {
69
+ // Parse date string
70
+ const parsedDate = Date.parse(date);
71
+
72
+ // Return null if date string is not parseable
73
+ if (isNaN(parsedDate)) return null;
74
+
75
+ // Use the Intl.DateTimeFormat with explicit options to ensure correct formatting
67
76
  const formattedDate = new Intl.DateTimeFormat(forClient ? "fr-CA" : "en-KE", {
68
77
  day: "2-digit",
69
78
  month: "2-digit",
70
- year: "numeric"
71
- // @ts-ignore: .fromString method exists but is not typed in `any-date-parser` package
72
- }).format(parser.fromString(date));
79
+ year: "numeric",
80
+ timeZone: "Africa/Nairobi"
81
+ }).format(parsedDate);
73
82
 
83
+ // Return formatted date string
74
84
  return formattedDate;
75
85
  } catch (error) {
86
+ console.log(error);
76
87
  return null;
77
88
  }
78
89
  },