@salespark/toolkit 2.1.0 → 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 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('<iframe src="evil.com"></iframe><div onclick="bad()">test</div>');
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('<p>Hello <strong>World</strong></p>');
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('<script>alert(1)</script>Safe text');
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
- { type: "scriptTags", severity: "critical", description: "Script injection detected" }
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"); // false (invalid check digit)
659
- isPTTaxId("123 456 789"); // false (spaces stripped automatically)
660
- isPTTaxId("513183504"); // true (valid NIF)
661
- isPTTaxId("423456789"); // false (invalid prefix - 4 not allowed)
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"); // true (valid Dutch IBAN)
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"); // true (valid German IBAN)
676
- isValidIBAN("NL91ABNA0417164301"); // false (invalid checksum)
677
- isValidIBAN("XX1234567890"); // false (invalid country code)
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
@@ -135,6 +135,33 @@ function flattenDepth(array, depth = 1, options) {
135
135
  return flattenDepthBase(array, depth, predicate, isStrict);
136
136
  }
137
137
 
138
+ // src/utils/bool.ts
139
+ var toBool = (value, def = false) => {
140
+ try {
141
+ if (value === null || value === void 0) return def;
142
+ if (typeof value === "boolean") return value;
143
+ if (typeof value === "number") return value === 1;
144
+ if (typeof value === "string") {
145
+ switch (value.toLowerCase().trim()) {
146
+ case "true":
147
+ case "yes":
148
+ case "1":
149
+ return true;
150
+ case "false":
151
+ case "no":
152
+ case "0":
153
+ return false;
154
+ default:
155
+ return def;
156
+ }
157
+ }
158
+ return def;
159
+ } catch {
160
+ return false;
161
+ }
162
+ };
163
+ var parseToBool = toBool;
164
+
138
165
  // src/utils/object.ts
139
166
  function pick(obj, keys) {
140
167
  const out = {};
@@ -283,6 +310,30 @@ function randomDigits(length = 6, options) {
283
310
  return out;
284
311
  }
285
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
+ };
286
337
 
287
338
  // src/utils/func.ts
288
339
  function debounce(fn, wait = 250) {
@@ -1588,6 +1639,7 @@ exports.flattenDepthBase = flattenDepthBase;
1588
1639
  exports.flattenOnce = flattenOnce;
1589
1640
  exports.formatBytes = formatBytes;
1590
1641
  exports.formatCurrency = formatCurrency;
1642
+ exports.formatDecimalNumber = formatDecimalNumber;
1591
1643
  exports.getStringSimilarity = getStringSimilarity;
1592
1644
  exports.groupBy = groupBy;
1593
1645
  exports.hasNilOrEmpty = hasNilOrEmpty;
@@ -1618,6 +1670,7 @@ exports.objectToString = objectToString;
1618
1670
  exports.omit = omit;
1619
1671
  exports.otp = otp;
1620
1672
  exports.parseName = parseName;
1673
+ exports.parseToBool = parseToBool;
1621
1674
  exports.parseToNumber = parseToNumber;
1622
1675
  exports.pick = pick;
1623
1676
  exports.pluck = pluck;
@@ -1634,6 +1687,7 @@ exports.sortBy = sortBy;
1634
1687
  exports.stringSimilarity = stringSimilarity;
1635
1688
  exports.symbolToCurrency = symbolToCurrency;
1636
1689
  exports.throttle = throttle;
1690
+ exports.toBool = toBool;
1637
1691
  exports.toInteger = toInteger;
1638
1692
  exports.toNumber = toNumber;
1639
1693
  exports.uniq = uniq;