@koloseum/utils 0.1.7 → 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.d.ts +1 -13
- package/dist/utils.js +14 -26
- package/package.json +2 -3
- package/src/utils.test.ts +3 -3
- package/src/utils.ts +17 -33
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import type { Database } from "@koloseum/types/database";
|
|
2
1
|
import type { CustomError, PronounsCheckboxes, SocialMediaPlatform } from "@koloseum/types/public/auth";
|
|
3
|
-
import type { PostgrestError
|
|
2
|
+
import type { PostgrestError } from "@supabase/supabase-js";
|
|
4
3
|
import type { MobilePhoneLocale } from "validator";
|
|
5
4
|
export declare const Status: {
|
|
6
5
|
/**
|
|
@@ -17,17 +16,6 @@ export declare const Status: {
|
|
|
17
16
|
PASSWORD_RESET_REQUESTED: string;
|
|
18
17
|
};
|
|
19
18
|
export declare const Utility: {
|
|
20
|
-
/**
|
|
21
|
-
* Calls an Edge function.
|
|
22
|
-
* @param {SupabaseClient} supabase - The Supabase client
|
|
23
|
-
* @param {string} path - The path to the Edge function
|
|
24
|
-
* @param {"GET" | "POST"} [method="GET"] - The HTTP method to use
|
|
25
|
-
* @returns The response from the Edge function
|
|
26
|
-
*/
|
|
27
|
-
callEdgeFunction: (supabase: SupabaseClient<Database>, path: string, method?: "GET" | "POST") => Promise<{
|
|
28
|
-
data?: any;
|
|
29
|
-
error?: string;
|
|
30
|
-
}>;
|
|
31
19
|
/**
|
|
32
20
|
* Capitalises a given word.
|
|
33
21
|
* @param {string} word - The word to be capitalised
|
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError } from "@supabase/supabase-js";
|
|
2
1
|
import { error as svelteError } from "@sveltejs/kit";
|
|
3
|
-
import parser from "any-date-parser";
|
|
4
2
|
import sanitize from "sanitize-html";
|
|
5
3
|
import validator from "validator";
|
|
6
4
|
/* Helper functions */
|
|
@@ -22,26 +20,6 @@ export const Status = {
|
|
|
22
20
|
};
|
|
23
21
|
/* Utility functions */
|
|
24
22
|
export const Utility = {
|
|
25
|
-
/**
|
|
26
|
-
* Calls an Edge function.
|
|
27
|
-
* @param {SupabaseClient} supabase - The Supabase client
|
|
28
|
-
* @param {string} path - The path to the Edge function
|
|
29
|
-
* @param {"GET" | "POST"} [method="GET"] - The HTTP method to use
|
|
30
|
-
* @returns The response from the Edge function
|
|
31
|
-
*/
|
|
32
|
-
callEdgeFunction: async (supabase, path, method = "GET") => {
|
|
33
|
-
// Fetch response
|
|
34
|
-
const { data, error } = await supabase.functions.invoke(path, { method });
|
|
35
|
-
// Return error if any
|
|
36
|
-
if (error instanceof FunctionsHttpError)
|
|
37
|
-
return { error: await error.context.json() };
|
|
38
|
-
else if (error instanceof FunctionsRelayError)
|
|
39
|
-
return { error: error.message };
|
|
40
|
-
else if (error instanceof FunctionsFetchError)
|
|
41
|
-
return { error: error.message };
|
|
42
|
-
// Return response
|
|
43
|
-
return { data };
|
|
44
|
-
},
|
|
45
23
|
/**
|
|
46
24
|
* Capitalises a given word.
|
|
47
25
|
* @param {string} word - The word to be capitalised
|
|
@@ -73,18 +51,28 @@ export const Utility = {
|
|
|
73
51
|
* @returns The formatted date string, or `null` if invalid input or in case of an error
|
|
74
52
|
*/
|
|
75
53
|
formatDate: (date, forClient = false) => {
|
|
76
|
-
if
|
|
54
|
+
// Return null if no date is provided
|
|
55
|
+
if (date === "" || typeof date !== "string")
|
|
77
56
|
return null;
|
|
57
|
+
// Handle input
|
|
78
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
|
|
79
65
|
const formattedDate = new Intl.DateTimeFormat(forClient ? "fr-CA" : "en-KE", {
|
|
80
66
|
day: "2-digit",
|
|
81
67
|
month: "2-digit",
|
|
82
|
-
year: "numeric"
|
|
83
|
-
|
|
84
|
-
}).format(
|
|
68
|
+
year: "numeric",
|
|
69
|
+
timeZone: "Africa/Nairobi"
|
|
70
|
+
}).format(parsedDate);
|
|
71
|
+
// Return formatted date string
|
|
85
72
|
return formattedDate;
|
|
86
73
|
}
|
|
87
74
|
catch (error) {
|
|
75
|
+
console.log(error);
|
|
88
76
|
return null;
|
|
89
77
|
}
|
|
90
78
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koloseum/utils",
|
|
3
|
-
"version": "0.1.
|
|
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)",
|
|
@@ -30,10 +30,9 @@
|
|
|
30
30
|
"test": "vitest --run"
|
|
31
31
|
},
|
|
32
32
|
"dependencies": {
|
|
33
|
-
"@koloseum/types": "^0.1.
|
|
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("
|
|
50
|
-
expect(formatDate("
|
|
51
|
-
expect(formatDate("
|
|
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
|
@@ -1,13 +1,9 @@
|
|
|
1
|
-
import type { Database } from "@koloseum/types/database";
|
|
2
1
|
import type { CustomError, PronounsCheckboxes, SocialMediaPlatform } from "@koloseum/types/public/auth";
|
|
3
|
-
|
|
4
|
-
import type { PostgrestError, SupabaseClient } from "@supabase/supabase-js";
|
|
2
|
+
import type { PostgrestError } from "@supabase/supabase-js";
|
|
5
3
|
import type { MobilePhoneLocale } from "validator";
|
|
6
4
|
|
|
7
|
-
import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError } from "@supabase/supabase-js";
|
|
8
5
|
import { error as svelteError } from "@sveltejs/kit";
|
|
9
6
|
|
|
10
|
-
import parser from "any-date-parser";
|
|
11
7
|
import sanitize from "sanitize-html";
|
|
12
8
|
import validator from "validator";
|
|
13
9
|
|
|
@@ -32,30 +28,6 @@ export const Status = {
|
|
|
32
28
|
|
|
33
29
|
/* Utility functions */
|
|
34
30
|
export const Utility = {
|
|
35
|
-
/**
|
|
36
|
-
* Calls an Edge function.
|
|
37
|
-
* @param {SupabaseClient} supabase - The Supabase client
|
|
38
|
-
* @param {string} path - The path to the Edge function
|
|
39
|
-
* @param {"GET" | "POST"} [method="GET"] - The HTTP method to use
|
|
40
|
-
* @returns The response from the Edge function
|
|
41
|
-
*/
|
|
42
|
-
callEdgeFunction: async (
|
|
43
|
-
supabase: SupabaseClient<Database>,
|
|
44
|
-
path: string,
|
|
45
|
-
method: "GET" | "POST" = "GET"
|
|
46
|
-
): Promise<{ data?: any; error?: string }> => {
|
|
47
|
-
// Fetch response
|
|
48
|
-
const { data, error } = await supabase.functions.invoke(path, { method });
|
|
49
|
-
|
|
50
|
-
// Return error if any
|
|
51
|
-
if (error instanceof FunctionsHttpError) return { error: await error.context.json() };
|
|
52
|
-
else if (error instanceof FunctionsRelayError) return { error: error.message };
|
|
53
|
-
else if (error instanceof FunctionsFetchError) return { error: error.message };
|
|
54
|
-
else if (error.message) return { error: error.message };
|
|
55
|
-
|
|
56
|
-
// Return response
|
|
57
|
-
return { data };
|
|
58
|
-
},
|
|
59
31
|
/**
|
|
60
32
|
* Capitalises a given word.
|
|
61
33
|
* @param {string} word - The word to be capitalised
|
|
@@ -89,17 +61,29 @@ export const Utility = {
|
|
|
89
61
|
* @returns The formatted date string, or `null` if invalid input or in case of an error
|
|
90
62
|
*/
|
|
91
63
|
formatDate: (date: string, forClient: boolean = false) => {
|
|
92
|
-
if
|
|
64
|
+
// Return null if no date is provided
|
|
65
|
+
if (date === "" || typeof date !== "string") return null;
|
|
66
|
+
|
|
67
|
+
// Handle input
|
|
93
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
|
|
94
76
|
const formattedDate = new Intl.DateTimeFormat(forClient ? "fr-CA" : "en-KE", {
|
|
95
77
|
day: "2-digit",
|
|
96
78
|
month: "2-digit",
|
|
97
|
-
year: "numeric"
|
|
98
|
-
|
|
99
|
-
}).format(
|
|
79
|
+
year: "numeric",
|
|
80
|
+
timeZone: "Africa/Nairobi"
|
|
81
|
+
}).format(parsedDate);
|
|
100
82
|
|
|
83
|
+
// Return formatted date string
|
|
101
84
|
return formattedDate;
|
|
102
85
|
} catch (error) {
|
|
86
|
+
console.log(error);
|
|
103
87
|
return null;
|
|
104
88
|
}
|
|
105
89
|
},
|