@salespark/toolkit 2.1.1 → 2.1.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 +42 -18
- package/dist/index.cjs +25 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +14 -1
- package/dist/index.d.ts +14 -1
- package/dist/index.js +25 -1
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -353,6 +353,24 @@ randomDigits(6);
|
|
|
353
353
|
// Result: "482751" (random)
|
|
354
354
|
```
|
|
355
355
|
|
|
356
|
+
**`randomDigits(length?: number, options?: { charset?: string; noLeadingZero?: boolean }): string`** — Generates a random string of digits with secure randomness when available.
|
|
357
|
+
|
|
358
|
+
```javascript
|
|
359
|
+
randomDigits(6);
|
|
360
|
+
// Result: "482751" (random)
|
|
361
|
+
```
|
|
362
|
+
|
|
363
|
+
**`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
|
+
|
|
365
|
+
```javascript
|
|
366
|
+
formatDecimalNumber(1234.5678); // "1234.57"
|
|
367
|
+
formatDecimalNumber("1.234,56", 2); // "1234.56" (European format)
|
|
368
|
+
formatDecimalNumber("1,234.56", 2); // "1234.56" (US format)
|
|
369
|
+
formatDecimalNumber("1234,56", 3); // "1234.560"
|
|
370
|
+
formatDecimalNumber(null, 2); // "0.00"
|
|
371
|
+
formatDecimalNumber("invalid", 2); // "0.00"
|
|
372
|
+
```
|
|
373
|
+
|
|
356
374
|
### ✅ Boolean Utilities
|
|
357
375
|
|
|
358
376
|
**`toBool(value: unknown, def?: boolean): boolean`** — Converts a value to boolean, supporting common string/number representations.
|
|
@@ -596,15 +614,17 @@ checkMarkdownSecurity("# Hello World\n\nThis is **bold** text.");
|
|
|
596
614
|
|
|
597
615
|
// Dangerous content with script
|
|
598
616
|
checkMarkdownSecurity('<script>alert("xss")</script>');
|
|
599
|
-
// Result: {
|
|
600
|
-
// isValid: false,
|
|
601
|
-
// text: "",
|
|
602
|
-
// risks: [{ type: "scriptTags", severity: "critical", description: "..." }],
|
|
603
|
-
// sanitized: true
|
|
617
|
+
// Result: {
|
|
618
|
+
// isValid: false,
|
|
619
|
+
// text: "",
|
|
620
|
+
// risks: [{ type: "scriptTags", severity: "critical", description: "..." }],
|
|
621
|
+
// sanitized: true
|
|
604
622
|
// }
|
|
605
623
|
|
|
606
624
|
// Content with multiple threats
|
|
607
|
-
checkMarkdownSecurity(
|
|
625
|
+
checkMarkdownSecurity(
|
|
626
|
+
'<iframe src="evil.com"></iframe><div onclick="bad()">test</div>'
|
|
627
|
+
);
|
|
608
628
|
// Result: Multiple risks detected, sorted by severity
|
|
609
629
|
```
|
|
610
630
|
|
|
@@ -613,13 +633,13 @@ checkMarkdownSecurity('<iframe src="evil.com"></iframe><div onclick="bad()">test
|
|
|
613
633
|
```typescript
|
|
614
634
|
import { sanitizeMarkdown } from "@salespark/toolkit";
|
|
615
635
|
|
|
616
|
-
sanitizeMarkdown(
|
|
636
|
+
sanitizeMarkdown("<p>Hello <strong>World</strong></p>");
|
|
617
637
|
// Result: "Hello World"
|
|
618
638
|
|
|
619
639
|
sanitizeMarkdown('javascript:alert("xss")');
|
|
620
640
|
// Result: "alert(\"xss\")"
|
|
621
641
|
|
|
622
|
-
sanitizeMarkdown(
|
|
642
|
+
sanitizeMarkdown("<script>alert(1)</script>Safe text");
|
|
623
643
|
// Result: "Safe text"
|
|
624
644
|
```
|
|
625
645
|
|
|
@@ -629,7 +649,11 @@ sanitizeMarkdown('<script>alert(1)</script>Safe text');
|
|
|
629
649
|
import { assessSecurityRisks } from "@salespark/toolkit";
|
|
630
650
|
|
|
631
651
|
const risks = [
|
|
632
|
-
{
|
|
652
|
+
{
|
|
653
|
+
type: "scriptTags",
|
|
654
|
+
severity: "critical",
|
|
655
|
+
description: "Script injection detected",
|
|
656
|
+
},
|
|
633
657
|
];
|
|
634
658
|
|
|
635
659
|
assessSecurityRisks(risks);
|
|
@@ -655,10 +679,10 @@ assessSecurityRisks([]);
|
|
|
655
679
|
```typescript
|
|
656
680
|
import { isPTTaxId } from "@salespark/toolkit";
|
|
657
681
|
|
|
658
|
-
isPTTaxId("123456789");
|
|
659
|
-
isPTTaxId("123 456 789");
|
|
660
|
-
isPTTaxId("513183504");
|
|
661
|
-
isPTTaxId("423456789");
|
|
682
|
+
isPTTaxId("123456789"); // false (invalid check digit)
|
|
683
|
+
isPTTaxId("123 456 789"); // false (spaces stripped automatically)
|
|
684
|
+
isPTTaxId("513183504"); // true (valid NIF)
|
|
685
|
+
isPTTaxId("423456789"); // false (invalid prefix - 4 not allowed)
|
|
662
686
|
|
|
663
687
|
// Deprecated alias (use isPTTaxId instead)
|
|
664
688
|
// isValidPTTaxId("513183504"); // true
|
|
@@ -669,12 +693,12 @@ isPTTaxId("423456789"); // false (invalid prefix - 4 not allowed)
|
|
|
669
693
|
```typescript
|
|
670
694
|
import { isValidIBAN } from "@salespark/toolkit";
|
|
671
695
|
|
|
672
|
-
isValidIBAN("NL91ABNA0417164300");
|
|
696
|
+
isValidIBAN("NL91ABNA0417164300"); // true (valid Dutch IBAN)
|
|
673
697
|
isValidIBAN("NL91 ABNA 0417 1643 00"); // true (spaces stripped automatically)
|
|
674
698
|
isValidIBAN("GB29NWBK60161331926819"); // true (valid UK IBAN)
|
|
675
|
-
isValidIBAN("DE89370400440532013000");
|
|
676
|
-
isValidIBAN("NL91ABNA0417164301");
|
|
677
|
-
isValidIBAN("XX1234567890");
|
|
699
|
+
isValidIBAN("DE89370400440532013000"); // true (valid German IBAN)
|
|
700
|
+
isValidIBAN("NL91ABNA0417164301"); // false (invalid checksum)
|
|
701
|
+
isValidIBAN("XX1234567890"); // false (invalid country code)
|
|
678
702
|
```
|
|
679
703
|
|
|
680
704
|
### 🌐 Environment Detection
|
|
@@ -710,7 +734,7 @@ The following functions are deprecated but maintained for backward compatibility
|
|
|
710
734
|
### ⚡ Function Utilities (Deprecated)
|
|
711
735
|
|
|
712
736
|
- `isNullOrUndefined` — Use `isNil` instead.
|
|
713
|
-
- `isNullOrUndefinedTextInc` — Use `isNilText` instead.
|
|
737
|
+
- `isNullOrUndefinedTextInc` — Use `isNilText` instead.
|
|
714
738
|
- `isNullUndefinedOrEmpty` — Use `isNilOrEmpty` instead.
|
|
715
739
|
- `isNullOrUndefinedInArray` — Use `hasNilOrEmpty` instead.
|
|
716
740
|
- `isNullOrUndefinedEmptyOrZero` — Use `isNilEmptyOrZeroLen` instead.
|
package/dist/index.cjs
CHANGED
|
@@ -310,6 +310,30 @@ function randomDigits(length = 6, options) {
|
|
|
310
310
|
return out;
|
|
311
311
|
}
|
|
312
312
|
var otp = randomDigits;
|
|
313
|
+
var formatDecimalNumber = (value, decimals = 2) => {
|
|
314
|
+
try {
|
|
315
|
+
let processedValue = value ?? 0;
|
|
316
|
+
if (typeof processedValue === "string") {
|
|
317
|
+
const trimmed = processedValue.trim();
|
|
318
|
+
if (trimmed.includes(",") && trimmed.includes(".")) {
|
|
319
|
+
const lastComma = trimmed.lastIndexOf(",");
|
|
320
|
+
const lastDot = trimmed.lastIndexOf(".");
|
|
321
|
+
processedValue = lastComma > lastDot ? trimmed.replace(/\./g, "").replace(",", ".") : trimmed.replace(/,/g, "");
|
|
322
|
+
} else if (trimmed.includes(",")) {
|
|
323
|
+
processedValue = trimmed.replace(/,/g, ".");
|
|
324
|
+
} else {
|
|
325
|
+
processedValue = trimmed;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
const numValue = parseFloat(String(processedValue));
|
|
329
|
+
if (isNaN(numValue)) {
|
|
330
|
+
return 0 .toFixed(Math.max(0, Math.floor(decimals)));
|
|
331
|
+
}
|
|
332
|
+
return numValue.toFixed(Math.max(0, Math.floor(decimals)));
|
|
333
|
+
} catch (error) {
|
|
334
|
+
return 0 .toFixed(Math.max(0, Math.floor(decimals)));
|
|
335
|
+
}
|
|
336
|
+
};
|
|
313
337
|
|
|
314
338
|
// src/utils/func.ts
|
|
315
339
|
function debounce(fn, wait = 250) {
|
|
@@ -1615,6 +1639,7 @@ exports.flattenDepthBase = flattenDepthBase;
|
|
|
1615
1639
|
exports.flattenOnce = flattenOnce;
|
|
1616
1640
|
exports.formatBytes = formatBytes;
|
|
1617
1641
|
exports.formatCurrency = formatCurrency;
|
|
1642
|
+
exports.formatDecimalNumber = formatDecimalNumber;
|
|
1618
1643
|
exports.getStringSimilarity = getStringSimilarity;
|
|
1619
1644
|
exports.groupBy = groupBy;
|
|
1620
1645
|
exports.hasNilOrEmpty = hasNilOrEmpty;
|