@etsoo/shared 1.1.6 → 1.1.10

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/README.md CHANGED
@@ -151,12 +151,18 @@ String and other related utilities
151
151
 
152
152
  |Name|Description|
153
153
  |---:|---|
154
+ |addBlankItem|Add blank item to collection|
154
155
  |charsToNumber|Base64 chars to number|
156
+ |correctTypes|Correct object's property value type|
155
157
  |equals|Two values equal|
156
158
  |formatInitial|Format inital character to lower case or upper case|
157
159
  |formatString|Format string with parameters|
158
160
  |getDataChanges|Get data changed fields with input data updated|
159
161
  |getTimeZone|Get time zone|
162
+ |hideData|Hide data|
163
+ |hideEmail|Hide email data|
164
+ |isDigits|Is digits string|
165
+ |isEmail|Is email string|
160
166
  |joinItems|Join items as a string|
161
167
  |mergeFormData|Merge form data to primary one|
162
168
  |mergeClasses|Merge class names|
@@ -166,5 +172,6 @@ String and other related utilities
166
172
  |objectKeys|Get two object's unqiue properties|
167
173
  |objectUpdated|Get the new object's updated fields contrast to the previous object|
168
174
  |parseString|Parse string (JSON) to specific type|
175
+ |removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
169
176
  |setLabels|Set source with new labels|
170
177
  |snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
@@ -1,5 +1,41 @@
1
1
  import { Utils } from '../src/Utils';
2
2
 
