@darthcav/ts-utils 0.10.7 → 0.10.8

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
@@ -52,8 +52,8 @@ const logger = await getConsoleLogger("my-app", "debug")
52
52
 
53
53
  ### `monitorMemory`
54
54
 
55
- Starts a periodic interval that logs process uptime and memory usage (in bytes). The interval
56
- defaults to every 24 hours.
55
+ Starts a periodic interval that logs process uptime and memory usage (in bytes) through a
56
+ `"monitorMemory"` child category of the given logger. The interval defaults to every 24 hours.
57
57
 
58
58
  ```ts
59
59
  import { getConsoleLogger, main, monitorMemory } from "@darthcav/ts-utils"
@@ -68,18 +68,20 @@ main("my-app", logger, () => {
68
68
 
69
69
  ### `millisecondsToString`
70
70
 
71
- Converts a duration in milliseconds to a human-readable string via `Intl.DurationFormat`. Sub-second
72
- values are rounded to the nearest second, and any zero-valued components are omitted — so a
73
- zero-millisecond input returns an empty string. Negative durations are formatted from their
74
- magnitude and prefixed with `"-"`. An optional BCP 47 locale tag (default `"en"`) controls the unit
75
- labels. Throws a `RangeError` if `ms` is not finite or `locale` is not a valid BCP 47 tag.
71
+ Converts a duration in milliseconds to a human-readable string via `Intl.DurationFormat`.
72
+ Milliseconds are shown as their own integer component (fractional inputs are rounded to the nearest
73
+ millisecond), and any zero-valued components are omitted — so a zero-millisecond input returns an
74
+ empty string. Negative durations are formatted from their magnitude and prefixed with `"-"`. An
75
+ optional BCP 47 locale tag (default `"en"`) controls the unit labels. Throws a `RangeError` if `ms`
76
+ is not finite or `locale` is not a valid BCP 47 tag.
76
77
 
77
78
  ```ts
78
79
  import { millisecondsToString } from "@darthcav/ts-utils"
79
80
 
80
81
  millisecondsToString(3_661_000) // "1h 1m 1s"
81
82
  millisecondsToString(90_000) // "1m 30s"
82
- millisecondsToString(5_000) // "5s"
83
+ millisecondsToString(5_250) // "5s 250ms"
84
+ millisecondsToString(499) // "499ms"
83
85
  millisecondsToString(-90_000) // "-1m 30s"
84
86
  millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
85
87
  ```
@@ -216,7 +218,7 @@ public/ # Documentation output (generated)
216
218
 
217
219
  [node-version]: https://img.shields.io/badge/node-%3E%3D26-orange.svg?style=flat-square
218
220
  [node-url]: https://nodejs.org
219
- [version-image]: https://img.shields.io/badge/version-0.10.7-blue.svg?style=flat-square
221
+ [version-image]: https://img.shields.io/badge/version-0.10.8-blue.svg?style=flat-square
220
222
  [ci-badge]: https://github.com/darthcav/ts-utils/actions/workflows/tests.yml/badge.svg
221
223
  [coverage-badge]:
222
224
  https://codecov.io/github/darthcav/ts-utils/branch/dev/graph/badge.svg?token=RNEPER4KEI
@@ -1,11 +1,12 @@
1
1
  /**
2
2
  * Converts a duration in milliseconds to a human-readable string such as
3
- * `"1d 4h 32m 10s"`.
3
+ * `"1d 4h 32m 10s 250ms"`.
4
4
  *
5
- * Sub-second values are rounded to the nearest second. Any zero-valued
6
- * components are omitted from the formatted output, so a zero-millisecond
7
- * input produces an empty string. Negative durations are formatted from their
8
- * magnitude and prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
5
+ * Milliseconds are shown as their own integer component; fractional inputs
6
+ * are rounded to the nearest millisecond. Any zero-valued components are
7
+ * omitted from the formatted output, so a zero-millisecond input produces an
8
+ * empty string. Negative durations are formatted from their magnitude and
9
+ * prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
9
10
  *
10
11
  * @param ms - Duration in milliseconds. Must be a finite number.
11
12
  * @param locale - BCP 47 locale tag passed to `Intl.DurationFormat`. Defaults
@@ -18,7 +19,8 @@
18
19
  * ```ts
19
20
  * millisecondsToString(3_661_000) // "1h 1m 1s"
