@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/dist/index.d.cts CHANGED
@@ -328,7 +328,8 @@ declare const toInteger: typeof safeParseInt;
328
328
  * @param {Number} decimals - Number of decimal places
329
329
  * History:
330
330
  * 21-08-2025: Created
331
- * * 29-10-2025: Renamed from toNumber to safeParseFloat
331
+ * 29-10-2025: Renamed from toNumber to safeParseFloat
332
+ * 01-12-2025: Fixed space-separated thousands handling and improved number parsing logic
332
333
  ****************************************************/
333
334
  declare function safeParseFloat(value: unknown, decimals?: number): number;
334
335
  /**
package/dist/index.d.ts CHANGED
@@ -328,7 +328,8 @@ declare const toInteger: typeof safeParseInt;
328
328
  * @param {Number} decimals - Number of decimal places
329
329
  * History:
330
330
  * 21-08-2025: Created
331
- * * 29-10-2025: Renamed from toNumber to safeParseFloat
331
+ * 29-10-2025: Renamed from toNumber to safeParseFloat
332
+ * 01-12-2025: Fixed space-separated thousands handling and improved number parsing logic
332
333
  ****************************************************/
333
334
  declare function safeParseFloat(value: unknown, decimals?: number): number;
334
335
  /**
package/dist/index.js CHANGED
@@ -262,16 +262,35 @@ function safeParseInt(value, defaultValue = 0) {
262
262
  var toInteger = safeParseInt;
263
263
  function safeParseFloat(value, decimals = 6) {
264
264
  try {
265
+ if (typeof value === "number") {
266
+ return isNaN(value) ? 0 : Number(value.toFixed(decimals));
267
+ }
265
268
  if (value === void 0 || value === null || value === "") return 0;
266
- let str = String(value);
269
+ let str = String(value).trim();
270
+ if (!str) return 0;
271
+ str = str.replace(/\s+/g, "");
272
+ let normalized;
267
273
  if (str.includes(",") && str.includes(".")) {
268
- str = str.replace(/,/g, "");
274
+ const lastComma = str.lastIndexOf(",");
275
+ const lastDot = str.lastIndexOf(".");
276
+ if (lastDot > lastComma) {
277
+ normalized = str.replace(/,/g, "");
278
+ } else {
279
+ normalized = str.replace(/\./g, "").replace(",", ".");
280
+ }
269
281
  } else if (str.includes(",")) {
270
- str = str.replace(",", ".");
282
+ const parts = str.split(",");
283
+ if (parts.length === 2 && parts[1].length <= 2) {
284
+ normalized = str.replace(",", ".");
285
+ } else {
286
+ normalized = str.replace(/,/g, "");
287
+ }
288
+ } else {
289
+ normalized = str;
271
290
  }
272
- const num = parseFloat(str);
273
- if (isNaN(num)) return 0;
274
- return parseFloat(num.toFixed(decimals));
291
+ const num = parseFloat(normalized);
292
+ if (!isFinite(num)) return 0;
293
+ return Number(num.toFixed(decimals));
275
294
  } catch {
276
295
  return 0;
277
296
  }