@etsoo/shared 1.1.10 → 1.1.13

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.
@@ -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) => {
@@ -1,3 +1,4 @@
1
+ import { DelayedExecutorType } from './types/DelayedExecutorType';
1
2
  /**
2
3
  * Extend utilities
3
4
  */
@@ -9,6 +10,13 @@ export declare namespace ExtendUtils {
9
10
  * @param baseCtors Mixin base classes
10
11
  */
11
12
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
13
+ /**
14
+ * Create delayed executor
15
+ * @param func Function
16
+ * @param delayMiliseconds Delay miliseconds
17
+ * @returns Result
18
+ */
19
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): DelayedExecutorType<P>;
12
20
  /**
13
21
  * Promise handler to catch error
14
22
  * @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
package/lib/cjs/Utils.js CHANGED
@@ -15,6 +15,8 @@ String.prototype.formatInitial = function (upperCase = false) {
15
15
  this.slice(1));
16
16
  };
17
17
  String.prototype.hideData = function (endChar) {
18
+ if (this.length === 0)
19
+ return this;
18
20
  if (endChar != null) {
19
21
  const index = this.indexOf(endChar);
20
22
  if (index === -1)
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
  export * from './storage/IStorage';
3
4
  export * from './storage/WindowStorage';
4
5
  export * from './DataTypes';
package/lib/cjs/index.js CHANGED
@@ -11,6 +11,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
13
  __exportStar(require("./types/FormData"), exports);
14
+ __exportStar(require("./types/DelayedExecutorType"), exports);
14
15
  __exportStar(require("./storage/IStorage"), exports);
15
16
  __exportStar(require("./storage/WindowStorage"), exports);
16
17
  __exportStar(require("./DataTypes"), exports);
@@ -0,0 +1,16 @@
1
+ export declare type DelayedExecutorType<P extends any[]> = {
2
+ /**
3
+ * Call the function
4
+ * @param args Args
5
+ */
6
+ call(...args: P): void;
7
+ /**
8
+ * Clear
9
+ */
10
+ clear(): void;
11
+ /**
12
+ * Is running or not
13
+ * @returns Result
14
+ */
15
+ isRunning(): boolean;
16
+ };
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,3 +1,4 @@
1
+ import { DelayedExecutorType } from './types/DelayedExecutorType';
1
2
  /**
2
3
  * Extend utilities
3
4
  */
@@ -9,6 +10,13 @@ export declare namespace ExtendUtils {
9
10
  * @param baseCtors Mixin base classes
10
11
  */
11
12
  function applyMixins(derivedCtor: any, baseCtors: any[]): void;
13
+ /**
14
+ * Create delayed executor
15
+ * @param func Function
16
+ * @param delayMiliseconds Delay miliseconds
17
+ * @returns Result
18
+ */
19
+ function delayedExecutor<P extends any[]>(func: (...args: P) => void, delayMiliseconds: number): DelayedExecutorType<P>;
12
20
  /**
13
21
  * Promise handler to catch error
14
22
  * @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
package/lib/mjs/Utils.js CHANGED
@@ -12,6 +12,8 @@ String.prototype.formatInitial = function (upperCase = false) {
12
12
  this.slice(1));
13
13
  };
14
14
  String.prototype.hideData = function (endChar) {
15
+ if (this.length === 0)
16
+ return this;
15
17
  if (endChar != null) {
16
18
  const index = this.indexOf(endChar);
17
19
  if (index === -1)
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
  export * from './storage/IStorage';
3
4
  export * from './storage/WindowStorage';
4
5
  export * from './DataTypes';
package/lib/mjs/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
  export * from './storage/IStorage';
3
4
  export * from './storage/WindowStorage';
4
5
  export * from './DataTypes';
@@ -0,0 +1,16 @@
1
+ export declare type DelayedExecutorType<P extends any[]> = {
2
+ /**
3
+ * Call the function
4
+ * @param args Args
5
+ */
6
+ call(...args: P): void;
7
+ /**
8
+ * Clear
9
+ */
10
+ clear(): void;
11
+ /**
12
+ * Is running or not
13
+ * @returns Result
14
+ */
15
+ isRunning(): boolean;
16
+ };
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etsoo/shared",
3
- "version": "1.1.10",
3
+ "version": "1.1.13",
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
  }
@@ -1,3 +1,5 @@
1
+ import { DelayedExecutorType } from './types/DelayedExecutorType';
2
+
1
3
  /**
2
4
  * Extend utilities
3
5
  */
@@ -19,6 +21,54 @@ export namespace ExtendUtils {
19
21
  });
20
22
  }
21
23
 
24
+ /**
25
+ * Create delayed executor
26
+ * @param func Function
27
+ * @param delayMiliseconds Delay miliseconds
28
+ * @returns Result
29
+ */
30
+ export function delayedExecutor<P extends any[]>(
31
+ func: (...args: P) => void,
32
+ delayMiliseconds: number
33
+ ): DelayedExecutorType<P> {
34
+ let seed: number = 0;
35
+ return {
36
+ /**
37
+ * Call the function
38
+ * @param args Args
39
+ */
40
+ call(...args: P) {
41
+ this.clear();
42
+ seed = window.setTimeout(
43
+ (...args: P) => {
44
+ func(...args);
45
+ seed = 0;
46
+ },
47
+ delayMiliseconds,
48
+ ...args
49
+ );
50
+ },
51
+
52
+ /**
53
+ * Clear
54
+ */
55
+ clear() {
56
+ if (this.isRunning()) {
57
+ window.clearTimeout(seed);
58
+ seed = 0;
59
+ }
60
+ },
61
+
62
+ /**
63
+ * Is running or not
64
+ * @returns Result
65
+ */
66
+ isRunning() {
67
+ return seed > 0;
68
+ }
69
+ };
70
+ }
71
+
22
72
  /**
23
73
  * Promise handler to catch error
24
74
  * @param promise Promise
package/src/Utils.ts CHANGED
@@ -73,6 +73,8 @@ String.prototype.formatInitial = function (
73
73
  };
74
74
 
75
75
  String.prototype.hideData = function (this: string, endChar?: string) {
76
+ if (this.length === 0) return this;
77
+
76
78
  if (endChar != null) {
77
79
  const index = this.indexOf(endChar);
78
80
  if (index === -1) return this.hideData();
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './types/FormData';
2
+ export * from './types/DelayedExecutorType';
2
3
 
3
4
  export * from './storage/IStorage';
4
5
  export * from './storage/WindowStorage';
@@ -0,0 +1,18 @@
1
+ export type DelayedExecutorType<P extends any[]> = {
2
+ /**
3
+ * Call the function
4
+ * @param args Args
5
+ */
6
+ call(...args: P): void;
7
+
8
+ /**
9
+ * Clear
10
+ */
11
+ clear(): void;
12
+
13
+ /**
14
+ * Is running or not
15
+ * @returns Result
16
+ */
17
+ isRunning(): boolean;
18
+ };