@contrail/util 1.0.49 → 1.0.50
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/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -19,3 +19,4 @@ __exportStar(require("./string-util/string-util"), exports);
|
|
|
19
19
|
__exportStar(require("./date-util/date-util"), exports);
|
|
20
20
|
__exportStar(require("./map-util/map-util"), exports);
|
|
21
21
|
__exportStar(require("./app-util/app-util"), exports);
|
|
22
|
+
__exportStar(require("./timer-util/timer-util"), exports);
|
|
@@ -7,7 +7,6 @@ export { ObjectDiff } from './compareDeep/compareDeep';
|
|
|
7
7
|
export declare class ObjectUtil {
|
|
8
8
|
static getByPath(obj: any, path: string, def?: any): any;
|
|
9
9
|
static setByPath(obj: any, path: string, value: any): void;
|
|
10
|
-
static chunk(array: any[], size: number): any[];
|
|
11
10
|
static isObject: typeof isObject;
|
|
12
11
|
static mergeDeep: typeof mergeDeep;
|
|
13
12
|
static cloneDeep: typeof cloneDeep;
|
|
@@ -30,13 +30,6 @@ class ObjectUtil {
|
|
|
30
30
|
}
|
|
31
31
|
o[a[0]] = value;
|
|
32
32
|
}
|
|
33
|
-
static chunk(array, size) {
|
|
34
|
-
const chunked = [];
|
|
35
|
-
for (let i = 0; i < array.length; i = i + size) {
|
|
36
|
-
chunked.push(array.slice(i, i + size));
|
|
37
|
-
}
|
|
38
|
-
return chunked;
|
|
39
|
-
}
|
|
40
33
|
}
|
|
41
34
|
exports.ObjectUtil = ObjectUtil;
|
|
42
35
|
ObjectUtil.isObject = isObject_1.isObject;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare enum TimeUnit {
|
|
2
|
+
MILLISECOND = "ms",
|
|
3
|
+
SECOND = "s"
|
|
4
|
+
}
|
|
5
|
+
export declare class Timer {
|
|
6
|
+
private startTime?;
|
|
7
|
+
private endTime?;
|
|
8
|
+
start(): void;
|
|
9
|
+
stop(): void;
|
|
10
|
+
reset(): void;
|
|
11
|
+
getElapsedSeconds(): number;
|
|
12
|
+
getElapsedMilliseconds(): number;
|
|
13
|
+
getElapsedTime(timeUnit?: TimeUnit): number;
|
|
14
|
+
private getCurrentTime;
|
|
15
|
+
}
|
|
16
|
+
export declare function delay(ms: number): Promise<unknown>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.delay = exports.Timer = exports.TimeUnit = void 0;
|
|
13
|
+
var TimeUnit;
|
|
14
|
+
(function (TimeUnit) {
|
|
15
|
+
TimeUnit["MILLISECOND"] = "ms";
|
|
16
|
+
TimeUnit["SECOND"] = "s";
|
|
17
|
+
})(TimeUnit = exports.TimeUnit || (exports.TimeUnit = {}));
|
|
18
|
+
const MILLISECONDS_PER_TIME_UNIT = {
|
|
19
|
+
[TimeUnit.MILLISECOND]: 1,
|
|
20
|
+
[TimeUnit.SECOND]: 1000,
|
|
21
|
+
};
|
|
22
|
+
class Timer {
|
|
23
|
+
start() {
|
|
24
|
+
if (this.startTime) {
|
|
25
|
+
throw new Error('Timer already started');
|
|
26
|
+
}
|
|
27
|
+
this.startTime = this.getCurrentTime();
|
|
28
|
+
}
|
|
29
|
+
stop() {
|
|
30
|
+
if (!this.startTime) {
|
|
31
|
+
throw new Error('Timer not started');
|
|
32
|
+
}
|
|
33
|
+
this.endTime = this.getCurrentTime();
|
|
34
|
+
}
|
|
35
|
+
reset() {
|
|
36
|
+
this.startTime = undefined;
|
|
37
|
+
this.endTime = undefined;
|
|
38
|
+
}
|
|
39
|
+
getElapsedSeconds() {
|
|
40
|
+
return this.getElapsedTime(TimeUnit.SECOND);
|
|
41
|
+
}
|
|
42
|
+
getElapsedMilliseconds() {
|
|
43
|
+
return this.getElapsedTime();
|
|
44
|
+
}
|
|
45
|
+
getElapsedTime(timeUnit = TimeUnit.MILLISECOND) {
|
|
46
|
+
if (!this.startTime) {
|
|
47
|
+
throw new Error('Timer not started');
|
|
48
|
+
}
|
|
49
|
+
const endTime = this.endTime || this.getCurrentTime();
|
|
50
|
+
const duration = endTime - this.startTime;
|
|
51
|
+
return duration / MILLISECONDS_PER_TIME_UNIT[timeUnit];
|
|
52
|
+
}
|
|
53
|
+
getCurrentTime() {
|
|
54
|
+
return performance.now();
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.Timer = Timer;
|
|
58
|
+
function delay(ms) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
return new Promise((resolve) => {
|
|
61
|
+
setTimeout(resolve, ms);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
exports.delay = delay;
|