@salespark/toolkit 2.1.9 → 2.1.11

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,21 @@ var formatDecimalNumber = (value, decimals = 2) => {
450
450
  };
451
451
 
452
452
  // src/utils/func.ts
453
+ function safeJSONParse(input, defaultValue) {
454
+ const def = isNilOrEmpty(defaultValue) === false ? defaultValue : {};
455
+ if (typeof input === "object" && input !== null) {
456
+ return input;
457
+ }
458
+ if (typeof input === "string") {
459
+ try {
460
+ const parsed = JSON.parse(input);
461
+ return parsed;
462
+ } catch {
463
+ return def;
464
+ }
465
+ }
466
+ return def;
467
+ }
453
468
  function debounce(fn, wait = 250) {
454
469
  let t;
455
470
  return function(...args) {
@@ -1803,6 +1818,7 @@ exports.removeDiacritics = removeDiacritics;
1803
1818
  exports.round = round;
1804
1819
  exports.safeAdd = safeAdd;
1805
1820
  exports.safeDivide = safeDivide;
1821
+ exports.safeJSONParse = safeJSONParse;
1806
1822
  exports.safeMultiply = safeMultiply;
1807
1823
  exports.safeParseFloat = safeParseFloat;
1808
1824
  exports.safeParseInt = safeParseInt;