@jsuites/utils 6.0.3 → 6.0.4

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.
Files changed (2) hide show
  1. package/dist/index.js +77 -57
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -285,7 +285,7 @@
285
285
  // Number
286
286
  fraction: [ '#{0,1}.*?\\?+\\/[0-9?]+' ],
287
287
  // Currency tokens
288
- currency: [ '#(.{1})##0?(.{1}0+)?( ?;(.*)?)?' ],
288
+ currency: [ '#(.{1})##0?(.{1}[0#]+)?( ?;(.*)?)?' ],
289
289
  // Scientific
290
290
  scientific: [ '[0#]+([.,]{1}0*#*)?E{1}\\+0+' ],
291
291
  // Percentage
@@ -1736,7 +1736,7 @@
1736
1736
  this.values[this.index] = '';
1737
1737
  }
1738
1738
  },
1739
- '#(.{1})##0?(.{1}0+)?( ?;(.*)?)?': function(v) {
1739
+ '#(.{1})##0?(.{1}[0#]+)?( ?;(.*)?)?': function(v) {
1740
1740
  // Process first the number
1741
1741
  parseMethods['[0#]+([.,]{1}0*#*)?'].call(this, v, true);
1742
1742
  // Create the separators
@@ -2369,77 +2369,89 @@
2369
2369
  const adjustNumberOfDecimalPlaces = function(config, value) {
2370
2370
  let temp = value;
2371
2371
  let mask = config.mask;
2372
- let expo;
2373
2372
 
2374
2373
  if (config.type === 'scientific') {
2374
+ // Scientific notation handling
2375
2375
  mask = config.mask.toUpperCase().split('E')[0];
2376
2376
 
2377
2377
  let numOfDecimalPlaces = mask.split(config.decimal);
2378
- numOfDecimalPlaces = numOfDecimalPlaces[1].match(/[0#]+/g);
2379
- numOfDecimalPlaces = numOfDecimalPlaces[0]?.length ?? 0;
2380
- temp = temp.toExponential(numOfDecimalPlaces);
2381
- expo = temp.toString().split('e+');
2382
- temp = Number(expo[0]);
2383
- }
2384
-
2385
- if (mask.indexOf(config.decimal) === -1) {
2386
- // No decimal places
2387
- if (! Number.isInteger(temp)) {
2388
- temp = temp.toFixed(0);
2389
- }
2390
- } else {
2391
- // Length of the decimal
2392
- let mandatoryDecimalPlaces = mask.split(config.decimal);
2393
- mandatoryDecimalPlaces = mandatoryDecimalPlaces[1].match(/0+/g);
2394
- if (mandatoryDecimalPlaces) {
2395
- mandatoryDecimalPlaces = mandatoryDecimalPlaces[0].length;
2396
- } else {
2397
- mandatoryDecimalPlaces = 0;
2398
- }
2399
- // Amount of decimal
2400
- let numOfDecimalPlaces = temp.toString().split(config.decimal)
2401
- numOfDecimalPlaces = numOfDecimalPlaces[1]?.length ?? 0;
2402
- // Necessary adjustment
2403
- let necessaryAdjustment = 0;
2404
- if (numOfDecimalPlaces < mandatoryDecimalPlaces) {
2405
- necessaryAdjustment = mandatoryDecimalPlaces;
2378
+ // Handle masks without decimal (e.g., '0E+00')
2379
+ if (numOfDecimalPlaces[1]) {
2380
+ numOfDecimalPlaces = numOfDecimalPlaces[1].match(/[0#]+/g);
2381
+ numOfDecimalPlaces = numOfDecimalPlaces[0]?.length ?? 0;
2406
2382
  } else {
2407
- // Optional
2408
- let optionalDecimalPlaces = mask.split(config.decimal);
2409
- optionalDecimalPlaces = optionalDecimalPlaces[1].match(/[0#]+/g);
2410
- if (optionalDecimalPlaces) {
2411
- optionalDecimalPlaces = optionalDecimalPlaces[0].length;
2412
- if (numOfDecimalPlaces > optionalDecimalPlaces) {
2413
- necessaryAdjustment = optionalDecimalPlaces;
2414
- }
2415
- }
2416
- }
2417
- // Adjust decimal numbers if applicable
2418
- if (necessaryAdjustment) {
2419
- let t = temp.toFixed(necessaryAdjustment);
2420
- let n = temp.toString().split('.');
2421
- let fraction = n[1];
2422
- if (fraction && fraction.length > necessaryAdjustment && fraction[fraction.length - 1] === '5') {
2423
- t = parseFloat(n[0] + '.' + fraction + '1').toFixed(necessaryAdjustment);
2424
- }
2425
- temp = t;
2383
+ numOfDecimalPlaces = 0;
2426
2384
  }
2427
- }
2385
+ temp = temp.toExponential(numOfDecimalPlaces);
2386
+ // Split by 'e' to handle both positive (e+) and negative (e-) exponents
2387
+ let expo = temp.toString().split('e');
2388
+ // Keep coefficient as string to preserve decimal places (e.g., '1.00' not 1)
2389
+ temp = expo[0];
2428
2390
 
2429
- if (config.type === 'scientific') {
2391
+ // Process padding zeros for coefficient
2430
2392
  let ret = processPaddingZeros(mask, temp, config.decimal);
2431
2393
  if (ret) {
2432
2394
  temp = ret;
2433
2395
  }
2434
2396
  expo[0] = temp;
2435
2397
 
2436
- mask = config.mask.toUpperCase().split('E+')[1];
2437
- ret = processPaddingZeros(mask, expo[1], config.decimal);
2398
+ // Handle both E+ and E- in mask for exponent
2399
+ mask = config.mask.toUpperCase().split(/E[+-]?/)[1];
2400
+ ret = processPaddingZeros(mask, expo[1]?.replace(/^[+-]/, ''), config.decimal);
2438
2401
  if (ret) {
2439
- expo[1] = ret;
2402
+ // Preserve the sign from the original exponent
2403
+ let sign = expo[1]?.charAt(0);
2404
+ expo[1] = (sign === '-' ? '-' : '+') + ret;
2440
2405
  }
2441
2406
 
2442
- temp = expo.join('e+');
2407
+ temp = expo.join('e');
2408
+ } else {
2409
+ // Non-scientific decimal adjustment
2410
+ if (mask.indexOf(config.decimal) === -1) {
2411
+ // No decimal places
2412
+ if (! Number.isInteger(temp)) {
2413
+ temp = temp.toFixed(0);
2414
+ }
2415
+ } else {
2416
+ // Length of the decimal
2417
+ let mandatoryDecimalPlaces = mask.split(config.decimal);
2418
+ mandatoryDecimalPlaces = mandatoryDecimalPlaces[1].match(/0+/g);
2419
+ if (mandatoryDecimalPlaces) {
2420
+ mandatoryDecimalPlaces = mandatoryDecimalPlaces[0].length;
2421
+ } else {
2422
+ mandatoryDecimalPlaces = 0;
2423
+ }
2424
+
2425
+ // Amount of decimal (use original value to check decimal separator)
2426
+ let valueStr = value.toString();
2427
+ let numOfDecimalPlaces = valueStr.split(valueStr.includes('.') ? '.' : (config.decimal || '.'))
2428
+ numOfDecimalPlaces = numOfDecimalPlaces[1]?.length ?? 0;
2429
+ // Necessary adjustment
2430
+ let necessaryAdjustment = 0;
2431
+ if (numOfDecimalPlaces < mandatoryDecimalPlaces) {
2432
+ necessaryAdjustment = mandatoryDecimalPlaces;
2433
+ } else {
2434
+ // Optional
2435
+ let optionalDecimalPlaces = mask.split(config.decimal);
2436
+ optionalDecimalPlaces = optionalDecimalPlaces[1].match(/[0#]+/g);
2437
+ if (optionalDecimalPlaces) {
2438
+ optionalDecimalPlaces = optionalDecimalPlaces[0].length;
2439
+ if (numOfDecimalPlaces > optionalDecimalPlaces) {
2440
+ necessaryAdjustment = optionalDecimalPlaces;
2441
+ }
2442
+ }
2443
+ }
2444
+ // Adjust decimal numbers if applicable
2445
+ if (necessaryAdjustment) {
2446
+ let t = temp.toFixed(necessaryAdjustment);
2447
+ let n = temp.toString().split('.');
2448
+ let fraction = n[1];
2449
+ if (fraction && fraction.length > necessaryAdjustment && fraction[fraction.length - 1] === '5') {
2450
+ t = parseFloat(n[0] + '.' + fraction + '1').toFixed(necessaryAdjustment);
2451
+ }
2452
+ temp = t;
2453
+ }
2454
+ }
2443
2455
  }
2444
2456
 
2445
2457
  return temp;
@@ -3585,12 +3597,15 @@
3585
3597
  return value;
3586
3598
  };
3587
3599
 
3588
- Component.render = function(value, options, fullMask) {
3600
+ Component.render = function(value, options, fullMask, strict) {
3589
3601
  // Nothing to render
3590
3602
  if (value === '' || value === undefined || value === null) {
3591
3603
  return '';
3592
3604
  }
3593
3605
 
3606
+ // Store original value for strict mode
3607
+ const originalValue = value;
3608
+
3594
3609
  // Config
3595
3610
  const config = getConfig(options, value);
3596
3611
 
@@ -3611,6 +3626,11 @@
3611
3626
  value = value.toString();
3612
3627
  }
3613
3628
  } else {
3629
+ // Strict mode: for numeric masks, if string input is not a valid number,
3630
+ // return original value unchanged (Excel-like behavior)
3631
+ if (strict && typeof(originalValue) === 'string' && !isNumber(originalValue)) {
3632
+ return originalValue;
3633
+ }
3614
3634
  if (config.type === 'percentage') {
3615
3635
  if (typeof (value) === 'string' && value.indexOf('%') !== -1) {
3616
3636
  value = value.replace('%', '');
package/package.json CHANGED
@@ -19,7 +19,7 @@
19
19
  "javascript utilities"
20
20
  ],
21
21
  "main": "dist/index.js",
22
- "version": "6.0.3",
22
+ "version": "6.0.4",
23
23
  "bugs": "https://github.com/jsuites/jsuites/issues",
24
24
  "homepage": "https://github.com/jsuites/jsuites",
25
25
  "docs": "https://jsuites.net/",