@salespark/toolkit 2.1.9 → 2.1.10

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
@@ -28,7 +28,7 @@ npm i @salespark/toolkit
28
28
  - **Object utilities**: pick, omit, clean objects, deep merge, etc.
29
29
  - **String utilities**: slugify, template fill, deburr, sanitize, capitalize words/sentences.
30
30
  - **Number utilities**: clamp, round, safe arithmetic/comparisons, safe parse (locale-aware), random digits, etc.
31
- - **Function utilities**: debounce, throttle, formatCurrency, parseName, currency conversions, etc.
31
+ - **Function utilities**: debounce, throttle, safeJSONParse, formatCurrency, parseName, currency conversions, etc.
32
32
  - **Boolean utilities**: safe boolean conversion with common representations
33
33
  - **Validation utilities**: IBAN validator (ISO 13616), Portuguese tax ID validator
34
34
  - **Security utilities**: Markdown XSS protection, content sanitization, risk assessment
@@ -607,6 +607,19 @@ addThousandsSpace(1234567);
607
607
  // Result: "1 234 567"
608
608
  ```
609
609
 
610
+ **`safeJSONParse<T>(input: unknown, defaultValue: T): T`** — Safely parses a JSON string or returns the object if already parsed. Falls back to default value on failure.
611
+
612
+ ```javascript
613
+ safeJSONParse('{"key": "value"}', {});
614
+ // Result: { key: "value" }
615
+
616
+ safeJSONParse({ already: "object" }, {});
617
+ // Result: { already: "object" }
618
+
619
+ safeJSONParse("{invalid}", { fallback: true });
620
+ // Result: { fallback: true }
621
+ ```
622
+
610
623
  **`formatCurrency(value: number | string | null | undefined, withoutCurrencySymbol?: boolean, currency?: string, locale?: string): string`** — Formats currency values with configurable currency and locale. Uses modern Intl.NumberFormat with automatic thousands separators, proper decimal handling, and graceful fallback for errors.
611
624
 
612
625
  ```javascript
@@ -895,5 +908,5 @@ MIT © [SalesPark](https://salespark.io)
895
908
 
896
909
  ---
897
910
 
898
- _Document version: 11_
899
- _Last update: 19-12-2025_
911
+ _Document version: 12_
912
+ _Last update: 21-12-2025_
package/dist/index.cjs CHANGED
@@ -450,6 +450,20 @@ var formatDecimalNumber = (value, decimals = 2) => {
450
450
  };
451
451
 
452
452
  // src/utils/func.ts
453
+ function safeJSONParse(input, defaultValue) {
454
+ if (typeof input === "object" && input !== null) {
455
+ return input;
456
+ }
457
+ if (typeof input === "string") {
458
+ try {
459
+ const parsed = JSON.parse(input);
460
+ return parsed;
461
+ } catch {
462
+ return defaultValue;
463
+ }
464
+ }
465
+ return defaultValue;
466
+ }
453
467
  function debounce(fn, wait = 250) {
454
468
  let t;
455
469
  return function(...args) {
@@ -1803,6 +1817,7 @@ exports.removeDiacritics = removeDiacritics;
1803
1817
  exports.round = round;
1804
1818
  exports.safeAdd = safeAdd;
1805
1819
  exports.safeDivide = safeDivide;
1820
+ exports.safeJSONParse = safeJSONParse;
1806
1821
  exports.safeMultiply = safeMultiply;
1807
1822
  exports.safeParseFloat = safeParseFloat;
1808
1823
  exports.safeParseInt = safeParseInt;