@turndown/library 0.0.10 → 0.0.16

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@turndown/library",
3
- "version": "0.0.10",
3
+ "version": "0.0.16",
4
4
  "description": "Shared TypeScript library for the Turndown suite.",
5
5
  "keywords": [
6
6
  "types",
@@ -16,20 +16,22 @@
16
16
  "access": "public"
17
17
  },
18
18
  "type": "module",
19
+ "main": "dist/index.js",
20
+ "module": "dist/index.js",
21
+ "source": "./index.ts",
22
+ "types": "./dist/index.d.ts",
23
+ "files": [
24
+ "src",
25
+ "dist"
26
+ ],
19
27
  "exports": {
20
28
  ".": {
21
29
  "types": "./dist/index.d.ts",
22
- "import": "./dist/index.js",
23
- "default": "./dist/index.js"
30
+ "browser": "./dist/index.js",
31
+ "import": "./dist/index.js"
24
32
  },
25
33
  "./package.json": "./package.json"
26
34
  },
27
- "types": "./dist/index.d.ts",
28
- "files": [
29
- "dist",
30
- "LICENSE",
31
- "README.md"
32
- ],
33
35
  "scripts": {
34
36
  "clean": "rimraf dist",
35
37
  "build": "npm run clean && tsc -p .",
@@ -0,0 +1,395 @@
1
+ import { FilterCondition, SortCondition, TurndownObject } from "@/types/base";
2
+
3
+ export interface Version {
4
+ major: number;
5
+ minor: number;
6
+ patch: number;
7
+ }
8
+
9
+ export type VersionInput = string | Version;
10
+
11
+ /**
12
+ * Safely parse a JSON string into a value.
13
+ *
14
+ * Returns `{}` if parsing fails instead of throwing.
15
+ *
16
+ * @param {TurndownObject} jsonString - The JSON string to parse.
17
+ * @returns {TurndownObject} Parsed value or `{}` on failure.
18
+ * @example
19
+ * parseJSON('{"a":1}') // => { a: 1 }
20
+ * parseJSON('not json') // => {}
21
+ */
22
+ export const parseJSON = (jsonString: TurndownObject): TurndownObject => {
23
+ try {
24
+ return JSON.parse(jsonString);
25
+ } catch (error) {
26
+ return {};
27
+ }
28
+ };
29
+
30
+ /**
31
+ * Stringify an object to JSON while skipping circular references.
32
+ *
33
+ * Uses an internal cache to omit repeated object references that would
34
+ * normally cause `JSON.stringify` to throw.
35
+ *
36
+ * @param {TurndownObject} obj - Value to stringify.
37
+ * @returns {string} JSON string with circulars omitted.
38
+ * @example
39
+ * const a:any = {}; a.self = a;
40
+ * JSONStringify(a) // => "{}"
41
+ */
42
+ export const JSONStringify = (obj: TurndownObject): string => {
43
+ let cache: TurndownObject = [];
44
+ let str = JSON.stringify(obj, function (_key, value) {
45
+ if (typeof value === "object" && value !== null) {
46
+ if (cache.indexOf(value) !== -1) {
47
+ return;
48
+ }
49
+ cache.push(value);
50
+ }
51
+ return value;
52
+ });
53
+ cache = null;
54
+ return str;
55
+ };
56
+
57
+ /**
58
+ * Deep-remove `undefined` properties by serializing & parsing.
59
+ *
60
+ * @param {TurndownObject} obj - Input object.
61
+ * @returns {TurndownObject} Cleaned clone with `undefined` removed.
62
+ */
63
+ export const removeUndefined = (obj: TurndownObject): TurndownObject => {
64
+ return JSON.parse(JSONStringify(obj));
65
+ };
66
+
67
+ /**
68
+ * Test whether a location object's `pathname` equals a key.
69
+ *
70
+ * @param {TurndownObject} location - Object expected to have a `pathname`.
71
+ * @param {string} key - Path to compare.
72
+ * @returns {boolean}
73
+ * @example
74
+ * validPath({ pathname: "/home" }, "/home") // true
75
+ */
76
+ export const validPath = (location: TurndownObject, key: string): boolean => {
77
+ return location?.pathname === key;
78
+ };
79
+
80
+ /**
81
+ * Return the first element if the input is an array; otherwise return the value itself.
82
+ *
83
+ * @typeParam T - Element type.
84
+ * @param {T | T[]} input - A single value or an array.
85
+ * @returns {T} First element or the input value.
86
+ * @example
87
+ * returnObject([1,2,3]) // 1
88
+ * returnObject(5) // 5
89
+ */
90
+ export const returnObject = <T>(input: T | T[]): T => {
91
+ return Array.isArray(input) ? input[0] : input;
92
+ };
93
+
94
+ /**
95
+ * Filter out items from `array1` whose `id` appears in `array2`.
96
+ *
97
+ * @typeParam T - Object type with an `id` field.
98
+ * @param {T[]} [array1] - Source array.
99
+ * @param {T[]} [array2] - Items whose `id`s should be excluded.
100
+ * @returns {T[]} Filtered array (or `[]` on errors/invalid input).
101
+ */
102
+ export const filterArrayById = <T extends { id: number | string }>(
103
+ array1?: T[],
104
+ array2?: T[]
105
+ ): T[] => {
106
+ try {
107
+ if (!array1 || !array2) return [];
108
+
109
+ const idsToExclude = new Set(array2.map((item) => item.id));
110
+ return array1.filter((item) => !idsToExclude.has(item.id));
111
+ } catch (error) {
112
+ console.error("An error occurred:", error);
113
+ return [];
114
+ }
115
+ };
116
+
117
+ /**
118
+ * Sort an array of objects by a given property (ascending).
119
+ *
120
+ * Mutates the original array (uses `Array.prototype.sort`).
121
+ *
122
+ * @typeParam T - Object type.
123
+ * @param {T[]} array - Array to sort.
124
+ * @param {keyof T} property - Property name to sort by.
125
+ * @returns {T[]} The same array instance, sorted (or empty array if input invalid).
126
+ */
127
+ export const sortArrayByProperty = <T extends Record<string, any>>(
128
+ array: T[],
129
+ property: keyof T
130
+ ): T[] => {
131
+ if (!array || array.length === 0) return [];
132
+
133
+ return array.sort((a, b) => {
134
+ if (a[property] < b[property]) return -1;
135
+ if (a[property] > b[property]) return 1;
136
+ return 0;
137
+ });
138
+ };
139
+
140
+ /**
141
+ * Recursively replace `null` values with empty strings.
142
+ *
143
+ * Works on primitives, arrays, and plain objects.
144
+ *
145
+ * @param {TurndownObject} obj - Input value.
146
+ * @returns {TurndownObject} Value with all `null` replaced by `""`.
147
+ */
148
+ export const replaceNulls = (obj: TurndownObject): TurndownObject => {
149
+ if (obj === null) {
150
+ return "";
151
+ } else if (Array.isArray(obj)) {
152
+ return obj.map(replaceNulls);
153
+ } else if (typeof obj === "object" && obj !== null) {
154
+ const newObj: any = {};
155
+ for (const key in obj) {
156
+ if (obj.hasOwnProperty(key)) {
157
+ newObj[key] = replaceNulls(obj[key]);
158
+ }
159
+ }
160
+ return newObj;
161
+ }
162
+ return obj;
163
+ };
164
+
165
+ /**
166
+ * Recursively remove object keys that contain a dot (`.`).
167
+ *
168
+ * @typeParam T - Object type.
169
+ * @param {T} obj - Input object.
170
+ * @returns {T} New object with dotted keys removed at all levels.
171
+ */
172
+ export const removeFormProperties = <T extends Record<string, TurndownObject>>(
173
+ obj: T
174
+ ): T => {
175
+ const recursiveRemove = (input: TurndownObject): TurndownObject => {
176
+ if (Array.isArray(input)) {
177
+ return input.map((item) => recursiveRemove(item));
178
+ } else if (typeof input === "object" && input !== null) {
179
+ const newObj: Record<string, TurndownObject> = { ...input };
180
+
181
+ Object.keys(newObj).forEach((key) => {
182
+ if (key.includes(".")) {
183
+ delete newObj[key];
184
+ } else {
185
+ newObj[key] = recursiveRemove(newObj[key]);
186
+ }
187
+ });
188
+ return newObj;
189
+ }
190
+ return input;
191
+ };
192
+
193
+ return recursiveRemove(obj);
194
+ };
195
+
196
+ /**
197
+ * Recursively convert string booleans `"true"`/`"false"` to actual booleans.
198
+ *
199
+ * Leaves all other values unchanged.
200
+ *
201
+ * @typeParam T - Object type.
202
+ * @param {T} obj - Input object or array.
203
+ * @returns {T} New value with boolean-like strings converted.
204
+ */
205
+ export const convertStringBooleans = <T extends Record<string, any>>(
206
+ obj: T
207
+ ): T => {
208
+ const recursiveConvert = (input: any): any => {
209
+ if (Array.isArray(input)) {
210
+ return input.map((item) => recursiveConvert(item));
211
+ } else if (typeof input === "object" && input !== null) {
212
+ const newObj: Record<string, any> = { ...input };
213
+
214
+ Object.keys(newObj).forEach((key) => {
215
+ const value = newObj[key];
216
+
217
+ if (value === "true") {
218
+ newObj[key] = true;
219
+ } else if (value === "false") {
220
+ newObj[key] = false;
221
+ } else if (typeof value === "object" && value !== null) {
222
+ newObj[key] = recursiveConvert(value);
223
+ }
224
+ });
225
+
226
+ return newObj;
227
+ }
228
+
229
+ return input;
230
+ };
231
+
232
+ return recursiveConvert(obj);
233
+ };
234
+
235
+ /**
236
+ * Convenience helper to clean form-like data:
237
+ * - Removes `undefined` properties
238
+ * - Converts string booleans to booleans
239
+ * - Removes keys containing a dot ('.')
240
+ *
241
+ * @param {TurndownObject} obj - Input data.
242
+ * @returns {TurndownObject} Cleaned clone.
243
+ */
244
+ export const cleanFormData = (obj: TurndownObject) => {
245
+ return removeFormProperties(convertStringBooleans(removeUndefined(obj)));
246
+ };
247
+
248
+ /**
249
+ * Return a default pagination object, allowing optional sort and filters.
250
+ *
251
+ * @param {SortCondition[]} [sort] - Optional sort conditions.
252
+ * @param {FilterCondition[]} [filters] - Optional filter conditions.
253
+ * @returns {{ page: number; size: number; sort: SortCondition[]; filters: FilterCondition[] }}
254
+ * @example
255
+ * resetPagination() // => { page:1, size:25, sort:[], filters:[] }
256
+ */
257
+ export const resetPagination = (
258
+ sort?: SortCondition[],
259
+ filters?: FilterCondition[]
260
+ ) => {
261
+ return {
262
+ page: 1,
263
+ size: 25,
264
+ sort: sort || [],
265
+ filters: filters || [],
266
+ };
267
+ };
268
+
269
+ /**
270
+ * Format a string of digits into a U.S. phone number.
271
+ *
272
+ * Strips non-numeric characters and formats as `(XXX) XXX-XXXX`.
273
+ * If fewer than 10 digits are provided, returns the input unchanged.
274
+ *
275
+ * @param {string | number} value - Phone number digits (string or number).
276
+ * @returns {string} Formatted phone number, or original input if invalid length.
277
+ * @example
278
+ * formatPhoneNumber("1234567890") // "(123) 456-7890"
279
+ * formatPhoneNumber(9876543210) // "(987) 654-3210"
280
+ * formatPhoneNumber("555") // "555"
281
+ */
282
+ export const formatPhoneNumber = (value: string | number): string => {
283
+ const digits = value.toString().replace(/\D/g, "");
284
+ if (digits.length !== 10) return value.toString();
285
+
286
+ const area = digits.slice(0, 3);
287
+ const prefix = digits.slice(3, 6);
288
+ const line = digits.slice(6);
289
+
290
+ return `(${area}) ${prefix}-${line}`;
291
+ };
292
+
293
+ /**
294
+ * Format a number with thousands separators (commas).
295
+ *
296
+ * @param {number} value - Number to format.
297
+ * @returns {string} String with commas.
298
+ * @example
299
+ * formatNumber(1234567) // "1,234,567"
300
+ */
301
+ export const formatNumber = (value: number): string => {
302
+ return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
303
+ };
304
+
305
+ /**
306
+ * Parse a number string (note: current implementation adds commas as well).
307
+ *
308
+ * @remarks
309
+ * This function uses the same regex as `formatNumber`, so it **does not remove**
310
+ * commas; it inserts them. If you intended to *strip* separators, consider:
311
+ * `value.toString().replace(/,/g, "")`.
312
+ *
313
+ * @param {number} value - Number to "parse".
314
+ * @returns {string} Currently returns a comma-formatted string.
315
+ */
316
+ export const parseNumber = (value: number): string => {
317
+ return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "");
318
+ };
319
+
320
+ /**
321
+ * Delete a property from an object if it exists (no-op if it doesn't).
322
+ *
323
+ * @param {TurndownObject} obj - Target object (mutated).
324
+ * @param {string} propertyName - Property to delete.
325
+ * @returns {void}
326
+ */
327
+ export const deletePropertyIfExists = (
328
+ obj: TurndownObject,
329
+ propertyName: string
330
+ ) => {
331
+ if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {
332
+ delete obj[propertyName];
333
+ }
334
+ };
335
+
336
+ /**
337
+ * Split an array into chunks of a given size.
338
+ *
339
+ * @typeParam T - Element type.
340
+ * @param {T[]} array - Source array.
341
+ * @param {number} chunkSize - Size of each chunk (no validation performed).
342
+ * @returns {T[][]} Array of chunks (last one may be smaller).
343
+ * @example
344
+ * chunkArray([1,2,3,4,5], 2) // [[1,2],[3,4],[5]]
345
+ */
346
+ export const chunkArray = <T>(array: T[], chunkSize: number): T[][] => {
347
+ const result: T[][] = [];
348
+ for (let i = 0; i < array.length; i += chunkSize) {
349
+ result.push(array.slice(i, i + chunkSize));
350
+ }
351
+ return result;
352
+ };
353
+
354
+ /**
355
+ * Return a shallow clone of `obj` without the listed properties.
356
+ *
357
+ * @param {TurndownObject} obj - Source object.
358
+ * @param {TurndownObject} propsToOmit - Iterable of property names (expects array-like).
359
+ * @returns {TurndownObject} New object without omitted props.
360
+ * @example
361
+ * omitProperties({a:1,b:2}, ["b"]) // { a:1 }
362
+ */
363
+ export const omitProperties = (
364
+ obj: TurndownObject,
365
+ propsToOmit: TurndownObject
366
+ ) => {
367
+ const newObj = { ...obj };
368
+ propsToOmit.forEach((prop: TurndownObject) => {
369
+ delete newObj[prop];
370
+ });
371
+
372
+ return newObj;
373
+ };
374
+
375
+ /**
376
+ * Safe `hasOwnProperty` check.
377
+ *
378
+ * @param {Record<string, any>} obj - Object to test.
379
+ * @param {string} key - Property name.
380
+ * @returns {boolean}
381
+ */
382
+ export const hasProperty = (obj: Record<string, any>, key: string): boolean => {
383
+ if (!obj) return false;
384
+ return Object.prototype.hasOwnProperty.call(obj, key);
385
+ };
386
+
387
+ /**
388
+ * Determine if an object has at least one own enumerable property.
389
+ *
390
+ * @param {object} obj - Object to test.
391
+ * @returns {boolean} `true` if there is at least one key.
392
+ */
393
+ export const hasProperties = (obj: object): boolean => {
394
+ return Object.keys(obj || {}).length > 0;
395
+ };
@@ -0,0 +1,171 @@
1
+ import { TurndownObject } from "@/types/base";
2
+
3
+ /**
4
+ * Convert a string to "Normal Case":
5
+ * - Inserts spaces between camel/pascal case boundaries
6
+ * - Capitalizes the first letter of each word, lowercases the rest
7
+ *
8
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
9
+ * @returns {string}
10
+ * @example
11
+ * normalCase("helloWorld") // "Hello World"
12
+ * normalCase("XMLHttpRequest") // "Xml Http Request"
13
+ */
14
+ export const normalCase = (str?: TurndownObject): string => {
15
+ if (!str) return "";
16
+
17
+ return str
18
+ .replace(/([a-z])([A-Z])/g, "$1 $2")
19
+ .replace(/([A-Z])([A-Z][a-z])/g, "$1 $2")
20
+ .split(/\s+/)
21
+ .map(
22
+ (word: string) =>
23
+ word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
24
+ )
25
+ .join(" ");
26
+ };
27
+
28
+ /**
29
+ * Capitalize only the first character; lowercases the rest.
30
+ * Trims leading/trailing spaces before processing.
31
+ *
32
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
33
+ * @returns {string}
34
+ * @example
35
+ * sentenceCase("hELLO WORLD") // "Hello world"
36
+ */
37
+ export const sentenceCase = (str?: TurndownObject): string => {
38
+ if (!str) return "";
39
+
40
+ const s = str.trim();
41
+ return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
42
+ };
43
+
44
+ /**
45
+ * Uppercase the entire string.
46
+ *
47
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
48
+ * @returns {string}
49
+ * @example
50
+ * upperCase("Hello world") // "HELLO WORLD"
51
+ */
52
+ export const upperCase = (str?: TurndownObject): string => {
53
+ return str ? str.toUpperCase() : "";
54
+ };
55
+
56
+ /**
57
+ * Lowercase the entire string.
58
+ *
59
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
60
+ * @returns {string}
61
+ * @example
62
+ * lowerCase("Hello WORLD") // "hello world"
63
+ */
64
+ export const lowerCase = (str?: TurndownObject): string => {
65
+ return str ? str.toLowerCase() : "";
66
+ };
67
+
68
+ /**
69
+ * Convert to camelCase.
70
+ * Splits on spaces, underscores, and hyphens; lowercases the first word,
71
+ * TitleCases the rest, then joins with no separators.
72
+ *
73
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
74
+ * @returns {string}
75
+ * @example
76
+ * camelCase("Hello world") // "helloWorld"
77
+ * camelCase("hello_world-again") // "helloWorldAgain"
78
+ */
79
+ export const camelCase = (str?: TurndownObject): string => {
80
+ if (!str) return "";
81
+ return str
82
+ .toLowerCase()
83
+ .split(/[\s_-]+/)
84
+ .map((word: string, i: number) =>
85
+ i === 0
86
+ ? word
87
+ : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
88
+ )
89
+ .join("");
90
+ };
91
+
92
+ /**
93
+ * Convert to PascalCase.
94
+ * Splits on spaces, underscores, and hyphens; TitleCases all words and joins them.
95
+ *
96
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
97
+ * @returns {string}
98
+ * @example
99
+ * pascalCase("hello world") // "HelloWorld"
100
+ * pascalCase("hello_world-again") // "HelloWorldAgain"
101
+ */
102
+ export const pascalCase = (str?: TurndownObject): string => {
103
+ if (!str) return "";
104
+ return str
105
+ .toLowerCase()
106
+ .split(/[\s_-]+/)
107
+ .map((word: string) => word.charAt(0).toUpperCase() + word.slice(1))
108
+ .join("");
109
+ };
110
+
111
+ /**
112
+ * Convert to kebab-case.
113
+ * Inserts hyphens between camelCase boundaries, then replaces spaces/underscores with hyphens.
114
+ *
115
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
116
+ * @returns {string}
117
+ * @example
118
+ * kebabCase("HelloWorld Again") // "hello-world-again"
119
+ * kebabCase("hello_world") // "hello-world"
120
+ */
121
+ export const kebabCase = (str?: TurndownObject): string => {
122
+ if (!str) return "";
123
+ return str
124
+ .replace(/([a-z])([A-Z])/g, "$1-$2")
125
+ .replace(/[\s_]+/g, "-")
126
+ .toLowerCase();
127
+ };
128
+
129
+ /**
130
+ * Convert to snake_case.
131
+ * Inserts underscores between camelCase boundaries, then replaces spaces/hyphens with underscores.
132
+ *
133
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
134
+ * @returns {string}
135
+ * @example
136
+ * snakeCase("HelloWorld Again") // "hello_world_again"
137
+ * snakeCase("hello-world") // "hello_world"
138
+ */
139
+ export const snakeCase = (str?: TurndownObject): string => {
140
+ if (!str) return "";
141
+ return str
142
+ .replace(/([a-z])([A-Z])/g, "$1_$2")
143
+ .replace(/[\s-]+/g, "_")
144
+ .toLowerCase();
145
+ };
146
+
147
+ /**
148
+ * Convert snake_case to space-delimited words.
149
+ *
150
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
151
+ * @returns {string}
152
+ * @example
153
+ * snakeCaseToSpaces("hello_world_again") // "hello world again"
154
+ */
155
+ export const snakeCaseToSpaces = (str?: TurndownObject): string => {
156
+ if (!str) return "";
157
+ return str.split("_").join(" ");
158
+ };
159
+
160
+ /**
161
+ * Convert kebab-case to space-delimited words.
162
+ *
163
+ * @param {TurndownObject} [str] Input value (falsy returns an empty string)
164
+ * @returns {string}
165
+ * @example
166
+ * kebabToSpaces("hello-world-again") // "hello world again"
167
+ */
168
+ export const kebabToSpaces = (str?: TurndownObject): string => {
169
+ if (!str) return "";
170
+ return str.split("-").join(" ");
171
+ };
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./helpers";
2
+ export * from "./types";
@@ -0,0 +1,26 @@
1
+ export type GenericTurndownObject = Record<string, any> | any | undefined;
2
+ export type TurndownObject<T = GenericTurndownObject> =
3
+ | Record<string, T>
4
+ | T
5
+ | undefined;
6
+
7
+ export type ObjectValues<T> = T[keyof T];
8
+ export type ObjectKeys<T> = [keyof T];
9
+
10
+ export interface MetaData {
11
+ company_id: string;
12
+ deleted: boolean;
13
+ updated_by: string;
14
+ created_by: string;
15
+ created_at: Date;
16
+ updated_at: Date;
17
+ }
18
+
19
+ export const STATUS = {
20
+ ACTIVE: "ACTIVE",
21
+ INACTIVE: "INACTIVE",
22
+ } as const;
23
+
24
+ export type Status = ObjectValues<typeof STATUS>;
25
+
26
+ export * from "./paging.types";
@@ -0,0 +1,35 @@
1
+ export interface PagingResult {
2
+ hasNextPage: boolean;
3
+ totalPages: number;
4
+ totalRecords: number;
5
+ }
6
+ export interface DataWithPagingResult<T> {
7
+ data: T;
8
+ pagination: PagingResult;
9
+ }
10
+ export interface SortCondition {
11
+ name: string;
12
+ direction: "ASC" | "DESC";
13
+ }
14
+ export interface FilterCondition {
15
+ name: string;
16
+ condition: "=" | ">" | "<" | "!=" | "LIKE" | "IN" | ">=" | "<=";
17
+ valueString?: string;
18
+ valueNumber?: number;
19
+ valueBoolean?: boolean;
20
+ useAnd?: boolean;
21
+ }
22
+ export interface PaginationRequest {
23
+ page: number;
24
+ size: number;
25
+ sort?: SortCondition[];
26
+ filters?: FilterCondition[];
27
+ }
28
+ export declare const createPagingObject: (
29
+ page: number,
30
+ size: number,
31
+ sort?: SortCondition[],
32
+ filters?: FilterCondition[]
33
+ ) => {
34
+ pagination: any;
35
+ };
@@ -0,0 +1,42 @@
1
+ import { MetaData, ObjectValues, Status } from "@/types";
2
+
3
+ export interface User extends Omit<MetaData, "created_by" | "updated_by"> {
4
+ id: string;
5
+ firstName: string;
6
+ lastName: string;
7
+ mi: string;
8
+ username: string;
9
+ email: string;
10
+ password: string;
11
+ loginAttempts: number;
12
+ locked: boolean;
13
+ passwordLastReset: Date;
14
+ passwordResetRequired: boolean;
15
+ type: AccountType;
16
+ status: Status;
17
+ companyId: number;
18
+ phoneNumber: string;
19
+ phoneFormat: string;
20
+ language: Language;
21
+ biometrics: string;
22
+ lastLogin: Date;
23
+ }
24
+
25
+ export const ACCOUNT_TYPE = {
26
+ TURNDOWN_ADMIN: "TURNDOWN_ADMIN",
27
+ OWNER: "OWNER",
28
+ ACCOUNT_ADMIN: "ACCOUNT_ADMIN",
29
+ MAINTAINER: "MAINTAINER",
30
+ CLEANER: "CLEANER",
31
+ } as const;
32
+
33
+ export type AccountType = ObjectValues<typeof ACCOUNT_TYPE>;
34
+
35
+ export const LANGUAGE = {
36
+ ENGLISH: "ENGLISH",
37
+ FRENCH: "FRENCH",
38
+ SPANISH: "SPANISH",
39
+ GERMAN: "GERMAN",
40
+ } as const;
41
+
42
+ export type Language = ObjectValues<typeof LANGUAGE>;
@@ -1,171 +0,0 @@
1
- export const parseJSON = (jsonString) => {
2
- try {
3
- return JSON.parse(jsonString);
4
- }
5
- catch (error) {
6
- return {};
7
- }
8
- };
9
- export const JSONStringify = (obj) => {
10
- let cache = [];
11
- let str = JSON.stringify(obj, function (_key, value) {
12
- if (typeof value === "object" && value !== null) {
13
- if (cache.indexOf(value) !== -1) {
14
- return;
15
- }
16
- cache.push(value);
17
- }
18
- return value;
19
- });
20
- cache = null;
21
- return str;
22
- };
23
- export const removeUndefined = (obj) => {
24
- return JSON.parse(JSONStringify(obj));
25
- };
26
- export const validPath = (location, key) => {
27
- return location?.pathname === key;
28
- };
29
- export const returnObject = (input) => {
30
- return Array.isArray(input) ? input[0] : input;
31
- };
32
- export const filterArrayById = (array1, array2) => {
33
- try {
34
- if (!array1 || !array2)
35
- return [];
36
- const idsToExclude = new Set(array2.map((item) => item.id));
37
- return array1.filter((item) => !idsToExclude.has(item.id));
38
- }
39
- catch (error) {
40
- console.error("An error occurred:", error);
41
- return [];
42
- }
43
- };
44
- export const sortArrayByProperty = (array, property) => {
45
- if (!array || array.length === 0)
46
- return [];
47
- return array.sort((a, b) => {
48
- if (a[property] < b[property])
49
- return -1;
50
- if (a[property] > b[property])
51
- return 1;
52
- return 0;
53
- });
54
- };
55
- export const replaceNulls = (obj) => {
56
- if (obj === null) {
57
- return "";
58
- }
59
- else if (Array.isArray(obj)) {
60
- return obj.map(replaceNulls);
61
- }
62
- else if (typeof obj === "object" && obj !== null) {
63
- const newObj = {};
64
- for (const key in obj) {
65
- if (obj.hasOwnProperty(key)) {
66
- newObj[key] = replaceNulls(obj[key]);
67
- }
68
- }
69
- return newObj;
70
- }
71
- return obj;
72
- };
73
- export const removeFormProperties = (obj) => {
74
- const recursiveRemove = (input) => {
75
- if (Array.isArray(input)) {
76
- return input.map((item) => recursiveRemove(item));
77
- }
78
- else if (typeof input === "object" && input !== null) {
79
- const newObj = { ...input };
80
- Object.keys(newObj).forEach((key) => {
81
- if (key.includes(".")) {
82
- delete newObj[key];
83
- }
84
- else {
85
- newObj[key] = recursiveRemove(newObj[key]);
86
- }
87
- });
88
- return newObj;
89
- }
90
- return input;
91
- };
92
- return recursiveRemove(obj);
93
- };
94
- export const convertStringBooleans = (obj) => {
95
- const recursiveConvert = (input) => {
96
- if (Array.isArray(input)) {
97
- return input.map((item) => recursiveConvert(item));
98
- }
99
- else if (typeof input === "object" && input !== null) {
100
- const newObj = { ...input };
101
- Object.keys(newObj).forEach((key) => {
102
- const value = newObj[key];
103
- if (value === "true") {
104
- newObj[key] = true;
105
- }
106
- else if (value === "false") {
107
- newObj[key] = false;
108
- }
109
- else if (typeof value === "object" && value !== null) {
110
- newObj[key] = recursiveConvert(value);
111
- }
112
- });
113
- return newObj;
114
- }
115
- return input;
116
- };
117
- return recursiveConvert(obj);
118
- };
119
- export const cleanFormData = (obj) => {
120
- return removeFormProperties(convertStringBooleans(removeUndefined(obj)));
121
- };
122
- export const resetPagination = (sort, filters) => {
123
- return {
124
- page: 1,
125
- size: 25,
126
- sort: sort || [],
127
- filters: filters || [],
128
- };
129
- };
130
- export const formatPhoneNumber = (value) => {
131
- const digits = value.toString().replace(/\D/g, "");
132
- if (digits.length !== 10)
133
- return value.toString();
134
- const area = digits.slice(0, 3);
135
- const prefix = digits.slice(3, 6);
136
- const line = digits.slice(6);
137
- return `(${area}) ${prefix}-${line}`;
138
- };
139
- export const formatNumber = (value) => {
140
- return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
141
- };
142
- export const parseNumber = (value) => {
143
- return value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, "");
144
- };
145
- export const deletePropertyIfExists = (obj, propertyName) => {
146
- if (Object.prototype.hasOwnProperty.call(obj, propertyName)) {
147
- delete obj[propertyName];
148
- }
149
- };
150
- export const chunkArray = (array, chunkSize) => {
151
- const result = [];
152
- for (let i = 0; i < array.length; i += chunkSize) {
153
- result.push(array.slice(i, i + chunkSize));
154
- }
155
- return result;
156
- };
157
- export const omitProperties = (obj, propsToOmit) => {
158
- const newObj = { ...obj };
159
- propsToOmit.forEach((prop) => {
160
- delete newObj[prop];
161
- });
162
- return newObj;
163
- };
164
- export const hasProperty = (obj, key) => {
165
- if (!obj)
166
- return false;
167
- return Object.prototype.hasOwnProperty.call(obj, key);
168
- };
169
- export const hasProperties = (obj) => {
170
- return Object.keys(obj || {}).length > 0;
171
- };
@@ -1,68 +0,0 @@
1
- export const normalCase = (str) => {
2
- if (!str)
3
- return "";
4
- return str
5
- .replace(/([a-z])([A-Z])/g, "$1 $2")
6
- .replace(/([A-Z])([A-Z][a-z])/g, "$1 $2")
7
- .split(/\s+/)
8
- .map((word) => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
9
- .join(" ");
10
- };
11
- export const sentenceCase = (str) => {
12
- if (!str)
13
- return "";
14
- const s = str.trim();
15
- return s.charAt(0).toUpperCase() + s.slice(1).toLowerCase();
16
- };
17
- export const upperCase = (str) => {
18
- return str ? str.toUpperCase() : "";
19
- };
20
- export const lowerCase = (str) => {
21
- return str ? str.toLowerCase() : "";
22
- };
23
- export const camelCase = (str) => {
24
- if (!str)
25
- return "";
26
- return str
27
- .toLowerCase()
28
- .split(/[\s_-]+/)
29
- .map((word, i) => i === 0
30
- ? word
31
- : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
32
- .join("");
33
- };
34
- export const pascalCase = (str) => {
35
- if (!str)
36
- return "";
37
- return str
38
- .toLowerCase()
39
- .split(/[\s_-]+/)
40
- .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
41
- .join("");
42
- };
43
- export const kebabCase = (str) => {
44
- if (!str)
45
- return "";
46
- return str
47
- .replace(/([a-z])([A-Z])/g, "$1-$2")
48
- .replace(/[\s_]+/g, "-")
49
- .toLowerCase();
50
- };
51
- export const snakeCase = (str) => {
52
- if (!str)
53
- return "";
54
- return str
55
- .replace(/([a-z])([A-Z])/g, "$1_$2")
56
- .replace(/[\s-]+/g, "_")
57
- .toLowerCase();
58
- };
59
- export const snakeCaseToSpaces = (str) => {
60
- if (!str)
61
- return "";
62
- return str.split("_").join(" ");
63
- };
64
- export const kebabToSpaces = (str) => {
65
- if (!str)
66
- return "";
67
- return str.split("-").join(" ");
68
- };
package/dist/index.js DELETED
@@ -1,5 +0,0 @@
1
- export * from "./helpers";
2
- export * from "./types";
3
- export const test = () => {
4
- console.log("working");
5
- };
@@ -1,5 +0,0 @@
1
- export const STATUS = {
2
- ACTIVE: "ACTIVE",
3
- INACTIVE: "INACTIVE",
4
- };
5
- export * from "./paging.types";
@@ -1 +0,0 @@
1
- export {};
@@ -1,13 +0,0 @@
1
- export const ACCOUNT_TYPE = {
2
- TURNDOWN_ADMIN: "TURNDOWN_ADMIN",
3
- OWNER: "OWNER",
4
- ACCOUNT_ADMIN: "ACCOUNT_ADMIN",
5
- MAINTAINER: "MAINTAINER",
6
- CLEANER: "CLEANER",
7
- };
8
- export const LANGUAGE = {
9
- ENGLISH: "ENGLISH",
10
- FRENCH: "FRENCH",
11
- SPANISH: "SPANISH",
12
- GERMAN: "GERMAN",
13
- };
File without changes
File without changes