@luxass/utils 2.0.0 → 2.1.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/index.d.ts +21 -2
- package/dist/index.js +25 -3
- package/dist/number.js +1 -1
- package/dist/{string-bY1lSRQs.js → string-pQApOGKP.js} +46 -1
- package/dist/{string.d-BSY3kSBV.d.ts → string.d-B2ppMtlR.d.ts} +19 -2
- package/dist/string.d.ts +2 -2
- package/dist/string.js +2 -2
- package/package.json +1 -1
- /package/dist/{number-Bfr1z0Nr.js → number-BS9T5WGO.js} +0 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import { isNotNull$1 as isNotNull, isNotNullish$1 as isNotNullish, isNotUndefined$1 as isNotUndefined, isTruthy$1 as isTruthy } from "./guards.d-n1BzCANy.js";
|
|
2
2
|
import { clamp$1 as clamp } from "./number.d-C2Xuq3Is.js";
|
|
3
|
-
import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string.d-
|
|
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.d-B2ppMtlR.js";
|
|
4
4
|
import { Arrayable, Awaitable, ElementOf, InferArguments, Nullable, Nullish } from "./types.d-BcKIY6l3.js";
|
|
5
|
-
|
|
5
|
+
|
|
6
|
+
//#region src/common.d.ts
|
|
7
|
+
declare class InvariantError extends Error {
|
|
8
|
+
readonly message: string;
|
|
9
|
+
constructor(message: string, ...positionals: any[]);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
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
|
+
*/
|
|
21
|
+
declare function invariant(predicate: unknown, message: string, ...positionals: unknown[]): asserts predicate;
|
|
22
|
+
|
|
23
|
+
//#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 };
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,27 @@
|
|
|
1
1
|
import { isNotNull, isNotNullish, isNotUndefined, isTruthy } from "./guards-O1HGJraI.js";
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
2
|
+
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-pQApOGKP.js";
|
|
3
|
+
import { clamp } from "./number-BS9T5WGO.js";
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
//#region src/common.ts
|
|
6
|
+
var InvariantError = class extends Error {
|
|
7
|
+
constructor(message, ...positionals) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.message = message;
|
|
10
|
+
this.message = formatStr(message, ...positionals);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Asserts that a condition is truthy. If the condition is falsy, throws an InvariantError.
|
|
15
|
+
* This function is useful for ensuring invariants and preconditions in code.
|
|
16
|
+
*
|
|
17
|
+
* @param {unknown} predicate - The condition to check
|
|
18
|
+
* @param {string} message - Error message to display if the condition fails
|
|
19
|
+
* @param {unknown[]} positionals - Values to substitute into the error message using formatStr
|
|
20
|
+
* @throws {InvariantError} Throws when the predicate is falsy
|
|
21
|
+
*/
|
|
22
|
+
function invariant(predicate, message, ...positionals) {
|
|
23
|
+
if (!predicate) throw new InvariantError(message, ...positionals);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { InvariantError, capitalize, clamp, dedent, dedentRaw, formatStr, invariant, isNotNull, isNotNullish, isNotUndefined, isTruthy, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/number.js
CHANGED
|
@@ -136,6 +136,51 @@ function sanitizeIdentifier(str) {
|
|
|
136
136
|
const cleaned = str.replace(/[^\w$]/g, "");
|
|
137
137
|
return /^[A-Z_$]/i.test(cleaned) ? cleaned : `_${cleaned}`;
|
|
138
138
|
}
|
|
139
|
+
const POSITION_REGEX = /(%?)(%([sdijo]))/g;
|
|
140
|
+
/** @internal */
|
|
141
|
+
function serializePositional(positional, flag) {
|
|
142
|
+
if (flag === "s") return positional;
|
|
143
|
+
if (flag === "d" || flag === "i") return Number(positional);
|
|
144
|
+
if (flag === "j") return JSON.stringify(positional);
|
|
145
|
+
if (flag === "o") {
|
|
146
|
+
if (typeof positional === "string") return positional;
|
|
147
|
+
const json = JSON.stringify(positional);
|
|
148
|
+
if (json === "{}" || json === "[]" || /^\[object .+?\]$/.test(json)) return positional;
|
|
149
|
+
return json;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
/**
|
|
153
|
+
* Formats a string by replacing placeholders with positional values
|
|
154
|
+
* @param {string} message - The string containing placeholders to be replaced
|
|
155
|
+
* @param {...any} positionals - The values to insert into the placeholders
|
|
156
|
+
* @returns {string} The formatted string
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```ts
|
|
160
|
+
* formatStr("Hello %s", "world") // "Hello world"
|
|
161
|
+
* formatStr("Count: %d", 5) // "Count: 5"
|
|
162
|
+
* formatStr("Data: %j", { name: "test" }) // "Data: {"name":"test"}"
|
|
163
|
+
* formatStr("Object: %o", { id: 1 }) // "Object: {"id":1}"
|
|
164
|
+
* formatStr("Escaped %%s", "value") // "Escaped %s"
|
|
165
|
+
* formatStr("Extra args", 1, 2) // "Extra args 1 2"
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
function formatStr(message, ...positionals) {
|
|
169
|
+
if (!positionals.length) return message;
|
|
170
|
+
let positionalIndex = 0;
|
|
171
|
+
let formattedMessage = message.replace(POSITION_REGEX, (match, isEscaped, _, flag) => {
|
|
172
|
+
const positional = positionals[positionalIndex];
|
|
173
|
+
const value = serializePositional(positional, flag);
|
|
174
|
+
if (!isEscaped) {
|
|
175
|
+
positionalIndex++;
|
|
176
|
+
return value;
|
|
177
|
+
}
|
|
178
|
+
return match;
|
|
179
|
+
});
|
|
180
|
+
if (positionalIndex < positionals.length) formattedMessage += ` ${positionals.slice(positionalIndex).join(" ")}`;
|
|
181
|
+
formattedMessage = formattedMessage.replace(/%{2}/g, "%");
|
|
182
|
+
return formattedMessage;
|
|
183
|
+
}
|
|
139
184
|
|
|
140
185
|
//#endregion
|
|
141
|
-
export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
186
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
@@ -114,6 +114,23 @@ declare function dedentRaw(strings: TemplateStringsArray, ...values: unknown[]):
|
|
|
114
114
|
* ```
|
|
115
115
|
*/
|
|
116
116
|
|
|
117
|
-
declare function sanitizeIdentifier(str: string): string;
|
|
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;
|
|
118
135
|
//#endregion
|
|
119
|
-
export { capitalize as capitalize$1, dedent as dedent$1, dedentRaw as dedentRaw$1, sanitizeIdentifier as sanitizeIdentifier$1, toCamelCase as toCamelCase$1, toKebabCase as toKebabCase$1, toPascalCase as toPascalCase$1, toSnakeCase as toSnakeCase$1 };
|
|
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 };
|
package/dist/string.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { capitalize$1 as capitalize, dedent$1 as dedent, dedentRaw$1 as dedentRaw, sanitizeIdentifier$1 as sanitizeIdentifier, toCamelCase$1 as toCamelCase, toKebabCase$1 as toKebabCase, toPascalCase$1 as toPascalCase, toSnakeCase$1 as toSnakeCase } from "./string.d-
|
|
2
|
-
export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
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.d-B2ppMtlR.js";
|
|
2
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/dist/string.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-
|
|
1
|
+
import { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase } from "./string-pQApOGKP.js";
|
|
2
2
|
|
|
3
|
-
export { capitalize, dedent, dedentRaw, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
|
3
|
+
export { capitalize, dedent, dedentRaw, formatStr, sanitizeIdentifier, toCamelCase, toKebabCase, toPascalCase, toSnakeCase };
|
package/package.json
CHANGED
|
File without changes
|