@etsoo/shared 1.0.78 → 1.0.79

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.
@@ -100,8 +100,10 @@ test('Tests for objectUpdated', () => {
100
100
  });
101
101
 
102
102
  test('Tests for parseString', () => {
103
- expect(Utils.parseString('test', '')).toBe('test');
103
+ expect(Utils.parseString<string>('test')).toBe('test');
104
104
  expect(Utils.parseString('true', false)).toBe(true);
105
+ expect(Utils.parseString('', false)).toBeFalsy();
106
+ expect(Utils.parseString<number>(undefined)).toBeUndefined();
105
107
  expect(Utils.parseString('3.14', 0)).toBe(3.14);
106
108
  expect(Utils.parseString('2021/4/13', new Date())).toStrictEqual(
107
109
  new Date('2021/4/13')
@@ -20,7 +20,7 @@ export declare namespace StorageUtils {
20
20
  * @param key Key name
21
21
  * @param defaultValue Default value
22
22
  */
23
- function getLocalData<T>(key: string, defaultValue: T): T;
23
+ function getLocalData<T>(key: string, defaultValue?: T): T | undefined;
24
24
  /**
25
25
  * Get local storage object data
26
26
  * @param key Key name
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
30
30
  * Get session storage data
31
31
  * @param key Key name
32
32
  */
33
- function getSessionData<T>(key: string, defaultValue: T): T;
33
+ function getSessionData<T>(key: string, defaultValue?: T): T | undefined;
34
34
  /**
35
35
  * Get session storage object data
36
36
  * @param key Key name
@@ -135,12 +135,13 @@ export declare namespace Utils {
135
135
  */
136
136
  function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
137
137
  /**
138
- * Parse string (JSON) to specific type
138
+ * Parse string (JSON) to specific type, no type conversion
139
+ * For type conversion, please use DataTypes.convert
139
140
  * @param input Input string
140
141
  * @param defaultValue Default value
141
142
  * @returns Parsed value
142
143
  */
143
- function parseString<T>(input: string | undefined | null, defaultValue: T): T;
144
+ function parseString<T>(input: string | undefined | null, defaultValue?: T): T | undefined;
144
145
  /**
145
146
  * Remove non letters
146
147
  * @param input Input string
package/lib/cjs/Utils.js CHANGED
@@ -267,14 +267,15 @@ var Utils;
267
267
  }
268
268
  Utils.objectUpdated = objectUpdated;
269
269
  /**
270
- * Parse string (JSON) to specific type
270
+ * Parse string (JSON) to specific type, no type conversion
271
+ * For type conversion, please use DataTypes.convert
271
272
  * @param input Input string
272
273
  * @param defaultValue Default value
273
274
  * @returns Parsed value
274
275
  */
275
276
  function parseString(input, defaultValue) {
276
- // Undefined case, return default value
277
- if (input == null)
277
+ // Undefined and empty case, return default value
278
+ if (input == null || input === '')
278
279
  return defaultValue;
279
280
  // String
280
281
  if (typeof defaultValue === 'string')
@@ -292,16 +293,11 @@ var Utils;
292
293
  // Return
293
294
  return json;
294
295
  }
295
- catch (e) {
296
- console.log('Utils.parseString error', e);
296
+ catch {
297
+ if (defaultValue == null)
298
+ return input;
297
299
  return defaultValue;
298
300
  }
299
- /*
300
- finally part will still return the boolean value
301
- finally {
302
- return defaultValue
303
- }
304
- */
305
301
  }
306
302
  Utils.parseString = parseString;
307
303
  /**
@@ -20,7 +20,7 @@ export declare namespace StorageUtils {
20
20
  * @param key Key name
21
21
  * @param defaultValue Default value
22
22
  */
23
- function getLocalData<T>(key: string, defaultValue: T): T;
23
+ function getLocalData<T>(key: string, defaultValue?: T): T | undefined;
24
24
  /**
25
25
  * Get local storage object data
26
26
  * @param key Key name
@@ -30,7 +30,7 @@ export declare namespace StorageUtils {
30
30
  * Get session storage data
31
31
  * @param key Key name
32
32
  */
33
- function getSessionData<T>(key: string, defaultValue: T): T;
33
+ function getSessionData<T>(key: string, defaultValue?: T): T | undefined;
34
34
  /**
35
35
  * Get session storage object data
36
36
  * @param key Key name
@@ -135,12 +135,13 @@ export declare namespace Utils {
135
135
  */
136
136
  function objectUpdated(objNew: {}, objPrev: {}, ignoreFields?: string[], strict?: number): string[];
137
137
  /**
138
- * Parse string (JSON) to specific type
138
+ * Parse string (JSON) to specific type, no type conversion
139
+ * For type conversion, please use DataTypes.convert
139
140
  * @param input Input string
140
141
  * @param defaultValue Default value
141
142
  * @returns Parsed value
142
143
  */
143
- function parseString<T>(input: string | undefined | null, defaultValue: T): T;
144
+ function parseString<T>(input: string | undefined | null, defaultValue?: T): T | undefined;
144
145
  /**
145
146
  * Remove non letters
146
147
  * @param input Input string
package/lib/mjs/Utils.js CHANGED
@@ -264,14 +264,15 @@ export var Utils;
264
264
  }
265
265
  Utils.objectUpdated = objectUpdated;
266
266
  /**
267
- * Parse string (JSON) to specific type
267
+ * Parse string (JSON) to specific type, no type conversion
268
+ * For type conversion, please use DataTypes.convert
268
269
  * @param input Input string
269
270
  * @param defaultValue Default value
270
271
  * @returns Parsed value
271
272
  */
272
273
  function parseString(input, defaultValue) {
273
- // Undefined case, return default value
274
- if (input == null)
274
+ // Undefined and empty case, return default value
275
+ if (input == null || input === '')
275
276
  return defaultValue;
276
277
  // String
277
278
  if (typeof defaultValue === 'string')
@@ -289,16 +290,11 @@ export var Utils;
289
290
  // Return
290
291
  return json;
291
292
  }
292
- catch (e) {
293
- console.log('Utils.parseString error', e);
293
+ catch {
294
+ if (defaultValue == null)
295
+ return input;
294
296
  return defaultValue;
295
297
  }
296
- /*
297
- finally part will still return the boolean value
298
- finally {
299
- return defaultValue
300
- }
301
- */
302
298
  }
303
299
  Utils.parseString = parseString;
304
300
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.0.78",
3
+ "version": "1.0.79",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -55,13 +55,13 @@
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
57
  "@types/jest": "^27.0.3",
58
- "@typescript-eslint/eslint-plugin": "^5.4.0",
59
- "@typescript-eslint/parser": "^5.4.0",
60
- "eslint": "^8.2.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.8.0",
59
+ "@typescript-eslint/parser": "^5.8.0",
60
+ "eslint": "^8.5.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.3",
63
- "jest": "^27.3.1",
64
- "ts-jest": "^27.0.7",
65
- "typescript": "^4.5.2"
63
+ "jest": "^27.4.5",
64
+ "ts-jest": "^27.1.2",
65
+ "typescript": "^4.5.4"
66
66
  }
67
67
  }
@@ -49,7 +49,7 @@ export namespace StorageUtils {
49
49
  * @param key Key name
50
50
  * @param defaultValue Default value
51
51
  */
52
- export function getLocalData<T>(key: string, defaultValue: T) {
52
+ export function getLocalData<T>(key: string, defaultValue?: T) {
53
53
  // Get storage
54
54
  const data = localStorage.getItem(key);
55
55
 
@@ -72,7 +72,7 @@ export namespace StorageUtils {
72
72
  * Get session storage data
73
73
  * @param key Key name
74
74
  */
75
- export function getSessionData<T>(key: string, defaultValue: T) {
75
+ export function getSessionData<T>(key: string, defaultValue?: T) {
76
76
  // Get storage
77
77
  const data = sessionStorage.getItem(key);
78
78
 
package/src/Utils.ts CHANGED
@@ -362,17 +362,18 @@ export namespace Utils {
362
362
  }
363
363
 
364
364
  /**
365
- * Parse string (JSON) to specific type
365
+ * Parse string (JSON) to specific type, no type conversion
366
+ * For type conversion, please use DataTypes.convert
366
367
  * @param input Input string
367
368
  * @param defaultValue Default value
368
369
  * @returns Parsed value
369
370
  */
370
371
  export function parseString<T>(
371
372
  input: string | undefined | null,
372
- defaultValue: T
373
- ): T {
374
- // Undefined case, return default value
375
- if (input == null) return defaultValue;
373
+ defaultValue?: T
374
+ ): T | undefined {
375
+ // Undefined and empty case, return default value
376
+ if (input == null || input === '') return defaultValue;
376
377
 
377
378
  // String
378
379
  if (typeof defaultValue === 'string') return <any>input;
@@ -390,16 +391,10 @@ export namespace Utils {
390
391
 
391
392
  // Return
392
393
  return <T>json;
393
- } catch (e) {
394
- console.log('Utils.parseString error', e);
394
+ } catch {
395
+ if (defaultValue == null) return <any>input;
395
396
  return defaultValue;
396
397
  }
397
- /*
398
- finally part will still return the boolean value
399
- finally {
400
- return defaultValue
401
- }
402
- */
403
398
  }
404
399
 
405
400
  /**