@koloseum/utils 0.2.9 → 0.2.10

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 CHANGED
@@ -207,6 +207,13 @@ export declare const Utility: {
207
207
  * @returns A new `Error` object if the error is unexpected, or a Svelte error otherwise
208
208
  */
209
209
  parseLoadError: (error: CustomError) => Error | never;
210
+ /**
211
+ * Parses a Postgres interval string and returns a string in the specified format.
212
+ * @param {string} interval - The interval string to parse
213
+ * @param {"postgres" | "iso" | "iso-short"} type - The format to return the interval in; defaults to `postgres`
214
+ * @returns A string in the specified format
215
+ */
216
+ parsePostgresInterval: (interval: string, type?: "postgres" | "iso" | "iso-short") => string | null;
210
217
  /**
211
218
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
212
219
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
package/dist/utils.js CHANGED
@@ -3,6 +3,7 @@ import { error as svelteError } from "@sveltejs/kit";
3
3
  import { FunctionsFetchError, FunctionsHttpError, FunctionsRelayError } from "@supabase/supabase-js";
4
4
  import validator from "validator";
5
5
  /* HELPERS */
6
+ const parsePgInterval = (await import("postgres-interval")).default;
6
7
  const sanitize = (await import("sanitize-html")).default;
7
8
  const { trim, escape, isMobilePhone, isURL } = validator;
8
9
  const { KenyaAdministrativeDivisions } = (await import("kenya-administrative-divisions"));
@@ -1225,6 +1226,27 @@ export const Utility = {
1225
1226
  * @returns A new `Error` object if the error is unexpected, or a Svelte error otherwise
1226
1227
  */
1227
1228
  parseLoadError: (error) => error.code === 500 ? new Error(error.message) : svelteError(error.code, { message: error.message }),
1229
+ /**
1230
+ * Parses a Postgres interval string and returns a string in the specified format.
1231
+ * @param {string} interval - The interval string to parse
1232
+ * @param {"postgres" | "iso" | "iso-short"} type - The format to return the interval in; defaults to `postgres`
1233
+ * @returns A string in the specified format
1234
+ */
1235
+ parsePostgresInterval: (interval, type = "postgres") => {
1236
+ // Return null if interval is falsy
1237
+ if (!interval)
1238
+ return null;
1239
+ // Parse interval and return in the specified format
1240
+ const parsed = parsePgInterval(interval);
1241
+ if (type === "postgres")
1242
+ return parsed.toPostgres();
1243
+ if (type === "iso")
1244
+ return parsed.toISOString();
1245
+ if (type === "iso-short")
1246
+ return parsed.toISOStringShort();
1247
+ // Return null if type is invalid
1248
+ return null;
1249
+ },
1228
1250
  /**
1229
1251
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
1230
1252
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.2.9",
3
+ "version": "0.2.10",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",