@salespark/toolkit 2.1.5 → 2.1.6

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
@@ -27,7 +27,7 @@ npm i @salespark/toolkit
27
27
  - **Array utilities**: chunk, uniqBy, deep equality, flatten, groupBy, etc.
28
28
  - **Object utilities**: pick, omit, clean objects, deep merge, etc.
29
29
  - **String utilities**: slugify, template fill, deburr, sanitize, etc.
30
- - **Number utilities**: clamp, round, safe parse, random digits, etc.
30
+ - **Number utilities**: clamp, round, safe parse (locale-aware), random digits, etc.
31
31
  - **Function utilities**: debounce, throttle, 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
@@ -333,13 +333,16 @@ safeParseInt("abc", 10); // 10
333
333
  safeParseInt(3.9); // 3
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
+ **`safeParseFloat(value: unknown, decimals?: number): number`** — Safely parses a value into a number with decimal precision, handling locale separators (commas, dots, spaces) and invalid inputs.
337
337
 
338
338
  ```javascript
339
339
  safeParseFloat("123.45"); // 123.45
340
340
  safeParseFloat("123,45"); // 123.45
341
341
  safeParseFloat("1,234.56"); // 1234.56
342
+ safeParseFloat("1 234,56"); // 1234.56 (spaces as thousands)
343
+ safeParseFloat("1.234,56"); // 1234.56 (dot thousands, comma decimals)
342
344
  safeParseFloat("abc"); // 0
345
+ safeParseFloat(NaN); // 0
343
346
  ```
344
347
 
345
348
  **`randomDigits(length?: number, options?: { charset?: string; noLeadingZero?: boolean }): string`** — Generates a random string of digits with secure randomness when available.
@@ -817,5 +820,5 @@ MIT © [SalesPark](https://salespark.io)
817
820
 
818
821
  ---
819
822
 
820
- _Document version: 7_
821
- _Last update: 08-11-2025_
823
+ _Document version: 8_
824
+ _Last update: 10-12-2025_
package/dist/index.cjs CHANGED
@@ -264,16 +264,35 @@ function safeParseInt(value, defaultValue = 0) {
264
264
  var toInteger = safeParseInt;
265
265
  function safeParseFloat(value, decimals = 6) {
266
266
  try {
267
+ if (typeof value === "number") {
268
+ return isNaN(value) ? 0 : Number(value.toFixed(decimals));
269
+ }
267
270
  if (value === void 0 || value === null || value === "") return 0;
268
- let str = String(value);
271
+ let str = String(value).trim();
272
+ if (!str) return 0;
273
+ str = str.replace(/\s+/g, "");
274
+ let normalized;
269
275
  if (str.includes(",") && str.includes(".")) {
270
- str = str.replace(/,/g, "");
276
+ const lastComma = str.lastIndexOf(",");
277
+ const lastDot = str.lastIndexOf(".");
278
+ if (lastDot > lastComma) {
279
+ normalized = str.replace(/,/g, "");
280
+ } else {
281
+ normalized = str.replace(/\./g, "").replace(",", ".");
282
+ }
271
283
  } else if (str.includes(",")) {
272
- str = str.replace(",", ".");
284
+ const parts = str.split(",");
285
+ if (parts.length === 2 && parts[1].length <= 2) {
286
+ normalized = str.replace(",", ".");
287
+ } else {
288
+ normalized = str.replace(/,/g, "");
289
+ }
290
+ } else {
291
+ normalized = str;
273
292
  }
274
- const num = parseFloat(str);
275
- if (isNaN(num)) return 0;
276
- return parseFloat(num.toFixed(decimals));
293
+ const num = parseFloat(normalized);
294
+ if (!isFinite(num)) return 0;
295
+ return Number(num.toFixed(decimals));
277
296
  } catch {
278
297
  return 0;
279
298
  }