3
+ test('Tests for addBlankItem', () => {
4
+ const options = [
5
+ { id: 1, name: 'a' },
6
+ { id: 2, name: 'b' }
7
+ ];
8
+ Utils.addBlankItem(options, 'id', 'name');
9
+ expect(options.length).toBe(3);
10
+ expect(options[0].id).toBe('');
11
+ expect(options[0].name).toBe('---');
12
+ });
13
+
14
+ test('Tests for correctTypes', () => {
15
+ const input = {
16
+ id: '1',
17
+ price: '6.0',
18
+ amount: '',
19
+ date: '2022/01/28',
20
+ enabled: 'true',
21
+ ids: ['1', '2']
22
+ };
23
+ Utils.correctTypes(input, {
24
+ id: 'number',
25
+ price: 'number',
26
+ amount: 'number',
27
+ date: 'date',
28
+ enabled: 'boolean',
29
+ ids: 'number[]'
30
+ });
31
+ expect(typeof input.id).toBe('number');
32
+ expect(typeof input.price).toBe('number');
33
+ expect(input.amount).toBeUndefined();
34
+ expect((input.date as any) instanceof Date ? true : false).toBeTruthy();
35
+ expect(input.enabled).toBeTruthy();
36
+ expect(input.ids).toStrictEqual([1, 2]);
37
+ });
38
+
3
39
  test('Tests for getDataChanges', () => {
4
40
  const input = {
5
41
  id: 1,
@@ -41,6 +77,13 @@ test('Tests for formatString', () => {
41
77
  expect(template.format('aa', 'bb')).toBe(result);
42
78
  });
43
79
 
80
+ test('Tests for hideData', () => {
81
+ expect('xz@etsoo.com'.hideEmail()).toBe('x***@etsoo.com');
82
+ expect('info@etsoo.com'.hideEmail()).toBe('in***@etsoo.com');
83
+ expect('info@etsoo.com'.hideData('@')).toBe('in***@etsoo.com');
84
+ expect('12345678'.hideData()).toBe('123***678');
85
+ });
86
+
44
87
  test('Tests for isDigits', () => {
45
88
  expect(Utils.isDigits('1')).toBeTruthy();
46
89
  expect(Utils.isDigits('12', 3)).toBeFalsy();
@@ -48,7 +48,7 @@ export declare namespace DateUtils {
48
48
  * Format to 'yyyy-MM-dd', especially used for date input min/max property
49
49
  * @param date Input date
50
50
  */
51
- function formatForInput(date?: Date | string): string;
51
+ function formatForInput(date?: Date | string | null): string;
52
52
  /**
53
53
  * Get month's days
54
54
  * @param year Year
@@ -13,6 +13,17 @@ declare global {
13
13
  * @param upperCase To upper case or lower case
14
14
  */
15
15
  formatInitial(this: string, upperCase: boolean): string;
16
+ /**
17
+ * Hide data
18
+ * @param this Input string
19
+ * @param endChar End char
20
+ */
21
+ hideData(this: string, endChar?: string): string;
22
+ /**
23
+ * Hide email data
24
+ * @param this Input email
25
+ */
26
+ hideEmail(this: string): string;
16
27
  /**
17
28
  * Is digits string
18
29
  * @param this Input string
@@ -35,12 +46,28 @@ declare global {
35
46
  * Utilities
36
47
  */
37
48
  export declare namespace Utils {
49
+ /**
50
+ * Add blank item to collection
51
+ * @param options Options
52
+ * @param idField Id field, default is id
53
+ * @param labelField Label field, default is label
54
+ * @param blankLabel Blank label, default is ---
55
+ */
56
+ function addBlankItem<T extends {}>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
38
57
  /**
39
58
  * Base64 chars to number
40
59
  * @param base64Chars Base64 chars
41
60
  * @returns Number
42
61
  */
43
62
  function charsToNumber(base64Chars: string): number;
63
+ /**
64
+ * Correct object's property value type
65
+ * @param input Input object
66
+ * @param fields Fields to correct
67
+ */
68
+ function correctTypes<T extends {}, F extends {
69
+ [P in keyof T]: DataTypes.BasicNames;
70
+ }>(input: T, fields: F): void;
44
71
  /**
45
72
  * Two values equal
46
73
  * @param v1 Value 1
package/lib/cjs/Utils.js CHANGED
@@ -14,6 +14,27 @@ String.prototype.formatInitial = function (upperCase = false) {
14
14
  return ((upperCase ? initial.toUpperCase() : initial.toLowerCase()) +
15
15
  this.slice(1));
16
16
  };
17
+ String.prototype.hideData = function (endChar) {
18
+ if (endChar != null) {
19
+ const index = this.indexOf(endChar);
20
+ if (index === -1)
21
+ return this.hideData();
22
+ return this.substring(0, index).hideData() + this.substring(index);
23
+ }
24
+ var len = this.length;
25
+ if (len < 4)
26
+ return this.substring(0, 1) + '***';
27
+ if (len < 6)
28
+ return this.substring(0, 2) + '***';
29
+ if (len < 8)
30
+ return this.substring(0, 2) + '***' + this.slice(-2);
31
+ if (len < 12)
32
+ return this.substring(0, 3) + '***' + this.slice(-3);
33
+ return this.substring(0, 4) + '***' + this.slice(-4);
34
+ };
35
+ String.prototype.hideEmail = function () {
36
+ return this.hideData('@');
37
+ };
17
38
  String.prototype.isDigits = function (minLength) {
18
39
  return this.length >= (minLength !== null && minLength !== void 0 ? minLength : 0) && /^\d+$/.test(this);
19
40
  };
@@ -29,6 +50,21 @@ String.prototype.removeNonLetters = function () {
29
50
  */
30
51
  var Utils;
31
52
  (function (Utils) {
53
+ /**
54
+ * Add blank item to collection
55
+ * @param options Options
56
+ * @param idField Id field, default is id
57
+ * @param labelField Label field, default is label
58
+ * @param blankLabel Blank label, default is ---
59
+ */
60
+ function addBlankItem(options, idField, labelField, blankLabel) {
61
+ const blankItem = {
62
+ [idField !== null && idField !== void 0 ? idField : 'id']: '',
63
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
64
+ };
65
+ options.unshift(blankItem);
66
+ }
67
+ Utils.addBlankItem = addBlankItem;
32
68
  /**
33
69
  * Base64 chars to number
34
70
  * @param base64Chars Base64 chars
@@ -43,6 +79,21 @@ var Utils;
43
79
  }, 0);
44
80
  }
45
81
  Utils.charsToNumber = charsToNumber;
82
+ /**
83
+ * Correct object's property value type
84
+ * @param input Input object
85
+ * @param fields Fields to correct
86
+ */
87
+ function correctTypes(input, fields) {
88
+ for (const field in fields) {
89
+ const value = Reflect.get(input, field);
90
+ const newValue = DataTypes_1.DataTypes.convertByType(value, fields[field]);
91
+ if (newValue !== value) {
92
+ Reflect.set(input, field, newValue);
93
+ }
94
+ }
95
+ }
96
+ Utils.correctTypes = correctTypes;
46
97
  /**
47
98
  * Two values equal
48
99
  * @param v1 Value 1
@@ -48,7 +48,7 @@ export declare namespace DateUtils {
48
48
  * Format to 'yyyy-MM-dd', especially used for date input min/max property
49
49
  * @param date Input date
50
50
  */
51
- function formatForInput(date?: Date | string): string;
51
+ function formatForInput(date?: Date | string | null): string;
52
52
  /**
53
53
  * Get month's days
54
54
  * @param year Year
@@ -13,6 +13,17 @@ declare global {
13
13
  * @param upperCase To upper case or lower case
14
14
  */
15
15
  formatInitial(this: string, upperCase: boolean): string;
16
+ /**
17
+ * Hide data
18
+ * @param this Input string
19
+ * @param endChar End char
20
+ */
21
+ hideData(this: string, endChar?: string): string;
22
+ /**
23
+ * Hide email data
24
+ * @param this Input email
25
+ */
26
+ hideEmail(this: string): string;
16
27
  /**
17
28
  * Is digits string
18
29
  * @param this Input string
@@ -35,12 +46,28 @@ declare global {
35
46
  * Utilities
36
47
  */
37
48
  export declare namespace Utils {
49
+ /**
50
+ * Add blank item to collection
51
+ * @param options Options
52
+ * @param idField Id field, default is id
53
+ * @param labelField Label field, default is label
54
+ * @param blankLabel Blank label, default is ---
55
+ */
56
+ function addBlankItem<T extends {}>(options: T[], idField?: string | keyof T, labelField?: unknown, blankLabel?: string): void;
38
57
  /**
39
58
  * Base64 chars to number
40
59
  * @param base64Chars Base64 chars
41
60
  * @returns Number
42
61
  */
43
62
  function charsToNumber(base64Chars: string): number;
63
+ /**
64
+ * Correct object's property value type
65
+ * @param input Input object
66
+ * @param fields Fields to correct
67
+ */
68
+ function correctTypes<T extends {}, F extends {
69
+ [P in keyof T]: DataTypes.BasicNames;
70
+ }>(input: T, fields: F): void;
44
71
  /**
45
72
  * Two values equal
46
73
  * @param v1 Value 1
package/lib/mjs/Utils.js CHANGED
@@ -11,6 +11,27 @@ String.prototype.formatInitial = function (upperCase = false) {
11
11
  return ((upperCase ? initial.toUpperCase() : initial.toLowerCase()) +
12
12
  this.slice(1));
13
13
  };
14
+ String.prototype.hideData = function (endChar) {
15
+ if (endChar != null) {
16
+ const index = this.indexOf(endChar);
17
+ if (index === -1)
18
+ return this.hideData();
19
+ return this.substring(0, index).hideData() + this.substring(index);
20
+ }
21
+ var len = this.length;
22
+ if (len < 4)
23
+ return this.substring(0, 1) + '***';
24
+ if (len < 6)
25
+ return this.substring(0, 2) + '***';
26
+ if (len < 8)
27
+ return this.substring(0, 2) + '***' + this.slice(-2);
28
+ if (len < 12)
29
+ return this.substring(0, 3) + '***' + this.slice(-3);
30
+ return this.substring(0, 4) + '***' + this.slice(-4);
31
+ };
32
+ String.prototype.hideEmail = function () {
33
+ return this.hideData('@');
34
+ };
14
35
  String.prototype.isDigits = function (minLength) {
15
36
  return this.length >= (minLength !== null && minLength !== void 0 ? minLength : 0) && /^\d+$/.test(this);
16
37
  };
@@ -26,6 +47,21 @@ String.prototype.removeNonLetters = function () {
26
47
  */
27
48
  export var Utils;
28
49
  (function (Utils) {
50
+ /**
51
+ * Add blank item to collection
52
+ * @param options Options
53
+ * @param idField Id field, default is id
54
+ * @param labelField Label field, default is label
55
+ * @param blankLabel Blank label, default is ---
56
+ */
57
+ function addBlankItem(options, idField, labelField, blankLabel) {
58
+ const blankItem = {
59
+ [idField !== null && idField !== void 0 ? idField : 'id']: '',
60
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
61
+ };
62
+ options.unshift(blankItem);
63
+ }
64
+ Utils.addBlankItem = addBlankItem;
29
65
  /**
30
66
  * Base64 chars to number
31
67
  * @param base64Chars Base64 chars
@@ -40,6 +76,21 @@ export var Utils;
40
76
  }, 0);
41
77
  }
42
78
  Utils.charsToNumber = charsToNumber;
79
+ /**
80
+ * Correct object's property value type
81
+ * @param input Input object
82
+ * @param fields Fields to correct
83
+ */
84
+ function correctTypes(input, fields) {
85
+ for (const field in fields) {
86
+ const value = Reflect.get(input, field);
87
+ const newValue = DataTypes.convertByType(value, fields[field]);
88
+ if (newValue !== value) {
89
+ Reflect.set(input, field, newValue);
90
+ }
91
+ }
92
+ }
93
+ Utils.correctTypes = correctTypes;
43
94
  /**
44
95
  * Two values equal
45
96
  * @param v1 Value 1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.6",
3
+ "version": "1.1.10",
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.4.0",
58
- "@typescript-eslint/eslint-plugin": "^5.10.0",
59
- "@typescript-eslint/parser": "^5.10.0",
60
- "eslint": "^8.7.0",
58
+ "@typescript-eslint/eslint-plugin": "^5.10.1",
59
+ "@typescript-eslint/parser": "^5.10.1",
60
+ "eslint": "^8.8.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
63
  "jest": "^27.4.7",
64
64
  "ts-jest": "^27.1.3",
65
- "typescript": "^4.5.4"
65
+ "typescript": "^4.5.5"
66
66
  }
67
67
  }
package/src/DateUtils.ts CHANGED
@@ -148,7 +148,7 @@ export namespace DateUtils {
148
148
  * Format to 'yyyy-MM-dd', especially used for date input min/max property
149
149
  * @param date Input date
150
150
  */
151
- export function formatForInput(date?: Date | string) {
151
+ export function formatForInput(date?: Date | string | null) {
152
152
  // Parse string as date
153
153
  if (typeof date === 'string') date = new Date(date);
154
154
 
package/src/Utils.ts CHANGED
@@ -16,6 +16,19 @@ declare global {
16
16
  */
17
17
  formatInitial(this: string, upperCase: boolean): string;
18
18
 
19
+ /**
20
+ * Hide data
21
+ * @param this Input string
22
+ * @param endChar End char
23
+ */
24
+ hideData(this: string, endChar?: string): string;
25
+
26
+ /**
27
+ * Hide email data
28
+ * @param this Input email
29
+ */
30
+ hideEmail(this: string): string;
31
+
19
32
  /**
20
33
  * Is digits string
21
34
  * @param this Input string
@@ -59,6 +72,26 @@ String.prototype.formatInitial = function (
59
72
  );
60
73
  };
61
74
 
75
+ String.prototype.hideData = function (this: string, endChar?: string) {
76
+ if (endChar != null) {
77
+ const index = this.indexOf(endChar);
78
+ if (index === -1) return this.hideData();
79
+ return this.substring(0, index).hideData() + this.substring(index);
80
+ }
81
+
82
+ var len = this.length;
83
+ if (len < 4) return this.substring(0, 1) + '***';
84
+ if (len < 6) return this.substring(0, 2) + '***';
85
+ if (len < 8) return this.substring(0, 2) + '***' + this.slice(-2);
86
+ if (len < 12) return this.substring(0, 3) + '***' + this.slice(-3);
87
+
88
+ return this.substring(0, 4) + '***' + this.slice(-4);
89
+ };
90
+
91
+ String.prototype.hideEmail = function (this: string) {
92
+ return this.hideData('@');
93
+ };
94
+
62
95
  String.prototype.isDigits = function (this: string, minLength?: number) {
63
96
  return this.length >= (minLength ?? 0) && /^\d+$/.test(this);
64
97
  };
@@ -77,6 +110,27 @@ String.prototype.removeNonLetters = function (this: string) {
77
110
  * Utilities
78
111
  */
79
112
  export namespace Utils {
113
+ /**
114
+ * Add blank item to collection
115
+ * @param options Options
116
+ * @param idField Id field, default is id
117
+ * @param labelField Label field, default is label
118
+ * @param blankLabel Blank label, default is ---
119
+ */
120
+ export function addBlankItem<T extends {}>(
121
+ options: T[],
122
+ idField?: string | keyof T,
123
+ labelField?: unknown,
124
+ blankLabel?: string
125
+ ) {
126
+ const blankItem: any = {
127
+ [idField ?? 'id']: '',
128
+ [typeof labelField === 'string' ? labelField : 'label']:
129
+ blankLabel ?? '---'
130
+ };
131
+ options.unshift(blankItem);
132
+ }
133
+
80
134
  /**
81
135
  * Base64 chars to number
82
136
  * @param base64Chars Base64 chars
@@ -93,6 +147,24 @@ export namespace Utils {
93
147
  }, 0);
94
148
  }
95
149
 
150
+ /**
151
+ * Correct object's property value type
152
+ * @param input Input object
153
+ * @param fields Fields to correct
154
+ */
155
+ export function correctTypes<
156
+ T extends {},
157
+ F extends { [P in keyof T]: DataTypes.BasicNames }
158
+ >(input: T, fields: F) {
159
+ for (const field in fields) {
160
+ const value = Reflect.get(input, field);
161
+ const newValue = DataTypes.convertByType(value, fields[field]);
162
+ if (newValue !== value) {
163
+ Reflect.set(input, field, newValue);
164
+ }
165
+ }
166
+ }
167
+
96
168
  /**
97
169
  * Two values equal
98
170
  * @param v1 Value 1