20
21
  * millisecondsToString(90_000) // "1m 30s"
21
- * millisecondsToString(5_000) // "5s"
22
+ * millisecondsToString(5_250) // "5s 250ms"
23
+ * millisecondsToString(499) // "499ms"
22
24
  * millisecondsToString(-90_000) // "-1m 30s"
23
25
  * millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
24
26
  * ```
@@ -1 +1 @@
1
- {"version":3,"file":"millisecondsToString.d.ts","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"AAmCA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,EAAE,EAAE,MAAM,EACV,MAAM,SAAO,GACd,MAAM,CAkBR"}
1
+ {"version":3,"file":"millisecondsToString.d.ts","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"AAmCA;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CACxC,EAAE,EAAE,MAAM,EACV,MAAM,SAAO,GACd,MAAM,CAmBR"}
@@ -29,12 +29,13 @@ function getDurationFormatter(locale) {
29
29
  }
30
30
  /**
31
31
  * Converts a duration in milliseconds to a human-readable string such as
32
- * `"1d 4h 32m 10s"`.
32
+ * `"1d 4h 32m 10s 250ms"`.
33
33
  *
34
- * Sub-second values are rounded to the nearest second. Any zero-valued
35
- * components are omitted from the formatted output, so a zero-millisecond
36
- * input produces an empty string. Negative durations are formatted from their
37
- * magnitude and prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
34
+ * Milliseconds are shown as their own integer component; fractional inputs
35
+ * are rounded to the nearest millisecond. Any zero-valued components are
36
+ * omitted from the formatted output, so a zero-millisecond input produces an
37
+ * empty string. Negative durations are formatted from their magnitude and
38
+ * prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
38
39
  *
39
40
  * @param ms - Duration in milliseconds. Must be a finite number.
40
41
  * @param locale - BCP 47 locale tag passed to `Intl.DurationFormat`. Defaults
@@ -47,7 +48,8 @@ function getDurationFormatter(locale) {
47
48
  * ```ts
48
49
  * millisecondsToString(3_661_000) // "1h 1m 1s"
49
50
  * millisecondsToString(90_000) // "1m 30s"
50
- * millisecondsToString(5_000) // "5s"
51
+ * millisecondsToString(5_250) // "5s 250ms"
52
+ * millisecondsToString(499) // "499ms"
51
53
  * millisecondsToString(-90_000) // "-1m 30s"
52
54
  * millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
