@darthcav/ts-utils 0.12.2 → 1.0.0

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.
Files changed (85) hide show
  1. package/README.md +152 -81
  2. package/dist/core/guards.d.ts +176 -0
  3. package/dist/core/guards.d.ts.map +1 -0
  4. package/dist/core/guards.js +207 -0
  5. package/dist/core/guards.js.map +1 -0
  6. package/dist/core/index.d.ts +16 -0
  7. package/dist/core/index.d.ts.map +1 -0
  8. package/dist/core/index.js +16 -0
  9. package/dist/core/index.js.map +1 -0
  10. package/dist/core/millisecondsToString.d.ts.map +1 -0
  11. package/dist/{millisecondsToString.js → core/millisecondsToString.js} +3 -4
  12. package/dist/core/millisecondsToString.js.map +1 -0
  13. package/dist/core/noop.d.ts +3 -0
  14. package/dist/core/noop.d.ts.map +1 -0
  15. package/dist/core/noop.js +3 -0
  16. package/dist/core/noop.js.map +1 -0
  17. package/dist/index.d.ts +13 -17
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +12 -10
  20. package/dist/index.js.map +1 -1
  21. package/dist/loggers/getConsoleLogger.d.ts +11 -12
  22. package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
  23. package/dist/loggers/getConsoleLogger.js +14 -19
  24. package/dist/loggers/getConsoleLogger.js.map +1 -1
  25. package/dist/loggers/index.d.ts +14 -0
  26. package/dist/loggers/index.d.ts.map +1 -0
  27. package/dist/loggers/index.js +14 -0
  28. package/dist/loggers/index.js.map +1 -0
  29. package/dist/node/index.d.ts +15 -0
  30. package/dist/node/index.d.ts.map +1 -0
  31. package/dist/node/index.js +15 -0
  32. package/dist/node/index.js.map +1 -0
  33. package/dist/node/main.d.ts +156 -0
  34. package/dist/node/main.d.ts.map +1 -0
  35. package/dist/node/main.js +261 -0
  36. package/dist/node/main.js.map +1 -0
  37. package/dist/{monitorMemory.d.ts → node/monitorMemory.d.ts} +3 -5
  38. package/dist/node/monitorMemory.d.ts.map +1 -0
  39. package/dist/{monitorMemory.js → node/monitorMemory.js} +4 -7
  40. package/dist/node/monitorMemory.js.map +1 -0
  41. package/dist/node/osRelease.d.ts +55 -0
  42. package/dist/node/osRelease.d.ts.map +1 -0
  43. package/dist/{osRelease.js → node/osRelease.js} +77 -24
  44. package/dist/node/osRelease.js.map +1 -0
  45. package/dist/type-testing/index.d.ts +18 -0
  46. package/dist/type-testing/index.d.ts.map +1 -0
  47. package/dist/type-testing/index.js +12 -0
  48. package/dist/type-testing/index.js.map +1 -0
  49. package/package.json +23 -8
  50. package/src/core/guards.ts +243 -0
  51. package/src/core/index.ts +31 -0
  52. package/src/{millisecondsToString.ts → core/millisecondsToString.ts} +3 -4
  53. package/src/core/noop.ts +2 -0
  54. package/src/index.ts +13 -49
  55. package/src/loggers/getConsoleLogger.ts +14 -19
  56. package/src/loggers/index.ts +14 -0
  57. package/src/node/index.ts +21 -0
  58. package/src/node/main.ts +427 -0
  59. package/src/{monitorMemory.ts → node/monitorMemory.ts} +4 -7
  60. package/src/node/osRelease.ts +251 -0
  61. package/src/type-testing/index.ts +18 -0
  62. package/dist/main.d.ts +0 -112
  63. package/dist/main.d.ts.map +0 -1
  64. package/dist/main.js +0 -219
  65. package/dist/main.js.map +0 -1
  66. package/dist/millisecondsToString.d.ts.map +0 -1
  67. package/dist/millisecondsToString.js.map +0 -1
  68. package/dist/monitorMemory.d.ts.map +0 -1
  69. package/dist/monitorMemory.js.map +0 -1
  70. package/dist/noop.d.ts +0 -7
  71. package/dist/noop.d.ts.map +0 -1
  72. package/dist/noop.js +0 -7
  73. package/dist/noop.js.map +0 -1
  74. package/dist/osRelease.d.ts +0 -46
  75. package/dist/osRelease.d.ts.map +0 -1
  76. package/dist/osRelease.js.map +0 -1
  77. package/dist/types.d.ts +0 -70
  78. package/dist/types.d.ts.map +0 -1
  79. package/dist/types.js +0 -84
  80. package/dist/types.js.map +0 -1
  81. package/src/main.ts +0 -302
  82. package/src/noop.ts +0 -6
  83. package/src/osRelease.ts +0 -174
  84. package/src/types.ts +0 -98
  85. /package/dist/{millisecondsToString.d.ts → core/millisecondsToString.d.ts} +0 -0
