@etsoo/shared 1.1.7 → 1.1.11

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,6 +151,7 @@ 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|
155
156
  |correctTypes|Correct object's property value type|
156
157
  |equals|Two values equal|
@@ -158,6 +159,10 @@ String and other related utilities
158
159
  |formatString|Format string with parameters|
159
160
  |getDataChanges|Get data changed fields with input data updated|
160
161
  |getTimeZone|Get time zone|
162
+ |hideData|Hide data|
163
+ |hideEmail|Hide email data|
164
+ |isDigits|Is digits string|
165
+ |isEmail|Is email string|
161
166
  |joinItems|Join items as a string|
162
167
  |mergeFormData|Merge form data to primary one|
163
168
  |mergeClasses|Merge class names|
@@ -167,5 +172,6 @@ String and other related utilities
167
172
  |objectKeys|Get two object's unqiue properties|
168
173
  |objectUpdated|Get the new object's updated fields contrast to the previous object|
169
174
  |parseString|Parse string (JSON) to specific type|
175
+ |removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
170
176
  |setLabels|Set source with new labels|
171
177
  |snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
@@ -1,5 +1,16 @@
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
+
3
14
  test('Tests for correctTypes', () => {
4
15
  const input = {
5
16
  id: '1',
@@ -66,6 +77,13 @@ test('Tests for formatString', () => {
66
77
  expect(template.format('aa', 'bb')).toBe(result);
67
78
  });
68
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
+
69
87
  test('Tests for isDigits', () => {
70
88
  expect(Utils.isDigits('1')).toBeTruthy();
71
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,6 +46,14 @@ 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
package/lib/cjs/Utils.js CHANGED
@@ -14,6 +14,29 @@ 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 (this.length === 0)
19
+ return this;
20
+ if (endChar != null) {
21
+ const index = this.indexOf(endChar);
22
+ if (index === -1)
23
+ return this.hideData();
24
+ return this.substring(0, index).hideData() + this.substring(index);
25
+ }
26
+ var len = this.length;
27
+ if (len < 4)
28
+ return this.substring(0, 1) + '***';
29
+ if (len < 6)
30
+ return this.substring(0, 2) + '***';
31
+ if (len < 8)
32
+ return this.substring(0, 2) + '***' + this.slice(-2);
33
+ if (len < 12)
34
+ return this.substring(0, 3) + '***' + this.slice(-3);
35
+ return this.substring(0, 4) + '***' + this.slice(-4);
36
+ };
37
+ String.prototype.hideEmail = function () {
38
+ return this.hideData('@');
39
+ };
17
40
  String.prototype.isDigits = function (minLength) {
18
41
  return this.length >= (minLength !== null && minLength !== void 0 ? minLength : 0) && /^\d+$/.test(this);
19
42
  };
@@ -29,6 +52,21 @@ String.prototype.removeNonLetters = function () {
29
52
  */
30
53
  var Utils;
31
54
  (function (Utils) {
55
+ /**
56
+ * Add blank item to collection
57
+ * @param options Options
58
+ * @param idField Id field, default is id
59
+ * @param labelField Label field, default is label
60
+ * @param blankLabel Blank label, default is ---
61
+ */
62
+ function addBlankItem(options, idField, labelField, blankLabel) {
63
+ const blankItem = {
64
+ [idField !== null && idField !== void 0 ? idField : 'id']: '',
65
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
66
+ };
67
+ options.unshift(blankItem);
68
+ }
69
+ Utils.addBlankItem = addBlankItem;
32
70
  /**
33
71
  * Base64 chars to number
34
72
  * @param base64Chars Base64 chars
@@ -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,6 +46,14 @@ 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
package/lib/mjs/Utils.js CHANGED
@@ -11,6 +11,29 @@ 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 (this.length === 0)
16
+ return this;
17
+ if (endChar != null) {
18
+ const index = this.indexOf(endChar);
19
+ if (index === -1)
20
+ return this.hideData();
21
+ return this.substring(0, index).hideData() + this.substring(index);
22
+ }
23
+ var len = this.length;
24
+ if (len < 4)
25
+ return this.substring(0, 1) + '***';
26
+ if (len < 6)
27
+ return this.substring(0, 2) + '***';
28
+ if (len < 8)
29
+ return this.substring(0, 2) + '***' + this.slice(-2);
30
+ if (len < 12)
31
+ return this.substring(0, 3) + '***' + this.slice(-3);
32
+ return this.substring(0, 4) + '***' + this.slice(-4);
33
+ };
34
+ String.prototype.hideEmail = function () {
35
+ return this.hideData('@');
36
+ };
14
37
  String.prototype.isDigits = function (minLength) {
15
38
  return this.length >= (minLength !== null && minLength !== void 0 ? minLength : 0) && /^\d+$/.test(this);
16
39
  };
@@ -26,6 +49,21 @@ String.prototype.removeNonLetters = function () {
26
49
  */
27
50
  export var Utils;
28
51
  (function (Utils) {
52
+ /**
53
+ * Add blank item to collection
54
+ * @param options Options
55
+ * @param idField Id field, default is id
56
+ * @param labelField Label field, default is label
57
+ * @param blankLabel Blank label, default is ---
58
+ */
59
+ function addBlankItem(options, idField, labelField, blankLabel) {
60
+ const blankItem = {
61
+ [idField !== null && idField !== void 0 ? idField : 'id']: '',
62
+ [typeof labelField === 'string' ? labelField : 'label']: blankLabel !== null && blankLabel !== void 0 ? blankLabel : '---'
63
+ };
64
+ options.unshift(blankItem);
65
+ }
66
+ Utils.addBlankItem = addBlankItem;
29
67
  /**
30
68
  * Base64 chars to number
31
69
  * @param base64Chars Base64 chars
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.7",
3
+ "version": "1.1.11",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -57,7 +57,7 @@
57
57
  "@types/jest": "^27.4.0",
58
58
  "@typescript-eslint/eslint-plugin": "^5.10.1",
59
59
  "@typescript-eslint/parser": "^5.10.1",
60
- "eslint": "^8.7.0",
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",
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,28 @@ String.prototype.formatInitial = function (
59
72
  );
60
73
  };
61
74
 
75
+ String.prototype.hideData = function (this: string, endChar?: string) {
76
+ if (this.length === 0) return this;
77
+
78
+ if (endChar != null) {
79
+ const index = this.indexOf(endChar);
80
+ if (index === -1) return this.hideData();
81
+ return this.substring(0, index).hideData() + this.substring(index);
82
+ }
83
+
84
+ var len = this.length;
85
+ if (len < 4) return this.substring(0, 1) + '***';
86
+ if (len < 6) return this.substring(0, 2) + '***';
87
+ if (len < 8) return this.substring(0, 2) + '***' + this.slice(-2);
88
+ if (len < 12) return this.substring(0, 3) + '***' + this.slice(-3);
89
+
90
+ return this.substring(0, 4) + '***' + this.slice(-4);
91
+ };
92
+
93
+ String.prototype.hideEmail = function (this: string) {
94
+ return this.hideData('@');
95
+ };
96
+
62
97
  String.prototype.isDigits = function (this: string, minLength?: number) {
63
98
  return this.length >= (minLength ?? 0) && /^\d+$/.test(this);
64
99
  };
@@ -77,6 +112,27 @@ String.prototype.removeNonLetters = function (this: string) {
77
112
  * Utilities
78
113
  */
79
114
  export namespace Utils {
115
+ /**
116
+ * Add blank item to collection
117
+ * @param options Options
118
+ * @param idField Id field, default is id
119
+ * @param labelField Label field, default is label
120
+ * @param blankLabel Blank label, default is ---
121
+ */
122
+ export function addBlankItem<T extends {}>(
123
+ options: T[],
124
+ idField?: string | keyof T,
125
+ labelField?: unknown,
126
+ blankLabel?: string
127
+ ) {
128
+ const blankItem: any = {
129
+ [idField ?? 'id']: '',
130
+ [typeof labelField === 'string' ? labelField : 'label']:
131
+ blankLabel ?? '---'
132
+ };
133
+ options.unshift(blankItem);
134
+ }
135
+
80
136
  /**
81
137
  * Base64 chars to number
82
138
  * @param base64Chars Base64 chars