@salespark/toolkit 2.1.2 → 2.1.3
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 +36 -24
- package/dist/index.cjs +7 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +18 -17
- package/dist/index.d.ts +18 -17
- package/dist/index.js +7 -6
- package/dist/index.js.map +1 -1
- package/package.json +1 -12
package/README.md
CHANGED
|
@@ -311,39 +311,35 @@ sanitize("<script>alert('hack')</script>Hello World!", 20);
|
|
|
311
311
|
|
|
312
312
|
### 🔢 Number Utilities
|
|
313
313
|
|
|
314
|
-
**`clamp(n: number, min: number, max: number): number`** — Restricts a number within min and max bounds.
|
|
314
|
+
**`clamp(n: number, min: number, max: number): number`** — Restricts a number to be within the min and max bounds.
|
|
315
315
|
|
|
316
316
|
```javascript
|
|
317
|
-
clamp(
|
|
318
|
-
|
|
317
|
+
clamp(10, 0, 5); // 5
|
|
318
|
+
clamp(-5, 0, 10); // 0
|
|
319
319
|
```
|
|
320
320
|
|
|
321
|
-
**`round(n: number, decimals?: number): number`** — Rounds a number to fixed
|
|
321
|
+
**`round(n: number, decimals?: number): number`** — Rounds a number to a fixed number of decimals without floating point surprises.
|
|
322
322
|
|
|
323
323
|
```javascript
|
|
324
|
-
round(
|
|
325
|
-
//
|
|
324
|
+
round(1.2345, 2); // 1.23
|
|
325
|
+
round(1.235, 2); // 1.24
|
|
326
326
|
```
|
|
327
327
|
|
|
328
|
-
**`
|
|
328
|
+
**`safeParseInt(value: unknown, defaultValue?: number): number`** — Safely converts a value to an integer with fallback.
|
|
329
329
|
|
|
330
330
|
```javascript
|
|
331
|
-
|
|
332
|
-
//
|
|
331
|
+
safeParseInt("42"); // 42
|
|
332
|
+
safeParseInt("abc", 10); // 10
|
|
333
|
+
safeParseInt(3.9); // 3
|
|
333
334
|
```
|
|
334
335
|
|
|
335
|
-
**`
|
|
336
|
+
**`safeParseFloat(value: unknown, decimals?: number): number`** — Safely parses a value into a number with decimal precision and comma/dot normalization.
|
|
336
337
|
|
|
337
338
|
```javascript
|
|
338
|
-
|
|
339
|
-
//
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
**`toNumber(value: unknown, decimals?: number): number`** — Safely parses a value into a number with optional decimal precision.
|
|
343
|
-
|
|
344
|
-
```javascript
|
|
345
|
-
toNumber("123,45", 2);
|
|
346
|
-
// Result: 123.45
|
|
339
|
+
safeParseFloat("123.45"); // 123.45
|
|
340
|
+
safeParseFloat("123,45"); // 123.45
|
|
341
|
+
safeParseFloat("1,234.56"); // 1234.56
|
|
342
|
+
safeParseFloat("abc"); // 0
|
|
347
343
|
```
|
|
348
344
|
|
|
349
345
|
**`randomDigits(length?: number, options?: { charset?: string; noLeadingZero?: boolean }): string`** — Generates a random string of digits with secure randomness when available.
|
|
@@ -356,12 +352,15 @@ randomDigits(6);
|
|
|
356
352
|
**`randomDigits(length?: number, options?: { charset?: string; noLeadingZero?: boolean }): string`** — Generates a random string of digits with secure randomness when available.
|
|
357
353
|
|
|
358
354
|
```javascript
|
|
359
|
-
randomDigits(6);
|
|
360
|
-
|
|
355
|
+
randomDigits(6); // "847293"
|
|
356
|
+
randomDigits(4, { noLeadingZero: true }); // "3847" (no leading zero)
|
|
357
|
+
randomDigits(6, { charset: "ABCDEF0123456789" }); // "3F2A9D" (hex)
|
|
361
358
|
```
|
|
362
359
|
|
|
363
360
|
**`formatDecimalNumber(value: number | string | null | undefined, decimals?: number): string`** — Formats a number with specified decimal places, intelligently handling European (`1.234,56`) and US (`1,234.56`) number formats.
|
|
364
361
|
|
|
362
|
+
**`formatDecimalNumber(value: number | string | null | undefined, decimals?: number): string`** — Formats a number with specified decimal places, intelligently handling European (`1.234,56`) and US (`1,234.56`) number formats.
|
|
363
|
+
|
|
365
364
|
```javascript
|
|
366
365
|
formatDecimalNumber(1234.5678); // "1234.57"
|
|
367
366
|
formatDecimalNumber("1.234,56", 2); // "1234.56" (European format)
|
|
@@ -371,6 +370,13 @@ formatDecimalNumber(null, 2); // "0.00"
|
|
|
371
370
|
formatDecimalNumber("invalid", 2); // "0.00"
|
|
372
371
|
```
|
|
373
372
|
|
|
373
|
+
#### 🗑️ Deprecated (Number Utilities)
|
|
374
|
+
|
|
375
|
+
- **`toInteger`** → Use `safeParseInt` instead
|
|
376
|
+
- **`toNumber`** → Use `safeParseFloat` instead
|
|
377
|
+
- **`parseToNumber`** → Use `safeParseFloat` instead
|
|
378
|
+
- **`otp`** → Use `randomDigits` instead
|
|
379
|
+
|
|
374
380
|
### ✅ Boolean Utilities
|
|
375
381
|
|
|
376
382
|
**`toBool(value: unknown, def?: boolean): boolean`** — Converts a value to boolean, supporting common string/number representations.
|
|
@@ -443,14 +449,20 @@ isNilText("undefined");
|
|
|
443
449
|
// Result: true
|
|
444
450
|
```
|
|
445
451
|
|
|
446
|
-
**`isNilOrEmpty(value: unknown): boolean`** — Checks if value is null/undefined or an empty string.
|
|
452
|
+
**`isNilOrEmpty(value: unknown): boolean`** — Checks if value is null/undefined or an empty string (trimmed).
|
|
447
453
|
|
|
448
454
|
```javascript
|
|
449
455
|
isNilOrEmpty("");
|
|
450
456
|
// Result: true
|
|
451
457
|
|
|
458
|
+
isNilOrEmpty(" ");
|
|
459
|
+
// Result: true
|
|
460
|
+
|
|
452
461
|
isNilOrEmpty(null);
|
|
453
462
|
// Result: true
|
|
463
|
+
|
|
464
|
+
isNilOrEmpty(undefined);
|
|
465
|
+
// Result: true
|
|
454
466
|
```
|
|
455
467
|
|
|
456
468
|
**`hasNilOrEmpty(array: unknown): boolean`** — Checks if any element in array is nil or empty.
|
|
@@ -803,5 +815,5 @@ MIT © [SalesPark](https://salespark.io)
|
|
|
803
815
|
|
|
804
816
|
---
|
|
805
817
|
|
|
806
|
-
_Document version:
|
|
807
|
-
_Last update:
|
|
818
|
+
_Document version: 6_
|
|
819
|
+
_Last update: 29-10-2025_
|
package/dist/index.cjs
CHANGED
|
@@ -253,7 +253,7 @@ function round(n, decimals = 0) {
|
|
|
253
253
|
const f = Math.pow(10, decimals);
|
|
254
254
|
return Math.round((n + Number.EPSILON) * f) / f;
|
|
255
255
|
}
|
|
256
|
-
function
|
|
256
|
+
function safeParseInt(value, defaultValue = 0) {
|
|
257
257
|
try {
|
|
258
258
|
const safeValue = parseInt(String(value), 10);
|
|
259
259
|
return isNaN(safeValue) ? defaultValue : safeValue;
|
|
@@ -261,8 +261,8 @@ function toInteger(value, defaultValue = 0) {
|
|
|
261
261
|
return defaultValue;
|
|
262
262
|
}
|
|
263
263
|
}
|
|
264
|
-
var
|
|
265
|
-
function
|
|
264
|
+
var toInteger = safeParseInt;
|
|
265
|
+
function safeParseFloat(value, decimals = 6) {
|
|
266
266
|
try {
|
|
267
267
|
if (value === void 0 || value === null || value === "") return 0;
|
|
268
268
|
let str = String(value);
|
|
@@ -278,7 +278,8 @@ function toNumber(value, decimals = 6) {
|
|
|
278
278
|
return 0;
|
|
279
279
|
}
|
|
280
280
|
}
|
|
281
|
-
var
|
|
281
|
+
var toNumber = safeParseFloat;
|
|
282
|
+
var parseToNumber = safeParseFloat;
|
|
282
283
|
function secureRandomIndex(max) {
|
|
283
284
|
if (max <= 0) return 0;
|
|
284
285
|
const g = globalThis;
|
|
@@ -372,7 +373,7 @@ var isNilText = (value) => {
|
|
|
372
373
|
};
|
|
373
374
|
var isNilOrEmpty = (value) => {
|
|
374
375
|
try {
|
|
375
|
-
return isNil(value) || value === "";
|
|
376
|
+
return isNil(value) || typeof value === "string" && value?.trim() === "";
|
|
376
377
|
} catch {
|
|
377
378
|
return true;
|
|
378
379
|
}
|
|
@@ -1678,6 +1679,7 @@ exports.pushAll = pushAll;
|
|
|
1678
1679
|
exports.randomDigits = randomDigits;
|
|
1679
1680
|
exports.removeDiacritics = removeDiacritics;
|
|
1680
1681
|
exports.round = round;
|
|
1682
|
+
exports.safeParseFloat = safeParseFloat;
|
|
1681
1683
|
exports.safeParseInt = safeParseInt;
|
|
1682
1684
|
exports.sanitize = sanitize;
|
|
1683
1685
|
exports.sanitizeMarkdown = sanitizeMarkdown;
|