@gtkx/utils 0.21.0 → 1.0.0-rc.2
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 +173 -0
- package/dist/array/index-before-or-end.d.ts +15 -0
- package/dist/array/index-before-or-end.d.ts.map +1 -0
- package/dist/array/index-before-or-end.js +21 -0
- package/dist/array/index-before-or-end.js.map +1 -0
- package/dist/array/index.d.ts +6 -0
- package/dist/array/index.d.ts.map +1 -0
- package/dist/array/index.js +6 -0
- package/dist/array/index.js.map +1 -0
- package/dist/array/remove.d.ts +14 -0
- package/dist/array/remove.d.ts.map +1 -0
- package/dist/array/remove.js +19 -0
- package/dist/array/remove.js.map +1 -0
- package/dist/array/sort-strings-by.d.ts +15 -0
- package/dist/array/sort-strings-by.d.ts.map +1 -0
- package/dist/array/sort-strings-by.js +17 -0
- package/dist/array/sort-strings-by.js.map +1 -0
- package/dist/array/sort-strings.d.ts +12 -0
- package/dist/array/sort-strings.d.ts.map +1 -0
- package/dist/array/sort-strings.js +14 -0
- package/dist/array/sort-strings.js.map +1 -0
- package/dist/array/uniq-by.d.ts +18 -0
- package/dist/array/uniq-by.d.ts.map +1 -0
- package/dist/array/uniq-by.js +29 -0
- package/dist/array/uniq-by.js.map +1 -0
- package/dist/class/any-class.d.ts +8 -0
- package/dist/class/any-class.d.ts.map +1 -0
- package/dist/class/any-class.js +2 -0
- package/dist/class/any-class.js.map +1 -0
- package/dist/class/get-parent-class.d.ts +10 -0
- package/dist/class/get-parent-class.d.ts.map +1 -0
- package/dist/class/get-parent-class.js +12 -0
- package/dist/class/get-parent-class.js.map +1 -0
- package/dist/class/index.d.ts +4 -0
- package/dist/class/index.d.ts.map +1 -0
- package/dist/class/index.js +3 -0
- package/dist/class/index.js.map +1 -0
- package/dist/class/walk-class-chain.d.ts +13 -0
- package/dist/class/walk-class-chain.d.ts.map +1 -0
- package/dist/class/walk-class-chain.js +23 -0
- package/dist/class/walk-class-chain.js.map +1 -0
- package/dist/error/error-like.d.ts +6 -0
- package/dist/error/error-like.d.ts.map +1 -0
- package/dist/error/error-like.js +15 -0
- package/dist/error/error-like.js.map +1 -0
- package/dist/error/error-message.d.ts +13 -0
- package/dist/error/error-message.d.ts.map +1 -0
- package/dist/error/error-message.js +16 -0
- package/dist/error/error-message.js.map +1 -0
- package/dist/error/format-child-process-error.d.ts +12 -0
- package/dist/error/format-child-process-error.d.ts.map +1 -0
- package/dist/error/format-child-process-error.js +21 -0
- package/dist/error/format-child-process-error.js.map +1 -0
- package/dist/error/index.d.ts +4 -0
- package/dist/error/index.d.ts.map +1 -0
- package/dist/error/index.js +4 -0
- package/dist/error/index.js.map +1 -0
- package/dist/error/normalize-error.d.ts +13 -0
- package/dist/error/normalize-error.d.ts.map +1 -0
- package/dist/error/normalize-error.js +20 -0
- package/dist/error/normalize-error.js.map +1 -0
- package/dist/function/call-method.d.ts +3 -0
- package/dist/function/call-method.d.ts.map +1 -0
- package/dist/function/call-method.js +19 -0
- package/dist/function/call-method.js.map +1 -0
- package/dist/function/index.d.ts +2 -0
- package/dist/function/index.d.ts.map +1 -0
- package/dist/function/index.js +2 -0
- package/dist/function/index.js.map +1 -0
- package/dist/index.d.ts +13 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -5
- package/dist/index.js.map +1 -1
- package/dist/log/create-logger.d.ts +15 -0
- package/dist/log/create-logger.d.ts.map +1 -0
- package/dist/log/create-logger.js +17 -0
- package/dist/log/create-logger.js.map +1 -0
- package/dist/log/default-logger.d.ts +36 -0
- package/dist/log/default-logger.d.ts.map +1 -0
- package/dist/log/default-logger.js +44 -0
- package/dist/log/default-logger.js.map +1 -0
- package/dist/log/index.d.ts +4 -0
- package/dist/log/index.d.ts.map +1 -0
- package/dist/log/index.js +4 -0
- package/dist/log/index.js.map +1 -0
- package/dist/log/logger.d.ts +64 -0
- package/dist/log/logger.d.ts.map +1 -0
- package/dist/log/logger.js +100 -0
- package/dist/log/logger.js.map +1 -0
- package/dist/map/get-or-insert.d.ts +23 -0
- package/dist/map/get-or-insert.d.ts.map +1 -0
- package/dist/map/get-or-insert.js +25 -0
- package/dist/map/get-or-insert.js.map +1 -0
- package/dist/map/index.d.ts +2 -0
- package/dist/map/index.d.ts.map +1 -0
- package/dist/map/index.js +2 -0
- package/dist/map/index.js.map +1 -0
- package/dist/object/index.d.ts +4 -0
- package/dist/object/index.d.ts.map +1 -0
- package/dist/object/index.js +4 -0
- package/dist/object/index.js.map +1 -0
- package/dist/object/omit.d.ts +15 -0
- package/dist/object/omit.d.ts.map +1 -0
- package/dist/object/omit.js +24 -0
- package/dist/object/omit.js.map +1 -0
- package/dist/object/pick-by.d.ts +15 -0
- package/dist/object/pick-by.d.ts.map +1 -0
- package/dist/object/pick-by.js +24 -0
- package/dist/object/pick-by.js.map +1 -0
- package/dist/object/structured-clone.d.ts +19 -0
- package/dist/object/structured-clone.d.ts.map +1 -0
- package/dist/object/structured-clone.js +28 -0
- package/dist/object/structured-clone.js.map +1 -0
- package/dist/predicate/are-object-keys-equal.d.ts +3 -0
- package/dist/predicate/are-object-keys-equal.d.ts.map +1 -0
- package/dist/predicate/are-object-keys-equal.js +9 -0
- package/dist/predicate/are-object-keys-equal.js.map +1 -0
- package/dist/predicate/index.d.ts +7 -0
- package/dist/predicate/index.d.ts.map +1 -0
- package/dist/predicate/index.js +7 -0
- package/dist/predicate/index.js.map +1 -0
- package/dist/predicate/is-deep-equal.d.ts +3 -0
- package/dist/predicate/is-deep-equal.d.ts.map +1 -0
- package/dist/predicate/is-deep-equal.js +35 -0
- package/dist/predicate/is-deep-equal.js.map +1 -0
- package/dist/predicate/is-plain-object.d.ts +17 -0
- package/dist/predicate/is-plain-object.d.ts.map +1 -0
- package/dist/predicate/is-plain-object.js +23 -0
- package/dist/predicate/is-plain-object.js.map +1 -0
- package/dist/predicate/is-record.d.ts +14 -0
- package/dist/predicate/is-record.d.ts.map +1 -0
- package/dist/predicate/is-record.js +16 -0
- package/dist/predicate/is-record.js.map +1 -0
- package/dist/predicate/is-same-array-by.d.ts +15 -0
- package/dist/predicate/is-same-array-by.d.ts.map +1 -0
- package/dist/predicate/is-same-array-by.js +20 -0
- package/dist/predicate/is-same-array-by.js.map +1 -0
- package/dist/predicate/is-same-array.d.ts +15 -0
- package/dist/predicate/is-same-array.d.ts.map +1 -0
- package/dist/predicate/is-same-array.js +18 -0
- package/dist/predicate/is-same-array.js.map +1 -0
- package/dist/predicate/is-shallow-equal.d.ts +3 -0
- package/dist/predicate/is-shallow-equal.d.ts.map +1 -0
- package/dist/predicate/is-shallow-equal.js +28 -0
- package/dist/predicate/is-shallow-equal.js.map +1 -0
- package/dist/process/exit-code-for-signal.d.ts +14 -0
- package/dist/process/exit-code-for-signal.d.ts.map +1 -0
- package/dist/process/exit-code-for-signal.js +19 -0
- package/dist/process/exit-code-for-signal.js.map +1 -0
- package/dist/process/index.d.ts +4 -0
- package/dist/process/index.d.ts.map +1 -0
- package/dist/process/index.js +4 -0
- package/dist/process/index.js.map +1 -0
- package/dist/process/install-graceful-shutdown.d.ts +16 -0
- package/dist/process/install-graceful-shutdown.d.ts.map +1 -0
- package/dist/process/install-graceful-shutdown.js +89 -0
- package/dist/process/install-graceful-shutdown.js.map +1 -0
- package/dist/process/resolve-executable.d.ts +14 -0
- package/dist/process/resolve-executable.d.ts.map +1 -0
- package/dist/process/resolve-executable.js +47 -0
- package/dist/process/resolve-executable.js.map +1 -0
- package/dist/set/drain.d.ts +11 -0
- package/dist/set/drain.d.ts.map +1 -0
- package/dist/set/drain.js +16 -0
- package/dist/set/drain.js.map +1 -0
- package/dist/set/index.d.ts +2 -0
- package/dist/set/index.d.ts.map +1 -0
- package/dist/set/index.js +2 -0
- package/dist/set/index.js.map +1 -0
- package/dist/source/index.d.ts +4 -0
- package/dist/source/index.d.ts.map +1 -0
- package/dist/source/index.js +4 -0
- package/dist/source/index.js.map +1 -0
- package/dist/source/sanitize-identifier.d.ts +14 -0
- package/dist/source/sanitize-identifier.d.ts.map +1 -0
- package/dist/source/sanitize-identifier.js +65 -0
- package/dist/source/sanitize-identifier.js.map +1 -0
- package/dist/source/source-string-literal.d.ts +13 -0
- package/dist/source/source-string-literal.d.ts.map +1 -0
- package/dist/source/source-string-literal.js +24 -0
- package/dist/source/source-string-literal.js.map +1 -0
- package/dist/source/to-camel-identifier.d.ts +13 -0
- package/dist/source/to-camel-identifier.d.ts.map +1 -0
- package/dist/source/to-camel-identifier.js +17 -0
- package/dist/source/to-camel-identifier.js.map +1 -0
- package/dist/string/camel-case.d.ts +17 -0
- package/dist/string/camel-case.d.ts.map +1 -0
- package/dist/string/camel-case.js +21 -0
- package/dist/string/camel-case.js.map +1 -0
- package/dist/string/index.d.ts +6 -0
- package/dist/string/index.d.ts.map +1 -0
- package/dist/string/index.js +6 -0
- package/dist/string/index.js.map +1 -0
- package/dist/string/kebab-case.d.ts +14 -0
- package/dist/string/kebab-case.d.ts.map +1 -0
- package/dist/string/kebab-case.js +16 -0
- package/dist/string/kebab-case.js.map +1 -0
- package/dist/string/lower-first.d.ts +13 -0
- package/dist/string/lower-first.d.ts.map +1 -0
- package/dist/string/lower-first.js +15 -0
- package/dist/string/lower-first.js.map +1 -0
- package/dist/string/pascal-case.d.ts +16 -0
- package/dist/string/pascal-case.d.ts.map +1 -0
- package/dist/string/pascal-case.js +20 -0
- package/dist/string/pascal-case.js.map +1 -0
- package/dist/string/upper-first.d.ts +13 -0
- package/dist/string/upper-first.d.ts.map +1 -0
- package/dist/string/upper-first.js +15 -0
- package/dist/string/upper-first.js.map +1 -0
- package/dist/string/word-segments.d.ts +3 -0
- package/dist/string/word-segments.d.ts.map +1 -0
- package/dist/string/word-segments.js +12 -0
- package/dist/string/word-segments.js.map +1 -0
- package/dist/util/index.d.ts +2 -0
- package/dist/util/index.d.ts.map +1 -0
- package/dist/util/index.js +2 -0
- package/dist/util/index.js.map +1 -0
- package/dist/util/package-version.d.ts +12 -0
- package/dist/util/package-version.d.ts.map +1 -0
- package/dist/util/package-version.js +15 -0
- package/dist/util/package-version.js.map +1 -0
- package/package.json +18 -7
- package/src/array/index-before-or-end.ts +27 -0
- package/src/array/index.ts +5 -0
- package/src/array/remove.ts +20 -0
- package/src/array/sort-strings-by.ts +17 -0
- package/src/array/sort-strings.ts +14 -0
- package/src/array/uniq-by.ts +33 -0
- package/src/class/any-class.ts +10 -0
- package/src/class/get-parent-class.ts +15 -0
- package/src/class/index.ts +3 -0
- package/src/class/walk-class-chain.ts +29 -0
- package/src/error/error-like.ts +19 -0
- package/src/error/error-message.ts +17 -0
- package/src/error/format-child-process-error.ts +24 -0
- package/src/error/index.ts +3 -0
- package/src/error/normalize-error.ts +22 -0
- package/src/function/call-method.ts +22 -0
- package/src/function/index.ts +1 -0
- package/src/index.ts +13 -10
- package/src/log/create-logger.ts +18 -0
- package/src/log/default-logger.ts +49 -0
- package/src/log/index.ts +3 -0
- package/src/log/logger.ts +142 -0
- package/src/map/get-or-insert.ts +33 -0
- package/src/map/index.ts +1 -0
- package/src/object/index.ts +3 -0
- package/src/object/omit.ts +26 -0
- package/src/object/pick-by.ts +30 -0
- package/src/object/structured-clone.ts +31 -0
- package/src/predicate/are-object-keys-equal.ts +15 -0
- package/src/predicate/index.ts +6 -0
- package/src/predicate/is-deep-equal.ts +41 -0
- package/src/predicate/is-plain-object.ts +25 -0
- package/src/predicate/is-record.ts +16 -0
- package/src/predicate/is-same-array-by.ts +21 -0
- package/src/predicate/is-same-array.ts +19 -0
- package/src/predicate/is-shallow-equal.ts +32 -0
- package/src/process/exit-code-for-signal.ts +20 -0
- package/src/process/index.ts +3 -0
- package/src/process/install-graceful-shutdown.ts +126 -0
- package/src/process/resolve-executable.ts +57 -0
- package/src/set/drain.ts +17 -0
- package/src/set/index.ts +1 -0
- package/src/source/index.ts +3 -0
- package/src/source/sanitize-identifier.ts +66 -0
- package/src/source/source-string-literal.ts +27 -0
- package/src/source/to-camel-identifier.ts +18 -0
- package/src/string/camel-case.ts +22 -0
- package/src/string/index.ts +5 -0
- package/src/string/kebab-case.ts +18 -0
- package/src/string/lower-first.ts +15 -0
- package/src/string/pascal-case.ts +21 -0
- package/src/string/upper-first.ts +15 -0
- package/src/string/word-segments.ts +15 -0
- package/src/util/index.ts +1 -0
- package/src/util/package-version.ts +16 -0
- package/dist/class.d.ts +0 -16
- package/dist/class.d.ts.map +0 -1
- package/dist/class.js +0 -2
- package/dist/class.js.map +0 -1
- package/dist/collection.d.ts +0 -58
- package/dist/collection.d.ts.map +0 -1
- package/dist/collection.js +0 -98
- package/dist/collection.js.map +0 -1
- package/dist/error.d.ts +0 -13
- package/dist/error.d.ts.map +0 -1
- package/dist/error.js +0 -13
- package/dist/error.js.map +0 -1
- package/dist/graceful-shutdown.d.ts +0 -70
- package/dist/graceful-shutdown.d.ts.map +0 -1
- package/dist/graceful-shutdown.js +0 -94
- package/dist/graceful-shutdown.js.map +0 -1
- package/dist/source.d.ts +0 -34
- package/dist/source.d.ts.map +0 -1
- package/dist/source.js +0 -99
- package/dist/source.js.map +0 -1
- package/dist/string.d.ts +0 -59
- package/dist/string.d.ts.map +0 -1
- package/dist/string.js +0 -73
- package/dist/string.js.map +0 -1
- package/src/class.ts +0 -17
- package/src/collection.ts +0 -103
- package/src/error.ts +0 -12
- package/src/graceful-shutdown.ts +0 -141
- package/src/source.ts +0 -103
- package/src/string.ts +0 -74
package/src/set/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { drain } from "./drain.js";
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
const RESERVED: Set<string> = new Set([
|
|
2
|
+
"arguments",
|
|
3
|
+
"await",
|
|
4
|
+
"break",
|
|
5
|
+
"case",
|
|
6
|
+
"catch",
|
|
7
|
+
"class",
|
|
8
|
+
"const",
|
|
9
|
+
"continue",
|
|
10
|
+
"debugger",
|
|
11
|
+
"default",
|
|
12
|
+
"delete",
|
|
13
|
+
"do",
|
|
14
|
+
"else",
|
|
15
|
+
"enum",
|
|
16
|
+
"eval",
|
|
17
|
+
"export",
|
|
18
|
+
"extends",
|
|
19
|
+
"false",
|
|
20
|
+
"finally",
|
|
21
|
+
"for",
|
|
22
|
+
"function",
|
|
23
|
+
"if",
|
|
24
|
+
"import",
|
|
25
|
+
"in",
|
|
26
|
+
"instanceof",
|
|
27
|
+
"interface",
|
|
28
|
+
"let",
|
|
29
|
+
"new",
|
|
30
|
+
"null",
|
|
31
|
+
"package",
|
|
32
|
+
"private",
|
|
33
|
+
"protected",
|
|
34
|
+
"public",
|
|
35
|
+
"return",
|
|
36
|
+
"static",
|
|
37
|
+
"super",
|
|
38
|
+
"switch",
|
|
39
|
+
"this",
|
|
40
|
+
"throw",
|
|
41
|
+
"true",
|
|
42
|
+
"try",
|
|
43
|
+
"typeof",
|
|
44
|
+
"var",
|
|
45
|
+
"void",
|
|
46
|
+
"while",
|
|
47
|
+
"with",
|
|
48
|
+
"yield",
|
|
49
|
+
]);
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Returns the name unchanged, or with a trailing underscore when it collides with a reserved word,
|
|
53
|
+
* so it is safe to emit as a JavaScript identifier.
|
|
54
|
+
*
|
|
55
|
+
* @param name - The candidate identifier.
|
|
56
|
+
* @returns The name, suffixed with `_` when it is a reserved word.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* sanitizeIdentifier("iconName"); // "iconName"
|
|
60
|
+
* sanitizeIdentifier("class"); // "class_"
|
|
61
|
+
*/
|
|
62
|
+
function sanitizeIdentifier(name: string): string {
|
|
63
|
+
return RESERVED.has(name) ? `${name}_` : name;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { sanitizeIdentifier };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const LINE_SEPARATOR = String.fromCodePoint(0x20_28);
|
|
2
|
+
const PARAGRAPH_SEPARATOR = String.fromCodePoint(0x20_29);
|
|
3
|
+
|
|
4
|
+
const SOURCE_ESCAPES: Record<string, string> = {
|
|
5
|
+
"<": String.raw`\u003C`,
|
|
6
|
+
">": String.raw`\u003E`,
|
|
7
|
+
[LINE_SEPARATOR]: String.raw`\u2028`,
|
|
8
|
+
[PARAGRAPH_SEPARATOR]: String.raw`\u2029`,
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const UNSAFE_SOURCE_CHARS = new RegExp(`[${Object.keys(SOURCE_ESCAPES).join("")}]`, "g");
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Encodes a string as a JavaScript string literal, additionally escaping characters that are unsafe
|
|
15
|
+
* to embed in generated source (angle brackets and the line and paragraph separators).
|
|
16
|
+
*
|
|
17
|
+
* @param value - The string to encode.
|
|
18
|
+
* @returns The quoted, source-safe string literal.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* sourceStringLiteral("hello"); // '"hello"'
|
|
22
|
+
*/
|
|
23
|
+
function sourceStringLiteral(value: string): string {
|
|
24
|
+
return JSON.stringify(value).replaceAll(UNSAFE_SOURCE_CHARS, (char) => SOURCE_ESCAPES[char] ?? char);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export { sourceStringLiteral };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { camelCase } from "../string/camel-case.js";
|
|
2
|
+
import { sanitizeIdentifier } from "./sanitize-identifier.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts a name to camel case and sanitizes it into a valid JavaScript identifier.
|
|
6
|
+
*
|
|
7
|
+
* @param name - The name to convert.
|
|
8
|
+
* @returns The camel-cased, reserved-word-safe identifier.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* toCamelIdentifier("icon_name"); // "iconName"
|
|
12
|
+
* toCamelIdentifier("class"); // "class_"
|
|
13
|
+
*/
|
|
14
|
+
function toCamelIdentifier(name: string): string {
|
|
15
|
+
return sanitizeIdentifier(camelCase(name));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { toCamelIdentifier };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { upperFirst } from "./upper-first.js";
|
|
2
|
+
import { mapWordSegments } from "./word-segments.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts an underscore- or hyphen-delimited string to camel case.
|
|
6
|
+
*
|
|
7
|
+
* Unlike a general-purpose converter, this splits only on `_` and `-` and keeps the first segment
|
|
8
|
+
* verbatim, so GObject acronyms are preserved (`camelCase("GLArea")` stays `"GLArea"`).
|
|
9
|
+
*
|
|
10
|
+
* @param str - The string to convert.
|
|
11
|
+
* @returns The camel-cased string.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* camelCase("icon_name"); // "iconName"
|
|
15
|
+
* camelCase("start-widget"); // "startWidget"
|
|
16
|
+
* camelCase("Box"); // "Box"
|
|
17
|
+
*/
|
|
18
|
+
function camelCase(str: string): string {
|
|
19
|
+
return mapWordSegments(str, (part, index) => (index === 0 ? part : upperFirst(part)));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export { camelCase };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a camelCase or PascalCase string to kebab case by lower-casing uppercase letters and
|
|
3
|
+
* inserting hyphens before interior ones.
|
|
4
|
+
*
|
|
5
|
+
* @param str - The string to convert.
|
|
6
|
+
* @returns The kebab-cased string.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* kebabCase("iconName"); // "icon-name"
|
|
10
|
+
* kebabCase("Title"); // "title"
|
|
11
|
+
*/
|
|
12
|
+
function kebabCase(str: string): string {
|
|
13
|
+
return str.replaceAll(/[A-Z]/g, (char, index: number) =>
|
|
14
|
+
index === 0 ? char.toLowerCase() : `-${char.toLowerCase()}`,
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export { kebabCase };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts the first character of a string to lower case.
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to transform.
|
|
5
|
+
* @returns The string with its first character lower-cased.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* lowerFirst("Fred"); // "fred"
|
|
9
|
+
* lowerFirst("FRED"); // "fRED"
|
|
10
|
+
*/
|
|
11
|
+
function lowerFirst(str: string): string {
|
|
12
|
+
return str.charAt(0).toLowerCase() + str.slice(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { lowerFirst };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { upperFirst } from "./upper-first.js";
|
|
2
|
+
import { mapWordSegments } from "./word-segments.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Converts an underscore- or hyphen-delimited string to Pascal case.
|
|
6
|
+
*
|
|
7
|
+
* Splits only on `_` and `-` and capitalizes each segment's first character, so GObject acronyms
|
|
8
|
+
* are preserved (`pascalCase("GLArea")` stays `"GLArea"`).
|
|
9
|
+
*
|
|
10
|
+
* @param str - The string to convert.
|
|
11
|
+
* @returns The Pascal-cased string.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* pascalCase("icon_name"); // "IconName"
|
|
15
|
+
* pascalCase("scrolled-window"); // "ScrolledWindow"
|
|
16
|
+
*/
|
|
17
|
+
function pascalCase(str: string): string {
|
|
18
|
+
return mapWordSegments(str, upperFirst);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export { pascalCase };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts the first character of a string to upper case.
|
|
3
|
+
*
|
|
4
|
+
* @param str - The string to transform.
|
|
5
|
+
* @returns The string with its first character upper-cased.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* upperFirst("fred"); // "Fred"
|
|
9
|
+
* upperFirst("Fred"); // "Fred"
|
|
10
|
+
*/
|
|
11
|
+
function upperFirst(str: string): string {
|
|
12
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { upperFirst };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
function splitWords(input: string): string[] {
|
|
2
|
+
return input.split(/[_-]/g).filter((part) => part.length > 0);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
function mapWordSegments(input: string, mapSegment: (part: string, index: number) => string): string {
|
|
6
|
+
const parts = splitWords(input);
|
|
7
|
+
|
|
8
|
+
if (parts.length === 0) {
|
|
9
|
+
return input;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
return parts.map((part, index) => mapSegment(part, index)).join("");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export { mapWordSegments };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { packageVersion } from "./package-version.js";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reads the `version` field from the `package.json` next to the calling module.
|
|
5
|
+
*
|
|
6
|
+
* @param importMetaUrl - The caller's `import.meta.url`, used to resolve the sibling `package.json`.
|
|
7
|
+
* @returns The package's version string.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* packageVersion(import.meta.url); // "1.2.3"
|
|
11
|
+
*/
|
|
12
|
+
function packageVersion(importMetaUrl: string): string {
|
|
13
|
+
return (createRequire(importMetaUrl)("../package.json") as { version: string }).version;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export { packageVersion };
|
package/dist/class.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Structural type matching any class — abstract or concrete — whose instances
|
|
3
|
-
* have type `T`.
|
|
4
|
-
*
|
|
5
|
-
* A class value carries a construct signature plus a `prototype`, but a bare
|
|
6
|
-
* construct signature types `prototype` as `any`. The `& { prototype: T }`
|
|
7
|
-
* intersection recovers the precise instance type so callers can read
|
|
8
|
-
* `cls.prototype` as `T`, and the `abstract new` form accepts both abstract and
|
|
9
|
-
* concrete classes, making this the widest supertype of every class value. The
|
|
10
|
-
* `never[]` constructor parameters admit any class while documenting that the
|
|
11
|
-
* type is used as an identity token, not to construct instances.
|
|
12
|
-
*/
|
|
13
|
-
export type AnyClass<T extends object = object> = (abstract new (...args: never[]) => T) & {
|
|
14
|
-
readonly prototype: T;
|
|
15
|
-
};
|
|
16
|
-
//# sourceMappingURL=class.d.ts.map
|
package/dist/class.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["../src/class.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,IAAI,CAAC,QAAQ,MACvD,GAAG,IAAI,EAAE,KAAK,EAAE,KACf,CAAC,CAAC,GAAG;IACN,QAAQ,CAAC,SAAS,EAAE,CAAC,CAAC;CACzB,CAAC"}
|
package/dist/class.js
DELETED
package/dist/class.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"class.js","sourceRoot":"","sources":["../src/class.ts"],"names":[],"mappings":""}
|
package/dist/collection.d.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure, runtime-agnostic collection helpers shared across GTKX packages.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Returns a shallow copy of `record` with the given `keys` removed.
|
|
6
|
-
*
|
|
7
|
-
* Keys absent from `record` are ignored, and `record` is not mutated. The
|
|
8
|
-
* result is typed as the input shape because callers treat the excluded keys
|
|
9
|
-
* as runtime-only concerns absent from the static type.
|
|
10
|
-
*
|
|
11
|
-
* @typeParam T - The record shape.
|
|
12
|
-
* @param record - The source object.
|
|
13
|
-
* @param keys - The keys to exclude from the copy.
|
|
14
|
-
* @returns A new object holding every own enumerable key of `record` except
|
|
15
|
-
* those listed in `keys`.
|
|
16
|
-
*/
|
|
17
|
-
export declare const omit: <T extends Record<string, unknown>>(record: T, keys: readonly string[]) => T;
|
|
18
|
-
/**
|
|
19
|
-
* Compares two arrays of primitives for element-wise strict equality.
|
|
20
|
-
*
|
|
21
|
-
* Returns `true` when both arguments are the same reference (including both
|
|
22
|
-
* being `null` or `undefined`), or when they have equal length and every
|
|
23
|
-
* element is strictly equal (`===`) at the same index. A `null`/`undefined`
|
|
24
|
-
* argument is equal only to another `null`/`undefined` argument.
|
|
25
|
-
*
|
|
26
|
-
* @typeParam T - The primitive element type.
|
|
27
|
-
* @param a - The first array, or `null`/`undefined`.
|
|
28
|
-
* @param b - The second array, or `null`/`undefined`.
|
|
29
|
-
* @returns Whether the two arrays are shallowly equal.
|
|
30
|
-
*/
|
|
31
|
-
export declare const isShallowEqual: <T extends string | number | boolean>(a: readonly T[] | null | undefined, b: readonly T[] | null | undefined) => boolean;
|
|
32
|
-
/**
|
|
33
|
-
* Compares two arrays of records by shallow per-element equality.
|
|
34
|
-
*
|
|
35
|
-
* Returns `true` when both arrays have equal length and, at every index, the
|
|
36
|
-
* two records expose the same set of keys with strictly equal (`===`) values.
|
|
37
|
-
* Values are compared one level deep only; nested objects are compared by
|
|
38
|
-
* reference.
|
|
39
|
-
*
|
|
40
|
-
* @typeParam T - The record element type.
|
|
41
|
-
* @param a - The first array of records.
|
|
42
|
-
* @param b - The second array of records.
|
|
43
|
-
* @returns Whether the two arrays are element-wise shallowly equal.
|
|
44
|
-
*/
|
|
45
|
-
export declare const isShallowArrayEqual: <T extends Record<string, unknown>>(a: readonly T[], b: readonly T[]) => boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Builds a reverse lookup from a numeric enum's values to their member names.
|
|
48
|
-
*
|
|
49
|
-
* A TypeScript numeric enum's runtime object carries both name-to-value and
|
|
50
|
-
* value-to-name entries; this keeps only the name-to-value direction and
|
|
51
|
-
* inverts it, yielding a `Map` from each numeric value to the name that
|
|
52
|
-
* declared it.
|
|
53
|
-
*
|
|
54
|
-
* @param enumObject - A numeric enum's runtime object.
|
|
55
|
-
* @returns A map from each numeric enum value to its declared member name.
|
|
56
|
-
*/
|
|
57
|
-
export declare const reverseNumericEnum: (enumObject: Record<string, string | number>) => Map<number, string>;
|
|
58
|
-
//# sourceMappingURL=collection.d.ts.map
|
package/dist/collection.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,IAAI,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,SAAS,MAAM,EAAE,KAAG,CAQ5F,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,GAAI,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,OAAO,EAC9D,GAAG,SAAS,CAAC,EAAE,GAAG,IAAI,GAAG,SAAS,EAClC,GAAG,SAAS,CAAC,EAAE,GAAG,IAAI,GAAG,SAAS,KACnC,OAQF,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,mBAAmB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,GAAG,SAAS,CAAC,EAAE,EAAE,GAAG,SAAS,CAAC,EAAE,KAAG,OAkBzG,CAAC;AAEF;;;;;;;;;;GAUG;AACH,eAAO,MAAM,kBAAkB,GAAI,YAAY,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,KAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAK9F,CAAC"}
|
package/dist/collection.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Pure, runtime-agnostic collection helpers shared across GTKX packages.
|
|
3
|
-
*/
|
|
4
|
-
/**
|
|
5
|
-
* Returns a shallow copy of `record` with the given `keys` removed.
|
|
6
|
-
*
|
|
7
|
-
* Keys absent from `record` are ignored, and `record` is not mutated. The
|
|
8
|
-
* result is typed as the input shape because callers treat the excluded keys
|
|
9
|
-
* as runtime-only concerns absent from the static type.
|
|
10
|
-
*
|
|
11
|
-
* @typeParam T - The record shape.
|
|
12
|
-
* @param record - The source object.
|
|
13
|
-
* @param keys - The keys to exclude from the copy.
|
|
14
|
-
* @returns A new object holding every own enumerable key of `record` except
|
|
15
|
-
* those listed in `keys`.
|
|
16
|
-
*/
|
|
17
|
-
export const omit = (record, keys) => {
|
|
18
|
-
const result = {};
|
|
19
|
-
for (const key of Object.keys(record)) {
|
|
20
|
-
if (!keys.includes(key)) {
|
|
21
|
-
result[key] = record[key];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return result;
|
|
25
|
-
};
|
|
26
|
-
/**
|
|
27
|
-
* Compares two arrays of primitives for element-wise strict equality.
|
|
28
|
-
*
|
|
29
|
-
* Returns `true` when both arguments are the same reference (including both
|
|
30
|
-
* being `null` or `undefined`), or when they have equal length and every
|
|
31
|
-
* element is strictly equal (`===`) at the same index. A `null`/`undefined`
|
|
32
|
-
* argument is equal only to another `null`/`undefined` argument.
|
|
33
|
-
*
|
|
34
|
-
* @typeParam T - The primitive element type.
|
|
35
|
-
* @param a - The first array, or `null`/`undefined`.
|
|
36
|
-
* @param b - The second array, or `null`/`undefined`.
|
|
37
|
-
* @returns Whether the two arrays are shallowly equal.
|
|
38
|
-
*/
|
|
39
|
-
export const isShallowEqual = (a, b) => {
|
|
40
|
-
if (a === b)
|
|
41
|
-
return true;
|
|
42
|
-
if (!a || !b)
|
|
43
|
-
return false;
|
|
44
|
-
if (a.length !== b.length)
|
|
45
|
-
return false;
|
|
46
|
-
for (let i = 0; i < a.length; i++) {
|
|
47
|
-
if (a[i] !== b[i])
|
|
48
|
-
return false;
|
|
49
|
-
}
|
|
50
|
-
return true;
|
|
51
|
-
};
|
|
52
|
-
/**
|
|
53
|
-
* Compares two arrays of records by shallow per-element equality.
|
|
54
|
-
*
|
|
55
|
-
* Returns `true` when both arrays have equal length and, at every index, the
|
|
56
|
-
* two records expose the same set of keys with strictly equal (`===`) values.
|
|
57
|
-
* Values are compared one level deep only; nested objects are compared by
|
|
58
|
-
* reference.
|
|
59
|
-
*
|
|
60
|
-
* @typeParam T - The record element type.
|
|
61
|
-
* @param a - The first array of records.
|
|
62
|
-
* @param b - The second array of records.
|
|
63
|
-
* @returns Whether the two arrays are element-wise shallowly equal.
|
|
64
|
-
*/
|
|
65
|
-
export const isShallowArrayEqual = (a, b) => {
|
|
66
|
-
if (a.length !== b.length)
|
|
67
|
-
return false;
|
|
68
|
-
for (let i = 0; i < a.length; i++) {
|
|
69
|
-
const itemA = a[i];
|
|
70
|
-
const itemB = b[i];
|
|
71
|
-
if (!itemA || !itemB)
|
|
72
|
-
return false;
|
|
73
|
-
const keysA = Object.keys(itemA);
|
|
74
|
-
const keysB = Object.keys(itemB);
|
|
75
|
-
if (keysA.length !== keysB.length)
|
|
76
|
-
return false;
|
|
77
|
-
for (const key of keysA) {
|
|
78
|
-
if (itemA[key] !== itemB[key])
|
|
79
|
-
return false;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return true;
|
|
83
|
-
};
|
|
84
|
-
/**
|
|
85
|
-
* Builds a reverse lookup from a numeric enum's values to their member names.
|
|
86
|
-
*
|
|
87
|
-
* A TypeScript numeric enum's runtime object carries both name-to-value and
|
|
88
|
-
* value-to-name entries; this keeps only the name-to-value direction and
|
|
89
|
-
* inverts it, yielding a `Map` from each numeric value to the name that
|
|
90
|
-
* declared it.
|
|
91
|
-
*
|
|
92
|
-
* @param enumObject - A numeric enum's runtime object.
|
|
93
|
-
* @returns A map from each numeric enum value to its declared member name.
|
|
94
|
-
*/
|
|
95
|
-
export const reverseNumericEnum = (enumObject) => new Map(Object.entries(enumObject)
|
|
96
|
-
.filter((entry) => typeof entry[1] === "number")
|
|
97
|
-
.map(([name, value]) => [value, name]));
|
|
98
|
-
//# sourceMappingURL=collection.js.map
|
package/dist/collection.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"collection.js","sourceRoot":"","sources":["../src/collection.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAoC,MAAS,EAAE,IAAuB,EAAK,EAAE;IAC7F,MAAM,MAAM,GAA4B,EAAE,CAAC;IAC3C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;QACpC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;YACtB,MAAM,CAAC,GAAG,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC9B,CAAC;IACL,CAAC;IACD,OAAO,MAAW,CAAC;AACvB,CAAC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC1B,CAAkC,EAClC,CAAkC,EAC3B,EAAE;IACT,IAAI,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IACzB,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC;QAAE,OAAO,KAAK,CAAC;IAC3B,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IACxC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAChC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YAAE,OAAO,KAAK,CAAC;IACpC,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAoC,CAAe,EAAE,CAAe,EAAW,EAAE;IAChH,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM;QAAE,OAAO,KAAK,CAAC;IAExC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAChC,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnB,MAAM,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACnB,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK;YAAE,OAAO,KAAK,CAAC;QAEnC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACjC,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACjC,IAAI,KAAK,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM;YAAE,OAAO,KAAK,CAAC;QAEhD,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;YACtB,IAAI,KAAK,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,GAAG,CAAC;gBAAE,OAAO,KAAK,CAAC;QAChD,CAAC;IACL,CAAC;IAED,OAAO,IAAI,CAAC;AAChB,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,kBAAkB,GAAG,CAAC,UAA2C,EAAuB,EAAE,CACnG,IAAI,GAAG,CACH,MAAM,CAAC,OAAO,CAAC,UAAU,CAAC;KACrB,MAAM,CAAC,CAAC,KAAK,EAA6B,EAAE,CAAC,OAAO,KAAK,CAAC,CAAC,CAAC,KAAK,QAAQ,CAAC;KAC1E,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC,CAC7C,CAAC"}
|
package/dist/error.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Coerces an unknown thrown value into a human-readable string.
|
|
3
|
-
*
|
|
4
|
-
* Returns `error.message` when `error` is an `Error` instance, otherwise
|
|
5
|
-
* delegates to `String(error)`. Use at boundaries where exceptions are
|
|
6
|
-
* surfaced to logs, IPC frames, or user-facing output and the type cannot
|
|
7
|
-
* be narrowed otherwise.
|
|
8
|
-
*
|
|
9
|
-
* @param error - The caught value of unknown shape.
|
|
10
|
-
* @returns A string describing the error.
|
|
11
|
-
*/
|
|
12
|
-
export declare const errorMessage: (error: unknown) => string;
|
|
13
|
-
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,eAAO,MAAM,YAAY,GAAI,OAAO,OAAO,KAAG,MAAkE,CAAC"}
|
package/dist/error.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Coerces an unknown thrown value into a human-readable string.
|
|
3
|
-
*
|
|
4
|
-
* Returns `error.message` when `error` is an `Error` instance, otherwise
|
|
5
|
-
* delegates to `String(error)`. Use at boundaries where exceptions are
|
|
6
|
-
* surfaced to logs, IPC frames, or user-facing output and the type cannot
|
|
7
|
-
* be narrowed otherwise.
|
|
8
|
-
*
|
|
9
|
-
* @param error - The caught value of unknown shape.
|
|
10
|
-
* @returns A string describing the error.
|
|
11
|
-
*/
|
|
12
|
-
export const errorMessage = (error) => (error instanceof Error ? error.message : String(error));
|
|
13
|
-
//# sourceMappingURL=error.js.map
|
package/dist/error.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"error.js","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAc,EAAU,EAAE,CAAC,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC"}
|
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cross-process graceful shutdown primitive for long-running Node processes.
|
|
3
|
-
*
|
|
4
|
-
* The helper installs `SIGINT`/`SIGTERM`/`SIGHUP` handlers, routes the first
|
|
5
|
-
* delivered signal through a user-supplied close callback, and escalates either
|
|
6
|
-
* on a second `SIGINT` or after a configurable timeout. On completion it calls
|
|
7
|
-
* `process.exit` with the canonical exit code for the signal.
|
|
8
|
-
*/
|
|
9
|
-
/**
|
|
10
|
-
* Maps the POSIX signal that ended a process into the exit code shells use to
|
|
11
|
-
* report it. `SIGINT` yields `130` (Ctrl-C), every other tracked signal yields
|
|
12
|
-
* `143` (`SIGTERM`). A `null` signal — i.e. a clean exit — yields `0`.
|
|
13
|
-
*
|
|
14
|
-
* @param signal - The signal name, or `null` for a clean exit.
|
|
15
|
-
* @returns The exit code shells expect for `signal`.
|
|
16
|
-
*/
|
|
17
|
-
export declare const exitCodeForSignal: (signal: NodeJS.Signals | null) => number;
|
|
18
|
-
/**
|
|
19
|
-
* Caller-supplied behaviour for {@link installGracefulShutdown}.
|
|
20
|
-
*/
|
|
21
|
-
export type GracefulShutdownOptions = {
|
|
22
|
-
/**
|
|
23
|
-
* Invoked once on the first delivered signal. Its returned promise is
|
|
24
|
-
* awaited before {@link process.exit} fires; rejection is logged but does
|
|
25
|
-
* not block the exit.
|
|
26
|
-
*/
|
|
27
|
-
onSignal: (signal: NodeJS.Signals) => void | Promise<void>;
|
|
28
|
-
/**
|
|
29
|
-
* Invoked when escalation is required: on a second `SIGINT`, or when
|
|
30
|
-
* {@link GracefulShutdownOptions.forceKillAfterMs} elapses before the
|
|
31
|
-
* primary close finishes.
|
|
32
|
-
*/
|
|
33
|
-
onForce?: () => void;
|
|
34
|
-
/**
|
|
35
|
-
* Milliseconds to wait for the primary close before invoking `onForce`
|
|
36
|
-
* and exiting. Defaults to {@link DEFAULT_FORCE_KILL_TIMEOUT_MS}. Set to
|
|
37
|
-
* `0` to disable timeout-based escalation.
|
|
38
|
-
*/
|
|
39
|
-
forceKillAfterMs?: number;
|
|
40
|
-
/**
|
|
41
|
-
* Overrides the exit code computed from the triggering signal. Use this
|
|
42
|
-
* when the caller wants to propagate a child's own exit code instead.
|
|
43
|
-
*/
|
|
44
|
-
exitCode?: (signal: NodeJS.Signals) => number;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Handle returned by {@link installGracefulShutdown}, used to detach the
|
|
48
|
-
* helper's signal handlers (for example in tests).
|
|
49
|
-
*/
|
|
50
|
-
export type GracefulShutdownHandle = {
|
|
51
|
-
/**
|
|
52
|
-
* Removes the installed signal handlers and cancels any pending
|
|
53
|
-
* escalation timer. Idempotent.
|
|
54
|
-
*/
|
|
55
|
-
uninstall: () => void;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Installs the graceful-shutdown primitive on the current process.
|
|
59
|
-
*
|
|
60
|
-
* On the first matching signal: invokes `onSignal`, optionally schedules
|
|
61
|
-
* `onForce` after `forceKillAfterMs`, awaits the close, then exits. A second
|
|
62
|
-
* `SIGINT` (the canonical "force-kill" gesture) invokes `onForce`
|
|
63
|
-
* immediately. The exit code defaults to {@link exitCodeForSignal} but can
|
|
64
|
-
* be overridden by `exitCode`.
|
|
65
|
-
*
|
|
66
|
-
* @param options - Shutdown behaviour.
|
|
67
|
-
* @returns A handle that detaches the installed signal handlers.
|
|
68
|
-
*/
|
|
69
|
-
export declare const installGracefulShutdown: (options: GracefulShutdownOptions) => GracefulShutdownHandle;
|
|
70
|
-
//# sourceMappingURL=graceful-shutdown.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"graceful-shutdown.d.ts","sourceRoot":"","sources":["../src/graceful-shutdown.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAKH;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB,GAAI,QAAQ,MAAM,CAAC,OAAO,GAAG,IAAI,KAAG,MAGjE,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,uBAAuB,GAAG;IAClC;;;;OAIG;IACH,QAAQ,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC3D;;;;OAIG;IACH,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;IACrB;;;;OAIG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,OAAO,KAAK,MAAM,CAAC;CACjD,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,sBAAsB,GAAG;IACjC;;;OAGG;IACH,SAAS,EAAE,MAAM,IAAI,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,uBAAuB,GAAI,SAAS,uBAAuB,KAAG,sBA8D1E,CAAC"}
|
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Cross-process graceful shutdown primitive for long-running Node processes.
|
|
3
|
-
*
|
|
4
|
-
* The helper installs `SIGINT`/`SIGTERM`/`SIGHUP` handlers, routes the first
|
|
5
|
-
* delivered signal through a user-supplied close callback, and escalates either
|
|
6
|
-
* on a second `SIGINT` or after a configurable timeout. On completion it calls
|
|
7
|
-
* `process.exit` with the canonical exit code for the signal.
|
|
8
|
-
*/
|
|
9
|
-
const HANDLED_SIGNALS = ["SIGINT", "SIGTERM", "SIGHUP"];
|
|
10
|
-
const DEFAULT_FORCE_KILL_TIMEOUT_MS = 5000;
|
|
11
|
-
/**
|
|
12
|
-
* Maps the POSIX signal that ended a process into the exit code shells use to
|
|
13
|
-
* report it. `SIGINT` yields `130` (Ctrl-C), every other tracked signal yields
|
|
14
|
-
* `143` (`SIGTERM`). A `null` signal — i.e. a clean exit — yields `0`.
|
|
15
|
-
*
|
|
16
|
-
* @param signal - The signal name, or `null` for a clean exit.
|
|
17
|
-
* @returns The exit code shells expect for `signal`.
|
|
18
|
-
*/
|
|
19
|
-
export const exitCodeForSignal = (signal) => {
|
|
20
|
-
if (!signal)
|
|
21
|
-
return 0;
|
|
22
|
-
return signal === "SIGINT" ? 130 : 143;
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Installs the graceful-shutdown primitive on the current process.
|
|
26
|
-
*
|
|
27
|
-
* On the first matching signal: invokes `onSignal`, optionally schedules
|
|
28
|
-
* `onForce` after `forceKillAfterMs`, awaits the close, then exits. A second
|
|
29
|
-
* `SIGINT` (the canonical "force-kill" gesture) invokes `onForce`
|
|
30
|
-
* immediately. The exit code defaults to {@link exitCodeForSignal} but can
|
|
31
|
-
* be overridden by `exitCode`.
|
|
32
|
-
*
|
|
33
|
-
* @param options - Shutdown behaviour.
|
|
34
|
-
* @returns A handle that detaches the installed signal handlers.
|
|
35
|
-
*/
|
|
36
|
-
export const installGracefulShutdown = (options) => {
|
|
37
|
-
const forceKillMs = options.forceKillAfterMs ?? DEFAULT_FORCE_KILL_TIMEOUT_MS;
|
|
38
|
-
let firstSignal = null;
|
|
39
|
-
let exited = false;
|
|
40
|
-
let forceTimer = null;
|
|
41
|
-
const clearTimer = () => {
|
|
42
|
-
if (forceTimer) {
|
|
43
|
-
clearTimeout(forceTimer);
|
|
44
|
-
forceTimer = null;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
const finish = (signal) => {
|
|
48
|
-
if (exited)
|
|
49
|
-
return;
|
|
50
|
-
exited = true;
|
|
51
|
-
clearTimer();
|
|
52
|
-
const code = options.exitCode ? options.exitCode(signal) : exitCodeForSignal(signal);
|
|
53
|
-
process.exit(code);
|
|
54
|
-
};
|
|
55
|
-
const handle = (signal) => {
|
|
56
|
-
if (firstSignal === null) {
|
|
57
|
-
firstSignal = signal;
|
|
58
|
-
if (options.onForce && forceKillMs > 0) {
|
|
59
|
-
forceTimer = setTimeout(() => {
|
|
60
|
-
options.onForce?.();
|
|
61
|
-
finish(signal);
|
|
62
|
-
}, forceKillMs);
|
|
63
|
-
forceTimer.unref?.();
|
|
64
|
-
}
|
|
65
|
-
Promise.resolve()
|
|
66
|
-
.then(() => options.onSignal(signal))
|
|
67
|
-
.catch((error) => {
|
|
68
|
-
console.error("Graceful shutdown error:", error);
|
|
69
|
-
})
|
|
70
|
-
.finally(() => finish(signal));
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
if (signal === "SIGINT" && firstSignal === "SIGINT") {
|
|
74
|
-
options.onForce?.();
|
|
75
|
-
finish(signal);
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
const handlers = new Map();
|
|
79
|
-
for (const sig of HANDLED_SIGNALS) {
|
|
80
|
-
const listener = () => handle(sig);
|
|
81
|
-
handlers.set(sig, listener);
|
|
82
|
-
process.on(sig, listener);
|
|
83
|
-
}
|
|
84
|
-
return {
|
|
85
|
-
uninstall: () => {
|
|
86
|
-
for (const [sig, listener] of handlers) {
|
|
87
|
-
process.removeListener(sig, listener);
|
|
88
|
-
}
|
|
89
|
-
handlers.clear();
|
|
90
|
-
clearTimer();
|
|
91
|
-
},
|
|
92
|
-
};
|
|
93
|
-
};
|
|
94
|
-
//# sourceMappingURL=graceful-shutdown.js.map
|