@murky-web/typebuddy 1.2.0 → 1.4.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/dist/collections.js +28 -0
- package/dist/empty.js +53 -0
- package/dist/guards.js +132 -0
- package/dist/ids.js +29 -0
- package/dist/index.js +6 -1
- package/dist/parse.js +81 -0
- package/dist/result.js +38 -0
- package/dist/rules/async_rule.js +45 -67
- package/dist/rules/deliberate_throw.js +31 -0
- package/dist/rules/maybe_promise_rule.js +131 -64
- package/dist/rules/optional_rule.js +1 -1
- package/dist/rules/own_subtree.js +45 -0
- package/dist/rules/typebuddy_import.js +79 -0
- package/jsr.json +1 -1
- package/package.json +35 -6
- package/rules/async_rule.ts +118 -141
- package/rules/deliberate_throw.ts +35 -0
- package/rules/maybe_promise_rule.ts +235 -187
- package/rules/optional_rule.ts +5 -1
- package/rules/own_subtree.ts +70 -0
- package/rules/typebuddy_import.ts +134 -0
- package/src/collections.ts +36 -0
- package/src/empty.ts +76 -0
- package/src/guards.ts +341 -0
- package/src/ids.ts +34 -0
- package/src/index.ts +2 -0
- package/src/parse.ts +215 -0
- package/src/result.ts +57 -0
- package/src/type_helper.ts +10 -666
- package/src/types/index.ts +9 -1
- package/src/types/maybe_promise.ts +24 -2
- package/dist/src/type_helper.js +0 -308
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
type AstNode = {
|
|
2
|
+
type: string;
|
|
3
|
+
parent?: AstNode;
|
|
4
|
+
[key: string]: unknown;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
const PACKAGE_NAME = "@murky-web/typebuddy";
|
|
8
|
+
|
|
9
|
+
function isNode(value: unknown): value is AstNode {
|
|
10
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function isIdentifierNamed(node: unknown, name: string): boolean {
|
|
14
|
+
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function isImportDeclaration(node: unknown): node is AstNode {
|
|
18
|
+
return isNode(node) && node.type === "ImportDeclaration";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getStringLiteralValue(node: unknown): string | null {
|
|
22
|
+
if (!isNode(node)) {
|
|
23
|
+
return null;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (
|
|
27
|
+
(node.type === "Literal" || node.type === "StringLiteral") &&
|
|
28
|
+
typeof node["value"] === "string"
|
|
29
|
+
) {
|
|
30
|
+
return node["value"];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getProgram(node: unknown): AstNode | null {
|
|
37
|
+
if (!isNode(node)) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
let current: AstNode | undefined = node;
|
|
42
|
+
while (current?.parent) {
|
|
43
|
+
current = current.parent;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return current?.type === "Program" ? current : null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function getProgramBody(program: AstNode): AstNode[] {
|
|
50
|
+
if (!Array.isArray(program["body"])) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return program["body"].filter(isNode);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function importsHelper(program: AstNode, helperName: string): boolean {
|
|
58
|
+
return getProgramBody(program).some((statement) => {
|
|
59
|
+
if (!isImportDeclaration(statement)) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (getStringLiteralValue(statement["source"]) !== PACKAGE_NAME) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// A type-only import brings no runtime binding, so it does not satisfy a
|
|
68
|
+
// fix that is about to call the helper.
|
|
69
|
+
if (statement["importKind"] === "type") {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const specifiers = Array.isArray(statement["specifiers"])
|
|
74
|
+
? statement["specifiers"]
|
|
75
|
+
: [];
|
|
76
|
+
|
|
77
|
+
return specifiers.some((specifier) => {
|
|
78
|
+
return (
|
|
79
|
+
isNode(specifier) &&
|
|
80
|
+
specifier.type === "ImportSpecifier" &&
|
|
81
|
+
isIdentifierNamed(specifier["local"], helperName)
|
|
82
|
+
);
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Where and what to insert so `helperName` is imported from the package.
|
|
89
|
+
*
|
|
90
|
+
* Both async rules add `ok`/`err` imports, and both used to carry their own
|
|
91
|
+
* copy of this — including the same bug. Whether the import lands after an
|
|
92
|
+
* existing one was inferred from the anchor's offset (`range[0] !== 0`), which
|
|
93
|
+
* is only the same question in a file whose first statement starts at byte
|
|
94
|
+
* zero. A leading comment moved the first statement off zero, the wrong branch
|
|
95
|
+
* won, and the fix emitted a leading newline instead of a trailing one:
|
|
96
|
+
*
|
|
97
|
+
* import { err } from "@murky-web/typebuddy";export async function load()
|
|
98
|
+
*
|
|
99
|
+
* The list of imports answers it directly, so that is what decides now.
|
|
100
|
+
*
|
|
101
|
+
* @param {unknown} node - Any node in the file; the program is found from it.
|
|
102
|
+
* @param {string} helperName - The named export to import, e.g. `"err"`.
|
|
103
|
+
* @returns {{ range: [number, number]; text: string } | null} The zero-width
|
|
104
|
+
* insertion to apply, or null when the helper is already imported or no
|
|
105
|
+
* anchor carries a usable range.
|
|
106
|
+
*/
|
|
107
|
+
function getTypeBuddyImportInsertion(
|
|
108
|
+
node: unknown,
|
|
109
|
+
helperName: string,
|
|
110
|
+
): { range: [number, number]; text: string } | null {
|
|
111
|
+
const program = getProgram(node);
|
|
112
|
+
if (!program || importsHelper(program, helperName)) {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const body = getProgramBody(program);
|
|
117
|
+
const imports = body.filter(isImportDeclaration);
|
|
118
|
+
const anchor = imports.at(-1) ?? body[0] ?? program;
|
|
119
|
+
const range = anchor["range"];
|
|
120
|
+
if (!Array.isArray(range) || range.length < 2) {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const statement = `import { ${helperName} } from "${PACKAGE_NAME}";`;
|
|
125
|
+
if (imports.length > 0) {
|
|
126
|
+
const end: number = range[1];
|
|
127
|
+
return { range: [end, end], text: `\n${statement}` };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const start: number = range[0];
|
|
131
|
+
return { range: [start, start], text: `${statement}\n` };
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
export { getTypeBuddyImportInsertion };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** Operations on collections rather than checks on them. */
|
|
2
|
+
|
|
3
|
+
import { isArray } from "./guards.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Get the keys of an object.
|
|
7
|
+
*
|
|
8
|
+
* @param {unknown} object - The object to get the keys of.
|
|
9
|
+
* @returns {Array} Keys of object.
|
|
10
|
+
*/
|
|
11
|
+
function getKeys<T extends Record<string, unknown>>(object: T): (keyof T)[] {
|
|
12
|
+
return Object.keys(object);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Compares two arrays and returns true if they have at least one common value.
|
|
17
|
+
*
|
|
18
|
+
* @param {readonly T[]} array1 - First array.
|
|
19
|
+
* @param {readonly T[]} array2 - Second array.
|
|
20
|
+
* @returns {boolean} True if the arrays have at least one common value.
|
|
21
|
+
*/
|
|
22
|
+
function arrayContainsCommonValue<T>(
|
|
23
|
+
array1: readonly T[],
|
|
24
|
+
array2: readonly T[],
|
|
25
|
+
): boolean {
|
|
26
|
+
if (!isArray(array1) || !isArray(array2)) {
|
|
27
|
+
return false;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const valueOccurrences = new Set(array1);
|
|
31
|
+
return array2.some((value) => {
|
|
32
|
+
return valueOccurrences.has(value);
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { arrayContainsCommonValue, getKeys };
|
package/src/empty.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The recursive "is this effectively blank" checks. Split out because they walk
|
|
3
|
+
* arbitrary object graphs — far more code than the one-line guards they are
|
|
4
|
+
* filed next to, and needed far less often.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import {
|
|
8
|
+
isArray,
|
|
9
|
+
isBoolean,
|
|
10
|
+
isEmptyObject,
|
|
11
|
+
isEmptyString,
|
|
12
|
+
isNull,
|
|
13
|
+
isObject,
|
|
14
|
+
isString,
|
|
15
|
+
isUndefined,
|
|
16
|
+
} from "./guards.js";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Returns true for values that behave like "empty" application input.
|
|
20
|
+
*
|
|
21
|
+
* @param {unknown} value - The value to check.
|
|
22
|
+
* @returns {boolean} True if the value is nullish, empty string, empty array,
|
|
23
|
+
* false, or a plain object whose values are all empty-like.
|
|
24
|
+
*/
|
|
25
|
+
function isEmptyLike(value: unknown): boolean {
|
|
26
|
+
if (isNull(value) || isUndefined(value)) {
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
if (isString(value)) {
|
|
30
|
+
return isEmptyString(value);
|
|
31
|
+
}
|
|
32
|
+
if (isArray(value)) {
|
|
33
|
+
return value.every((entry) => {
|
|
34
|
+
return isEmptyLike(entry);
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
if (isBoolean(value)) {
|
|
38
|
+
return !value;
|
|
39
|
+
}
|
|
40
|
+
if (isObject(value)) {
|
|
41
|
+
if (Object.getPrototypeOf(value) !== Object.prototype) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
return Object.values(value).every((entry) => {
|
|
45
|
+
return isEmptyLike(entry);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Checks if the provided value contains empty values.
|
|
53
|
+
*
|
|
54
|
+
* This function determines if the given value is either an empty string, an
|
|
55
|
+
* empty object, or a string representation of an empty object.
|
|
56
|
+
*
|
|
57
|
+
* @param {unknown} value - The value to check for emptiness. It can be of any
|
|
58
|
+
* type.
|
|
59
|
+
* @returns {boolean} `true` if the value is an empty string, an empty object,
|
|
60
|
+
* or a string representation of an empty object; otherwise, `false`.
|
|
61
|
+
*/
|
|
62
|
+
function hasEmptyValues(value: unknown): boolean {
|
|
63
|
+
if (isString(value)) {
|
|
64
|
+
try {
|
|
65
|
+
if (isEmptyObject(JSON.parse(value))) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
} catch {
|
|
69
|
+
return isEmptyString(value);
|
|
70
|
+
}
|
|
71
|
+
return isEmptyString(value);
|
|
72
|
+
}
|
|
73
|
+
return isEmptyObject(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { hasEmptyValues, isEmptyLike };
|
package/src/guards.ts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime type guards. The leaf module: everything else here builds on it, it
|
|
3
|
+
* builds on nothing.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import type { Maybe } from "./types/maybe.js";
|
|
7
|
+
import type { Nullable } from "./types/nullable.js";
|
|
8
|
+
import type { Optional } from "./types/optional.js";
|
|
9
|
+
|
|
10
|
+
type UnknownFunction = (...args: readonly never[]) => unknown;
|
|
11
|
+
|
|
12
|
+
const EMPTY_LENGTH = 0;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Check if a value is a string.
|
|
16
|
+
*
|
|
17
|
+
* @param {unknown} value - The value to check.
|
|
18
|
+
* @returns {boolean} True if the value is a string.
|
|
19
|
+
*/
|
|
20
|
+
function isString<T extends string>(value: Nullable<T>): value is T;
|
|
21
|
+
function isString(value: unknown): value is string;
|
|
22
|
+
function isString(value: unknown): value is string {
|
|
23
|
+
return typeof value === "string";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Parses the input value as a boolean. Returns false if the value is no string.
|
|
28
|
+
*
|
|
29
|
+
* @param {unknown} value - The value to check.
|
|
30
|
+
* @returns {boolean} True if the value is a string.
|
|
31
|
+
*/
|
|
32
|
+
function isEmptyString(value: unknown): boolean {
|
|
33
|
+
if (!isString(value)) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
return value.trim() === "";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Returns true if the value is null.
|
|
41
|
+
*
|
|
42
|
+
* @param {unknown} value - The value to check.
|
|
43
|
+
* @returns {boolean} True if the value is null.
|
|
44
|
+
*/
|
|
45
|
+
function isNull(value: unknown): value is null {
|
|
46
|
+
return value === null;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Returns true if the value is undefined.
|
|
51
|
+
*
|
|
52
|
+
* @param {unknown} value - The value to check.
|
|
53
|
+
* @returns {boolean} True if the value is undefined.
|
|
54
|
+
*/
|
|
55
|
+
function isUndefined(value: unknown): value is undefined {
|
|
56
|
+
return typeof value === "undefined";
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Checks whether an Optional value is currently in its missing state.
|
|
61
|
+
*
|
|
62
|
+
* @param {Optional<T>} value - The Optional value to check.
|
|
63
|
+
* @returns {boolean} True when the value is undefined.
|
|
64
|
+
*/
|
|
65
|
+
function isOptional<T>(value: Optional<T>): value is undefined {
|
|
66
|
+
return isUndefined(value);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Checks whether a Maybe value is currently in its missing state.
|
|
71
|
+
*
|
|
72
|
+
* @param {Maybe<T>} value - The Maybe value to check.
|
|
73
|
+
* @returns {boolean} True when the value is null.
|
|
74
|
+
*/
|
|
75
|
+
function isMaybe<T>(value: Maybe<T>): value is null {
|
|
76
|
+
return isNull(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Checks whether a Nullable value is currently in its missing state.
|
|
81
|
+
*
|
|
82
|
+
* @param {Nullable<T>} value - The Nullable value to check.
|
|
83
|
+
* @returns {boolean} True when the value is null or undefined.
|
|
84
|
+
*/
|
|
85
|
+
function isNullable<T>(value: Nullable<T>): value is null | undefined {
|
|
86
|
+
return isNull(value) || isUndefined(value);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Check if a value is an array.
|
|
91
|
+
*
|
|
92
|
+
* @param {unknown} value - The value to check.
|
|
93
|
+
* @returns {boolean} True if the value is an array.
|
|
94
|
+
*/
|
|
95
|
+
function isArray<T>(value: Nullable<readonly T[]>): value is readonly T[];
|
|
96
|
+
function isArray(value: unknown): value is readonly unknown[];
|
|
97
|
+
function isArray(value: unknown): value is readonly unknown[] {
|
|
98
|
+
return Array.isArray(value);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Check if a value is an empty array.
|
|
103
|
+
*
|
|
104
|
+
* @param {unknown} value - The value to check.
|
|
105
|
+
* @returns {boolean} True if the value is an empty array.
|
|
106
|
+
*/
|
|
107
|
+
function isEmptyArray<T>(value: Nullable<readonly T[]>): value is readonly T[];
|
|
108
|
+
function isEmptyArray(value: unknown): value is readonly unknown[];
|
|
109
|
+
function isEmptyArray(value: unknown): value is readonly unknown[] {
|
|
110
|
+
return Array.isArray(value) && value.length === EMPTY_LENGTH;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* More performant implementation of isArray for large arrays
|
|
115
|
+
*
|
|
116
|
+
* @param {unknown} value - The value to check.
|
|
117
|
+
* @returns {boolean} True if the value is an array.
|
|
118
|
+
*/
|
|
119
|
+
function fastIsArray<T>(value: Nullable<readonly T[]>): value is readonly T[];
|
|
120
|
+
function fastIsArray(value: unknown): value is readonly unknown[];
|
|
121
|
+
function fastIsArray(value: unknown): value is readonly unknown[] {
|
|
122
|
+
return Object.prototype.toString.call(value) === "[object Array]";
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Check if a value is a number.
|
|
127
|
+
*
|
|
128
|
+
* @param {unknown} value - The value to check.
|
|
129
|
+
* @returns {boolean} True if the value is a number.
|
|
130
|
+
*/
|
|
131
|
+
function isNumber<T extends number>(value: Nullable<T>): value is T;
|
|
132
|
+
function isNumber(value: unknown): value is number;
|
|
133
|
+
function isNumber(value: unknown): value is number {
|
|
134
|
+
if (typeof value === "string" && value.trim() === "") {
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
return (
|
|
138
|
+
typeof value === "number" &&
|
|
139
|
+
!Number.isNaN(value) &&
|
|
140
|
+
Number.isFinite(value)
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns true if the value is an object.
|
|
146
|
+
*
|
|
147
|
+
* @param {unknown} value - The value to check.
|
|
148
|
+
* @returns {boolean} True if the value is an object.
|
|
149
|
+
*/
|
|
150
|
+
function isObject<T extends Record<string, unknown>>(
|
|
151
|
+
value: Nullable<T>,
|
|
152
|
+
): value is T;
|
|
153
|
+
function isObject(value: unknown): value is Record<string, unknown>;
|
|
154
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
155
|
+
if (
|
|
156
|
+
typeof value !== "object" ||
|
|
157
|
+
value === null ||
|
|
158
|
+
isArray(value) ||
|
|
159
|
+
Object.prototype.toString.call(value) !== "[object Object]"
|
|
160
|
+
) {
|
|
161
|
+
return false;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const objectValue: object = value;
|
|
165
|
+
return Object.getPrototypeOf(objectValue) === Object.prototype;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Returns true if the value is a boolean.
|
|
170
|
+
*
|
|
171
|
+
* @param {unknown} value - The value to check.
|
|
172
|
+
* @returns {boolean} True if the value is a boolean.
|
|
173
|
+
*/
|
|
174
|
+
function isBoolean<T extends boolean>(value: Nullable<T>): value is T;
|
|
175
|
+
function isBoolean(value: unknown): value is boolean;
|
|
176
|
+
function isBoolean(value: unknown): value is boolean {
|
|
177
|
+
return typeof value === "boolean";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Returns true if the value is a function.
|
|
182
|
+
*
|
|
183
|
+
* @param {unknown} value - The value to check.
|
|
184
|
+
* @returns {boolean} True if the value is a function.
|
|
185
|
+
*/
|
|
186
|
+
function isFunction<T extends UnknownFunction>(value: Nullable<T>): value is T;
|
|
187
|
+
function isFunction(value: unknown): value is UnknownFunction;
|
|
188
|
+
function isFunction(value: unknown): value is UnknownFunction {
|
|
189
|
+
return typeof value === "function";
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Returns true if the value is a promise.
|
|
194
|
+
*
|
|
195
|
+
* @param {unknown} value - The value to check.
|
|
196
|
+
* @returns {boolean} True if the value is a promise.
|
|
197
|
+
*/
|
|
198
|
+
function isPromise<T>(
|
|
199
|
+
value: Nullable<Readonly<PromiseLike<T>>>,
|
|
200
|
+
): value is PromiseLike<T>;
|
|
201
|
+
function isPromise(value: unknown): value is PromiseLike<unknown>;
|
|
202
|
+
function isPromise(value: unknown): value is PromiseLike<unknown> {
|
|
203
|
+
if (typeof value !== "object" || value === null) {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return typeof Reflect.get(value, "then") === "function";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Returns true if the value is an error.
|
|
212
|
+
*
|
|
213
|
+
* @param {unknown} value - The value to check.
|
|
214
|
+
* @returns {boolean} True if the value is an error.
|
|
215
|
+
*/
|
|
216
|
+
function isError<T extends Error>(value: Nullable<T>): value is T;
|
|
217
|
+
function isError(value: unknown): value is Error;
|
|
218
|
+
function isError(value: unknown): value is Error {
|
|
219
|
+
return value instanceof Error;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Returns true if the value is a date.
|
|
224
|
+
*
|
|
225
|
+
* @param {unknown} value - The value to check.
|
|
226
|
+
* @returns {boolean} True if the value is a date.
|
|
227
|
+
*/
|
|
228
|
+
function isDate<T extends Date>(value: Nullable<T>): value is T;
|
|
229
|
+
function isDate(value: unknown): value is Date;
|
|
230
|
+
function isDate(value: unknown): value is Date {
|
|
231
|
+
return value instanceof Date;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Returns true if the value is a RegExp.
|
|
236
|
+
*
|
|
237
|
+
* @param {unknown} value - The value to check.
|
|
238
|
+
* @returns {boolean} True if the value is a RegExp.
|
|
239
|
+
*/
|
|
240
|
+
function isRegExp<T extends RegExp>(value: Nullable<T>): value is T;
|
|
241
|
+
function isRegExp(value: unknown): value is RegExp;
|
|
242
|
+
function isRegExp(value: unknown): value is RegExp {
|
|
243
|
+
return value instanceof RegExp;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Returns true if the value is a symbol.
|
|
248
|
+
*
|
|
249
|
+
* @param {unknown} value - The value to check.
|
|
250
|
+
* @returns {boolean} True if the value is a symbol.
|
|
251
|
+
*/
|
|
252
|
+
function isSymbol<T extends symbol>(value: Nullable<T>): value is T;
|
|
253
|
+
function isSymbol(value: unknown): value is symbol;
|
|
254
|
+
function isSymbol(value: unknown): value is symbol {
|
|
255
|
+
return typeof value === "symbol";
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Check if a value is a plain object.
|
|
260
|
+
*
|
|
261
|
+
* @param {unknown} value - The value to check.
|
|
262
|
+
* @returns {boolean} True if the value is a plain object.
|
|
263
|
+
*/
|
|
264
|
+
function isEmptyObject<T extends Record<string, unknown>>(
|
|
265
|
+
value: Nullable<T>,
|
|
266
|
+
): value is T;
|
|
267
|
+
function isEmptyObject(value: unknown): value is Record<string, unknown>;
|
|
268
|
+
function isEmptyObject(value: unknown): value is Record<string, unknown> {
|
|
269
|
+
return (
|
|
270
|
+
typeof value === "object" &&
|
|
271
|
+
!isNull(value) &&
|
|
272
|
+
!isUndefined(value) &&
|
|
273
|
+
!isEmptyArray(value) &&
|
|
274
|
+
Object.getPrototypeOf(value) === Object.prototype &&
|
|
275
|
+
Object.keys(value).length === EMPTY_LENGTH
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Check if a value is an instance of a class.
|
|
281
|
+
*
|
|
282
|
+
* @param {unknown} value - The value to check.
|
|
283
|
+
* @param {unknown} constructor - The class constructor to check against.
|
|
284
|
+
* @returns {boolean} True if the value is an instance of the class.
|
|
285
|
+
*/
|
|
286
|
+
function isInstanceOf<T>(
|
|
287
|
+
value: unknown,
|
|
288
|
+
constructor: new (...args: unknown[]) => T,
|
|
289
|
+
): value is T {
|
|
290
|
+
return value instanceof constructor;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Check if a value is an integer.
|
|
295
|
+
*
|
|
296
|
+
* @param {unknown} value - The value to check.
|
|
297
|
+
* @returns {boolean} True if the value is an integer.
|
|
298
|
+
*/
|
|
299
|
+
function isInteger(value: unknown): value is number {
|
|
300
|
+
return typeof value === "number" && Number.isInteger(value);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Check if a value is a float.
|
|
305
|
+
*
|
|
306
|
+
* @param {unknown} value - The value to check.
|
|
307
|
+
* @returns {boolean} True if the value is a float.
|
|
308
|
+
*/
|
|
309
|
+
function isFloat(value: unknown): value is number {
|
|
310
|
+
return (
|
|
311
|
+
typeof value === "number" &&
|
|
312
|
+
!Number.isNaN(value) &&
|
|
313
|
+
!Number.isInteger(value)
|
|
314
|
+
);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
export {
|
|
318
|
+
fastIsArray,
|
|
319
|
+
isArray,
|
|
320
|
+
isBoolean,
|
|
321
|
+
isDate,
|
|
322
|
+
isEmptyArray,
|
|
323
|
+
isEmptyObject,
|
|
324
|
+
isEmptyString,
|
|
325
|
+
isError,
|
|
326
|
+
isFloat,
|
|
327
|
+
isFunction,
|
|
328
|
+
isInstanceOf,
|
|
329
|
+
isInteger,
|
|
330
|
+
isMaybe,
|
|
331
|
+
isNull,
|
|
332
|
+
isNullable,
|
|
333
|
+
isNumber,
|
|
334
|
+
isObject,
|
|
335
|
+
isOptional,
|
|
336
|
+
isPromise,
|
|
337
|
+
isRegExp,
|
|
338
|
+
isString,
|
|
339
|
+
isSymbol,
|
|
340
|
+
isUndefined,
|
|
341
|
+
};
|
package/src/ids.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Identifier-format checks. Their two regex literals are the only module-level
|
|
3
|
+
* allocations in the package, which is the whole reason they live alone: a
|
|
4
|
+
* bundler that keeps anything in a module keeps those too.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { isString } from "./guards.js";
|
|
8
|
+
|
|
9
|
+
const uuidRegex =
|
|
10
|
+
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}$/;
|
|
11
|
+
|
|
12
|
+
const ulidRegex = /^[0123456789ABCDEFGHJKMNPQRSTVWXYZ]{26}$/;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Returns true if the input is a UUID string.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} input - The input to check.
|
|
18
|
+
* @returns {boolean} True if the input is a UUID string.
|
|
19
|
+
*/
|
|
20
|
+
function isUuidString(input: unknown): input is string {
|
|
21
|
+
return isString(input) && uuidRegex.test(input);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Returns true if the input is a ULID string.
|
|
26
|
+
*
|
|
27
|
+
* @param {string} input - The input to check.
|
|
28
|
+
* @returns {boolean} True if the input is a ULID string.
|
|
29
|
+
*/
|
|
30
|
+
function isUlidString(input: unknown): input is string {
|
|
31
|
+
return typeof input === "string" && ulidRegex.test(input);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export { isUlidString, isUuidString };
|
package/src/index.ts
CHANGED
|
@@ -10,6 +10,7 @@ import type {} from "../globals.js";
|
|
|
10
10
|
export * from "./type_helper.js";
|
|
11
11
|
|
|
12
12
|
export type {
|
|
13
|
+
AsyncResult,
|
|
13
14
|
Failed,
|
|
14
15
|
JsonifiedObject,
|
|
15
16
|
JsonifiedValue,
|
|
@@ -20,6 +21,7 @@ export type {
|
|
|
20
21
|
ResolveMaybe,
|
|
21
22
|
ResolveNullable,
|
|
22
23
|
ResolveOptional,
|
|
24
|
+
Result,
|
|
23
25
|
Stringified,
|
|
24
26
|
Success,
|
|
25
27
|
} from "./types/index.js";
|