@slickgrid-universal/utils 5.10.2 → 5.12.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/src/utils.ts CHANGED
@@ -74,10 +74,10 @@ export function deepMerge(target: any, ...sources: any[]): any {
74
74
  const source = sources.shift();
75
75
 
76
76
  // when target is not an object but source is an object, then we'll assign as object
77
- target = (!isObject(target) && isObject(source)) ? {} : target;
77
+ target = !isObject(target) && isObject(source) ? {} : target;
78
78
 
79
79
  if (isObject(target) && isObject(source)) {
80
- Object.keys(source).forEach(prop => {
80
+ Object.keys(source).forEach((prop) => {
81
81
  if (source.hasOwnProperty(prop)) {
82
82
  if (prop in target) {
83
83
  // handling merging of two properties with equal names
@@ -112,7 +112,7 @@ export function deepMerge(target: any, ...sources: any[]): any {
112
112
  */
113
113
  export function emptyObject(obj: any): any {
114
114
  if (isObject(obj)) {
115
- Object.keys(obj).forEach(key => {
115
+ Object.keys(obj).forEach((key) => {
116
116
  if (obj.hasOwnProperty(key)) {
117
117
  delete obj[key];
118
118
  }
@@ -131,7 +131,10 @@ export function emptyObject(obj: any): any {
131
131
  * @param {Boolean} [addReturn] - when using ES6 function as single liner, we could add the missing `return ...`
132
132
  * @returns
133
133
  */
134
- export function getFunctionDetails(fn: AnyFunction, addReturn = true): {
134
+ export function getFunctionDetails(
135
+ fn: AnyFunction,
136
+ addReturn = true
137
+ ): {
135
138
  params: string[];
136
139
  body: string;
137
140
  isAsync: boolean;
@@ -143,13 +146,13 @@ export function getFunctionDetails(fn: AnyFunction, addReturn = true): {
143
146
  isAsyncFn = fnStr.includes('async ');
144
147
 
145
148
  // when fn is one liner arrow fn returning an object in brackets e.g. `() => ({ hello: 'world' })`
146
- if ((fnStr.replaceAll(' ', '').includes('=>({'))) {
149
+ if (fnStr.replaceAll(' ', '').includes('=>({')) {
147
150
  const matches = fnStr.match(/(({.*}))/g) || [];
148
151
  return matches.length >= 1 ? `return ${matches[0]!.trimStart()}` : fnStr;
149
152
  }
150
- const isOneLinerArrowFn = (!fnStr.includes('{') && fnStr.includes('=>'));
153
+ const isOneLinerArrowFn = !fnStr.includes('{') && fnStr.includes('=>');
151
154
  const body = fnStr.substring(
152
- (fnStr.indexOf('{') + 1) || (fnStr.indexOf('=>') + 2),
155
+ fnStr.indexOf('{') + 1 || fnStr.indexOf('=>') + 2,
153
156
  fnStr.includes('}') ? fnStr.lastIndexOf('}') : fnStr.length
154
157
  );
155
158
  if (addReturn && isOneLinerArrowFn && !body.startsWith('return')) {
@@ -159,7 +162,7 @@ export function getFunctionDetails(fn: AnyFunction, addReturn = true): {
159
162
  };
160
163
 
161
164
  const getFunctionParams = (func: AnyFunction): string[] => {
162
- const STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,)]*))/mg;
165
+ const STRIP_COMMENTS = /(\/\/.*$)|(\/\*[\s\S]*?\*\/)|(\s*=[^,)]*(('(?:\\'|[^'\r\n])*')|("(?:\\"|[^"\r\n])*"))|(\s*=[^,)]*))/gm;
163
166
  const ARG_NAMES = /([^\s,]+)/g;
164
167
  const fnStr = func.toString().replace(STRIP_COMMENTS, '');
165
168
  return fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')')).match(ARG_NAMES) ?? [];
@@ -222,9 +225,9 @@ export function isPrimitiveOrHTML(val: any): boolean {
222
225
  */
223
226
  export function isNumber(value: any, strict = false): value is number {
224
227
  if (strict) {
225
- return (value === null || value === undefined || typeof value === 'string') ? false : !isNaN(value);
228
+ return value === null || value === undefined || typeof value === 'string' ? false : !isNaN(value);
226
229
  }
227
- return (value === null || value === undefined || value === '') ? false : !isNaN(+value);
230
+ return value === null || value === undefined || value === '' ? false : !isNaN(+value);
228
231
  }
229
232
 
230
233
  /** Check if an object is empty, it will also be considered empty when the input is null, undefined or isn't an object */
@@ -244,7 +247,7 @@ export function parseBoolean(input: any): boolean {
244
247
  * @returns
245
248
  */
246
249
  export function removeAccentFromText(text: string, shouldLowerCase = false): string {
247
- const normalizedText = (typeof text.normalize === 'function') ? text.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : text;
250
+ const normalizedText = typeof text.normalize === 'function' ? text.normalize('NFD').replace(/[\u0300-\u036f]/g, '') : text;
248
251
  return shouldLowerCase ? normalizedText.toLowerCase() : normalizedText;
249
252
  }
250
253
 
@@ -258,15 +261,16 @@ export function setDeepValue<T = unknown>(obj: T, path: string | string[], value
258
261
  const e = path.shift() as keyof T;
259
262
  if (obj && e !== undefined) {
260
263
  setDeepValue(
261
- (obj)[e] = (isDefined(obj[e]) && (Array.isArray(obj[e]) || Object.prototype.toString.call((obj)[e]) === '[object Object]'))
262
- ? (obj)[e]
263
- : {} as any,
264
+ (obj[e] =
265
+ isDefined(obj[e]) && (Array.isArray(obj[e]) || Object.prototype.toString.call(obj[e]) === '[object Object]')
266
+ ? obj[e]
267
+ : ({} as any)),
264
268
  path,
265
269
  value
266
270
  );
267
271
  }
268
272
  } else if (obj && path[0]) {
269
- (obj)[path[0] as keyof T] = value;
273
+ obj[path[0] as keyof T] = value;
270
274
  }
271
275
  }
272
276
 
@@ -314,7 +318,9 @@ export function toCamelCase(inputStr: string): string {
314
318
  */
315
319
  export function toKebabCase(inputStr: string): string {
316
320
  if (typeof inputStr === 'string') {
317
- return toCamelCase(inputStr).replace(/([A-Z])|([-_])/g, '-$1').toLowerCase();
321
+ return toCamelCase(inputStr)
322
+ .replace(/([A-Z])|([-_])/g, '-$1')
323
+ .toLowerCase();
318
324
  }
319
325
  return inputStr;
320
326
  }
@@ -326,7 +332,10 @@ export function toKebabCase(inputStr: string): string {
326
332
  */
327
333
  export function toSentenceCase(inputStr: string): string {
328
334
  if (typeof inputStr === 'string') {
329
- const result = inputStr.replace(/([A-Z])|([-_])/g, ' $1').replace(/\s+/g, ' ').trim();
335
+ const result = inputStr
336
+ .replace(/([A-Z])|([-_])/g, ' $1')
337
+ .replace(/\s+/g, ' ')
338
+ .trim();
330
339
  return result.charAt(0).toUpperCase() + result.slice(1);
331
340
  }
332
341
  return inputStr;
@@ -339,7 +348,9 @@ export function toSentenceCase(inputStr: string): string {
339
348
  */
340
349
  export function toSnakeCase(inputStr: string): string {
341
350
  if (typeof inputStr === 'string') {
342
- return toCamelCase(inputStr).replace(/([A-Z])/g, '_$1').toLowerCase();
351
+ return toCamelCase(inputStr)
352
+ .replace(/([A-Z])/g, '_$1')
353
+ .toLowerCase();
343
354
  }
344
355
  return inputStr;
345
356
  }
@@ -372,10 +383,10 @@ export function uniqueObjectArray(arr: any[], propertyName = 'id'): any[] {
372
383
 
373
384
  for (const item of arr) {
374
385
  if (item && !map.has(item[propertyName])) {
375
- map.set(item[propertyName], true); // set any value to Map
386
+ map.set(item[propertyName], true); // set any value to Map
376
387
  result.push({
377
388
  id: item[propertyName],
378
- name: item.name
389
+ name: item.name,
379
390
  });
380
391
  }
381
392
  }