@@ -10,12 +10,23 @@ const OS_RELEASE_FILES = ["/etc/os-release", "/usr/lib/os-release"];
10
10
  * also rules out keys such as `__proto__` or `constructor`.
11
11
  */
12
12
  const OS_RELEASE_KEY = /^[A-Z][A-Z0-9_]*$/;
13
- /**
14
- * Reads the first readable os-release file, or returns `null` when none can be
15
- * read (missing, unreadable, or not a regular file).
16
- */
17
- function readOsReleaseFile() {
18
- for (const file of OS_RELEASE_FILES) {
13
+ /** The macOS property list with the product name and version. */
14
+ const MACOS_SYSTEM_VERSION_FILE = "/System/Library/CoreServices/SystemVersion.plist";
15
+ /** A `<key>…</key>` followed by a `<string>…</string>` value in a plist. */
16
+ const PLIST_STRING_ENTRY = /<key>([^<]*)<\/key>\s*<string>([^<]*)<\/string>/g;
17
+ /** Valid plist keys to copy; this also rules out keys such as `__proto__`. */
18
+ const PLIST_KEY = /^[A-Za-z][A-Za-z0-9_]*$/;
19
+ /** The predefined XML entities. */
20
+ const XML_ENTITIES = {
21
+ amp: "&",
22
+ apos: "'",
23
+ gt: ">",
24
+ lt: "<",
25
+ quot: '"',
26
+ };
27
+ /** Reads the first readable file of `files`, or returns `null` if none is. */
28
+ function readFirstFile(files) {
29
+ for (const file of files) {
19
30
  try {
20
31
  return readFileSync(file, "utf-8");
21
32
  }
@@ -25,6 +36,34 @@ function readOsReleaseFile() {
25
36
  }
26
37
  return null;
27
38
  }
39
+ /** Decodes the predefined and numeric character references of XML text. */
40
+ function decodeXml(text) {
41
+ return text.replace(/&(?:#x([0-9a-fA-F]+)|#([0-9]+)|([a-z]+));/g, (match, hex, decimal, name) => {
42
+ if (hex !== undefined || decimal !== undefined) {
43
+ const codePoint = Number.parseInt(hex ?? decimal ?? "", hex === undefined ? 10 : 16);
44
+ return codePoint <= 0x10ffff
45
+ ? String.fromCodePoint(codePoint)
46
+ : match;
47
+ }
48
+ return ((name !== undefined && Object.hasOwn(XML_ENTITIES, name)
49
+ ? XML_ENTITIES[name]
50
+ : undefined) ?? match);
51
+ });
52
+ }
53
+ /**
54
+ * Parses the `<string>` entries of an XML property list into a
55
+ * null-prototype record, skipping other value types and invalid keys.
56
+ */
57
+ function parsePlistStrings(content) {
58
+ const raw = Object.create(null);
59
+ for (const [, rawKey = "", value = ""] of content.matchAll(PLIST_STRING_ENTRY)) {
60
+ const key = decodeXml(rawKey.trim());
61
+ if (PLIST_KEY.test(key)) {
62
+ raw[key] = decodeXml(value.trim());
63
+ }
64
+ }
65
+ return raw;
66
+ }
28
67
  /**
29
68
  * Removes one level of shell-style quoting from an os-release value. Double
30
69
  * quotes allow the `\\`, `\"`, `` \` `` and `\$` escapes; single quotes are
@@ -43,9 +82,8 @@ function unquote(value) {
43
82
  return value;
44
83
  }
45
84
  /**
46
- * Parses os-release content into a null-prototype record. Blank lines,
47
- * comments, lines without `=`, and keys that are not valid variable names are
48
- * skipped.
85
+ * Parses os-release content into a null-prototype record, skipping blank
86
+ * lines, comments, lines without `=`, and invalid keys.
49
87
  */
50
88
  function parseOsRelease(content) {
51
89
  const raw = Object.create(null);
@@ -67,36 +105,38 @@ function parseOsRelease(content) {
67
105
  return raw;
68
106
  }
69
107
  /**
70
- * Returns OS release information for the current platform, or `null` on
71
- * unsupported platforms or when no os-release file can be read on Linux.
108
+ * Returns OS release information for the current platform (Linux, macOS, or
109
+ * Windows). `arch` always comes from `os.arch()`.
72
110
  *
73
- * On Linux, the information is read from `/etc/os-release`, falling back to
74
- * `/usr/lib/os-release`. `name`, `version`, and `arch` are normalized from
75
- * `NAME`/`PRETTY_NAME`, `VERSION_ID`/`VERSION`, and `os.arch()` respectively.
76
- * All raw keys from the file are also present on the returned object. Comment
77
- * lines are ignored, single- and double-quoted values are unquoted (with
78
- * backslash escapes inside double quotes), and keys that are not upper-case
79
- * shell variable names are skipped.
111
+ * - On Linux, it reads `/etc/os-release`, falling back to
112
+ * `/usr/lib/os-release`. `name` comes from `NAME` (or `PRETTY_NAME`) and
113
+ * `version` from `VERSION_ID` (or `VERSION`). Values are unquoted as in a
114
+ * shell; comments and keys that are not upper-case variable names are
115
+ * skipped.
116
+ * - On macOS, it reads `/System/Library/CoreServices/SystemVersion.plist`.
117
+ * `name` is `ProductName` (or `"macOS"`) and `version` is
118
+ * `ProductVersion` (e.g. `"15.1"`), never the Darwin kernel version.
119
+ * - On Windows, `version` is the kernel version, and Windows 11 is told
120
+ * apart from Windows 10 by NT build number (>= 22000).
80
121
  *
81
- * On Windows, distinguishes Windows 11 from Windows 10 by NT build number
82
- * (>= 22000 → Windows 11).
122
+ * On Linux and macOS, the raw keys of the file are also present.
83
123
  *
84
- * @returns An {@link OsRelease} object on supported platforms, or `null` when
85
- * the platform is unsupported or no os-release file can be read on Linux.
124
+ * @returns An {@link OsRelease}, or `null` on unsupported platforms or when
125
+ * the release file cannot be read.
86
126
  *
87
127
  * @example
88
128
  * ```ts
89
129
  * import { osRelease } from "@darthcav/ts-utils"
90
130
  *
91
131
  * const info = osRelease()
92
- * if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
132
+ * if (info) console.log(info.name) // e.g. "Ubuntu", "macOS", or "Windows 11"
93
133
  * if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
94
134
  * ```
95
135
  */
96
136
  export function osRelease() {
97
137
  const p = platform();
98
138
  if (p === "linux") {
99
- const content = readOsReleaseFile();
139
+ const content = readFirstFile(OS_RELEASE_FILES);
100
140
  if (content === null) {
101
141
  return null;
102
142
  }
@@ -108,6 +148,19 @@ export function osRelease() {
108
148
  arch: arch(),
109
149
  };
110
150
  }
151
+ if (p === "darwin") {
152
+ const content = readFirstFile([MACOS_SYSTEM_VERSION_FILE]);
153
+ if (content === null) {
154
+ return null;
155
+ }
156
+ const raw = parsePlistStrings(content);
157
+ return {
158
+ ...raw,
159
+ name: raw["ProductName"] || "macOS",
160
+ version: raw["ProductVersion"] ?? "",
161
+ arch: arch(),
162
+ };
163
+ }
111
164
  if (p === "win32") {
112
165
  const version = kernelRelease();
113
166
  const [major, , build] = version.split(".").map(Number);
@@ -0,0 +1 @@
1
+ {"version":3,"file":"osRelease.js","sourceRoot":"","sources":["../../src/node/osRelease.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AACtC,OAAO,EAAE,IAAI,EAAE,OAAO,IAAI,aAAa,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAA;AA2BlE;;;GAGG;AACH,MAAM,gBAAgB,GAAG,CAAC,iBAAiB,EAAE,qBAAqB,CAAU,CAAA;AAE5E;;;GAGG;AACH,MAAM,cAAc,GAAG,mBAAmB,CAAA;AAE1C,iEAAiE;AACjE,MAAM,yBAAyB,GAC3B,kDAAkD,CAAA;AAEtD,4EAA4E;AAC5E,MAAM,kBAAkB,GAAG,kDAAkD,CAAA;AAE7E,8EAA8E;AAC9E,MAAM,SAAS,GAAG,yBAAyB,CAAA;AAE3C,mCAAmC;AACnC,MAAM,YAAY,GAAqC;IACnD,GAAG,EAAE,GAAG;IACR,IAAI,EAAE,GAAG;IACT,EAAE,EAAE,GAAG;IACP,EAAE,EAAE,GAAG;IACP,IAAI,EAAE,GAAG;CACZ,CAAA;AAED,8EAA8E;AAC9E,SAAS,aAAa,CAAC,KAAwB;IAC3C,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QACvB,IAAI,CAAC;YACD,OAAO,YAAY,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QACtC,CAAC;QAAC,MAAM,CAAC;YACL,0BAA0B;QAC9B,CAAC;IACL,CAAC;IACD,OAAO,IAAI,CAAA;AACf,CAAC;AAED,2EAA2E;AAC3E,SAAS,SAAS,CAAC,IAAY;IAC3B,OAAO,IAAI,CAAC,OAAO,CACf,4CAA4C,EAC5C,CAAC,KAAK,EAAE,GAAY,EAAE,OAAgB,EAAE,IAAa,EAAE,EAAE;QACrD,IAAI,GAAG,KAAK,SAAS,IA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AC,GAAG,CAAC,CAAC,GAAG,CAAC,MAAM,CAAC,CAAA;QACvD,IAAI,IAAI,GAAG,UAAU,CAAA;QACrB,IAAI,KAAK,KAAK,EAAE,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,IAAI,KAAK,EAAE,CAAC;YACxC,IAAI,IAAI,IAAI,CAAA;QAChB,CAAC;aAAM,CAAC;YACJ,QAAQ,OAAO,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC;gBAC9B,KAAK,KAAK;oBACN,IAAI,IAAI,IAAI,CAAA;oBACZ,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,KAAK,CAAA;oBACb,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,GAAG,CAAA;oBACX,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,GAAG,CAAA;oBACX,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,OAAO,CAAA;oBACf,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,mBAAmB,CAAA;oBAC3B,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,IAAI,CAAA;oBACZ,MAAK;gBACT,KAAK,KAAK;oBACN,IAAI,IAAI,MAAM,CAAA;oBACd,MAAK;gBACT;oBACI,IAAI,IAAI,iBAAiB,CAAA;oBACzB,MAAK;YACb,CAAC;QACL,CAAC;QACD,OAAO,EAAE,IAAI,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,CAAA;IAC1C,CAAC;IACD,OAAO,IAAI,CAAA;AACf,CAAC"}
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Type-level testing helpers. They are types only and add no code at runtime.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import type { Assert, Equal } from "@darthcav/ts-utils/type-testing"
7
+ * ```
8
+ *
9
+ * @module type-testing
10
+ */
11
+ export type { Assert } from "./Assert.ts";
12
+ export type { Equal } from "./Equal.ts";
13
+ export type { Extends } from "./Extends.ts";
14
+ export type { IsAny } from "./IsAny.ts";
15
+ export type { IsNever } from "./IsNever.ts";
16
+ export type { IsUnknown } from "./IsUnknown.ts";
17
+ export type { Not } from "./Not.ts";
18
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/type-testing/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,YAAY,EAAE,MAAM,EAAE,MAAM,aAAa,CAAA;AACzC,YAAY,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,YAAY,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AACvC,YAAY,EAAE,OAAO,EAAE,MAAM,cAAc,CAAA;AAC3C,YAAY,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC/C,YAAY,EAAE,GAAG,EAAE,MAAM,UAAU,CAAA"}
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Type-level testing helpers. They are types only and add no code at runtime.
3
+ *
4
+ * @example
5
+ * ```ts
6
+ * import type { Assert, Equal } from "@darthcav/ts-utils/type-testing"
7
+ * ```
8
+ *
9
+ * @module type-testing
10
+ */
11
+ export {};
12
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/type-testing/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG"}
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.12.2",
4
+ "version": "1.0.0",
5
5
  "author": {
6
6
  "name": "darthcav",
7
7
  "url": "https://github.com/darthcav"
@@ -15,21 +15,36 @@
15
15
  ".": {
16
16
  "types": "./dist/index.d.ts",
17
17
  "default": "./dist/index.js"
18
+ },
19
+ "./core": {
20
+ "types": "./dist/core/index.d.ts",
21
+ "default": "./dist/core/index.js"
22
+ },
23
+ "./node": {
24
+ "types": "./dist/node/index.d.ts",
25
+ "default": "./dist/node/index.js"
26
+ },
27
+ "./loggers": {
28
+ "types": "./dist/loggers/index.d.ts",
29
+ "default": "./dist/loggers/index.js"
30
+ },
31
+ "./type-testing": {
32
+ "types": "./dist/type-testing/index.d.ts",
33
+ "default": "./dist/type-testing/index.js"
18
34
  }
19
35
  },
20
36
  "files": [
21
37
  "src",
22
38
  "!src/__tests__",
23
- "dist",
24
- "!dist/__tests__"
39
+ "dist"
25
40
  ],
26
41
  "dependencies": {
27
- "@logtape/logtape": "2.3.11"
42
+ "@logtape/logtape": "2.4.2"
28
43
  },
29
44
  "devDependencies": {
30
- "@biomejs/biome": "2.5.15",
31
- "@types/node": "26.6.4",
32
- "prettier": "3.9.9",
45
+ "@biomejs/biome": "2.6.8",
46
+ "@types/node": "26.6.6",
47
+ "prettier": "3.10.1",
33
48
  "typedoc": "0.28.20"
34
49
  },
35
50
  "repository": {
@@ -42,7 +57,7 @@
42
57
  "scripts": {
43
58
  "\n===== Build =====": "",
44
59
  "clean": "node -e \"fs.rmSync('dist', { recursive: true, force: true })\"",
45
- "build": "npm run clean && tsc",
60
+ "build": "npm run clean && tsc -p tsconfig.build.json",
46
61
  "prepare": "npm run build",
47
62
  "\n===== Typecheck =====": "",
48
63
  "typecheck": "tsc --noEmit",
@@ -0,0 +1,243 @@
1
+ /**
2
+ * A generic runtime object with property names and values that are not known at
3
+ * compile time.
4
+ *
5
+ * Uses `unknown` instead of `any` so callers must narrow values before using
6
+ * them in a type-safe way.
7
+ */
8
+ export type RuntimeObject = Record<string, unknown>
9
+
10
+ /**
11
+ * Type guard that checks whether a value is a plain object that can be treated
12
+ * as a runtime JSON object.
13
+ *
14
+ * Plain objects are object literals, `JSON.parse` results, and
15
+ * `Object.create(null)` objects, including those from another realm (e.g. a
16
+ * `node:vm` context). Arrays, class instances, and built-ins such as `Date`,
17
+ * `Map`, `Set`, `RegExp`, or module namespace objects are rejected.
18
+ *
19
+ * The check is shallow and keys are not sanitized: an own `__proto__` or
20
+ * `constructor` key (as `JSON.parse` can produce from untrusted input) is kept.
21
+ * Do not merge the value into other objects key by key without guarding
22
+ * against those keys, or prototype pollution becomes possible.
23
+ *
24
+ * @param value - Value to check.
25
+ * @returns `true` when the value is a plain object, narrowing it to
26
+ * {@link RuntimeObject}.
27
+ *
28
+ * @example
29
+ * ```ts
30
+ * const body: unknown = JSON.parse(input)
31
+ * if (isRuntimeObject(body)) {
32
+ * console.log(Object.keys(body)) // body: RuntimeObject
33
+ * }
34
+ * ```
35
+ */
36
+ export function isRuntimeObject(value: unknown): value is RuntimeObject {
37
+ if (typeof value !== "object" || value === null) {
38
+ return false
39
+ }
40
+ const prototype: unknown = Object.getPrototypeOf(value)
41
+ // A null prototype, or a realm's root `Object.prototype` (whose own
42
+ // prototype is null), marks a plain object.
43
+ if (
44
+ prototype !== null &&
45
+ prototype !== Object.prototype &&
46
+ Object.getPrototypeOf(prototype) !== null
47
+ ) {
48
+ return false
49
+ }
50
+ // Exclude null-prototype exotics such as module namespace objects.
51
+ return !(Symbol.toStringTag in value || Symbol.iterator in value)
52
+ }
53
+
54
+ /**
55
+ * Returns the input when it is a plain object, as decided by
56
+ * {@link isRuntimeObject} (see there for the caveats about unsanitized keys).
57
+ *
58
+ * @param value - Value to narrow.
59
+ * @returns The same value as a {@link RuntimeObject}, or `undefined` when the
60
+ * value is not a plain object.
61
+ */
62
+ export function asRuntimeObject(value: unknown): RuntimeObject | undefined {
63
+ return isRuntimeObject(value) ? value : undefined
64
+ }
65
+
66
+ /**
67
+ * Type guard that checks whether a value is a string.
68
+ *
69
+ * @param value - Value to check.
70
+ * @returns `true` when the value is a string, narrowing it to `string`.
71
+ */
72
+ export function isString(value: unknown): value is string {
73
+ return typeof value === "string"
74
+ }
75
+
76
+ /**
77
+ * Returns the input when it is a string.
78
+ *
79
+ * @param value - Value to narrow.
80
+ * @returns The same value as a string, or `undefined` when the value is not a string.
81
+ */
82
+ export function asString(value: unknown): string | undefined {
83
+ return isString(value) ? value : undefined
84
+ }
85
+
86
+ /**
87
+ * Type guard that checks whether a value is a boolean primitive (`true` or
88
+ * `false`). Boxed `Boolean` objects are rejected.
89
+ *
90
+ * @param value - Value to check.
91
+ * @returns `true` when the value is a boolean, narrowing it to `boolean`.
92
+ */
93
+ export function isBoolean(value: unknown): value is boolean {
94
+ return typeof value === "boolean"
95
+ }
96
+
97
+ /**
98
+ * Returns the input when it is a boolean primitive, as decided by
99
+ * {@link isBoolean}.
100
+ *
101
+ * @param value - Value to narrow.
102
+ * @returns The same value as a boolean, or `undefined` when the value is not
103
+ * a boolean.
104
+ *
105
+ * @example
106
+ * ```ts
107
+ * const enabled = asBoolean(config["enabled"]) ?? false
108
+ * ```
109
+ */
110
+ export function asBoolean(value: unknown): boolean | undefined {
111
+ return isBoolean(value) ? value : undefined
112
+ }
113
+
114
+ /**
115
+ * Type guard that checks whether a value is a finite number primitive. `NaN`,
116
+ * `Infinity`, `-Infinity`, boxed `Number` objects, bigints, and numeric
117
+ * strings are rejected.
118
+ *
119
+ * @param value - Value to check.
120
+ * @returns `true` when the value is a finite number, narrowing it to
121
+ * `number`.
122
+ */
123
+ export function isFiniteNumber(value: unknown): value is number {
124
+ return Number.isFinite(value)
125
+ }
126
+
127
+ /**
128
+ * Returns the input when it is a finite number primitive, as decided by
129
+ * {@link isFiniteNumber}.
130
+ *
131
+ * @param value - Value to narrow.
132
+ * @returns The same value as a number, or `undefined` when the value is not a
133
+ * finite number.
134
+ *
135
+ * @example
136
+ * ```ts
137
+ * const port = asFiniteNumber(config["port"]) ?? 3000
138
+ * ```
139
+ */
140
+ export function asFiniteNumber(value: unknown): number | undefined {
141
+ return isFiniteNumber(value) ? value : undefined
142
+ }
143
+
144
+ /**
145
+ * A predicate that narrows an `unknown` value to `T`, such as
146
+ * {@link isString}. Used by {@link isArrayOf}, {@link asArrayOf}, and
147
+ * {@link filterByGuard}.
148
+ *
149
+ * @typeParam T - The type the guard narrows to.
150
+ */
151
+ export type TypeGuard<T> = (value: unknown) => value is T
152
+
153
+ /**
154
+ * Type guard that checks whether a value is an array whose elements all pass
155
+ * `guard`. A single invalid element makes it fail (use {@link filterByGuard}
156
+ * to keep only the valid ones); an empty array passes. Holes in sparse arrays
157
+ * are checked as `undefined`.
158
+ *
159
+ * @typeParam T - The element type the guard narrows to.
160
+ * @param value - Value to check.
161
+ * @param guard - Type guard every element must pass.
162
+ * @returns `true` when the value is an array of `T`, narrowing it to `T[]`.
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * const body: unknown = JSON.parse(input)
167
+ * if (isArrayOf(body, isString)) {
168
+ * body.map((tag) => tag.toUpperCase()) // body: string[]
169
+ * }
170
+ * ```
171
+ */
172
+ export function isArrayOf<T>(
173
+ value: unknown,
174
+ guard: TypeGuard<T>,
175
+ ): value is T[] {
176
+ if (!Array.isArray(value)) {
177
+ return false
178
+ }
179
+ // An indexed loop, unlike `every`, also checks the holes of sparse arrays.
180
+ for (let i = 0; i < value.length; i++) {
181
+ if (!guard(value[i])) {
182
+ return false
183
+ }
184
+ }
185
+ return true
186
+ }
187
+
188
+ /**
189
+ * Returns the input when it is an array whose elements all pass `guard`, as
190
+ * decided by {@link isArrayOf}.
191
+ *
192
+ * @typeParam T - The element type the guard narrows to.
193
+ * @param value - Value to narrow.
194
+ * @param guard - Type guard every element must pass.
195
+ * @returns The same array as `T[]`, or `undefined` when the value is not an
196
+ * array or any element fails the guard.
197
+ *
198
+ * @example
199
+ * ```ts
200
+ * const tags = asArrayOf(asRuntimeObject(body)?.["tags"], isString) ?? []
201
+ * ```
202
+ */
203
+ export function asArrayOf<T>(
204
+ value: unknown,
205
+ guard: TypeGuard<T>,
206
+ ): T[] | undefined {
207
+ return isArrayOf(value, guard) ? value : undefined
208
+ }
209
+
210
+ /**
211
+ * Keeps the elements of an array that pass `guard` and silently drops the
212
+ * others, unlike {@link isArrayOf}, which rejects the whole array. A value
213
+ * that is not an array yields an empty array, and holes in sparse arrays are
214
+ * skipped.
215
+ *
216
+ * @typeParam T - The element type the guard narrows to.
217
+ * @param value - Value that may be an array.
218
+ * @param guard - Type guard deciding which elements to keep.
219
+ * @returns A new array with the elements that pass the guard.
220
+ *
221
+ * @example
222
+ * ```ts
223
+ * filterByGuard(["a", 1, "b", null], isString) // ["a", "b"]
224
+ * filterByGuard("not an array", isString) // []
225
+ * ```
226
+ */
227
+ export function filterByGuard<T>(value: unknown, guard: TypeGuard<T>): T[] {
228
+ return Array.isArray(value)
229
+ ? value.filter((element: unknown) => guard(element))
230
+ : []
231
+ }
232
+
233
+ /**
234
+ * Keeps the plain objects of an array: {@link filterByGuard} with
235
+ * {@link isRuntimeObject}, whose caveats about unsanitized keys apply. A value
236
+ * that is not an array yields an empty array.
237
+ *
238
+ * @param value - Value that may contain runtime objects.
239
+ * @returns Only the elements that can be treated as {@link RuntimeObject} values.
240
+ */
241
+ export function toRuntimeObjectArray(value: unknown): RuntimeObject[] {
242
+ return filterByGuard(value, isRuntimeObject)
243
+ }
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Portable helpers: type guards and narrowing helpers, duration formatting,
3
+ * and `noop`. They import no Node.js built-ins or packages, so they run in any
4
+ * ES2025 runtime (`millisecondsToString` needs `Intl.DurationFormat`).
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { asString, millisecondsToString } from "@darthcav/ts-utils/core"
9
+ * ```
10
+ *
11
+ * @module core
12
+ */
13
+
14
+ export {
15
+ asArrayOf,
16
+ asBoolean,
17
+ asFiniteNumber,
18
+ asRuntimeObject,
19
+ asString,
20
+ filterByGuard,
21
+ isArrayOf,
22
+ isBoolean,
23
+ isFiniteNumber,
24
+ isRuntimeObject,
25
+ isString,
26
+ type RuntimeObject,
27
+ type TypeGuard,
28
+ toRuntimeObjectArray,
29
+ } from "./guards.ts"
30
+ export { millisecondsToString } from "./millisecondsToString.ts"
31
+ export { noop } from "./noop.ts"
@@ -1,8 +1,7 @@
1
1
  /**
2
- * Maximum number of {@link Intl.DurationFormat} instances retained in the
3
- * cache. Bounds memory growth when many distinct locales are requested (e.g.
4
- * a locale derived from untrusted input). When the limit is reached, the
5
- * least-recently-inserted entry is evicted.
2
+ * Maximum number of cached formatters, so many distinct locales (e.g. from
3
+ * untrusted input) cannot grow memory without bound. The oldest entry is
4
+ * evicted first.
6
5
  */
7
6
  const MAX_CACHED_FORMATTERS = 64
8
7
 
@@ -0,0 +1,2 @@
1
+ /** Does nothing. Useful as a placeholder callback, default handler, or in tests. */
2
+ export const noop = (): void => {}
package/src/index.ts CHANGED
@@ -1,54 +1,18 @@
1
1
  /**
2
2
  * A collection of utility functions for TypeScript applications and modules.
3
3
  *
4
- * @packageDocumentation
4
+ * The root entry point re-exports every public utility. Import from a subpath
5
+ * to load only one domain:
6
+ *
7
+ * - `@darthcav/ts-utils/core`: portable helpers (no Node.js built-ins).
8
+ * - `@darthcav/ts-utils/node`: Node.js process utilities.
9
+ * - `@darthcav/ts-utils/loggers`: LogTape logger helpers.
10
+ * - `@darthcav/ts-utils/type-testing`: type-level testing helpers.
11
+ *
12
+ * @module @darthcav/ts-utils
5
13
  */
6
14
 
7
- import { getConsoleLogger } from "./loggers/getConsoleLogger.ts"
8
- import { getDummyLogger } from "./loggers/getDummyLogger.ts"
9
- import { type LauncherFunction, type MainOptions, main } from "./main.ts"
10
- import { millisecondsToString } from "./millisecondsToString.ts"
11
- import { monitorMemory } from "./monitorMemory.ts"
12
- import { noop } from "./noop.ts"
13
- import { type OsRelease, osRelease } from "./osRelease.ts"
14
- import type { Assert } from "./type-testing/Assert.ts"
15
- import type { Equal } from "./type-testing/Equal.ts"
16
- import type { Extends } from "./type-testing/Extends.ts"
17
- import type { IsAny } from "./type-testing/IsAny.ts"
18
- import type { IsNever } from "./type-testing/IsNever.ts"
19
- import type { IsUnknown } from "./type-testing/IsUnknown.ts"
20
- import type { Not } from "./type-testing/Not.ts"
21
- import {
22
- asRuntimeObject,
23
- asString,
24
- isRuntimeObject,
25
- isString,
26
- type RuntimeObject,
27
- toRuntimeObjectArray,
28
- } from "./types.ts"
29
-
30
- export {
31
- type Assert,
32
- asRuntimeObject,
33
- asString,
34
- type Equal,
35
- type Extends,
36
- getConsoleLogger,
37
- getDummyLogger,
38
- type IsAny,
39
- type IsNever,
40
- type IsUnknown,
41
- isRuntimeObject,
42
- isString,
43
- type LauncherFunction,
44
- type MainOptions,
45
- main,
46
- millisecondsToString,
47
- monitorMemory,
48
- type Not,
49
- noop,
50
- type OsRelease,
51
- osRelease,
52
- type RuntimeObject,
53
- toRuntimeObjectArray,
54
- }
15
+ export * from "./core/index.ts"
16
+ export * from "./loggers/index.ts"
17
+ export * from "./node/index.ts"
18
+ export type * from "./type-testing/index.ts"