@koloseum/utils 0.2.9 → 0.2.11

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,10 +3,10 @@ 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"));
9
- const kenyaAdmin = new KenyaAdministrativeDivisions();
10
10
  /* DUMMY DATA */
11
11
  export const Data = {
12
12
  /**
@@ -336,6 +336,8 @@ export const Utility = {
336
336
  * @returns {Promise<County[]>} A list of objects with the county `name`, `code`, and a list of `subCounties`
337
337
  */
338
338
  getKenyaCounties: async (sortBy = "name") => {
339
+ // Create Kenya administrative divisions instance
340
+ const kenyaAdmin = new KenyaAdministrativeDivisions();
339
341
  // Get all counties
340
342
  const countiesData = await kenyaAdmin.getAll();
341
343
  // Format counties
@@ -1225,6 +1227,27 @@ export const Utility = {
1225
1227
  * @returns A new `Error` object if the error is unexpected, or a Svelte error otherwise
1226
1228
  */
1227
1229
  parseLoadError: (error) => error.code === 500 ? new Error(error.message) : svelteError(error.code, { message: error.message }),
1230
+ /**
1231
+ * Parses a Postgres interval string and returns a string in the specified format.
1232
+ * @param {string} interval - The interval string to parse
1233
+ * @param {"postgres" | "iso" | "iso-short"} type - The format to return the interval in; defaults to `postgres`
1234
+ * @returns A string in the specified format
1235
+ */
1236
+ parsePostgresInterval: (interval, type = "postgres") => {
1237
+ // Return null if interval is falsy
1238
+ if (!interval)
1239
+ return null;
1240
+ // Parse interval and return in the specified format
1241
+ const parsed = parsePgInterval(interval);
1242
+ if (type === "postgres")
1243
+ return parsed.toPostgres();
1244
+ if (type === "iso")
1245
+ return parsed.toISOString();
1246
+ if (type === "iso-short")
1247
+ return parsed.toISOStringShort();
1248
+ // Return null if type is invalid
1249
+ return null;
1250
+ },
1228
1251
  /**
1229
1252
  * Parses a `PostgrestError` object and returns a custom error object if any has occurred.
1230
1253
  * @param {PostgrestError | null} postgrestError - The `PostgrestError` object, or `null` if no error occurred
@@ -1324,6 +1347,8 @@ export const Utility = {
1324
1347
  * @returns An object with the validated `address`, or an `error` if any has occurred
1325
1348
  */
1326
1349
  validateAddress: async (formData) => {
1350
+ // Create Kenya administrative divisions instance
1351
+ const kenyaAdmin = new KenyaAdministrativeDivisions();
1327
1352
  // Building name
1328
1353
  let buildingName = formData.get("building-name");
1329
1354
  if (!buildingName)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koloseum/utils",
3
- "version": "0.2.9",
3
+ "version": "0.2.11",
4
4
  "author": "Koloseum Technologies Limited",
5
5
  "type": "module",
6
6
  "description": "Utility logic for use across Koloseum web apps (TypeScript)",