some-common-functions-js 1.2.0 → 2.0.0
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/CHANGELOG.md +9 -2
- package/README.md +286 -26
- package/index.d.ts +281 -0
- package/index.js +635 -658
- package/index.js.map +1 -0
- package/index.ts +733 -0
- package/package.json +9 -4
- package/tsconfig.json +42 -0
package/index.d.ts
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
/** File: ./backend/utilityFunctions/commonFunctions.js
|
|
2
|
+
* Description: Common functions to be put here.
|
|
3
|
+
* Start Date End Date Dev Version Description
|
|
4
|
+
* 2025/11/19 ITA 1.00 Genesis.
|
|
5
|
+
* 2025/11/28 ITA 1.01 Added function hasOnlyAll().
|
|
6
|
+
* 2025/12/22 ITA 1.02 Improved documentation of the functions and moved in more functions.
|
|
7
|
+
* 2025/12/26 ITA 1.03 Removed lodash dependency by re-implementing get() and set() object functions, significantly reducing this package size.
|
|
8
|
+
* Added function getNextDifferent() to deal better with duplicate removal from arrays of objects.
|
|
9
|
+
* 2025/12/27 ITA 1.04 Improved the functions getNoDuplicatesArray() and getNextDifferent() to handle more test cases.
|
|
10
|
+
* Added function unset().
|
|
11
|
+
* 2025/12/28 ITA 1.05 Improved documentation of functions to show better on the tooltip in IDEs.
|
|
12
|
+
* Improved deepClone() function to handle Date objects and arrays.
|
|
13
|
+
* Updated get() function to return undefined or supplied default value for paths that do not exist.
|
|
14
|
+
* Updated test.js file accordingly.
|
|
15
|
+
* 2025/12/29 ITA 1.06 Removed unnecessary use of the getPaths() function in the get() function to improve effieciency.
|
|
16
|
+
* 2026/01/01 ITA 1.07 Changed recursive functions to iterative functions, so as to overcome stack limits when functions are used in the front-end (browsers).
|
|
17
|
+
* 2026/01/01 ITA 1.08 Changed an additional recursive function to an iterative function.
|
|
18
|
+
* 2026/01/03 ITA 1.09 deepClone(): Used object spread to prevent reference sharing during object assignment.
|
|
19
|
+
* unset(): Replaced falsy-value evaluation with the `in` operator to correctly detect existing fields.
|
|
20
|
+
* 2026/01/10 2026/10/10 ITA 1.10 get() and unset() functions: For field existence check, replaced the use of 'in' operator with checking whether the field value of the object is undefined.
|
|
21
|
+
* This prevents crashes resulting from using 'in' operator on undefined fields and objects.
|
|
22
|
+
* 2026/01/10 2026/10/10 ITA 1.11 Added more robustness in dealing with non-existent fields in get() and unset() functions.
|
|
23
|
+
* 2026/05/07 2026/05/08 ITA 1.12 Migrated to Typescript.
|
|
24
|
+
* Added a robust functionality for verifying whether a variable is a plain Typescript/Javacript object.
|
|
25
|
+
|
|
26
|
+
*/
|
|
27
|
+
/**Return true if userName is valid
|
|
28
|
+
* @param {string} userName
|
|
29
|
+
* @returns {boolean} true if a userName is valid, otherwise false.
|
|
30
|
+
*/
|
|
31
|
+
export declare function isValidUserName(userName: string): boolean;
|
|
32
|
+
/**Return true if a name is valid
|
|
33
|
+
* @param {string} name
|
|
34
|
+
* @returns {boolean} true if a name is valid, otherwise false.
|
|
35
|
+
*/
|
|
36
|
+
declare function isValidName(name: string): boolean;
|
|
37
|
+
export { isValidName };
|
|
38
|
+
/**Return true if userName is valid
|
|
39
|
+
* @param {string} email
|
|
40
|
+
* @returns {boolean} true if an email is valid, otherwise false.
|
|
41
|
+
*/
|
|
42
|
+
declare function isValidEmail(email: string): boolean;
|
|
43
|
+
export { isValidEmail };
|
|
44
|
+
/**Return true if userName is valid
|
|
45
|
+
* @param {string} num phone number
|
|
46
|
+
* @returns {boolean} true if phone number is valid, otherwise false.
|
|
47
|
+
*/
|
|
48
|
+
declare function isValidPhoneNum(num: string): boolean;
|
|
49
|
+
export { isValidPhoneNum };
|
|
50
|
+
/**Return true if the name of an organisation is valid
|
|
51
|
+
* @param {string} name an organisation name
|
|
52
|
+
* @returns {boolean} true if an organisation name is valid, otherwise false.
|
|
53
|
+
*/
|
|
54
|
+
declare function isValidOrganisationName(name: string): boolean;
|
|
55
|
+
export { isValidOrganisationName };
|
|
56
|
+
/**Return true if a password is valid
|
|
57
|
+
* @param {string} password
|
|
58
|
+
* @returns {boolean} true if a password is valid, otherwise false.
|
|
59
|
+
*/
|
|
60
|
+
declare function isValidPassword(password: string): boolean;
|
|
61
|
+
export { isValidPassword };
|
|
62
|
+
/** Converts the date object to a string of the form CCYY-MM-DD
|
|
63
|
+
* @param {Date} dateObj
|
|
64
|
+
* @returns {string} string of the form CCYY-MM-DD
|
|
65
|
+
*/
|
|
66
|
+
declare function timeStampYyyyMmDd(dateObj: Date): string;
|
|
67
|
+
export { timeStampYyyyMmDd };
|
|
68
|
+
/** Converts a date object to a string of the form CCYY-MM-DDThh:mm:ss.ccc, e.g. '2024-02-25T15:00:25.251'
|
|
69
|
+
* @param {Date} dateObj
|
|
70
|
+
* @returns {string} a string of the form CCYY-MM-DDThh:mm:ss.ccc.
|
|
71
|
+
*/
|
|
72
|
+
declare function timeStampString(dateObj: Date): string;
|
|
73
|
+
export { timeStampString };
|
|
74
|
+
/** Returns a numeric string with trailing zeros.
|
|
75
|
+
* E.g.
|
|
76
|
+
* addLeadingZeros(9, 4) = '0009', addLeadingZeros(123, 5) = '00123'
|
|
77
|
+
* @param {string | number} aNumber a numerical string or number.
|
|
78
|
+
* @param {number} newLength the new length of the resulting string.
|
|
79
|
+
* @returns a string of a number with the specified number of leading zeros.
|
|
80
|
+
*/
|
|
81
|
+
declare function addLeadingZeros(aNumber: string | number, newLength: number): string;
|
|
82
|
+
export { addLeadingZeros };
|
|
83
|
+
/**Convert numeric input to ZAR currency format string.
|
|
84
|
+
* @param {number | string} aNumber a number or numeric string.
|
|
85
|
+
* @returns a string of the form R 256,534.00
|
|
86
|
+
*/
|
|
87
|
+
declare function toZarCurrencyFormat(aNumber: number | string): string;
|
|
88
|
+
export { toZarCurrencyFormat };
|
|
89
|
+
/**
|
|
90
|
+
* Check if a value is a plain Typescript/JavaScript object.
|
|
91
|
+
* @param {any} value
|
|
92
|
+
* @returns {boolean}
|
|
93
|
+
*/
|
|
94
|
+
declare function isPlainObject(value: any): value is Record<string, any>;
|
|
95
|
+
export { isPlainObject };
|
|
96
|
+
/**Return a deep clone of a document object.
|
|
97
|
+
* By using deep cloning, you create a new object that is entirely separate from the original object.
|
|
98
|
+
*
|
|
99
|
+
* So that whatever you do to that clone, such as deletion of fields, does not affect the original object.
|
|
100
|
+
*
|
|
101
|
+
* NB. Works only plain Javascript/Typescript objects. Field values are not cloned if they are not plain Javascript objects, but are returned as is.
|
|
102
|
+
* @template T
|
|
103
|
+
* @param {T} obj a plain Typescript/Javascript object.
|
|
104
|
+
* @returns a Typescript/Javascript object that is separate from the original object.
|
|
105
|
+
* @note For a full deep clone of Arrays, Maps, and Sets (that throws on functions), consider using the native {@link structuredClone}.
|
|
106
|
+
*/
|
|
107
|
+
declare function deepClone<T extends object>(obj: T): T;
|
|
108
|
+
export { deepClone };
|
|
109
|
+
/**
|
|
110
|
+
* Get the paths (fields) of the plain Javascript object.
|
|
111
|
+
* @template T
|
|
112
|
+
* @param {T} anObject a plain Typescript/Javascript object.
|
|
113
|
+
* @returns a sorted string array of paths.
|
|
114
|
+
*/
|
|
115
|
+
declare function getPaths<T extends object>(anObject: T): string[];
|
|
116
|
+
export { getPaths };
|
|
117
|
+
/** Return an object with sorted fields, ordered by field name ascending.
|
|
118
|
+
*
|
|
119
|
+
* The returned object is independent of the source object.
|
|
120
|
+
*
|
|
121
|
+
* NB. For comparison of objects, please see objCompare() function.
|
|
122
|
+
* @template T
|
|
123
|
+
* @param {T} pObject plain Typescript/Javascript object.
|
|
124
|
+
* @returns {T} an object with fields sorted in ascending order of field names.
|
|
125
|
+
*/
|
|
126
|
+
declare function getSortedObject<T extends object>(pObject: T): T;
|
|
127
|
+
export { getSortedObject };
|
|
128
|
+
/** Get the value of a field specified by the path from an object.
|
|
129
|
+
* @template T
|
|
130
|
+
* @param {T} anObject a Typescript/Javascript object.
|
|
131
|
+
* @param {string} path a path specifying the field whose value is to be obtained.
|
|
132
|
+
* @param {any} [defaultVal=undefined] a default value to return if the path does not exist on the object.
|
|
133
|
+
* @returns {U|undefined} the value of the field specified by the path, otherwise a default value if supplied.
|
|
134
|
+
*/
|
|
135
|
+
declare function get<T extends object, U extends any>(anObject: T, path: string, defaultVal?: U | undefined): U | undefined;
|
|
136
|
+
export { get };
|
|
137
|
+
/** Set the value of a field specified by the path on an object.
|
|
138
|
+
* @template T, U
|
|
139
|
+
* @param {object} anObject a Typescript/Javascript object.
|
|
140
|
+
* @param {string} path a path specifying the field whose value is to be set.
|
|
141
|
+
* @param {U} value the value to set.
|
|
142
|
+
*/
|
|
143
|
+
declare function set<T extends object, U>(anObject: T, path: string, value: U): void;
|
|
144
|
+
export { set };
|
|
145
|
+
/** Unset the value of a field specified by the path on an object.
|
|
146
|
+
* @template T
|
|
147
|
+
* @param {T} anObject a Typescript/Javascript object.
|
|
148
|
+
* @param {string} path a path specifying the field whose value is to be set.
|
|
149
|
+
*/
|
|
150
|
+
declare function unset<T extends object>(anObject: T, path: string): void;
|
|
151
|
+
export { unset };
|
|
152
|
+
/**
|
|
153
|
+
* Determine whether an object contains only 1, some or all of the specified fields, and not any other fields.
|
|
154
|
+
* @template T
|
|
155
|
+
* @param {T} anObject a Javascript object.
|
|
156
|
+
* @param {...string[]} fields one or more field names.
|
|
157
|
+
* @returns boolean true or false.
|
|
158
|
+
*/
|
|
159
|
+
declare function hasOnly<T extends object>(anObject: T, ...fields: string[]): boolean;
|
|
160
|
+
export { hasOnly };
|
|
161
|
+
/**
|
|
162
|
+
* Determine whether an object contains all of the specified fields in addition to other fields.
|
|
163
|
+
* @template T
|
|
164
|
+
* @param {T} anObject a Javascript object.
|
|
165
|
+
* @param {...string[]} fields one or field names.
|
|
166
|
+
* @returns boolean true or false.
|
|
167
|
+
*/
|
|
168
|
+
declare function hasAll<T extends object>(anObject: T, ...fields: string[]): boolean;
|
|
169
|
+
export { hasAll };
|
|
170
|
+
/**
|
|
171
|
+
* Determine whether an object contains only all of the specified fields. Nothing more, nothing less.
|
|
172
|
+
* @param {T} anObject a Javascript object.
|
|
173
|
+
* @param {...string[]} fields one or field names.
|
|
174
|
+
* @returns boolean true or false.
|
|
175
|
+
*/
|
|
176
|
+
declare function hasOnlyAll<T extends object>(anObject: T, ...fields: string[]): boolean;
|
|
177
|
+
export { hasOnlyAll };
|
|
178
|
+
/**Binary Search the sorted primitive data array for a value and return the index.
|
|
179
|
+
*
|
|
180
|
+
* ArraySortDir specifies the direction in which the array is sorted (desc or asc).
|
|
181
|
+
*
|
|
182
|
+
* If the array contains the value searched for, then the index returned is the location of this value on the array,
|
|
183
|
+
* otherwise, the index is of closest value in the array that is before or after the search value in terms of sort order.
|
|
184
|
+
*
|
|
185
|
+
* This function can be used also in cases where values are to be inserted into the array while maintaining sort order.
|
|
186
|
+
* @template T
|
|
187
|
+
* @param {Array<T>} anArray an array of primitve type. All element must be the same type.
|
|
188
|
+
* @param {T} searchVal search value
|
|
189
|
+
* @param {number} [startFrom=0] index from which to start. Default: 0.
|
|
190
|
+
* @param {'asc' | 'desc'} [arraySortDir='asc'] sort direction. Must be 'asc' or 'desc'. Default: 'asc'
|
|
191
|
+
* @returns {number} an index. -1 mean value not found.
|
|
192
|
+
*/
|
|
193
|
+
declare function binarySearch<T>(anArray: T[], searchVal: T, startFrom?: number, arraySortDir?: 'asc' | 'desc'): number;
|
|
194
|
+
export { binarySearch };
|
|
195
|
+
/** Compare two values of the same primitive type, according to the sort direction.
|
|
196
|
+
* May be used with dates, numbers, booleans and strings. For other types, the result is unpredictable.
|
|
197
|
+
*
|
|
198
|
+
* A return value of -1 means that value1 is before value2 in terms of sort order.
|
|
199
|
+
*
|
|
200
|
+
* A return value of 1 means that value1 is after value2 in terms of sort order.
|
|
201
|
+
*
|
|
202
|
+
* A return value of 0 means that value1 is equal to value2.
|
|
203
|
+
* @template T
|
|
204
|
+
* @param {T} value1
|
|
205
|
+
* @param {T} value2
|
|
206
|
+
* @param {'asc' | 'desc'} [sortDir='asc'] sort direction. Must be 'asc' or 'desc'. Default: 'asc'
|
|
207
|
+
* @returns {number} -1, 0 or 1
|
|
208
|
+
*/
|
|
209
|
+
declare function compare<T>(value1: T, value2: T, sortDir?: 'asc' | 'desc'): number;
|
|
210
|
+
export { compare };
|
|
211
|
+
/**Binary Search the sorted (ascending or descending order) array of plain Typescript/Javascript objects for a value and return the index.
|
|
212
|
+
*
|
|
213
|
+
* The assumption is that the array is sorted in order of 1 or more sort fields,
|
|
214
|
+
*
|
|
215
|
+
* Examples of sort fields: 'lastName asc', 'firstName', 'address.province asc', 'address.townOrCity asc'.
|
|
216
|
+
*
|
|
217
|
+
* If the array contains the object with values searched for, then the index returned is the location of this value in the array, otherwise,
|
|
218
|
+
* the index is of the closest value in the array that is before or after the searchObj value.
|
|
219
|
+
* Return -1 for an empty array.
|
|
220
|
+
* Assumed field data types are numbers, strings, booleans and dates.
|
|
221
|
+
* This function is to be used also in cases where objects are to be inserted into the array while maintaining sort order.
|
|
222
|
+
* @template T
|
|
223
|
+
* @param {Array<T>} objArray an array of Javascript objects.
|
|
224
|
+
* @param {T} searchObj an object to search for.
|
|
225
|
+
* @param {number} [startFrom=0] index from which to start searching.
|
|
226
|
+
* @param {...string[]} sortFields one or more search fields.
|
|
227
|
+
* @returns {number} an index.
|
|
228
|
+
*/
|
|
229
|
+
declare function binarySearchObj<T extends object>(objArray: T[], searchObj: T, startFrom?: number, ...sortFields: string[]): number;
|
|
230
|
+
export { binarySearchObj };
|
|
231
|
+
/**Get the index of the first element in an object array that is different from the target element
|
|
232
|
+
* according to the comparison fields.
|
|
233
|
+
* @template T
|
|
234
|
+
* @param {Array<T>} objArray an array of objects
|
|
235
|
+
* @param {T} targetObj target object
|
|
236
|
+
* @param {number} startFrom index from which to start searching
|
|
237
|
+
* @param {...string[]} comparisonFields the fields sort order of the array. e.g. 'score desc', 'numGames asc'.
|
|
238
|
+
* @returns index of the next different object.
|
|
239
|
+
*/
|
|
240
|
+
declare function getNextDifferent<T extends object>(objArray: T[], targetObj: T, startFrom: number, ...comparisonFields: string[]): number;
|
|
241
|
+
export { getNextDifferent };
|
|
242
|
+
/**Create an array with duplicates eliminated, according to certain fields. Taking only the first or last object from each duplicate set.
|
|
243
|
+
*
|
|
244
|
+
* If firstOfDuplicates === true, then the first element in each set of duplicates is taken.
|
|
245
|
+
*
|
|
246
|
+
* if firstOfDuplicates === false, then the last element is taken from each set of duplicates.
|
|
247
|
+
*
|
|
248
|
+
* Assumed comparison field data types are Boolean, Number, String, Date.
|
|
249
|
+
*
|
|
250
|
+
* The array must be sorted according to the comparison fields before calling this function.
|
|
251
|
+
* The value of the comparison field must include both the field name and sort direction.
|
|
252
|
+
* Sort direction assumed to be "asc" if not provided.
|
|
253
|
+
* Examples of comparison fields: "firstName", "lastName desc", "address.province asc", "address.townOrCity".
|
|
254
|
+
* @template T
|
|
255
|
+
* @param {Array<T>} objArray an input array of objects
|
|
256
|
+
* @param {boolean} firstOfDuplicates specify whether to take the first or last object in each a duplicate set.
|
|
257
|
+
* @param {...string[]} comparisonFields comparison fieds plus sort order.
|
|
258
|
+
* @returns {Array<T>} an array with no duplicates.
|
|
259
|
+
*/
|
|
260
|
+
declare function getObjArrayWithNoDuplicates<T extends object>(objArray: T[], firstOfDuplicates: boolean, ...comparisonFields: string[]): T[];
|
|
261
|
+
export { getObjArrayWithNoDuplicates };
|
|
262
|
+
/**Compare 2 objects according to the comparison fields, and return the result of:
|
|
263
|
+
*
|
|
264
|
+
* -1 if obj1 is before obj2, 1 if obj1 is after obj2, 0 if obj1 is equal to obj2.
|
|
265
|
+
*
|
|
266
|
+
* Each each of the comparisonFields must be of the form 'fieldName sortDirection' or 'fieldName'.
|
|
267
|
+
*
|
|
268
|
+
* Sort directions: 'asc', 'desc'.
|
|
269
|
+
*
|
|
270
|
+
* Field/sort-direction examples: 'lastName desc', 'firstName', 'firstName asc', 'address.provinceName asc'.
|
|
271
|
+
*
|
|
272
|
+
* If sort direction is not provided, then it is assumed to be ascending.
|
|
273
|
+
* @template T
|
|
274
|
+
* @param {T} obj1 first object to compare
|
|
275
|
+
* @param {T} obj2 second object to compare
|
|
276
|
+
* @param {...string[]} comparisonFields one or more comparison fields plus sort order.
|
|
277
|
+
* @returns {number} comparison result: -1, 0 or 1.
|
|
278
|
+
*/
|
|
279
|
+
declare function objCompare<T extends object>(obj1: T, obj2: T, ...comparisonFields: string[]): number;
|
|
280
|
+
export { objCompare };
|
|
281
|
+
//# sourceMappingURL=index.d.ts.map
|