@slickgrid-universal/utils 2.5.0 → 2.6.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/dist/esm/utils.js CHANGED
@@ -1,359 +1,359 @@
1
- /**
2
- * Add an item to an array only when the item does not exists, when the item is an object we will be using their "id" to compare
3
- * @param inputArray
4
- * @param inputItem
5
- * @param itemIdPropName
6
- */
7
- export function addToArrayWhenNotExists(inputArray, inputItem, itemIdPropName = 'id') {
8
- let arrayRowIndex = -1;
9
- if (inputItem && typeof inputItem === 'object' && itemIdPropName in inputItem) {
10
- arrayRowIndex = inputArray.findIndex((item) => item[itemIdPropName] === inputItem[itemIdPropName]);
11
- }
12
- else {
13
- arrayRowIndex = inputArray.findIndex((item) => item === inputItem);
14
- }
15
- if (arrayRowIndex < 0) {
16
- inputArray.push(inputItem);
17
- }
18
- }
19
- /**
20
- * Simple function to which will loop and create as demanded the number of white spaces,
21
- * this is used in the CSV export
22
- * @param {Number} nbSpaces - number of white spaces to create
23
- * @param {String} spaceChar - optionally provide character to use as a space (could be override to use &nbsp; in html)
24
- */
25
- export function addWhiteSpaces(nbSpaces, spaceChar = ' ') {
26
- let result = '';
27
- for (let i = 0; i < nbSpaces; i++) {
28
- result += spaceChar;
29
- }
30
- return result;
31
- }
32
- /**
33
- * Remove a column from the grid by it's index in the grid
34
- * @param array input
35
- * @param index
36
- */
37
- export function arrayRemoveItemByIndex(array, index) {
38
- return array.filter((_el, i) => index !== i);
39
- }
40
- /**
41
- * Create an immutable clone of an array or object
42
- * (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
43
- * @param {Array|Object} objectOrArray - the array or object to copy
44
- * @return {Array|Object} - the clone of the array or object
45
- */
46
- export function deepCopy(objectOrArray) {
47
- /**
48
- * Create an immutable copy of an object
49
- * @return {Object}
50
- */
51
- const cloneObj = () => {
52
- // Create new object
53
- const clone = {};
54
- // Loop through each item in the original
55
- // Recursively copy it's value and add to the clone
56
- for (const key in objectOrArray) {
57
- if (Object.prototype.hasOwnProperty.call(objectOrArray, key)) {
58
- clone[key] = deepCopy(objectOrArray[key]);
59
- }
60
- }
61
- return clone;
62
- };
63
- /**
64
- * Create an immutable copy of an array
65
- * @return {Array}
66
- */
67
- const cloneArr = () => objectOrArray.map((item) => deepCopy(item));
68
- // -- init --//
69
- // Get object type
70
- const type = Object.prototype.toString.call(objectOrArray).slice(8, -1).toLowerCase();
71
- // If an object
72
- if (type === 'object') {
73
- return cloneObj();
74
- }
75
- // If an array
76
- if (type === 'array') {
77
- return cloneArr();
78
- }
79
- // Otherwise, return it as-is
80
- return objectOrArray;
81
- }
82
- /**
83
- * Performs a deep merge of objects and returns new object, it does not modify the source object, objects (immutable) and merges arrays via concatenation.
84
- * Also, if first argument is undefined/null but next argument is an object then it will proceed and output will be an object
85
- * @param {Object} target - the target object — what to apply the sources' properties to, which is returned after it is modified.
86
- * @param {Object} sources - the source object(s) — objects containing the properties you want to apply.
87
- * @returns {Object} The target object.
88
- */
89
- export function deepMerge(target, ...sources) {
90
- if (!sources.length) {
91
- return target;
92
- }
93
- const source = sources.shift();
94
- // when target is not an object but source is an object, then we'll assign as object
95
- target = (!isObject(target) && isObject(source)) ? {} : target;
96
- if (isObject(target) && isObject(source)) {
97
- for (const prop in source) {
98
- if (source.hasOwnProperty(prop)) {
99
- if (prop in target) {
100
- // handling merging of two properties with equal names
101
- if (typeof target[prop] !== 'object') {
102
- target[prop] = source[prop];
103
- }
104
- else {
105
- if (typeof source[prop] !== 'object') {
106
- target[prop] = source[prop];
107
- }
108
- else {
109
- if (target[prop].concat && source[prop].concat) {
110
- // two arrays get concatenated
111
- target[prop] = target[prop].concat(source[prop]);
112
- }
113
- else {
114
- // two objects get merged recursively
115
- target[prop] = deepMerge(target[prop], source[prop]);
116
- }
117
- }
118
- }
119
- }
120
- else {
121
- // new properties get added to target
122
- target[prop] = source[prop];
123
- }
124
- }
125
- }
126
- }
127
- return deepMerge(target, ...sources);
128
- }
129
- /**
130
- * This method is similar to `Object.assign` with the exception that it will also extend the object properties when filled.
131
- * There's also a distinction with extend vs merge, we are only extending when the property is not filled (if it is filled then it remains untouched and will not be merged)
132
- * It also applies the change directly on the target object which mutates the original object.
133
- * For example using these 2 objects: obj1 = { a: 1, b: { c: 2, d: 3 }} and obj2 = { b: { d: 2, e: 3}}:
134
- * - Object.assign(obj1, obj2) => { a: 1, b: { e: 4 }}
135
- * - objectAssignAndExtend(obj1, obj2) => { a: 1, b: { c: 2, d: 3, e: 4 }
136
- * @param {Object} target - the target object — what to apply the sources properties and mutate into
137
- * @param {Object} sources - the source object(s) — objects containing the properties you want to apply.
138
- * @returns {Object} The target object.
139
- */
140
- export function objectAssignAndExtend(target, ...sources) {
141
- if (!sources.length || sources[0] === undefined) {
142
- return target;
143
- }
144
- const source = sources.shift();
145
- // when target is not an object but source is an object, then we'll assign as object
146
- target = (!isObject(target) && isObject(source)) ? {} : target;
147
- if (isObject(target) && isObject(source)) {
148
- for (const key of Object.keys(source)) {
149
- if (typeof source[key] === 'object' && source[key] !== null) {
150
- objectAssignAndExtend(target[key], source[key]);
151
- }
152
- if ((target[key] === null || target[key] === undefined) && source[key] !== null && source[key] !== undefined) {
153
- target[key] = source[key];
154
- }
155
- }
156
- }
157
- return objectAssignAndExtend(target, ...sources);
158
- }
159
- /**
160
- * Empty an object properties by looping through them all and deleting them
161
- * @param obj - input object
162
- */
163
- export function emptyObject(obj) {
164
- for (const key in obj) {
165
- if (obj.hasOwnProperty(key)) {
166
- delete obj[key];
167
- }
168
- }
169
- obj = null;
170
- obj = {};
171
- return obj;
172
- }
173
- /**
174
- * Check if an object is empty
175
- * @param obj - input object
176
- * @returns - boolean
177
- */
178
- export function isEmptyObject(obj) {
179
- if (obj === null || obj === undefined) {
180
- return true;
181
- }
182
- return Object.entries(obj).length === 0;
183
- }
184
- /**
185
- * Simple object check.
186
- * @param item
187
- * @returns {boolean}
188
- */
189
- export function isObject(item) {
190
- return item !== null && typeof item === 'object' && !Array.isArray(item) && !(item instanceof Date);
191
- }
192
- /**
193
- * Simple check to detect if the value is a primitive type
194
- * @param val
195
- * @returns {boolean}
196
- */
197
- export function isPrimmitive(val) {
198
- return val === null || val === undefined || typeof val === 'boolean' || typeof val === 'number' || typeof val === 'string';
199
- }
200
- /**
201
- * Check if a value has any data (undefined, null or empty string will return False...)
202
- * NOTE: a `false` boolean is consider as having data so it will return True
203
- */
204
- export function hasData(value) {
205
- return value !== undefined && value !== null && value !== '';
206
- }
207
- /**
208
- * Check if input value is a number, by default it won't be a strict checking
209
- * but optionally we could check for strict equality, for example in strict "3" will return False but without strict it will return True
210
- * @param value - input value of any type
211
- * @param strict - when using strict it also check for strict equality, for example in strict "3" would return False but without strict it would return True
212
- */
213
- export function isNumber(value, strict = false) {
214
- if (strict) {
215
- return (value === null || value === undefined || typeof value === 'string') ? false : !isNaN(value);
216
- }
217
- return (value === null || value === undefined || value === '') ? false : !isNaN(+value);
218
- }
219
- /** Check if an object is empty, it will also be considered empty when the input is null, undefined or isn't an object */
220
- export function isObjectEmpty(obj) {
221
- return !obj || (obj && typeof obj === 'object' && Object.keys(obj).length === 0);
222
- }
223
- /** Parse any input (bool, number, string) and return a boolean or False when not possible */
224
- export function parseBoolean(input) {
225
- return /(true|1)/i.test(input + '');
226
- }
227
- /**
228
- * Remove any accents from a string by normalizing it
229
- * @param {String} text - input text
230
- * @param {Boolean} shouldLowerCase - should we also lowercase the string output?
231
- * @returns
232
- */
233
- export function removeAccentFromText(text, shouldLowerCase = false) {
234
- const normalizedText = (typeof text.normalize === 'function') ? text.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : text;
235
- return shouldLowerCase ? normalizedText.toLowerCase() : normalizedText;
236
- }
237
- /** Set the object value of deeper node from a given dot (.) notation path (e.g.: "user.firstName") */
238
- export function setDeepValue(obj, path, value) {
239
- if (typeof path === 'string') {
240
- path = path.split('.');
241
- }
242
- if (path.length > 1) {
243
- const e = path.shift();
244
- if (obj && e !== undefined) {
245
- setDeepValue((obj)[e] = (hasData(obj[e]) && (Array.isArray(obj[e]) || Object.prototype.toString.call((obj)[e]) === '[object Object]'))
246
- ? (obj)[e]
247
- : {}, path, value);
248
- }
249
- }
250
- else if (obj && path[0]) {
251
- (obj)[path[0]] = value;
252
- }
253
- }
254
- /**
255
- * Title case (or capitalize) first char of a string, for example "hello world" will become "Hello world"
256
- * Change the string to be title case on the complete sentence (upper case first char of each word while changing everything else to lower case)
257
- * @param inputStr
258
- * @returns string
259
- */
260
- export function titleCase(inputStr, shouldTitleCaseEveryWords = false) {
261
- if (typeof inputStr === 'string') {
262
- if (shouldTitleCaseEveryWords) {
263
- return inputStr.replace(/\w\S*/g, (outputStr) => {
264
- return outputStr.charAt(0).toUpperCase() + outputStr.substring(1).toLowerCase();
265
- });
266
- }
267
- return inputStr.charAt(0).toUpperCase() + inputStr.slice(1);
268
- }
269
- return inputStr;
270
- }
271
- /**
272
- * Converts a string to camel case (camelCase), for example "hello-world" (or "hellow world") will become "helloWorld"
273
- * @param inputStr the string to convert
274
- * @return the string in camel case
275
- */
276
- export function toCamelCase(inputStr) {
277
- if (typeof inputStr === 'string') {
278
- return inputStr.replace(/(?:^\w|[A-Z]|\b\w|[\s+\-_\/])/g, (match, offset) => {
279
- // remove white space or hypens or underscores
280
- if (/[\s+\-_\/]/.test(match)) {
281
- return '';
282
- }
283
- return offset === 0 ? match.toLowerCase() : match.toUpperCase();
284
- });
285
- }
286
- return inputStr;
287
- }
288
- /**
289
- * Converts a string to kebab (hypen) case, for example "helloWorld" will become "hello-world"
290
- * @param str the string to convert
291
- * @return the string in kebab case
292
- */
293
- export function toKebabCase(inputStr) {
294
- if (typeof inputStr === 'string') {
295
- return toCamelCase(inputStr).replace(/([A-Z])/g, '-$1').toLowerCase();
296
- }
297
- return inputStr;
298
- }
299
- /**
300
- * Converts a camelCase or kebab-case string to a sentence case, for example "helloWorld" will become "Hello World" and "hello-world" will become "Hello world"
301
- * @param str the string to convert
302
- * @return the string in kebab case
303
- */
304
- export function toSentenceCase(inputStr) {
305
- if (typeof inputStr === 'string') {
306
- const result = inputStr.replace(/([A-Z])|(\-)/g, ' $1').replace(/\s+/g, ' ').trim();
307
- return result.charAt(0).toUpperCase() + result.slice(1);
308
- }
309
- return inputStr;
310
- }
311
- /**
312
- * Converts a string from camelCase to snake_case (underscore) case
313
- * @param str the string to convert
314
- * @return the string in kebab case
315
- */
316
- export function toSnakeCase(inputStr) {
317
- if (typeof inputStr === 'string') {
318
- return toCamelCase(inputStr).replace(/([A-Z])/g, '_$1').toLowerCase();
319
- }
320
- return inputStr;
321
- }
322
- /**
323
- * Takes an input array and makes sure the array has unique values by removing duplicates
324
- * @param array input with possible duplicates
325
- * @return array output without duplicates
326
- */
327
- export function uniqueArray(arr) {
328
- if (Array.isArray(arr) && arr.length > 0) {
329
- return arr.filter((item, index) => {
330
- return arr.indexOf(item) >= index;
331
- });
332
- }
333
- return arr;
334
- }
335
- /**
336
- * Takes an input array of objects and makes sure the array has unique object values by removing duplicates
337
- * it will loop through the array using a property name (or "id" when is not provided) to compare uniqueness
338
- * @param array input with possible duplicates
339
- * @param propertyName defaults to "id"
340
- * @return array output without duplicates
341
- */
342
- export function uniqueObjectArray(arr, propertyName = 'id') {
343
- if (Array.isArray(arr) && arr.length > 0) {
344
- const result = [];
345
- const map = new Map();
346
- for (const item of arr) {
347
- if (item && !map.has(item[propertyName])) {
348
- map.set(item[propertyName], true); // set any value to Map
349
- result.push({
350
- id: item[propertyName],
351
- name: item.name
352
- });
353
- }
354
- }
355
- return result;
356
- }
357
- return arr;
358
- }
1
+ /**
2
+ * Add an item to an array only when the item does not exists, when the item is an object we will be using their "id" to compare
3
+ * @param inputArray
4
+ * @param inputItem
5
+ * @param itemIdPropName
6
+ */
7
+ export function addToArrayWhenNotExists(inputArray, inputItem, itemIdPropName = 'id') {
8
+ let arrayRowIndex = -1;
9
+ if (inputItem && typeof inputItem === 'object' && itemIdPropName in inputItem) {
10
+ arrayRowIndex = inputArray.findIndex((item) => item[itemIdPropName] === inputItem[itemIdPropName]);
11
+ }
12
+ else {
13
+ arrayRowIndex = inputArray.findIndex((item) => item === inputItem);
14
+ }
15
+ if (arrayRowIndex < 0) {
16
+ inputArray.push(inputItem);
17
+ }
18
+ }
19
+ /**
20
+ * Simple function to which will loop and create as demanded the number of white spaces,
21
+ * this is used in the CSV export
22
+ * @param {Number} nbSpaces - number of white spaces to create
23
+ * @param {String} spaceChar - optionally provide character to use as a space (could be override to use &nbsp; in html)
24
+ */
25
+ export function addWhiteSpaces(nbSpaces, spaceChar = ' ') {
26
+ let result = '';
27
+ for (let i = 0; i < nbSpaces; i++) {
28
+ result += spaceChar;
29
+ }
30
+ return result;
31
+ }
32
+ /**
33
+ * Remove a column from the grid by it's index in the grid
34
+ * @param array input
35
+ * @param index
36
+ */
37
+ export function arrayRemoveItemByIndex(array, index) {
38
+ return array.filter((_el, i) => index !== i);
39
+ }
40
+ /**
41
+ * Create an immutable clone of an array or object
42
+ * (c) 2019 Chris Ferdinandi, MIT License, https://gomakethings.com
43
+ * @param {Array|Object} objectOrArray - the array or object to copy
44
+ * @return {Array|Object} - the clone of the array or object
45
+ */
46
+ export function deepCopy(objectOrArray) {
47
+ /**
48
+ * Create an immutable copy of an object
49
+ * @return {Object}
50
+ */
51
+ const cloneObj = () => {
52
+ // Create new object
53
+ const clone = {};
54
+ // Loop through each item in the original
55
+ // Recursively copy it's value and add to the clone
56
+ for (const key in objectOrArray) {
57
+ if (Object.prototype.hasOwnProperty.call(objectOrArray, key)) {
58
+ clone[key] = deepCopy(objectOrArray[key]);
59
+ }
60
+ }
61
+ return clone;
62
+ };
63
+ /**
64
+ * Create an immutable copy of an array
65
+ * @return {Array}
66
+ */
67
+ const cloneArr = () => objectOrArray.map((item) => deepCopy(item));
68
+ // -- init --//
69
+ // Get object type
70
+ const type = Object.prototype.toString.call(objectOrArray).slice(8, -1).toLowerCase();
71
+ // If an object
72
+ if (type === 'object') {
73
+ return cloneObj();
74
+ }
75
+ // If an array
76
+ if (type === 'array') {
77
+ return cloneArr();
78
+ }
79
+ // Otherwise, return it as-is
80
+ return objectOrArray;
81
+ }
82
+ /**
83
+ * Performs a deep merge of objects and returns new object, it does not modify the source object, objects (immutable) and merges arrays via concatenation.
84
+ * Also, if first argument is undefined/null but next argument is an object then it will proceed and output will be an object
85
+ * @param {Object} target - the target object — what to apply the sources' properties to, which is returned after it is modified.
86
+ * @param {Object} sources - the source object(s) — objects containing the properties you want to apply.
87
+ * @returns {Object} The target object.
88
+ */
89
+ export function deepMerge(target, ...sources) {
90
+ if (!sources.length) {
91
+ return target;
92
+ }
93
+ const source = sources.shift();
94
+ // when target is not an object but source is an object, then we'll assign as object
95
+ target = (!isObject(target) && isObject(source)) ? {} : target;
96
+ if (isObject(target) && isObject(source)) {
97
+ for (const prop in source) {
98
+ if (source.hasOwnProperty(prop)) {
99
+ if (prop in target) {
100
+ // handling merging of two properties with equal names
101
+ if (typeof target[prop] !== 'object') {
102
+ target[prop] = source[prop];
103
+ }
104
+ else {
105
+ if (typeof source[prop] !== 'object') {
106
+ target[prop] = source[prop];
107
+ }
108
+ else {
109
+ if (target[prop].concat && source[prop].concat) {
110
+ // two arrays get concatenated
111
+ target[prop] = target[prop].concat(source[prop]);
112
+ }
113
+ else {
114
+ // two objects get merged recursively
115
+ target[prop] = deepMerge(target[prop], source[prop]);
116
+ }
117
+ }
118
+ }
119
+ }
120
+ else {
121
+ // new properties get added to target
122
+ target[prop] = source[prop];
123
+ }
124
+ }
125
+ }
126
+ }
127
+ return deepMerge(target, ...sources);
128
+ }
129
+ /**
130
+ * This method is similar to `Object.assign` with the exception that it will also extend the object properties when filled.
131
+ * There's also a distinction with extend vs merge, we are only extending when the property is not filled (if it is filled then it remains untouched and will not be merged)
132
+ * It also applies the change directly on the target object which mutates the original object.
133
+ * For example using these 2 objects: obj1 = { a: 1, b: { c: 2, d: 3 }} and obj2 = { b: { d: 2, e: 3}}:
134
+ * - Object.assign(obj1, obj2) => { a: 1, b: { e: 4 }}
135
+ * - objectAssignAndExtend(obj1, obj2) => { a: 1, b: { c: 2, d: 3, e: 4 }
136
+ * @param {Object} target - the target object — what to apply the sources properties and mutate into
137
+ * @param {Object} sources - the source object(s) — objects containing the properties you want to apply.
138
+ * @returns {Object} The target object.
139
+ */
140
+ export function objectAssignAndExtend(target, ...sources) {
141
+ if (!sources.length || sources[0] === undefined) {
142
+ return target;
143
+ }
144
+ const source = sources.shift();
145
+ // when target is not an object but source is an object, then we'll assign as object
146
+ target = (!isObject(target) && isObject(source)) ? {} : target;
147
+ if (isObject(target) && isObject(source)) {
148
+ for (const key of Object.keys(source)) {
149
+ if (typeof source[key] === 'object' && source[key] !== null) {
150
+ objectAssignAndExtend(target[key], source[key]);
151
+ }
152
+ if ((target[key] === null || target[key] === undefined) && source[key] !== null && source[key] !== undefined) {
153
+ target[key] = source[key];
154
+ }
155
+ }
156
+ }
157
+ return objectAssignAndExtend(target, ...sources);
158
+ }
159
+ /**
160
+ * Empty an object properties by looping through them all and deleting them
161
+ * @param obj - input object
162
+ */
163
+ export function emptyObject(obj) {
164
+ for (const key in obj) {
165
+ if (obj.hasOwnProperty(key)) {
166
+ delete obj[key];
167
+ }
168
+ }
169
+ obj = null;
170
+ obj = {};
171
+ return obj;
172
+ }
173
+ /**
174
+ * Check if an object is empty
175
+ * @param obj - input object
176
+ * @returns - boolean
177
+ */
178
+ export function isEmptyObject(obj) {
179
+ if (obj === null || obj === undefined) {
180
+ return true;
181
+ }
182
+ return Object.entries(obj).length === 0;
183
+ }
184
+ /**
185
+ * Simple object check.
186
+ * @param item
187
+ * @returns {boolean}
188
+ */
189
+ export function isObject(item) {
190
+ return item !== null && typeof item === 'object' && !Array.isArray(item) && !(item instanceof Date);
191
+ }
192
+ /**
193
+ * Simple check to detect if the value is a primitive type
194
+ * @param val
195
+ * @returns {boolean}
196
+ */
197
+ export function isPrimmitive(val) {
198
+ return val === null || val === undefined || typeof val === 'boolean' || typeof val === 'number' || typeof val === 'string';
199
+ }
200
+ /**
201
+ * Check if a value has any data (undefined, null or empty string will return False...)
202
+ * NOTE: a `false` boolean is consider as having data so it will return True
203
+ */
204
+ export function hasData(value) {
205
+ return value !== undefined && value !== null && value !== '';
206
+ }
207
+ /**
208
+ * Check if input value is a number, by default it won't be a strict checking
209
+ * but optionally we could check for strict equality, for example in strict "3" will return False but without strict it will return True
210
+ * @param value - input value of any type
211
+ * @param strict - when using strict it also check for strict equality, for example in strict "3" would return False but without strict it would return True
212
+ */
213
+ export function isNumber(value, strict = false) {
214
+ if (strict) {
215
+ return (value === null || value === undefined || typeof value === 'string') ? false : !isNaN(value);
216
+ }
217
+ return (value === null || value === undefined || value === '') ? false : !isNaN(+value);
218
+ }
219
+ /** Check if an object is empty, it will also be considered empty when the input is null, undefined or isn't an object */
220
+ export function isObjectEmpty(obj) {
221
+ return !obj || (obj && typeof obj === 'object' && Object.keys(obj).length === 0);
222
+ }
223
+ /** Parse any input (bool, number, string) and return a boolean or False when not possible */
224
+ export function parseBoolean(input) {
225
+ return /(true|1)/i.test(input + '');
226
+ }
227
+ /**
228
+ * Remove any accents from a string by normalizing it
229
+ * @param {String} text - input text
230
+ * @param {Boolean} shouldLowerCase - should we also lowercase the string output?
231
+ * @returns
232
+ */
233
+ export function removeAccentFromText(text, shouldLowerCase = false) {
234
+ const normalizedText = (typeof text.normalize === 'function') ? text.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : text;
235
+ return shouldLowerCase ? normalizedText.toLowerCase() : normalizedText;
236
+ }
237
+ /** Set the object value of deeper node from a given dot (.) notation path (e.g.: "user.firstName") */
238
+ export function setDeepValue(obj, path, value) {
239
+ if (typeof path === 'string') {
240
+ path = path.split('.');
241
+ }
242
+ if (path.length > 1) {
243
+ const e = path.shift();
244
+ if (obj && e !== undefined) {
245
+ setDeepValue((obj)[e] = (hasData(obj[e]) && (Array.isArray(obj[e]) || Object.prototype.toString.call((obj)[e]) === '[object Object]'))
246
+ ? (obj)[e]
247
+ : {}, path, value);
248
+ }
249
+ }
250
+ else if (obj && path[0]) {
251
+ (obj)[path[0]] = value;
252
+ }
253
+ }
254
+ /**
255
+ * Title case (or capitalize) first char of a string, for example "hello world" will become "Hello world"
256
+ * Change the string to be title case on the complete sentence (upper case first char of each word while changing everything else to lower case)
257
+ * @param inputStr
258
+ * @returns string
259
+ */
260
+ export function titleCase(inputStr, shouldTitleCaseEveryWords = false) {
261
+ if (typeof inputStr === 'string') {
262
+ if (shouldTitleCaseEveryWords) {
263
+ return inputStr.replace(/\w\S*/g, (outputStr) => {
264
+ return outputStr.charAt(0).toUpperCase() + outputStr.substring(1).toLowerCase();
265
+ });
266
+ }
267
+ return inputStr.charAt(0).toUpperCase() + inputStr.slice(1);
268
+ }
269
+ return inputStr;
270
+ }
271
+ /**
272
+ * Converts a string to camel case (camelCase), for example "hello-world" (or "hellow world") will become "helloWorld"
273
+ * @param inputStr the string to convert
274
+ * @return the string in camel case
275
+ */
276
+ export function toCamelCase(inputStr) {
277
+ if (typeof inputStr === 'string') {
278
+ return inputStr.replace(/(?:^\w|[A-Z]|\b\w|[\s+\-_\/])/g, (match, offset) => {
279
+ // remove white space or hypens or underscores
280
+ if (/[\s+\-_\/]/.test(match)) {
281
+ return '';
282
+ }
283
+ return offset === 0 ? match.toLowerCase() : match.toUpperCase();
284
+ });
285
+ }
286
+ return inputStr;
287
+ }
288
+ /**
289
+ * Converts a string to kebab (hypen) case, for example "helloWorld" will become "hello-world"
290
+ * @param str the string to convert
291
+ * @return the string in kebab case
292
+ */
293
+ export function toKebabCase(inputStr) {
294
+ if (typeof inputStr === 'string') {
295
+ return toCamelCase(inputStr).replace(/([A-Z])/g, '-$1').toLowerCase();
296
+ }
297
+ return inputStr;
298
+ }
299
+ /**
300
+ * Converts a camelCase or kebab-case string to a sentence case, for example "helloWorld" will become "Hello World" and "hello-world" will become "Hello world"
301
+ * @param str the string to convert
302
+ * @return the string in kebab case
303
+ */
304
+ export function toSentenceCase(inputStr) {
305
+ if (typeof inputStr === 'string') {
306
+ const result = inputStr.replace(/([A-Z])|(\-)/g, ' $1').replace(/\s+/g, ' ').trim();
307
+ return result.charAt(0).toUpperCase() + result.slice(1);
308
+ }
309
+ return inputStr;
310
+ }
311
+ /**
312
+ * Converts a string from camelCase to snake_case (underscore) case
313
+ * @param str the string to convert
314
+ * @return the string in kebab case
315
+ */
316
+ export function toSnakeCase(inputStr) {
317
+ if (typeof inputStr === 'string') {
318
+ return toCamelCase(inputStr).replace(/([A-Z])/g, '_$1').toLowerCase();
319
+ }
320
+ return inputStr;
321
+ }
322
+ /**
323
+ * Takes an input array and makes sure the array has unique values by removing duplicates
324
+ * @param array input with possible duplicates
325
+ * @return array output without duplicates
326
+ */
327
+ export function uniqueArray(arr) {
328
+ if (Array.isArray(arr) && arr.length > 0) {
329
+ return arr.filter((item, index) => {
330
+ return arr.indexOf(item) >= index;
331
+ });
332
+ }
333
+ return arr;
334
+ }
335
+ /**
336
+ * Takes an input array of objects and makes sure the array has unique object values by removing duplicates
337
+ * it will loop through the array using a property name (or "id" when is not provided) to compare uniqueness
338
+ * @param array input with possible duplicates
339
+ * @param propertyName defaults to "id"
340
+ * @return array output without duplicates
341
+ */
342
+ export function uniqueObjectArray(arr, propertyName = 'id') {
343
+ if (Array.isArray(arr) && arr.length > 0) {
344
+ const result = [];
345
+ const map = new Map();
346
+ for (const item of arr) {
347
+ if (item && !map.has(item[propertyName])) {
348
+ map.set(item[propertyName], true); // set any value to Map
349
+ result.push({
350
+ id: item[propertyName],
351
+ name: item.name
352
+ });
353
+ }
354
+ }
355
+ return result;
356
+ }
357
+ return arr;
358
+ }
359
359
  //# sourceMappingURL=utils.js.map