@luxass/utils 2.1.0 → 2.2.1
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/guards-C19fJmlu.d.ts +60 -0
- package/dist/guards.d.ts +1 -1
- package/dist/index.d.ts +13 -14
- package/dist/number-C2E_sOOy.d.ts +24 -0
- package/dist/number.d.ts +1 -1
- package/dist/string-CBWIJiC0.d.ts +132 -0
- package/dist/string.d.ts +1 -1
- package/dist/types-B3iMkmi_.d.ts +95 -0
- package/dist/types.d.ts +2 -2
- package/package.json +5 -5
- package/dist/guards.d-n1BzCANy.d.ts +0 -62
- package/dist/number.d-C2Xuq3Is.d.ts +0 -23
- package/dist/string.d-B2ppMtlR.d.ts +0 -136
- package/dist/types.d-BcKIY6l3.d.ts +0 -72
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//#region src/guards.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Checks if a value is not null or undefined.
|
|
4
|
+
*
|
|
5
|
+
* @template T - The type of the input value.
|
|
6
|
+
* @param {T | null | undefined} v - The value to check for not being null or undefined.
|
|
7
|
+
* @returns {v is NonNullable<T>} - True if the value is not null or undefined, false otherwise.
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNullish)
|
|
12
|
+
* // [true, false, 0, 1, "", "hello"]
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
declare function isNotNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
16
|
+
/**
|
|
17
|
+
* Checks if a value is not null.
|
|
18
|
+
*
|
|
19
|
+
* @template T - The type of the input value.
|
|
20
|
+
* @param {T | null} v - The value to check for not being null.
|
|
21
|
+
* @returns {v is Exclude<T, null>} True if the value is not null, false otherwise.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNull)
|
|
26
|
+
* // [true, false, 0, 1, "", "hello", undefined]
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
declare function isNotNull<T>(v: T | null): v is Exclude<T, null>;
|
|
30
|
+
/**
|
|
31
|
+
* Checks if a value is defined.
|
|
32
|
+
*
|
|
33
|
+
* @template T - The type of the input value.
|
|
34
|
+
* @param {T} v - The value to check for being defined.
|
|
35
|
+
* @returns {v is Exclude<T, undefined>} True if the value is defined, false otherwise.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isDefined)
|
|
40
|
+
* // [true, false, 0, 1, "", "hello", null]
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
declare function isNotUndefined<T>(v: T): v is Exclude<T, undefined>;
|
|
44
|
+
/**
|
|
45
|
+
* Checks if a value is truthy, excluding null and undefined.
|
|
46
|
+
*
|
|
47
|
+
* @template T - The type of the input value.
|
|
48
|
+
* @param {T} v - The value to check for truthiness.
|
|
49
|
+
* @returns {v is NonNullable<T>} True if the value is truthy, false otherwise.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* [true, false, 0, 1, "", "hello", null, undefined].filter(isTruthy)
|
|
54
|
+
* // [true, 1, "hello"]
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
declare function isTruthy<T>(v: T): v is NonNullable<T>;
|
|
58
|
+
|
|
59
|
+
//#endregion
|
|
60
|
+
export { isNotNull as isNotNull$1, isNotNullish as isNotNullish$1, isNotUndefined as isNotUndefined$1, isTruthy as isTruthy$1 };
|
package/dist/guards.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { isNotNull$1 as isNotNull, isNotNullish$1 as isNotNullish, isNotUndefined$1 as isNotUndefined, isTruthy$1 as isTruthy } from "./guards
|
|
1
|
+
import { isNotNull$1 as isNotNull, isNotNullish$1 as isNotNullish, isNotUndefined$1 as isNotUndefined, isTruthy$1 as isTruthy } from "./guards-C19fJmlu.js";
|
|
2
2
|
export { isNotNull, isNotNullish, isNotUndefined, isTruthy };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,24 +1,23 @@
|
|
|
1
|
-
import { isNotNull$1 as isNotNull, isNotNullish$1 as isNotNullish, isNotUndefined$1 as isNotUndefined, isTruthy$1 as isTruthy } from "./guards
|
|
2
|
-
import { clamp$1 as clamp } from "./number
|
|
3
|
-
import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, formatStr$1 as formatStr, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string
|
|
4
|
-
import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from "./types
|
|
1
|
+
import { isNotNull$1 as isNotNull, isNotNullish$1 as isNotNullish, isNotUndefined$1 as isNotUndefined, isTruthy$1 as isTruthy } from "./guards-C19fJmlu.js";
|
|
2
|
+
import { clamp$1 as clamp } from "./number-C2E_sOOy.js";
|
|
3
|
+
import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, formatStr$1 as formatStr, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string-CBWIJiC0.js";
|
|
4
|
+
import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish, Prettify, RemoveIndexSignature } from "./types-B3iMkmi_.js";
|
|
5
5
|
|
|
6
6
|
//#region src/common.d.ts
|
|
7
7
|
declare class InvariantError extends Error {
|
|
8
8
|
readonly message: string;
|
|
9
9
|
constructor(message: string, ...positionals: any[]);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
11
|
/**
|
|
13
|
-
* Asserts that a condition is truthy. If the condition is falsy, throws an InvariantError.
|
|
14
|
-
* This function is useful for ensuring invariants and preconditions in code.
|
|
15
|
-
*
|
|
16
|
-
* @param {unknown} predicate - The condition to check
|
|
17
|
-
* @param {string} message - Error message to display if the condition fails
|
|
18
|
-
* @param {unknown[]} positionals - Values to substitute into the error message using formatStr
|
|
19
|
-
* @throws {InvariantError} Throws when the predicate is falsy
|
|
20
|
-
*/
|
|
12
|
+
* Asserts that a condition is truthy. If the condition is falsy, throws an InvariantError.
|
|
13
|
+
* This function is useful for ensuring invariants and preconditions in code.
|
|
14
|
+
*
|
|
15
|
+
* @param {unknown} predicate - The condition to check
|
|
16
|
+
* @param {string} message - Error message to display if the condition fails
|
|
17
|
+
* @param {unknown[]} positionals - Values to substitute into the error message using formatStr
|
|
18
|
+
* @throws {InvariantError} Throws when the predicate is falsy
|
|
19
|
+
*/
|
|
21
20
|
declare function invariant(predicate: unknown, message: string, ...positionals: unknown[]): asserts predicate;
|
|
22
21
|
|
|
23
22
|
//#endregion
|
|
24
|
-
export { Arrayable, Awaitable, ElementOf, InferArguments, InvariantError, Nullable, Nullish, capitalize, clamp, dedent, dedentRaw, formatStr, invariant, isNotNull, isNotNullish, isNotUndefined, isTruthy, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
23
|
+
export { Arrayable, Awaitable, ElementOf, InferArguments, InvariantError, Nullable, Nullish, Prettify, RemoveIndexSignature, capitalize, clamp, dedent, dedentRaw, formatStr, invariant, isNotNull, isNotNullish, isNotUndefined, isTruthy, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
//#region src/number.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Clamp a value between a min and max value.
|
|
4
|
+
* @param {number} value
|
|
5
|
+
* @param {number} min
|
|
6
|
+
* @param {number} max
|
|
7
|
+
* @returns {number} the clamped value
|
|
8
|
+
*
|
|
9
|
+
* @example
|
|
10
|
+
* ```ts
|
|
11
|
+
* clamp(5, 0, 10)
|
|
12
|
+
* // 5
|
|
13
|
+
*
|
|
14
|
+
* clamp(5, 10, 20)
|
|
15
|
+
* // 10
|
|
16
|
+
*
|
|
17
|
+
* clamp(5, 0, 4)
|
|
18
|
+
* // 4
|
|
19
|
+
* ```
|
|
20
|
+
*/
|
|
21
|
+
declare function clamp(value: number, min: number, max: number): number;
|
|
22
|
+
|
|
23
|
+
//#endregion
|
|
24
|
+
export { clamp as clamp$1 };
|
package/dist/number.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { clamp$1 as clamp } from "./number
|
|
1
|
+
import { clamp$1 as clamp } from "./number-C2E_sOOy.js";
|
|
2
2
|
export { clamp };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
//#region src/string.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Capitalizes the first character of a string and converts the rest to lowercase
|
|
4
|
+
* @param {string} str - The string to capitalize
|
|
5
|
+
* @returns {string} The capitalized string
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* capitalize("hello") // "Hello"
|
|
9
|
+
* capitalize("hELLO") // "Hello"
|
|
10
|
+
* capitalize("") // ""
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
declare function capitalize(str: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Converts a string to camelCase format
|
|
16
|
+
* @param {string} str - The string to convert to camelCase
|
|
17
|
+
* @returns {string} The string in camelCase format
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* toCamelCase("hello world") // "helloWorld"
|
|
21
|
+
* toCamelCase("hello-world") // "helloWorld"
|
|
22
|
+
* toCamelCase("hello_world") // "helloWorld"
|
|
23
|
+
* toCamelCase("HelloWorld") // "helloWorld"
|
|
24
|
+
* toCamelCase("") // ""
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
declare function toCamelCase(str: string): string;
|
|
28
|
+
/**
|
|
29
|
+
* Converts a string to kebab-case format
|
|
30
|
+
* @param {string} str - The string to convert to kebab-case
|
|
31
|
+
* @returns {string} The string in kebab-case format
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* toKebabCase("hello world") // "hello-world"
|
|
35
|
+
* toKebabCase("helloWorld") // "hello-world"
|
|
36
|
+
* toKebabCase("hello_world") // "hello-world"
|
|
37
|
+
* toKebabCase("HelloWorld") // "hello-world"
|
|
38
|
+
* toKebabCase("") // ""
|
|
39
|
+
* ```
|
|
40
|
+
*/
|
|
41
|
+
declare function toKebabCase(str: string): string;
|
|
42
|
+
/**
|
|
43
|
+
* Converts a string to PascalCase format
|
|
44
|
+
* @param {string} str - The string to convert to PascalCase
|
|
45
|
+
* @returns {string} The string in PascalCase format
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* toPascalCase("hello world") // "HelloWorld"
|
|
49
|
+
* toPascalCase("hello-world") // "HelloWorld"
|
|
50
|
+
* toPascalCase("hello_world") // "HelloWorld"
|
|
51
|
+
* toPascalCase("helloWorld") // "HelloWorld"
|
|
52
|
+
* toPascalCase("") // ""
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
declare function toPascalCase(str: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Converts a string to snake_case format
|
|
58
|
+
* @param {string} str - The string to convert to snake_case
|
|
59
|
+
* @returns {string} The string in snake_case format
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* toSnakeCase("hello world") // "hello_world"
|
|
63
|
+
* toSnakeCase("helloWorld") // "hello_world"
|
|
64
|
+
* toSnakeCase("hello-world") // "hello_world"
|
|
65
|
+
* toSnakeCase("HelloWorld") // "hello_world"
|
|
66
|
+
* toSnakeCase("") // ""
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
declare function toSnakeCase(str: string): string;
|
|
70
|
+
/**
|
|
71
|
+
* Removes leading and trailing whitespace from each line of a string
|
|
72
|
+
* @param {TemplateStringsArray | string} literals - The string to dedent
|
|
73
|
+
* @returns {string} The dedented string
|
|
74
|
+
* @example ```ts
|
|
75
|
+
* dedent`
|
|
76
|
+
* This is a test.
|
|
77
|
+
* This is another line.
|
|
78
|
+
* `
|
|
79
|
+
* // "This is a test.\nThis is another line."
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
declare function dedent(literals: string): string;
|
|
83
|
+
declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
|
|
84
|
+
declare namespace dedent {
|
|
85
|
+
var raw: typeof dedentRaw;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Removes leading and trailing whitespace from each line of a string
|
|
89
|
+
* @param {TemplateStringsArray | string} literals - The string to dedent
|
|
90
|
+
* @returns {string} The dedented string
|
|
91
|
+
* @example ```ts
|
|
92
|
+
* dedent`
|
|
93
|
+
* This is a test.
|
|
94
|
+
* This is another line.
|
|
95
|
+
* `
|
|
96
|
+
* // "This is a test.\nThis is another line."
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function dedentRaw(literals: string): string;
|
|
100
|
+
declare function dedentRaw(strings: TemplateStringsArray, ...values: unknown[]): string;
|
|
101
|
+
/**
|
|
102
|
+
* Ensures a string is a valid JavaScript identifier by prefixing with an underscore if necessary
|
|
103
|
+
* @param {string} str - The string to sanitize
|
|
104
|
+
* @returns {string} A valid JavaScript identifier
|
|
105
|
+
* @example
|
|
106
|
+
* ```ts
|
|
107
|
+
* sanitizeIdentifier("validName") // "validName"
|
|
108
|
+
* sanitizeIdentifier("123invalid") // "_123invalid"
|
|
109
|
+
* sanitizeIdentifier("$valid") // "$valid"
|
|
110
|
+
* sanitizeIdentifier("_valid") // "_valid"
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
declare function sanitizeIdentifier(str: string): string;
|
|
114
|
+
/**
|
|
115
|
+
* Formats a string by replacing placeholders with positional values
|
|
116
|
+
* @param {string} message - The string containing placeholders to be replaced
|
|
117
|
+
* @param {...any} positionals - The values to insert into the placeholders
|
|
118
|
+
* @returns {string} The formatted string
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```ts
|
|
122
|
+
* formatStr("Hello %s", "world") // "Hello world"
|
|
123
|
+
* formatStr("Count: %d", 5) // "Count: 5"
|
|
124
|
+
* formatStr("Data: %j", { name: "test" }) // "Data: {"name":"test"}"
|
|
125
|
+
* formatStr("Object: %o", { id: 1 }) // "Object: {"id":1}"
|
|
126
|
+
* formatStr("Escaped %%s", "value") // "Escaped %s"
|
|
127
|
+
* formatStr("Extra args", 1, 2) // "Extra args 1 2"
|
|
128
|
+
* ```
|
|
129
|
+
*/
|
|
130
|
+
declare function formatStr(message: string, ...positionals: unknown[]): string;
|
|
131
|
+
//#endregion
|
|
132
|
+
export { capitalize as capitalize$1, dedent as dedent$1, dedentRaw as dedentRaw$1, formatStr as formatStr$1, sanitizeIdentifier as sanitizeIdentifier$1, toCamelCase as toCamelCase$1, toKebabCase as toKebabCase$1, toPascalCase as toPascalCase$1, toSnakeCase as toSnakeCase$1 };
|
package/dist/string.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, formatStr$1 as formatStr, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string
|
|
1
|
+
import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, formatStr$1 as formatStr, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string-CBWIJiC0.js";
|
|
2
2
|
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
//#region src/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* Whatever type, or Promise of that type
|
|
4
|
+
* @param T - Type
|
|
5
|
+
* @returns T or Promise<T>
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```ts
|
|
9
|
+
* type A = Awaitable<string>
|
|
10
|
+
* // string | Promise<string>
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
type Awaitable<T> = T | PromiseLike<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Whatever type, or null
|
|
16
|
+
* @param T - Type
|
|
17
|
+
* @returns T or null
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```ts
|
|
21
|
+
* type A = Nullable<string>
|
|
22
|
+
* // string | null
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
type Nullable<T> = T | null;
|
|
26
|
+
/**
|
|
27
|
+
* Whatever type, null or undefined
|
|
28
|
+
* @param T - Type
|
|
29
|
+
* @returns T, undefined or null
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* type A = Nullish<string>
|
|
34
|
+
* // string | null | undefined
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
type Nullish<T> = T | null | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Array, or not yet
|
|
40
|
+
*/
|
|
41
|
+
type Arrayable<T> = T | Array<T>;
|
|
42
|
+
/**
|
|
43
|
+
* Infers the element type of an array
|
|
44
|
+
* @param T - Array type
|
|
45
|
+
* @returns The inferred element type
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
* ```ts
|
|
49
|
+
* type A = ElementOf<string[]>
|
|
50
|
+
* // string
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
53
|
+
type ElementOf<T> = T extends (infer E)[] ? E : never;
|
|
54
|
+
/**
|
|
55
|
+
* Infers the arguments type of a function
|
|
56
|
+
* @param T - Function type
|
|
57
|
+
* @returns The inferred arguments type
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* type A = InferArguments<(a: string, b: number) => void>
|
|
62
|
+
* // [string, number]
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
65
|
+
type InferArguments<T> = T extends ((...args: infer A) => any) ? A : never;
|
|
66
|
+
/**
|
|
67
|
+
* Makes complex nested types more readable in editor tooltips by flattening
|
|
68
|
+
* the type to a simple object type with all properties
|
|
69
|
+
* @param T - The type to prettify
|
|
70
|
+
* @returns A simplified representation of the same type
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* type Messy = { a: string } & { b: number } & { c: boolean }
|
|
75
|
+
* type Clean = Prettify<Messy>
|
|
76
|
+
* // { a: string; b: number; c: boolean }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
80
|
+
/**
|
|
81
|
+
* Removes index signatures from a type while preserving specific properties
|
|
82
|
+
* @param T - The type to remove index signatures from
|
|
83
|
+
* @returns A new type without index signatures
|
|
84
|
+
*
|
|
85
|
+
* @example
|
|
86
|
+
* ```ts
|
|
87
|
+
* type WithIndex = { id: number; [key: string]: any }
|
|
88
|
+
* type Clean = RemoveIndexSignature<WithIndex>
|
|
89
|
+
* // { id: number }
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
92
|
+
type RemoveIndexSignature<T> = { [K in keyof T as {} extends Record<K, 1> ? never : K]: T[K] };
|
|
93
|
+
|
|
94
|
+
//#endregion
|
|
95
|
+
export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish, Prettify, RemoveIndexSignature };
|
package/dist/types.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from "./types
|
|
2
|
-
export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish };
|
|
1
|
+
import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish, Prettify, RemoveIndexSignature } from "./types-B3iMkmi_.js";
|
|
2
|
+
export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish, Prettify, RemoveIndexSignature };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@luxass/utils",
|
|
3
|
-
"version": "2.1
|
|
3
|
+
"version": "2.2.1",
|
|
4
4
|
"description": "A collection of utilities for JavaScript/TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": {
|
|
@@ -41,13 +41,13 @@
|
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@luxass/eslint-config": "^4.18.1",
|
|
43
43
|
"@types/node": "^22.15.2",
|
|
44
|
-
"@vitest/coverage-v8": "3.1.
|
|
45
|
-
"eslint": "^9.
|
|
44
|
+
"@vitest/coverage-v8": "3.1.4",
|
|
45
|
+
"eslint": "^9.27.0",
|
|
46
46
|
"eslint-plugin-format": "^1.0.1",
|
|
47
47
|
"publint": "^0.3.12",
|
|
48
|
-
"tsdown": "^0.
|
|
48
|
+
"tsdown": "^0.11.12",
|
|
49
49
|
"typescript": "^5.8.3",
|
|
50
|
-
"vitest": "^3.1.
|
|
50
|
+
"vitest": "^3.1.4",
|
|
51
51
|
"vitest-package-exports": "^0.1.1"
|
|
52
52
|
},
|
|
53
53
|
"scripts": {
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
//#region src/guards.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Checks if a value is not null or undefined.
|
|
4
|
-
*
|
|
5
|
-
* @template T - The type of the input value.
|
|
6
|
-
* @param {T | null | undefined} v - The value to check for not being null or undefined.
|
|
7
|
-
* @returns {v is NonNullable<T>} - True if the value is not null or undefined, false otherwise.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNullish)
|
|
12
|
-
* // [true, false, 0, 1, "", "hello"]
|
|
13
|
-
* ```
|
|
14
|
-
*/declare function isNotNullish<T>(v: T | null | undefined): v is NonNullable<T>;
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Checks if a value is not null.
|
|
18
|
-
*
|
|
19
|
-
* @template T - The type of the input value.
|
|
20
|
-
* @param {T | null} v - The value to check for not being null.
|
|
21
|
-
* @returns {v is Exclude<T, null>} True if the value is not null, false otherwise.
|
|
22
|
-
*
|
|
23
|
-
* @example
|
|
24
|
-
* ```ts
|
|
25
|
-
* [true, false, 0, 1, "", "hello", null, undefined].filter(isNotNull)
|
|
26
|
-
* // [true, false, 0, 1, "", "hello", undefined]
|
|
27
|
-
* ```
|
|
28
|
-
*/
|
|
29
|
-
declare function isNotNull<T>(v: T | null): v is Exclude<T, null>;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Checks if a value is defined.
|
|
33
|
-
*
|
|
34
|
-
* @template T - The type of the input value.
|
|
35
|
-
* @param {T} v - The value to check for being defined.
|
|
36
|
-
* @returns {v is Exclude<T, undefined>} True if the value is defined, false otherwise.
|
|
37
|
-
*
|
|
38
|
-
* @example
|
|
39
|
-
* ```ts
|
|
40
|
-
* [true, false, 0, 1, "", "hello", null, undefined].filter(isDefined)
|
|
41
|
-
* // [true, false, 0, 1, "", "hello", null]
|
|
42
|
-
* ```
|
|
43
|
-
*/
|
|
44
|
-
declare function isNotUndefined<T>(v: T): v is Exclude<T, undefined>;
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Checks if a value is truthy, excluding null and undefined.
|
|
48
|
-
*
|
|
49
|
-
* @template T - The type of the input value.
|
|
50
|
-
* @param {T} v - The value to check for truthiness.
|
|
51
|
-
* @returns {v is NonNullable<T>} True if the value is truthy, false otherwise.
|
|
52
|
-
*
|
|
53
|
-
* @example
|
|
54
|
-
* ```ts
|
|
55
|
-
* [true, false, 0, 1, "", "hello", null, undefined].filter(isTruthy)
|
|
56
|
-
* // [true, 1, "hello"]
|
|
57
|
-
* ```
|
|
58
|
-
*/
|
|
59
|
-
declare function isTruthy<T>(v: T): v is NonNullable<T>;
|
|
60
|
-
|
|
61
|
-
//#endregion
|
|
62
|
-
export { isNotNull as isNotNull$1, isNotNullish as isNotNullish$1, isNotUndefined as isNotUndefined$1, isTruthy as isTruthy$1 };
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
//#region src/number.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Clamp a value between a min and max value.
|
|
4
|
-
* @param {number} value
|
|
5
|
-
* @param {number} min
|
|
6
|
-
* @param {number} max
|
|
7
|
-
* @returns {number} the clamped value
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* clamp(5, 0, 10)
|
|
12
|
-
* // 5
|
|
13
|
-
*
|
|
14
|
-
* clamp(5, 10, 20)
|
|
15
|
-
* // 10
|
|
16
|
-
*
|
|
17
|
-
* clamp(5, 0, 4)
|
|
18
|
-
* // 4
|
|
19
|
-
* ```
|
|
20
|
-
*/declare function clamp(value: number, min: number, max: number): number;
|
|
21
|
-
|
|
22
|
-
//#endregion
|
|
23
|
-
export { clamp as clamp$1 };
|
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
//#region src/string.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Capitalizes the first character of a string and converts the rest to lowercase
|
|
4
|
-
* @param {string} str - The string to capitalize
|
|
5
|
-
* @returns {string} The capitalized string
|
|
6
|
-
* @example
|
|
7
|
-
* ```ts
|
|
8
|
-
* capitalize("hello") // "Hello"
|
|
9
|
-
* capitalize("hELLO") // "Hello"
|
|
10
|
-
* capitalize("") // ""
|
|
11
|
-
* ```
|
|
12
|
-
*/declare function capitalize(str: string): string;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Converts a string to camelCase format
|
|
16
|
-
* @param {string} str - The string to convert to camelCase
|
|
17
|
-
* @returns {string} The string in camelCase format
|
|
18
|
-
* @example
|
|
19
|
-
* ```ts
|
|
20
|
-
* toCamelCase("hello world") // "helloWorld"
|
|
21
|
-
* toCamelCase("hello-world") // "helloWorld"
|
|
22
|
-
* toCamelCase("hello_world") // "helloWorld"
|
|
23
|
-
* toCamelCase("HelloWorld") // "helloWorld"
|
|
24
|
-
* toCamelCase("") // ""
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
declare function toCamelCase(str: string): string;
|
|
28
|
-
|
|
29
|
-
/**
|
|
30
|
-
* Converts a string to kebab-case format
|
|
31
|
-
* @param {string} str - The string to convert to kebab-case
|
|
32
|
-
* @returns {string} The string in kebab-case format
|
|
33
|
-
* @example
|
|
34
|
-
* ```ts
|
|
35
|
-
* toKebabCase("hello world") // "hello-world"
|
|
36
|
-
* toKebabCase("helloWorld") // "hello-world"
|
|
37
|
-
* toKebabCase("hello_world") // "hello-world"
|
|
38
|
-
* toKebabCase("HelloWorld") // "hello-world"
|
|
39
|
-
* toKebabCase("") // ""
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
declare function toKebabCase(str: string): string;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Converts a string to PascalCase format
|
|
46
|
-
* @param {string} str - The string to convert to PascalCase
|
|
47
|
-
* @returns {string} The string in PascalCase format
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* toPascalCase("hello world") // "HelloWorld"
|
|
51
|
-
* toPascalCase("hello-world") // "HelloWorld"
|
|
52
|
-
* toPascalCase("hello_world") // "HelloWorld"
|
|
53
|
-
* toPascalCase("helloWorld") // "HelloWorld"
|
|
54
|
-
* toPascalCase("") // ""
|
|
55
|
-
* ```
|
|
56
|
-
*/
|
|
57
|
-
declare function toPascalCase(str: string): string;
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Converts a string to snake_case format
|
|
61
|
-
* @param {string} str - The string to convert to snake_case
|
|
62
|
-
* @returns {string} The string in snake_case format
|
|
63
|
-
* @example
|
|
64
|
-
* ```ts
|
|
65
|
-
* toSnakeCase("hello world") // "hello_world"
|
|
66
|
-
* toSnakeCase("helloWorld") // "hello_world"
|
|
67
|
-
* toSnakeCase("hello-world") // "hello_world"
|
|
68
|
-
* toSnakeCase("HelloWorld") // "hello_world"
|
|
69
|
-
* toSnakeCase("") // ""
|
|
70
|
-
* ```
|
|
71
|
-
*/
|
|
72
|
-
declare function toSnakeCase(str: string): string;
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* Removes leading and trailing whitespace from each line of a string
|
|
76
|
-
* @param {TemplateStringsArray | string} literals - The string to dedent
|
|
77
|
-
* @returns {string} The dedented string
|
|
78
|
-
* @example ```ts
|
|
79
|
-
* dedent`
|
|
80
|
-
* This is a test.
|
|
81
|
-
* This is another line.
|
|
82
|
-
* `
|
|
83
|
-
* // "This is a test.\nThis is another line."
|
|
84
|
-
* ```
|
|
85
|
-
*/
|
|
86
|
-
declare function dedent(literals: string): string;
|
|
87
|
-
declare function dedent(strings: TemplateStringsArray, ...values: unknown[]): string;
|
|
88
|
-
declare namespace dedent {
|
|
89
|
-
var raw: typeof dedentRaw;
|
|
90
|
-
} /**
|
|
91
|
-
* Removes leading and trailing whitespace from each line of a string
|
|
92
|
-
* @param {TemplateStringsArray | string} literals - The string to dedent
|
|
93
|
-
* @returns {string} The dedented string
|
|
94
|
-
* @example ```ts
|
|
95
|
-
* dedent`
|
|
96
|
-
* This is a test.
|
|
97
|
-
* This is another line.
|
|
98
|
-
* `
|
|
99
|
-
* // "This is a test.\nThis is another line."
|
|
100
|
-
* ```
|
|
101
|
-
*/
|
|
102
|
-
|
|
103
|
-
declare function dedentRaw(literals: string): string;
|
|
104
|
-
declare function dedentRaw(strings: TemplateStringsArray, ...values: unknown[]): string; /**
|
|
105
|
-
* Ensures a string is a valid JavaScript identifier by prefixing with an underscore if necessary
|
|
106
|
-
* @param {string} str - The string to sanitize
|
|
107
|
-
* @returns {string} A valid JavaScript identifier
|
|
108
|
-
* @example
|
|
109
|
-
* ```ts
|
|
110
|
-
* sanitizeIdentifier("validName") // "validName"
|
|
111
|
-
* sanitizeIdentifier("123invalid") // "_123invalid"
|
|
112
|
-
* sanitizeIdentifier("$valid") // "$valid"
|
|
113
|
-
* sanitizeIdentifier("_valid") // "_valid"
|
|
114
|
-
* ```
|
|
115
|
-
*/
|
|
116
|
-
|
|
117
|
-
declare function sanitizeIdentifier(str: string): string; /**
|
|
118
|
-
* Formats a string by replacing placeholders with positional values
|
|
119
|
-
* @param {string} message - The string containing placeholders to be replaced
|
|
120
|
-
* @param {...any} positionals - The values to insert into the placeholders
|
|
121
|
-
* @returns {string} The formatted string
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* ```ts
|
|
125
|
-
* formatStr("Hello %s", "world") // "Hello world"
|
|
126
|
-
* formatStr("Count: %d", 5) // "Count: 5"
|
|
127
|
-
* formatStr("Data: %j", { name: "test" }) // "Data: {"name":"test"}"
|
|
128
|
-
* formatStr("Object: %o", { id: 1 }) // "Object: {"id":1}"
|
|
129
|
-
* formatStr("Escaped %%s", "value") // "Escaped %s"
|
|
130
|
-
* formatStr("Extra args", 1, 2) // "Extra args 1 2"
|
|
131
|
-
* ```
|
|
132
|
-
*/
|
|
133
|
-
|
|
134
|
-
declare function formatStr(message: string, ...positionals: unknown[]): string;
|
|
135
|
-
//#endregion
|
|
136
|
-
export { capitalize as capitalize$1, dedent as dedent$1, dedentRaw as dedentRaw$1, formatStr as formatStr$1, sanitizeIdentifier as sanitizeIdentifier$1, toCamelCase as toCamelCase$1, toKebabCase as toKebabCase$1, toPascalCase as toPascalCase$1, toSnakeCase as toSnakeCase$1 };
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
//#region src/types.d.ts
|
|
2
|
-
/**
|
|
3
|
-
* Whatever type, or Promise of that type
|
|
4
|
-
* @param T - Type
|
|
5
|
-
* @returns T or Promise<T>
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* type A = Awaitable<string>
|
|
10
|
-
* // string | Promise<string>
|
|
11
|
-
* ```
|
|
12
|
-
*/type Awaitable<T> = T | PromiseLike<T>;
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Whatever type, or null
|
|
16
|
-
* @param T - Type
|
|
17
|
-
* @returns T or null
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* type A = Nullable<string>
|
|
22
|
-
* // string | null
|
|
23
|
-
* ```
|
|
24
|
-
*/
|
|
25
|
-
type Nullable<T> = T | null;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Whatever type, null or undefined
|
|
29
|
-
* @param T - Type
|
|
30
|
-
* @returns T, undefined or null
|
|
31
|
-
*
|
|
32
|
-
* @example
|
|
33
|
-
* ```ts
|
|
34
|
-
* type A = Nullish<string>
|
|
35
|
-
* // string | null | undefined
|
|
36
|
-
* ```
|
|
37
|
-
*/
|
|
38
|
-
type Nullish<T> = T | null | undefined;
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Array, or not yet
|
|
42
|
-
*/
|
|
43
|
-
type Arrayable<T> = T | Array<T>;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Infers the element type of an array
|
|
47
|
-
* @param T - Array type
|
|
48
|
-
* @returns The inferred element type
|
|
49
|
-
*
|
|
50
|
-
* @example
|
|
51
|
-
* ```ts
|
|
52
|
-
* type A = ElementOf<string[]>
|
|
53
|
-
* // string
|
|
54
|
-
* ```
|
|
55
|
-
*/
|
|
56
|
-
type ElementOf<T> = T extends (infer E)[] ? E : never;
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Infers the arguments type of a function
|
|
60
|
-
* @param T - Function type
|
|
61
|
-
* @returns The inferred arguments type
|
|
62
|
-
*
|
|
63
|
-
* @example
|
|
64
|
-
* ```ts
|
|
65
|
-
* type A = InferArguments<(a: string, b: number) => void>
|
|
66
|
-
* // [string, number]
|
|
67
|
-
* ```
|
|
68
|
-
*/
|
|
69
|
-
type InferArguments<T> = T extends ((...args: infer A) => any) ? A : never;
|
|
70
|
-
|
|
71
|
-
//#endregion
|
|
72
|
-
export { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish };
|