@universal-packages/time-measurer 1.3.1 → 1.4.1

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
@@ -12,7 +12,9 @@ Time Measurer is a simple wrap for `process.hrtime.bigint` to measure time with
12
12
  npm install @universal-packages/time-measurer
13
13
  ```
14
14
 
15
- ## startMeasurement()
15
+ ## Global methods
16
+
17
+ #### **`startMeasurement()`**
16
18
 
17
19
  Creates a new TimeMeasurer instance to start a measurement.
18
20
 
@@ -31,6 +33,18 @@ getAll()
31
33
  // > All records - 2.23ms
32
34
  ```
33
35
 
36
+ #### **`sleep(milliseconds: number)`**
37
+
38
+ Time measurer ships with a convenient sleep function that takes a single parameter `time` in milliseconds, internally it is just a promise with a timeout that resolves it.
39
+
40
+ ```js
41
+ import { sleep } from '@universal-packages/time-measurer'
42
+
43
+ async function awaitable() {
44
+ await sleep(2000)
45
+ }
46
+ ```
47
+
34
48
  ## TimeMeasurer
35
49
 
36
50
  Class `TimeMeasurer` provides an instantiable interface to start measuring time from any part of your code. The measurement starts at instancing time.
@@ -41,6 +55,8 @@ import { TimeMeasurer } from '@universal-packages/time-measurer'
41
55
  async function getAll() {
42
56
  const measurer = new TimeMeasurer()
43
57
 
58
+ measurer.start()
59
+
44
60
  const data = await myDB.getAllRecords()
45
61
  const measurement = measurer.finish()
46
62
 
@@ -51,22 +67,27 @@ getAll()
51
67
  // > All records - 2.23ms
52
68
  ```
53
69
 
70
+ ### Instance methods
71
+
72
+ #### **`start()`**
73
+
74
+ Resets the initial time.
75
+
76
+ #### **`stop()`**
77
+
78
+ Returns a measurement representing the time passed from when start was called.
79
+
54
80
  ## Measurement
55
81
 
56
82
  A `Measurement` object is the time representation after a measure, it provides the interface to express time as a formatted string or even as a date object.
57
83
 
58
- ### .toString()
84
+ ### Instance methods
59
85
 
60
- Get the time representation as a string, this function takes one param `TimeFormat`, that can be one of `Condensed`, `Human`, `Expressive`, default: `Human`.
86
+ #### **`toString(format: TimeFormat)`**
61
87
 
62
- ```js
63
- measurement.toString()
64
- measurement.toString('Condensed')
65
- measurement.toString('Human')
66
- measurement.toString('Expressive')
67
- ```
88
+ Get the time representation as a string, this function takes one param `TimeFormat`, that can be one of `Condensed`, `Human`, `Expressive`, default: `Human`.
68
89
 
69
- You will get something like
90
+ Example output
70
91
 
71
92
  ```
72
93
  2hrs 35min 51.235sec
@@ -84,26 +105,10 @@ It will take into account parts of the representation that are not contributing
84
105
  51.235 Seconds
85
106
  ```
86
107
 
87
- ### .toString()
108
+ #### **`toDate()`**
88
109
 
89
110
  Get the time representation as a date object this can be helpful if you want to use the `Date` api to format or do whatever with the date.
90
111
 
91
- ```js
92
- measurement.toDate()
93
- ```
94
-
95
- ## Sleep
96
-
97
- Time measurer ships with a convenient sleep function that takes a single parameter `time` in milliseconds, internally it is just a promise with a timeout that resolves it.
98
-
99
- ```js
100
- import { sleep } from '@universal-packages/time-measurer'
101
-
102
- async function waitable() {
103
- await sleep(2000)
104
- }
105
- ```
106
-
107
112
  ## Typescript
108
113
 
109
114
  This library is developed in TypeScript and shipped fully typed.
package/TimeMeasurer.d.ts CHANGED
@@ -7,7 +7,8 @@ import Measurement from './Measurement';
7
7
  */
8
8
  export default class TimeMeasurer {
9
9
  private hrStart;
10
- constructor();
10
+ /** Resets the initial time */
11
+ start(): void;
11
12
  /** Returns a measurement representing the time passed from when start was called */
12
13
  finish(): Measurement;
13
14
  }
package/TimeMeasurer.js CHANGED
@@ -12,6 +12,10 @@ const Measurement_1 = __importDefault(require("./Measurement"));
12
12
  */
13
13
  class TimeMeasurer {
14
14
  constructor() {
15
+ this.hrStart = 0n;
16
+ }
17
+ /** Resets the initial time */
18
+ start() {
15
19
  this.hrStart = process.hrtime.bigint();
16
20
  }
17
21
  /** Returns a measurement representing the time passed from when start was called */
@@ -1 +1 @@
1
- {"version":3,"file":"TimeMeasurer.js","sourceRoot":"","sources":["../src/TimeMeasurer.ts"],"names":[],"mappings":";;;;;AAAA,gEAAuC;AAEvC;;;;;GAKG;AACH,MAAqB,YAAY;IAG/B;QACE,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAA;IACxC,CAAC;IAED,oFAAoF;IAC7E,MAAM;QACX,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAE1D,OAAO,IAAI,qBAAW,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;CACF;AAbD,+BAaC"}
1
+ {"version":3,"file":"TimeMeasurer.js","sourceRoot":"","sources":["../src/TimeMeasurer.ts"],"names":[],"mappings":";;;;;AAAA,gEAAuC;AAEvC;;;;;GAKG;AACH,MAAqB,YAAY;IAAjC;QACU,YAAO,GAAW,EAAE,CAAA;IAa9B,CAAC;IAXC,8BAA8B;IACvB,KAAK;QACV,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,CAAA;IACxC,CAAC;IAED,oFAAoF;IAC7E,MAAM;QACX,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,OAAO,CAAA;QAE1D,OAAO,IAAI,qBAAW,CAAC,WAAW,CAAC,CAAA;IACrC,CAAC;CACF;AAdD,+BAcC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@universal-packages/time-measurer",
3
- "version": "1.3.1",
3
+ "version": "1.4.1",
4
4
  "description": "Utility to measure routines times with precision",
5
5
  "author": "David De Anda <david@universal-packages.com> (https://github.com/universal-packages)",
6
6
  "license": "MIT",
@@ -11,14 +11,16 @@
11
11
  "build": "tsc --p tsconfig.dis.json",
12
12
  "test": "jest --watch",
13
13
  "test:full": "jest --coverage --verbose",
14
- "test:clear": "jest --clearCache"
14
+ "test:clear": "jest --clearCache",
15
+ "format": "prettier --write \"./{src,tests}/**/*.{ts,tsx,js,jsx,json}\""
15
16
  },
16
- "dependencies": {},
17
17
  "devDependencies": {
18
+ "@trivago/prettier-plugin-sort-imports": "^4.1.1",
18
19
  "@types/jest": "^28.1.0",
19
20
  "@types/node": "^17.0.39",
20
21
  "jest": "^28.1.0",
21
22
  "jest-circus": "^28.1.0",
23
+ "prettier": "^2.8.7",
22
24
  "ts-jest": "^28.0.4",
23
25
  "typescript": "^4.7.3"
24
26
  },
@@ -42,6 +44,17 @@
42
44
  "semi": false,
43
45
  "singleQuote": true,
44
46
  "printWidth": 180,
45
- "trailingComma": "none"
47
+ "trailingComma": "none",
48
+ "importOrder": [
49
+ "^[./]"
50
+ ],
51
+ "importOrderSeparation": true,
52
+ "importOrderSortSpecifiers": true,
53
+ "importOrderParserPlugins": [
54
+ "typescript",
55
+ "jsx",
56
+ "classProperties",
57
+ "decorators-legacy"
58
+ ]
46
59
  }
47
60
  }
package/sleep.d.ts CHANGED
@@ -1,2 +1,2 @@
1
- /** Simple waitable timeout to sleep the process */
2
- export default function sleep(time: number): Promise<void>;
1
+ /** Simple awaitable timeout to sleep the process */
2
+ export default function sleep(milliseconds: number): Promise<void>;
package/sleep.js CHANGED
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- /** Simple waitable timeout to sleep the process */
4
- async function sleep(time) {
5
- return new Promise((resolve) => setTimeout(() => resolve(), time));
3
+ /** Simple awaitable timeout to sleep the process */
4
+ async function sleep(milliseconds) {
5
+ return new Promise((resolve) => setTimeout(() => resolve(), milliseconds));
6
6
  }
7
7
  exports.default = sleep;
8
8
  //# sourceMappingURL=sleep.js.map
package/sleep.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"sleep.js","sourceRoot":"","sources":["../src/sleep.ts"],"names":[],"mappings":";;AAAA,mDAAmD;AACpC,KAAK,UAAU,KAAK,CAAC,IAAY;IAC9C,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAkB,EAAE,CAAC,UAAU,CAAC,GAAS,EAAE,CAAC,OAAO,EAAE,EAAE,IAAI,CAAC,CAAC,CAAA;AAC1F,CAAC;AAFD,wBAEC"}
1
+ {"version":3,"file":"sleep.js","sourceRoot":"","sources":["../src/sleep.ts"],"names":[],"mappings":";;AAAA,oDAAoD;AACrC,KAAK,UAAU,KAAK,CAAC,YAAoB;IACtD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAkB,EAAE,CAAC,UAAU,CAAC,GAAS,EAAE,CAAC,OAAO,EAAE,EAAE,YAAY,CAAC,CAAC,CAAA;AAClG,CAAC;AAFD,wBAEC"}
@@ -6,7 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.startMeasurement = void 0;
7
7
  const TimeMeasurer_1 = __importDefault(require("./TimeMeasurer"));
8
8
  function startMeasurement() {
9
- return new TimeMeasurer_1.default();
9
+ const measurer = new TimeMeasurer_1.default();
10
+ measurer.start();
11
+ return measurer;
10
12
  }
11
13
  exports.startMeasurement = startMeasurement;
12
14
  //# sourceMappingURL=startMeasurement.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"startMeasurement.js","sourceRoot":"","sources":["../src/startMeasurement.ts"],"names":[],"mappings":";;;;;;AAAA,kEAAyC;AAEzC,SAAgB,gBAAgB;IAC9B,OAAO,IAAI,sBAAY,EAAE,CAAA;AAC3B,CAAC;AAFD,4CAEC"}
1
+ {"version":3,"file":"startMeasurement.js","sourceRoot":"","sources":["../src/startMeasurement.ts"],"names":[],"mappings":";;;;;;AAAA,kEAAyC;AAEzC,SAAgB,gBAAgB;IAC9B,MAAM,QAAQ,GAAG,IAAI,sBAAY,EAAE,CAAA;IAEnC,QAAQ,CAAC,KAAK,EAAE,CAAA;IAEhB,OAAO,QAAQ,CAAA;AACjB,CAAC;AAND,4CAMC"}