53
55
  * ```
@@ -56,14 +58,15 @@ export default function millisecondsToString(ms, locale = "en") {
56
58
  if (!Number.isFinite(ms)) {
57
59
  throw new RangeError(`millisecondsToString: "ms" must be a finite number, received ${ms}`);
58
60
  }
59
- const totalSeconds = Math.round(ms / 1000);
60
- const magnitude = Math.abs(totalSeconds);
61
+ const totalMilliseconds = Math.round(ms);
62
+ const magnitude = Math.abs(totalMilliseconds);
61
63
  const formatted = getDurationFormatter(locale).format({
62
- days: Math.floor(magnitude / 86400),
63
- hours: Math.floor((magnitude % 86400) / 3600),
64
- minutes: Math.floor((magnitude % 3600) / 60),
65
- seconds: magnitude % 60,
64
+ days: Math.floor(magnitude / 86_400_000),
65
+ hours: Math.floor((magnitude % 86_400_000) / 3_600_000),
66
+ minutes: Math.floor((magnitude % 3_600_000) / 60_000),
67
+ seconds: Math.floor((magnitude % 60_000) / 1000),
68
+ milliseconds: magnitude % 1000,
66
69
  });
67
- return totalSeconds < 0 && formatted ? `-${formatted}` : formatted;
70
+ return totalMilliseconds < 0 && formatted ? `-${formatted}` : formatted;
68
71
  }
69
72
  //# sourceMappingURL=millisecondsToString.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"millisecondsToString.js","sourceRoot":"","sources":["../src/millisecondsToString.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,qBAAqB,GAAG,EAAE,CAAA;AAEhC,MAAM,kBAAkB,GAAG,IAAI,GAAG,EAA+B,CAAA;AAEjE,SAAS,oBAAoB,CAAC,MAAc;IACxC,IAAI,SAAS,GAAG,kBAAkB,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;IAC9C,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC1B,IAAI,CAAC;YACD,SAAS,GAAG,IAAI,IAAI,CAAC,cAAc,CAAC,MAAM,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AAC,OAAO,UAAU,oBAAoB,CACxC,EAAU,EACV,MAAM,GAAG,IAAI;IAEb,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,EAAE,CAAC,EAAE,CAAC;QACvB,MAAM,IAAI,UAAU,CAChB,gEAAgE,EAAE,EAAE,CACvE,CAAA;IACL,CAAC;IAED,MAAM,iBAAiB,GAAG,IAAI,CAAC,KAAK,CAAC,EAAE,CAAC,CAAA;IACxC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAA;IAE7C,MAAM,SAAS,GAAG,oBAAoB,CAAC,MAAM,CAAC,CAAC,MAAM,CAAC;QAClD,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,SAAS,GAAG,UAAU,CAAC;QACxC,KAAK,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,UAAU,CAAC,GAAG,SAAS,CAAC;QACvD,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,SAAS,CAAC,GAAG,MAAM,CAAC;QACrD,OAAO,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,SAAS,GAAG,MAAM,CAAC,GAAG,IAAI,CAAC;QAChD,YAAY,EAAE,SAAS,GAAG,IAAI;KACjC,CAAC,CAAA;IAEF,OAAO,iBAAiB,GAAG,CAAC,IAAI,SAAS,CAAC,CAAC,CAAC,IAAI,SAAS,EAAE,CAAC,CAAC,CAAC,SAAS,CAAA;AAC3E,CAAC"}
@@ -4,8 +4,11 @@ import type { Logger } from "@logtape/logtape";
4
4
  *
5
5
  * Memory figures are reported in bytes as returned by Node.js
6
6
  * {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
7
+ * Reports are emitted through a `"monitorMemory"` child category of the given
8
+ * logger.
7
9
  *
8
- * @param logger - Logger instance used to emit the memory reports.
10
+ * @param logger - Logger whose `"monitorMemory"` child emits the memory
11
+ * reports.
9
12
  * @param hours - Interval between reports in hours. Must be a finite number
10
13
  * greater than `0`. Defaults to `24`.
11
14
  * @throws {RangeError} If `hours` is not a finite number greater than `0`.
@@ -14,8 +17,10 @@ import type { Logger } from "@logtape/logtape";
14
17
  * ```ts
15
18
  * import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
16
19
  *
17
- * monitorMemory(getConsoleLogger()) // every 24 hours
18
- * monitorMemory(getConsoleLogger(), 1) // every hour
20
+ * const logger = await getConsoleLogger("my-app")
21
+ *
22
+ * monitorMemory(logger) // every 24 hours
23
+ * monitorMemory(logger, 1) // every hour
19
24
  * ```
20
25
  */
21
26
  export default function monitorMemory(logger: Logger, hours?: number): void;
@@ -1 +1 @@
1
- {"version":3,"file":"monitorMemory.d.ts","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAG9C;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAW,GACnB,IAAI,CAcN"}
1
+ {"version":3,"file":"monitorMemory.d.ts","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AAG9C;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,MAAW,GACnB,IAAI,CAkBN"}
@@ -5,8 +5,11 @@ import millisecondsToString from "./millisecondsToString.js";
5
5
  *
6
6
  * Memory figures are reported in bytes as returned by Node.js
7
7
  * {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
8
+ * Reports are emitted through a `"monitorMemory"` child category of the given
9
+ * logger.
8
10
  *
9
- * @param logger - Logger instance used to emit the memory reports.
11
+ * @param logger - Logger whose `"monitorMemory"` child emits the memory
12
+ * reports.
10
13
  * @param hours - Interval between reports in hours. Must be a finite number
11
14
  * greater than `0`. Defaults to `24`.
12
15
  * @throws {RangeError} If `hours` is not a finite number greater than `0`.
@@ -15,19 +18,22 @@ import millisecondsToString from "./millisecondsToString.js";
15
18
  * ```ts
16
19
  * import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
17
20
  *
