@etsoo/shared 1.1.9 → 1.1.12

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
@@ -159,6 +159,10 @@ String and other related utilities
159
159
  |formatString|Format string with parameters|
160
160
  |getDataChanges|Get data changed fields with input data updated|
161
161
  |getTimeZone|Get time zone|
162
+ |hideData|Hide data|
163
+ |hideEmail|Hide email data|
164
+ |isDigits|Is digits string|
165
+ |isEmail|Is email string|
162
166
  |joinItems|Join items as a string|
163
167
  |mergeFormData|Merge form data to primary one|
164
168
  |mergeClasses|Merge class names|
@@ -168,5 +172,6 @@ String and other related utilities
168
172
  |objectKeys|Get two object's unqiue properties|
169
173
  |objectUpdated|Get the new object's updated fields contrast to the previous object|
170
174
  |parseString|Parse string (JSON) to specific type|
175
+ |removeNonLetters|Remove non letters (0-9, a-z, A-Z)|
171
176
  |setLabels|Set source with new labels|
172
177
  |snakeNameToWord|Snake name to works, 'snake_name' to 'Snake Name'|
@@ -23,6 +23,26 @@ test('Tests for applyMixins', () => {
23
23
  expect(item.m2()).toBe('hello');
24
24
  });
25
25
 
