@salespark/toolkit 2.1.4 → 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
  }
@@ -389,12 +408,11 @@ var hasNilOrEmpty = (array) => {
389
408
  };
390
409
  var isNilEmptyOrZeroLength = (value) => {
391
410
  try {
392
- if (isNil(value) || value === "") return true;
393
- if (typeof value === "object" && value !== null && "length" in value) {
394
- const length = value.length;
395
- return typeof length === "number" && length === 0;
396
- }
397
- return false;
411
+ if (isNil(value)) return true;
412
+ if (value === "") return true;
413
+ if (typeof value === "string" && value.length > 0) return false;
414
+ if (Array.isArray(value) && value.length > 0) return false;
415
+ return true;
398
416
  } catch {
399
417
  return true;
400
418
  }
@@ -403,11 +421,8 @@ var isNilEmptyOrZeroLen = isNilEmptyOrZeroLength;
403
421
  var isNilOrZeroLength = (value) => {
404
422
  try {
405
423
  if (isNil(value)) return true;
406
- if (typeof value === "object" && value !== null && "length" in value) {
407
- const length = value.length;
408
- return typeof length === "number" && length === 0;
409
- }
410
- return false;
424
+ if (Array.isArray(value) && value.length > 0) return false;
425
+ return true;
411
426
  } catch {
412
427
  return true;
413
428
  }