18
- * monitorMemory(getConsoleLogger()) // every 24 hours
19
- * monitorMemory(getConsoleLogger(), 1) // every hour
21
+ * const logger = await getConsoleLogger("my-app")
22
+ *
23
+ * monitorMemory(logger) // every 24 hours
24
+ * monitorMemory(logger, 1) // every hour
20
25
  * ```
21
26
  */
22
27
  export default function monitorMemory(logger, hours = 24) {
23
28
  if (!Number.isFinite(hours) || hours <= 0) {
24
29
  throw new RangeError(`monitorMemory: "hours" must be a finite number greater than 0, received ${hours}`);
25
30
  }
31
+ const __logger = logger.getChild(["monitorMemory"]);
26
32
  const delay = 60 * 60 * 1_000 * hours;
27
33
  setInterval(() => {
28
34
  const { rss, heapTotal, heapUsed, external } = memoryUsage();
29
- logger.info(`Process uptime: ${millisecondsToString(uptime() * 1_000)}`);
30
- logger.info(`Process memory - Resident set size: ${rss} | Heap total: ${heapTotal} | Heap used: ${heapUsed} | External: ${external}`);
35
+ __logger.info(`Process uptime: ${millisecondsToString(uptime() * 1_000)}`);
36
+ __logger.info(`Process memory - Resident set size: ${rss} | Heap total: ${heapTotal} | Heap used: ${heapUsed} | External: ${external}`);
31
37
  }, delay);
32
38
  }
33
39
  //# sourceMappingURL=monitorMemory.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"monitorMemory.js","sourceRoot":"","sources":["../src/monitorMemory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,EAAE,MAAM,cAAc,CAAA;AAElD,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAE5D;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,OAAO,UAAU,aAAa,CACjC,MAAc,EACd,QAAgB,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CAAC,GAAG,EAAE;QACb,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,CAAA;QAC5D,QAAQ,CAAC,IAAI,CACT,mBAAmB,oBAAoB,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,EAAE,CAC9D,CAAA;QACD,QAAQ,CAAC,IAAI,CACT,uCAAuC,GAAG,kBAAkB,SAAS,iBAAiB,QAAQ,gBAAgB,QAAQ,EAAE,CAC3H,CAAA;IACL,CAAC,EAAE,KAAK,CAAC,CAAA;AACb,CAAC"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@darthcav/ts-utils",
3
3
  "description": "A collection of utility functions for TypeScript applications and modules",
4
- "version": "0.10.7",
4
+ "version": "0.10.8",
5
5
  "author": {
6
6
  "name": "darthcav",
7
7
  "url": "https://github.com/darthcav"
@@ -24,15 +24,15 @@
24
24
  "!dist/__tests__"
25
25
  ],
26
26
  "dependencies": {
27
- "@logtape/logtape": "2.1.5"
27
+ "@logtape/logtape": "2.2.4"
28
28
  },
29
29
  "devDependencies": {
30
- "@biomejs/biome": "2.5.0",
31
- "@types/node": "25.9.3",
30
+ "@biomejs/biome": "2.5.4",
31
+ "@types/node": "26.1.1",
32
32
  "asserttt": "1.0.1",
33
- "prettier": "3.8.4",
33
+ "prettier": "3.9.5",
34
34
  "shx": "0.4.0",
35
- "typedoc": "0.28.19"
35
+ "typedoc": "0.28.20"
36
36
  },
37
37
  "repository": {
38
38
  "type": "git",
@@ -35,12 +35,13 @@ function getDurationFormatter(locale: string): Intl.DurationFormat {
35
35
 
36
36
  /**
37
37
  * Converts a duration in milliseconds to a human-readable string such as
38
- * `"1d 4h 32m 10s"`.
38
+ * `"1d 4h 32m 10s 250ms"`.
39
39
  *
40
- * Sub-second values are rounded to the nearest second. Any zero-valued
41
- * components are omitted from the formatted output, so a zero-millisecond
42
- * input produces an empty string. Negative durations are formatted from their
43
- * magnitude and prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
40
+ * Milliseconds are shown as their own integer component; fractional inputs
41
+ * are rounded to the nearest millisecond. Any zero-valued components are
42
+ * omitted from the formatted output, so a zero-millisecond input produces an
43
+ * empty string. Negative durations are formatted from their magnitude and
44
+ * prefixed with `"-"` (e.g. `-90_000` → `"-1m 30s"`).
44
45
  *
45
46
  * @param ms - Duration in milliseconds. Must be a finite number.
46
47
  * @param locale - BCP 47 locale tag passed to `Intl.DurationFormat`. Defaults
@@ -53,7 +54,8 @@ function getDurationFormatter(locale: string): Intl.DurationFormat {
53
54
  * ```ts
