@darthcav/ts-utils 0.10.10 → 0.12.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.
- package/README.md +170 -39
- package/dist/index.d.ts +16 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -8
- package/dist/index.js.map +1 -1
- package/dist/loggers/getConsoleLogger.d.ts +10 -4
- package/dist/loggers/getConsoleLogger.d.ts.map +1 -1
- package/dist/loggers/getConsoleLogger.js +36 -9
- package/dist/loggers/getConsoleLogger.js.map +1 -1
- package/dist/loggers/getDummyLogger.d.ts +14 -3
- package/dist/loggers/getDummyLogger.d.ts.map +1 -1
- package/dist/loggers/getDummyLogger.js +29 -11
- package/dist/loggers/getDummyLogger.js.map +1 -1
- package/dist/main.d.ts +83 -51
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +200 -40
- package/dist/main.js.map +1 -1
- package/dist/millisecondsToString.d.ts +1 -1
- package/dist/millisecondsToString.d.ts.map +1 -1
- package/dist/millisecondsToString.js +1 -1
- package/dist/millisecondsToString.js.map +1 -1
- package/dist/monitorMemory.d.ts +25 -7
- package/dist/monitorMemory.d.ts.map +1 -1
- package/dist/monitorMemory.js +52 -14
- package/dist/monitorMemory.js.map +1 -1
- package/dist/noop.d.ts +1 -2
- package/dist/noop.d.ts.map +1 -1
- package/dist/noop.js +1 -2
- package/dist/noop.js.map +1 -1
- package/dist/{os-utils.d.ts → osRelease.d.ts} +11 -7
- package/dist/osRelease.d.ts.map +1 -0
- package/dist/osRelease.js +153 -0
- package/dist/osRelease.js.map +1 -0
- package/dist/type-testing/Assert.d.ts +23 -0
- package/dist/type-testing/Assert.d.ts.map +1 -0
- package/dist/type-testing/Assert.js +2 -0
- package/dist/type-testing/Assert.js.map +1 -0
- package/dist/type-testing/Equal.d.ts +25 -0
- package/dist/type-testing/Equal.d.ts.map +1 -0
- package/dist/type-testing/Equal.js +2 -0
- package/dist/type-testing/Equal.js.map +1 -0
- package/dist/type-testing/Extends.d.ts +22 -0
- package/dist/type-testing/Extends.d.ts.map +1 -0
- package/dist/type-testing/Extends.js +2 -0
- package/dist/type-testing/Extends.js.map +1 -0
- package/dist/type-testing/IsAny.d.ts +17 -0
- package/dist/type-testing/IsAny.d.ts.map +1 -0
- package/dist/type-testing/IsAny.js +2 -0
- package/dist/type-testing/IsAny.js.map +1 -0
- package/dist/type-testing/IsNever.d.ts +17 -0
- package/dist/type-testing/IsNever.d.ts.map +1 -0
- package/dist/type-testing/IsNever.js +2 -0
- package/dist/type-testing/IsNever.js.map +1 -0
- package/dist/type-testing/IsUnknown.d.ts +20 -0
- package/dist/type-testing/IsUnknown.d.ts.map +1 -0
- package/dist/type-testing/IsUnknown.js +2 -0
- package/dist/type-testing/IsUnknown.js.map +1 -0
- package/dist/type-testing/Not.d.ts +17 -0
- package/dist/type-testing/Not.d.ts.map +1 -0
- package/dist/type-testing/Not.js +2 -0
- package/dist/type-testing/Not.js.map +1 -0
- package/dist/types.d.ts +42 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +61 -16
- package/dist/types.js.map +1 -1
- package/package.json +10 -12
- package/src/index.ts +26 -7
- package/src/loggers/getConsoleLogger.ts +41 -8
- package/src/loggers/getDummyLogger.ts +31 -11
- package/src/main.ts +249 -126
- package/src/millisecondsToString.ts +1 -4
- package/src/monitorMemory.ts +58 -19
- package/src/noop.ts +1 -3
- package/src/osRelease.ts +172 -0
- package/src/type-testing/Assert.ts +22 -0
- package/src/type-testing/Equal.ts +27 -0
- package/src/type-testing/Extends.ts +21 -0
- package/src/type-testing/IsAny.ts +16 -0
- package/src/type-testing/IsNever.ts +16 -0
- package/src/type-testing/IsUnknown.ts +21 -0
- package/src/type-testing/Not.ts +16 -0
- package/src/types.ts +65 -17
- package/dist/os-utils.d.ts.map +0 -1
- package/dist/os-utils.js +0 -94
- package/dist/os-utils.js.map +0 -1
- package/src/os-utils.ts +0 -110
|
@@ -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;
|
|
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,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAA;AAEhE,uDAAuD;AACvD,MAAM,YAAY,GAAG,EAAE,GAAG,KAAK,CAAA;AAE/B;;;GAGG;AACH,MAAM,YAAY,GAAG,aAAa,CAAA;AAElC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAyCG;AACH,MAAM,UAAU,aAAa,CAAC,MAAc,EAAE,QAAgB,EAAE;IAC5D,MAAM,KAAK,GAAG,EAAE,GAAG,EAAE,GAAG,KAAK,GAAG,KAAK,CAAA;IACrC,IACI,CAAC,MAAM,CAAC,QAAQ,CAAC,KAAK,CAAC;QACvB,KAAK,GAAG,YAAY;QACpB,KAAK,GAAG,YAAY,EACtB,CAAC;QACC,MAAM,IAAI,UAAU,CAChB,0DAA0D,YAAY,GAAG,SAAS,QAAQ,YAAY,GAAG,SAAS,wCAAwC,KAAK,EAAE,CACpK,CAAA;IACL,CAAC;IACD,MAAM,QAAQ,GAAG,MAAM,CAAC,QAAQ,CAAC,CAAC,eAAe,CAAC,CAAC,CAAA;IAEnD,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,EAAE;QAC3B,MAAM,QAAQ,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,GAAG,KAAK,CAAC,CAAA;QAC7C,MAAM,eAAe,GAAG,oBAAoB,CAAC,QAAQ,CAAC,CAAA;QACtD,MAAM,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,CAAA;QAC5D,kEAAkE;QAClE,qEAAqE;QACrE,iEAAiE;QACjE,QAAQ,CAAC,IAAI,CAAC,mBAAmB,eAAe,EAAE,EAAE;YAChD,MAAM,EAAE,eAAe;YACvB,QAAQ;SACX,CAAC,CAAA;QACF,QAAQ,CAAC,IAAI,CACT,oHAAoH,EACpH,EAAE,GAAG,EAAE,SAAS,EAAE,QAAQ,EAAE,QAAQ,EAAE,CACzC,CAAA;IACL,CAAC,EAAE,KAAK,CAAC,CAAA;IACT,oDAAoD;IACpD,KAAK,CAAC,KAAK,EAAE,CAAA;IAEb,OAAO,GAAG,EAAE,CAAC,aAAa,CAAC,KAAK,CAAC,CAAA;AACrC,CAAC"}
|
package/dist/noop.d.ts
CHANGED
package/dist/noop.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,
|
|
1
|
+
{"version":3,"file":"noop.d.ts","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,eAAO,MAAM,IAAI,QAAO,IAAU,CAAA"}
|
package/dist/noop.js
CHANGED
package/dist/noop.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,IAAI,GAAG,GAAS,EAAE,GAAE,CAAC,CAAA
|
|
1
|
+
{"version":3,"file":"noop.js","sourceRoot":"","sources":["../src/noop.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,GAAS,EAAE,GAAE,CAAC,CAAA"}
|
|
@@ -14,18 +14,22 @@ export type OsRelease = {
|
|
|
14
14
|
arch: string;
|
|
15
15
|
} & RuntimeObject;
|
|
16
16
|
/**
|
|
17
|
-
* Returns OS release information for the current platform, or `null` on
|
|
18
|
-
* platforms or when
|
|
17
|
+
* Returns OS release information for the current platform, or `null` on
|
|
18
|
+
* unsupported platforms or when no os-release file can be read on Linux.
|
|
19
19
|
*
|
|
20
|
-
* On Linux,
|
|
21
|
-
* `
|
|
22
|
-
*
|
|
20
|
+
* On Linux, the information is read from `/etc/os-release`, falling back to
|
|
21
|
+
* `/usr/lib/os-release`. `name`, `version`, and `arch` are normalized from
|
|
22
|
+
* `NAME`/`PRETTY_NAME`, `VERSION_ID`/`VERSION`, and `os.arch()` respectively.
|
|
23
|
+
* All raw keys from the file are also present on the returned object. Comment
|
|
24
|
+
* lines are ignored, single- and double-quoted values are unquoted (with
|
|
25
|
+
* backslash escapes inside double quotes), and keys that are not upper-case
|
|
26
|
+
* shell variable names are skipped.
|
|
23
27
|
*
|
|
24
28
|
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
25
29
|
* (>= 22000 → Windows 11).
|
|
26
30
|
*
|
|
27
31
|
* @returns An {@link OsRelease} object on supported platforms, or `null` when
|
|
28
|
-
* the platform is unsupported or
|
|
32
|
+
* the platform is unsupported or no os-release file can be read on Linux.
|
|
29
33
|
*
|
|
30
34
|
* @example
|
|
31
35
|
* ```ts
|
|
@@ -37,4 +41,4 @@ export type OsRelease = {
|
|
|
37
41
|
* ```
|
|
38
42
|
*/
|
|
39
43
|
export declare function osRelease(): OsRelease | null;
|
|
40
|
-
//# sourceMappingURL=
|
|
44
|
+
//# sourceMappingURL=osRelease.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"osRelease.d.ts","sourceRoot":"","sources":["../src/osRelease.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAE/C;;;;;GAKG;AACH,MAAM,MAAM,SAAS,GAAG;IACpB,iEAAiE;IACjE,IAAI,EAAE,MAAM,CAAA;IACZ,2DAA2D;IAC3D,OAAO,EAAE,MAAM,CAAA;IACf,uEAAuE;IACvE,IAAI,EAAE,MAAM,CAAA;CACf,GAAG,aAAa,CAAA;AAwEjB;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,SAAS,IAAI,SAAS,GAAG,IAAI,CAuD5C"}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { arch, release as kernelRelease, platform } from "node:os";
|
|
3
|
+
/**
|
|
4
|
+
* Candidate os-release files, in lookup order. Per the freedesktop.org spec,
|
|
5
|
+
* `/etc/os-release` takes precedence and `/usr/lib/os-release` is the fallback.
|
|
6
|
+
*/
|
|
7
|
+
const OS_RELEASE_FILES = ["/etc/os-release", "/usr/lib/os-release"];
|
|
8
|
+
/**
|
|
9
|
+
* Valid os-release keys: shell-compatible, upper-case variable names. This
|
|
10
|
+
* also rules out keys such as `__proto__` or `constructor`.
|
|
11
|
+
*/
|
|
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) {
|
|
19
|
+
try {
|
|
20
|
+
return readFileSync(file, "utf-8");
|
|
21
|
+
}
|
|
22
|
+
catch {
|
|
23
|
+
// Try the next candidate.
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Removes one level of shell-style quoting from an os-release value. Double
|
|
30
|
+
* quotes allow the `\\`, `\"`, `` \` `` and `\$` escapes; single quotes are
|
|
31
|
+
* literal.
|
|
32
|
+
*/
|
|
33
|
+
function unquote(value) {
|
|
34
|
+
if (value.length >= 2) {
|
|
35
|
+
const quote = value[0];
|
|
36
|
+
if (quote === '"' && value.endsWith('"')) {
|
|
37
|
+
return value.slice(1, -1).replace(/\\([\\"`$])/g, "$1");
|
|
38
|
+
}
|
|
39
|
+
if (quote === "'" && value.endsWith("'")) {
|
|
40
|
+
return value.slice(1, -1);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
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.
|
|
49
|
+
*/
|
|
50
|
+
function parseOsRelease(content) {
|
|
51
|
+
const raw = Object.create(null);
|
|
52
|
+
for (const line of content.split("\n")) {
|
|
53
|
+
const trimmed = line.trim();
|
|
54
|
+
if (trimmed === "" || trimmed.startsWith("#")) {
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
const eq = trimmed.indexOf("=");
|
|
58
|
+
if (eq === -1) {
|
|
59
|
+
continue;
|
|
60
|
+
}
|
|
61
|
+
const key = trimmed.substring(0, eq).trim();
|
|
62
|
+
if (!OS_RELEASE_KEY.test(key)) {
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
raw[key] = unquote(trimmed.substring(eq + 1).trim());
|
|
66
|
+
}
|
|
67
|
+
return raw;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
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.
|
|
72
|
+
*
|
|
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.
|
|
80
|
+
*
|
|
81
|
+
* On Windows, distinguishes Windows 11 from Windows 10 by NT build number
|
|
82
|
+
* (>= 22000 → Windows 11).
|
|
83
|
+
*
|
|
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.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* import { osRelease } from "@darthcav/ts-utils"
|
|
90
|
+
*
|
|
91
|
+
* const info = osRelease()
|
|
92
|
+
* if (info) console.log(info.name) // e.g. "Ubuntu" or "Windows 11"
|
|
93
|
+
* if (info) console.log(info.PRETTY_NAME) // e.g. "Ubuntu 24.04 LTS" (Linux only)
|
|
94
|
+
* ```
|
|
95
|
+
*/
|
|
96
|
+
export function osRelease() {
|
|
97
|
+
const p = platform();
|
|
98
|
+
if (p === "linux") {
|
|
99
|
+
const content = readOsReleaseFile();
|
|
100
|
+
if (content === null) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
const raw = parseOsRelease(content);
|
|
104
|
+
return {
|
|
105
|
+
...raw,
|
|
106
|
+
name: raw["NAME"] ?? raw["PRETTY_NAME"] ?? "",
|
|
107
|
+
version: raw["VERSION_ID"] ?? raw["VERSION"] ?? "",
|
|
108
|
+
arch: arch(),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (p === "win32") {
|
|
112
|
+
const version = kernelRelease();
|
|
113
|
+
const [major, , build] = version.split(".").map(Number);
|
|
114
|
+
let name = "Windows ";
|
|
115
|
+
if (major === 10 && (build ?? 0) >= 22000) {
|
|
116
|
+
name += "11";
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
switch (version.substring(0, 3)) {
|
|
120
|
+
case "10.":
|
|
121
|
+
name += "10";
|
|
122
|
+
break;
|
|
123
|
+
case "6.3":
|
|
124
|
+
name += "8.1";
|
|
125
|
+
break;
|
|
126
|
+
case "6.2":
|
|
127
|
+
name += "8";
|
|
128
|
+
break;
|
|
129
|
+
case "6.1":
|
|
130
|
+
name += "7";
|
|
131
|
+
break;
|
|
132
|
+
case "6.0":
|
|
133
|
+
name += "Vista";
|
|
134
|
+
break;
|
|
135
|
+
case "5.2":
|
|
136
|
+
name += "XP 64-Bit Edition";
|
|
137
|
+
break;
|
|
138
|
+
case "5.1":
|
|
139
|
+
name += "XP";
|
|
140
|
+
break;
|
|
141
|
+
case "5.0":
|
|
142
|
+
name += "2000";
|
|
143
|
+
break;
|
|
144
|
+
default:
|
|
145
|
+
name += "unknown version";
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
return { name, version, arch: arch() };
|
|
150
|
+
}
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
//# sourceMappingURL=osRelease.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"osRelease.js","sourceRoot":"","sources":["../src/osRelease.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compile-time assertion: type-checks only when `T` is exactly `true`.
|
|
3
|
+
*
|
|
4
|
+
* Wrap one of the type-level predicates of this module ({@link Equal},
|
|
5
|
+
* {@link Extends}, {@link Not}, {@link IsAny}, {@link IsNever},
|
|
6
|
+
* {@link IsUnknown}) in it and declare the result as an (unused) type alias.
|
|
7
|
+
* A predicate that evaluates to `false` or `boolean` becomes a type error that
|
|
8
|
+
* `tsc` reports. The alias resolves to `true` and, like every type, is erased
|
|
9
|
+
* at runtime.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The predicate result that must be `true`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Equal } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Ok = Assert<Equal<ReturnType<typeof parseInt>, number>>
|
|
18
|
+
* // @ts-expect-error -- `string` is not `number`
|
|
19
|
+
* type _Fails = Assert<Equal<string, number>>
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type Assert<T extends true> = T;
|
|
23
|
+
//# sourceMappingURL=Assert.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Assert.d.ts","sourceRoot":"","sources":["../../src/type-testing/Assert.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,MAAM,CAAC,CAAC,SAAS,IAAI,IAAI,CAAC,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Assert.js","sourceRoot":"","sources":["../../src/type-testing/Assert.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` and `Y` are identical types, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* The comparison is the one the compiler uses for type identity, so it is
|
|
6
|
+
* stricter than mutual assignability: `any` only equals `any`, and modifiers
|
|
7
|
+
* such as `readonly` and `?` must match. An intersection is not identical to
|
|
8
|
+
* the equivalent flattened object type (`{ a: 1 } & { b: 2 }` is not
|
|
9
|
+
* `{ a: 1; b: 2 }`); flatten it with a mapped type before comparing when
|
|
10
|
+
* needed.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam X - The first type to compare.
|
|
13
|
+
* @typeParam Y - The second type to compare.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
18
|
+
*
|
|
19
|
+
* type _Same = Assert<Equal<{ a: string }, { a: string }>>
|
|
20
|
+
* type _Readonly = Assert<Not<Equal<{ readonly a: string }, { a: string }>>>
|
|
21
|
+
* type _Any = Assert<Not<Equal<any, string>>>
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
25
|
+
//# sourceMappingURL=Equal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Equal.d.ts","sourceRoot":"","sources":["../../src/type-testing/Equal.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,EAAE,CAAC,IAClB,CAAC,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,SAAS,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,GAC7D,IAAI,GACJ,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Equal.js","sourceRoot":"","sources":["../../src/type-testing/Equal.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `X` is assignable to `Y`, `false`
|
|
3
|
+
* otherwise.
|
|
4
|
+
*
|
|
5
|
+
* Unions are compared as a whole rather than member by member, so
|
|
6
|
+
* `Extends<string | number, string>` is `false`. Since `any` and `never` are
|
|
7
|
+
* assignable to every type, `Extends<any, Y>` and `Extends<never, Y>` are
|
|
8
|
+
* always `true`; use {@link Equal} to check for an exact type.
|
|
9
|
+
*
|
|
10
|
+
* @typeParam X - The type to check.
|
|
11
|
+
* @typeParam Y - The type `X` must be assignable to.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import type { Assert, Extends, Not } from "@darthcav/ts-utils"
|
|
16
|
+
*
|
|
17
|
+
* type _Literal = Assert<Extends<"a", string>>
|
|
18
|
+
* type _Union = Assert<Not<Extends<string | number, string>>>
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
export type Extends<X, Y> = [X] extends [Y] ? true : false;
|
|
22
|
+
//# sourceMappingURL=Extends.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Extends.d.ts","sourceRoot":"","sources":["../../src/type-testing/Extends.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Extends.js","sourceRoot":"","sources":["../../src/type-testing/Extends.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `any`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to catch a value or return type that silently widened to `any`.
|
|
5
|
+
*
|
|
6
|
+
* @typeParam T - The type to check.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* import type { Assert, IsAny, Not } from "@darthcav/ts-utils"
|
|
11
|
+
*
|
|
12
|
+
* type _Parsed = Assert<IsAny<ReturnType<typeof JSON.parse>>>
|
|
13
|
+
* type _Typed = Assert<Not<IsAny<unknown>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
17
|
+
//# sourceMappingURL=IsAny.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsAny.d.ts","sourceRoot":"","sources":["../../src/type-testing/IsAny.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,KAAK,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsAny.js","sourceRoot":"","sources":["../../src/type-testing/IsAny.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: `true` when `T` is `never`, `false` otherwise.
|
|
3
|
+
*
|
|
4
|
+
* Useful to check that a union has been exhausted or that a conditional type
|
|
5
|
+
* filters out every member.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The type to check.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, IsNever } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Empty = Assert<IsNever<Exclude<"a" | "b", string>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type IsNever<T> = [T] extends [never] ? true : false;
|
|
17
|
+
//# sourceMappingURL=IsNever.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsNever.d.ts","sourceRoot":"","sources":["../../src/type-testing/IsNever.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,KAAK,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsNever.js","sourceRoot":"","sources":["../../src/type-testing/IsNever.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { IsAny } from "./IsAny.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Type-level predicate: `true` when `T` is `unknown`, `false` otherwise
|
|
4
|
+
* (including for `any`).
|
|
5
|
+
*
|
|
6
|
+
* Useful to check that an untrusted value (e.g. parsed input) is typed as
|
|
7
|
+
* `unknown` and has to be narrowed before use.
|
|
8
|
+
*
|
|
9
|
+
* @typeParam T - The type to check.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import type { Assert, IsUnknown, Not } from "@darthcav/ts-utils"
|
|
14
|
+
*
|
|
15
|
+
* type _Unknown = Assert<IsUnknown<unknown>>
|
|
16
|
+
* type _Any = Assert<Not<IsUnknown<any>>>
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export type IsUnknown<T> = IsAny<T> extends true ? false : unknown extends T ? true : false;
|
|
20
|
+
//# sourceMappingURL=IsUnknown.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsUnknown.d.ts","sourceRoot":"","sources":["../../src/type-testing/IsUnknown.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,YAAY,CAAA;AAEvC;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,SAAS,CAAC,CAAC,IACnB,KAAK,CAAC,CAAC,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,OAAO,SAAS,CAAC,GAAG,IAAI,GAAG,KAAK,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IsUnknown.js","sourceRoot":"","sources":["../../src/type-testing/IsUnknown.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type-level predicate: negates a boolean predicate result.
|
|
3
|
+
*
|
|
4
|
+
* `Not<true>` is `false` and `Not<false>` is `true`. An undecided `boolean`
|
|
5
|
+
* stays `boolean`, so `Assert<Not<boolean>>` still fails.
|
|
6
|
+
*
|
|
7
|
+
* @typeParam T - The predicate result to negate.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* import type { Assert, Equal, Not } from "@darthcav/ts-utils"
|
|
12
|
+
*
|
|
13
|
+
* type _Different = Assert<Not<Equal<string, number>>>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export type Not<T extends boolean> = T extends true ? false : true;
|
|
17
|
+
//# sourceMappingURL=Not.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Not.d.ts","sourceRoot":"","sources":["../../src/type-testing/Not.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,MAAM,MAAM,GAAG,CAAC,CAAC,SAAS,OAAO,IAAI,CAAC,SAAS,IAAI,GAAG,KAAK,GAAG,IAAI,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Not.js","sourceRoot":"","sources":["../../src/type-testing/Not.ts"],"names":[],"mappings":""}
|
package/dist/types.d.ts
CHANGED
|
@@ -7,12 +7,49 @@
|
|
|
7
7
|
*/
|
|
8
8
|
export type RuntimeObject = Record<string, unknown>;
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Type guard that checks whether a value is a plain object that can be treated
|
|
11
|
+
* as a runtime JSON object.
|
|
12
|
+
*
|
|
13
|
+
* Plain objects are object literals, `JSON.parse` results, and
|
|
14
|
+
* `Object.create(null)` objects, including those from another realm (e.g. a
|
|
15
|
+
* `node:vm` context). Arrays, class instances, and built-ins such as `Date`,
|
|
16
|
+
* `Map`, `Set`, `RegExp`, or module namespace objects are rejected.
|
|
17
|
+
*
|
|
18
|
+
* The check is shallow and keys are not sanitized: an own `__proto__` or
|
|
19
|
+
* `constructor` key (as `JSON.parse` can produce from untrusted input) is kept.
|
|
20
|
+
* Do not merge the value into other objects key by key without guarding
|
|
21
|
+
* against those keys, or prototype pollution becomes possible.
|
|
22
|
+
*
|
|
23
|
+
* @param value - Value to check.
|
|
24
|
+
* @returns `true` when the value is a plain object, narrowing it to
|
|
25
|
+
* {@link RuntimeObject}.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* const body: unknown = JSON.parse(input)
|
|
30
|
+
* if (isRuntimeObject(body)) {
|
|
31
|
+
* console.log(Object.keys(body)) // body: RuntimeObject
|
|
32
|
+
* }
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare function isRuntimeObject(value: unknown): value is RuntimeObject;
|
|
36
|
+
/**
|
|
37
|
+
* Returns the input when it is a plain object that can be treated as a runtime
|
|
38
|
+
* JSON object, as decided by {@link isRuntimeObject} (see there for what counts
|
|
39
|
+
* as a plain object and the caveats about unsanitized keys).
|
|
11
40
|
*
|
|
12
41
|
* @param value - Value to narrow.
|
|
13
|
-
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
42
|
+
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
43
|
+
* value is not a plain object.
|
|
14
44
|
*/
|
|
15
45
|
export declare function asRuntimeObject(value: unknown): RuntimeObject | undefined;
|
|
46
|
+
/**
|
|
47
|
+
* Type guard that checks whether a value is a string.
|
|
48
|
+
*
|
|
49
|
+
* @param value - Value to check.
|
|
50
|
+
* @returns `true` when the value is a string, narrowing it to `string`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function isString(value: unknown): value is string;
|
|
16
53
|
/**
|
|
17
54
|
* Returns the input when it is a string.
|
|
18
55
|
*
|
|
@@ -23,6 +60,9 @@ export declare function asString(value: unknown): string | undefined;
|
|
|
23
60
|
/**
|
|
24
61
|
* Filters an unknown value down to an array of runtime JSON objects.
|
|
25
62
|
*
|
|
63
|
+
* Elements are kept when {@link isRuntimeObject} accepts them, i.e. only plain
|
|
64
|
+
* objects; the same caveats about unsanitized keys apply.
|
|
65
|
+
*
|
|
26
66
|
* @param value - Value that may contain runtime objects.
|
|
27
67
|
* @returns Only the elements that can be treated as {@link RuntimeObject} values.
|
|
28
68
|
*/
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEnD
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,MAAM,MAAM,aAAa,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAEnD;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,aAAa,CAgBtE;AAED;;;;;;;;GAQG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,GAAG,SAAS,CAEzE;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,MAAM,CAExD;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAE3D;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,aAAa,EAAE,CAEpE"}
|
package/dist/types.js
CHANGED
|
@@ -1,13 +1,64 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Type guard that checks whether a value is a plain object that can be treated
|
|
3
|
+
* as a runtime JSON object.
|
|
4
|
+
*
|
|
5
|
+
* Plain objects are object literals, `JSON.parse` results, and
|
|
6
|
+
* `Object.create(null)` objects, including those from another realm (e.g. a
|
|
7
|
+
* `node:vm` context). Arrays, class instances, and built-ins such as `Date`,
|
|
8
|
+
* `Map`, `Set`, `RegExp`, or module namespace objects are rejected.
|
|
9
|
+
*
|
|
10
|
+
* The check is shallow and keys are not sanitized: an own `__proto__` or
|
|
11
|
+
* `constructor` key (as `JSON.parse` can produce from untrusted input) is kept.
|
|
12
|
+
* Do not merge the value into other objects key by key without guarding
|
|
13
|
+
* against those keys, or prototype pollution becomes possible.
|
|
14
|
+
*
|
|
15
|
+
* @param value - Value to check.
|
|
16
|
+
* @returns `true` when the value is a plain object, narrowing it to
|
|
17
|
+
* {@link RuntimeObject}.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* const body: unknown = JSON.parse(input)
|
|
22
|
+
* if (isRuntimeObject(body)) {
|
|
23
|
+
* console.log(Object.keys(body)) // body: RuntimeObject
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
export function isRuntimeObject(value) {
|
|
28
|
+
if (typeof value !== "object" || value === null) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
const prototype = Object.getPrototypeOf(value);
|
|
32
|
+
// A null prototype, or a realm's root `Object.prototype` (whose own
|
|
33
|
+
// prototype is null), marks a plain object.
|
|
34
|
+
if (prototype !== null &&
|
|
35
|
+
prototype !== Object.prototype &&
|
|
36
|
+
Object.getPrototypeOf(prototype) !== null) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
// Exclude null-prototype exotics such as module namespace objects.
|
|
40
|
+
return !(Symbol.toStringTag in value || Symbol.iterator in value);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Returns the input when it is a plain object that can be treated as a runtime
|
|
44
|
+
* JSON object, as decided by {@link isRuntimeObject} (see there for what counts
|
|
45
|
+
* as a plain object and the caveats about unsanitized keys).
|
|
3
46
|
*
|
|
4
47
|
* @param value - Value to narrow.
|
|
5
|
-
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
48
|
+
* @returns The same value as a {@link RuntimeObject}, or `undefined` when the
|
|
49
|
+
* value is not a plain object.
|
|
6
50
|
*/
|
|
7
51
|
export function asRuntimeObject(value) {
|
|
8
|
-
return
|
|
9
|
-
|
|
10
|
-
|
|
52
|
+
return isRuntimeObject(value) ? value : undefined;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Type guard that checks whether a value is a string.
|
|
56
|
+
*
|
|
57
|
+
* @param value - Value to check.
|
|
58
|
+
* @returns `true` when the value is a string, narrowing it to `string`.
|
|
59
|
+
*/
|
|
60
|
+
export function isString(value) {
|
|
61
|
+
return typeof value === "string";
|
|
11
62
|
}
|
|
12
63
|
/**
|
|
13
64
|
* Returns the input when it is a string.
|
|
@@ -16,24 +67,18 @@ export function asRuntimeObject(value) {
|
|
|
16
67
|
* @returns The same value as a string, or `undefined` when the value is not a string.
|
|
17
68
|
*/
|
|
18
69
|
export function asString(value) {
|
|
19
|
-
return
|
|
70
|
+
return isString(value) ? value : undefined;
|
|
20
71
|
}
|
|
21
72
|
/**
|
|
22
73
|
* Filters an unknown value down to an array of runtime JSON objects.
|
|
23
74
|
*
|
|
75
|
+
* Elements are kept when {@link isRuntimeObject} accepts them, i.e. only plain
|
|
76
|
+
* objects; the same caveats about unsanitized keys apply.
|
|
77
|
+
*
|
|
24
78
|
* @param value - Value that may contain runtime objects.
|
|
25
79
|
* @returns Only the elements that can be treated as {@link RuntimeObject} values.
|
|
26
80
|
*/
|
|
27
81
|
export function toRuntimeObjectArray(value) {
|
|
28
|
-
|
|
29
|
-
return [];
|
|
30
|
-
}
|
|
31
|
-
return value.reduce((objects, item) => {
|
|
32
|
-
const object = asRuntimeObject(item);
|
|
33
|
-
if (object) {
|
|
34
|
-
objects.push(object);
|
|
35
|
-
}
|
|
36
|
-
return objects;
|
|
37
|
-
}, []);
|
|
82
|
+
return Array.isArray(value) ? value.filter(isRuntimeObject) : [];
|
|
38
83
|
}
|
|
39
84
|
//# sourceMappingURL=types.js.map
|