26
+ test('Tests for delayedExecutor', () => {
27
+ // Arrange
28
+ const f = jest.fn();
29
+
30
+ jest.useFakeTimers();
31
+ jest.spyOn(global, 'setTimeout');
32
+
33
+ const e = ExtendUtils.delayedExecutor(f, 50);
34
+
35
+ e.call(1, false, 'a');
36
+ expect(e.isRunning()).toBeTruthy();
37
+
38
+ e.call(2, true, 'b');
39
+ expect(setTimeout).toHaveBeenCalledTimes(2);
40
+
41
+ jest.runOnlyPendingTimers();
42
+ expect(f).toBeCalledTimes(1);
43
+ expect(e.isRunning()).toBeFalsy();
44
+ });
45
+
26
46
  test('Tests for promiseHandler', async () => {
27
47
  // Arrange
28
48
  const p = (id: number) => {
@@ -77,6 +77,13 @@ test('Tests for formatString', () => {
77
77
  expect(template.format('aa', 'bb')).toBe(result);
78
78
  });
79
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
+
80
87
  test('Tests for isDigits', () => {
81
88
  expect(Utils.isDigits('1')).toBeTruthy();
82
89
  expect(Utils.isDigits('12', 3)).toBeFalsy();
@@ -9,6 +9,28 @@ export declare namespace ExtendUtils {
9
9
  * @param baseCtors Mixin base classes
10
10
  */
11
11
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
12
+ /**
13
+ * Create delayed executor
14
+ * @param func Function
15
+ * @param delayMiliseconds Delay miliseconds
16
+ * @returns Result
17
+ */
18
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): {
19
+ /**
20
+ * Call the function
21
+ * @param args Args
22
+ */
23
+ call(...args: P): void;
24
+ /**
25
+ * Clear
26
+ */
27
+ clear(): void;
28
+ /**
29
+ * Is running or not
30
+ * @returns Result
31
+ */
32
+ isRunning(): boolean;
33
+ };
12
34
  /**
13
35
  * Promise handler to catch error
14
36
  * @param promise Promise
@@ -23,6 +23,45 @@ var ExtendUtils;
23
23
  });
24
24
  }
25
25
  ExtendUtils.applyMixins = applyMixins;
26
+ /**
27
+ * Create delayed executor
28
+ * @param func Function
29
+ * @param delayMiliseconds Delay miliseconds
30
+ * @returns Result
31
+ */
32
+ function delayedExecutor(func, delayMiliseconds) {
33
+ let seed = 0;
34
+ return {
35
+ /**
36
+ * Call the function
37
+ * @param args Args
38
+ */
39
+ call(...args) {
40
+ this.clear();
41
+ seed = window.setTimeout((...args) => {
42
+ func(...args);
43
+ seed = 0;
44
+ }, delayMiliseconds, ...args);
45
+ },
46
+ /**
47
+ * Clear
48
+ */
49
+ clear() {
50
+ if (this.isRunning()) {
51
+ window.clearTimeout(seed);
52
+ seed = 0;
53
+ }
54
+ },
55
+ /**
56
+ * Is running or not
57
+ * @returns Result
58
+ */
59
+ isRunning() {
60
+ return seed > 0;
61
+ }
62
+ };
63
+ }
64
+ ExtendUtils.delayedExecutor = delayedExecutor;
26
65
  /**
27
66
  * Promise handler to catch error
28
67
  * @param promise Promise
@@ -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
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
  };
@@ -9,6 +9,28 @@ export declare namespace ExtendUtils {
9
9
  * @param baseCtors Mixin base classes
10
10
  */
11
11
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
12
+ /**
13
+ * Create delayed executor
14
+ * @param func Function
15
+ * @param delayMiliseconds Delay miliseconds
16
+ * @returns Result
17
+ */
18
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): {
19
+ /**
20
+ * Call the function
21
+ * @param args Args
22
+ */
23
+ call(...args: P): void;
24
+ /**
25
+ * Clear
26
+ */
27
+ clear(): void;
28
+ /**
29
+ * Is running or not
30
+ * @returns Result
31
+ */
32
+ isRunning(): boolean;
33
+ };
12
34
  /**
13
35
  * Promise handler to catch error
14
36
  * @param promise Promise
@@ -20,6 +20,45 @@ export var ExtendUtils;
20
20
  });
21
21
  }
22
22
  ExtendUtils.applyMixins = applyMixins;
23
+ /**
24
+ * Create delayed executor
25
+ * @param func Function
26
+ * @param delayMiliseconds Delay miliseconds
27
+ * @returns Result
28
+ */
29
+ function delayedExecutor(func, delayMiliseconds) {
30
+ let seed = 0;
31
+ return {
32
+ /**
33
+ * Call the function
34
+ * @param args Args
35
+ */
36
+ call(...args) {
37
+ this.clear();
38
+ seed = window.setTimeout((...args) => {
39
+ func(...args);
40
+ seed = 0;
41
+ }, delayMiliseconds, ...args);
42
+ },
43
+ /**
44
+ * Clear
45
+ */
46
+ clear() {
47
+ if (this.isRunning()) {
48
+ window.clearTimeout(seed);
49
+ seed = 0;
50
+ }
51
+ },
52
+ /**
53
+ * Is running or not
54
+ * @returns Result
55
+ */
56
+ isRunning() {
57
+ return seed > 0;
58
+ }
59
+ };
60
+ }
61
+ ExtendUtils.delayedExecutor = delayedExecutor;
23
62
  /**
24
63
  * Promise handler to catch error
25
64
  * @param promise Promise
@@ -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
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
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.9",
3
+ "version": "1.1.12",
4
4
  "description": "TypeScript shared utilities and functions",
5
5
  "main": "lib/cjs/index.js",
6
6
  "module": "lib/mjs/index.js",
@@ -54,13 +54,13 @@
54
54
  "homepage": "https://github.com/ETSOO/Shared#readme",
55
55
  "dependencies": {},
56
56
  "devDependencies": {
57
- "@types/jest": "^27.4.0",
58
- "@typescript-eslint/eslint-plugin": "^5.10.1",
59
- "@typescript-eslint/parser": "^5.10.1",
60
- "eslint": "^8.8.0",
57
+ "@types/jest": "^27.4.1",
58
+ "@typescript-eslint/eslint-plugin": "^5.12.1",
59
+ "@typescript-eslint/parser": "^5.12.1",
60
+ "eslint": "^8.10.0",
61
61
  "eslint-config-airbnb-base": "^15.0.0",
62
62
  "eslint-plugin-import": "^2.25.4",
63
- "jest": "^27.4.7",
63
+ "jest": "^27.5.1",
64
64
  "ts-jest": "^27.1.3",
65
65
  "typescript": "^4.5.5"
66
66
  }
@@ -19,6 +19,54 @@ export namespace ExtendUtils {
19
19
  });
20
20
  }
21
21
 
22
+ /**
23
+ * Create delayed executor
24
+ * @param func Function
25
+ * @param delayMiliseconds Delay miliseconds
26
+ * @returns Result
27
+ */
28
+ export function delayedExecutor<P extends any[]>(
29
+ func: (...args: P) => void,
30
+ delayMiliseconds: number
31
+ ) {
32
+ let seed: number = 0;
33
+ return {
34
+ /**
35
+ * Call the function
36
+ * @param args Args
37
+ */
38
+ call(...args: P) {
39
+ this.clear();
40
+ seed = window.setTimeout(
41
+ (...args: P) => {
42
+ func(...args);
43
+ seed = 0;
44
+ },
45
+ delayMiliseconds,
46
+ ...args
47
+ );
48
+ },
49
+
50
+ /**
51
+ * Clear
52
+ */
53
+ clear() {
54
+ if (this.isRunning()) {
55
+ window.clearTimeout(seed);
56
+ seed = 0;
57
+ }
58
+ },
59
+
60
+ /**
61
+ * Is running or not
62
+ * @returns Result
63
+ */
64
+ isRunning() {
65
+ return seed > 0;
66
+ }
67
+ };
68
+ }
69
+
22
70
  /**
23
71
  * Promise handler to catch error
24
72
  * @param promise Promise
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
  };