54
55
  * millisecondsToString(3_661_000) // "1h 1m 1s"
55
56
  * millisecondsToString(90_000) // "1m 30s"
56
- * millisecondsToString(5_000) // "5s"
57
+ * millisecondsToString(5_250) // "5s 250ms"
58
+ * millisecondsToString(499) // "499ms"
57
59
  * millisecondsToString(-90_000) // "-1m 30s"
58
60
  * millisecondsToString(90_061_000, "es") // "1d 1h 1min 1s"
59
61
  * ```
@@ -68,15 +70,16 @@ export default function millisecondsToString(
68
70
  )
69
71
  }
70
72
 
71
- const totalSeconds = Math.round(ms / 1000)
72
- const magnitude = Math.abs(totalSeconds)
73
+ const totalMilliseconds = Math.round(ms)
74
+ const magnitude = Math.abs(totalMilliseconds)
73
75
 
74
76
  const formatted = getDurationFormatter(locale).format({
75
- days: Math.floor(magnitude / 86400),
76
- hours: Math.floor((magnitude % 86400) / 3600),
77
- minutes: Math.floor((magnitude % 3600) / 60),
78
- seconds: magnitude % 60,
77
+ days: Math.floor(magnitude / 86_400_000),
78
+ hours: Math.floor((magnitude % 86_400_000) / 3_600_000),
79
+ minutes: Math.floor((magnitude % 3_600_000) / 60_000),
80
+ seconds: Math.floor((magnitude % 60_000) / 1000),
81
+ milliseconds: magnitude % 1000,
79
82
  })
80
83
 
81
- return totalSeconds < 0 && formatted ? `-${formatted}` : formatted
84
+ return totalMilliseconds < 0 && formatted ? `-${formatted}` : formatted
82
85
  }
@@ -7,8 +7,11 @@ import millisecondsToString from "./millisecondsToString.ts"
7
7
  *
8
8
  * Memory figures are reported in bytes as returned by Node.js
9
9
  * {@link https://nodejs.org/api/process.html#processmemoryusage | process.memoryUsage()}.
10
+ * Reports are emitted through a `"monitorMemory"` child category of the given
11
+ * logger.
10
12
  *
11
- * @param logger - Logger instance used to emit the memory reports.
13
+ * @param logger - Logger whose `"monitorMemory"` child emits the memory
14
+ * reports.
12
15
  * @param hours - Interval between reports in hours. Must be a finite number
13
16
  * greater than `0`. Defaults to `24`.
14
17
  * @throws {RangeError} If `hours` is not a finite number greater than `0`.
@@ -17,8 +20,10 @@ import millisecondsToString from "./millisecondsToString.ts"
17
20
  * ```ts
18
21
  * import { getConsoleLogger, monitorMemory } from "@darthcav/ts-utils"
19
22
  *
20
- * monitorMemory(getConsoleLogger()) // every 24 hours
21
- * monitorMemory(getConsoleLogger(), 1) // every hour
23
+ * const logger = await getConsoleLogger("my-app")
24
+ *
25
+ * monitorMemory(logger) // every 24 hours
26
+ * monitorMemory(logger, 1) // every hour
22
27
  * ```
23
28
  */
24
29
  export default function monitorMemory(
@@ -30,11 +35,15 @@ export default function monitorMemory(
30
35
  `monitorMemory: "hours" must be a finite number greater than 0, received ${hours}`,
31
36
  )
32
37
  }
38
+ const __logger = logger.getChild(["monitorMemory"])
39
+
33
40
  const delay = 60 * 60 * 1_000 * hours
34
41
  setInterval(() => {
35
42
  const { rss, heapTotal, heapUsed, external } = memoryUsage()
36
- logger.info(`Process uptime: ${millisecondsToString(uptime() * 1_000)}`)
37
- logger.info(
43
+ __logger.info(
44
+ `Process uptime: ${millisecondsToString(uptime() * 1_000)}`,
45
+ )
46
+ __logger.info(
38
47
  `Process memory - Resident set size: ${rss} | Heap total: ${heapTotal} | Heap used: ${heapUsed} | External: ${external}`,
39
48
  )
40
49
  }, delay)