@jsuites/utils 6.0.2 → 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 +84 -70
  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
@@ -994,14 +994,8 @@
994
994
  const hiddenCaret = "\u200B";
995
995
  // Locale for date parsing
996
996
  const userLocale = (typeof navigator !== 'undefined' && navigator.language) || 'en-US';
997
- // Labels
998
- const weekDaysFull = Helpers.weekdays;
999
- const weekDays = Helpers.weekdaysShort;
1000
- const monthsFull = Helpers.months;
1001
- const months = Helpers.monthsShort;
1002
997
 
1003
998
  // Helpers
1004
-
1005
999
  const focus = function(el) {
1006
1000
  if (el.textContent.length) {
1007
1001
  // Handle contenteditable elements
@@ -1373,9 +1367,9 @@
1373
1367
  this.values[this.index] = '';
1374
1368
  }
1375
1369
  let value = (this.values[this.index] + v).toLowerCase();
1376
- for (var i = 0; i < monthsFull.length; i++) {
1377
- if (monthsFull[i][0].toLowerCase().indexOf(value) === 0) {
1378
- this.values[this.index] = monthsFull[i][0];
1370
+ for (var i = 0; i < Helpers.months.length; i++) {
1371
+ if (Helpers.months[i][0].toLowerCase().indexOf(value) === 0) {
1372
+ this.values[this.index] = Helpers.months[i][0];
1379
1373
  this.date[1] = i + 1;
1380
1374
  this.index++;
1381
1375
  break;
@@ -1383,13 +1377,13 @@
1383
1377
  }
1384
1378
  },
1385
1379
  'MMMM': function(v) {
1386
- let ret = parseMethods.FIND.call(this, v, monthsFull);
1380
+ let ret = parseMethods.FIND.call(this, v, Helpers.months);
1387
1381
  if (typeof(ret) !== 'undefined') {
1388
1382
  this.date[1] = ret + 1;
1389
1383
  }
1390
1384
  },
1391
1385
  'MMM': function(v) {
1392
- let ret = parseMethods.FIND.call(this, v, months);
1386
+ let ret = parseMethods.FIND.call(this, v, Helpers.monthsShort);
1393
1387
  if (typeof(ret) !== 'undefined') {
1394
1388
  this.date[1] = ret + 1;
1395
1389
  }
@@ -1442,10 +1436,10 @@
1442
1436
  return parseMethods['MMM'].call(this, v);
1443
1437
  },
1444
1438
  'DDDD': function(v) {
1445
- return parseMethods.FIND.call(this, v, weekDaysFull);
1439
+ return parseMethods.FIND.call(this, v, Helpers.weekdays);
1446
1440
  },
1447
1441
  'DDD': function(v) {
1448
- return parseMethods.FIND.call(this, v, weekDays);
1442
+ return parseMethods.FIND.call(this, v, Helpers.weekdaysShort);
1449
1443
  },
1450
1444
  'DD': function(v, single) {
1451
1445
  const commit = () => {
@@ -1742,7 +1736,7 @@
1742
1736
  this.values[this.index] = '';
1743
1737
  }
1744
1738
  },
1745
- '#(.{1})##0?(.{1}0+)?( ?;(.*)?)?': function(v) {
1739
+ '#(.{1})##0?(.{1}[0#]+)?( ?;(.*)?)?': function(v) {
1746
1740
  // Process first the number
1747
1741
  parseMethods['[0#]+([.,]{1}0*#*)?'].call(this, v, true);
1748
1742
  // Create the separators
@@ -2375,77 +2369,89 @@
2375
2369
  const adjustNumberOfDecimalPlaces = function(config, value) {
2376
2370
  let temp = value;
2377
2371
  let mask = config.mask;
2378
- let expo;
2379
2372
 
2380
2373
  if (config.type === 'scientific') {
2374
+ // Scientific notation handling
2381
2375
  mask = config.mask.toUpperCase().split('E')[0];
2382
2376
 
2383
2377
  let numOfDecimalPlaces = mask.split(config.decimal);
2384
- numOfDecimalPlaces = numOfDecimalPlaces[1].match(/[0#]+/g);
2385
- numOfDecimalPlaces = numOfDecimalPlaces[0]?.length ?? 0;
2386
- temp = temp.toExponential(numOfDecimalPlaces);
2387
- expo = temp.toString().split('e+');
2388
- temp = Number(expo[0]);
2389
- }
2390
-
2391
- if (mask.indexOf(config.decimal) === -1) {
2392
- // No decimal places
2393
- if (! Number.isInteger(temp)) {
2394
- temp = temp.toFixed(0);
2395
- }
2396
- } else {
2397
- // Length of the decimal
2398
- let mandatoryDecimalPlaces = mask.split(config.decimal);
2399
- mandatoryDecimalPlaces = mandatoryDecimalPlaces[1].match(/0+/g);
2400
- if (mandatoryDecimalPlaces) {
2401
- mandatoryDecimalPlaces = mandatoryDecimalPlaces[0].length;
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;
2402
2382
  } else {
2403
- mandatoryDecimalPlaces = 0;
2404
- }
2405
- // Amount of decimal
2406
- let numOfDecimalPlaces = temp.toString().split(config.decimal)
2407
- numOfDecimalPlaces = numOfDecimalPlaces[1]?.length ?? 0;
2408
- // Necessary adjustment
2409
- let necessaryAdjustment = 0;
2410
- if (numOfDecimalPlaces < mandatoryDecimalPlaces) {
2411
- necessaryAdjustment = mandatoryDecimalPlaces;
2412
- } else {
2413
- // Optional
2414
- let optionalDecimalPlaces = mask.split(config.decimal);
2415
- optionalDecimalPlaces = optionalDecimalPlaces[1].match(/[0#]+/g);
2416
- if (optionalDecimalPlaces) {
2417
- optionalDecimalPlaces = optionalDecimalPlaces[0].length;
2418
- if (numOfDecimalPlaces > optionalDecimalPlaces) {
2419
- necessaryAdjustment = optionalDecimalPlaces;
2420
- }
2421
- }
2422
- }
2423
- // Adjust decimal numbers if applicable
2424
- if (necessaryAdjustment) {
2425
- let t = temp.toFixed(necessaryAdjustment);
2426
- let n = temp.toString().split('.');
2427
- let fraction = n[1];
2428
- if (fraction && fraction.length > necessaryAdjustment && fraction[fraction.length - 1] === '5') {
2429
- t = parseFloat(n[0] + '.' + fraction + '1').toFixed(necessaryAdjustment);
2430
- }
2431
- temp = t;
2383
+ numOfDecimalPlaces = 0;
2432
2384
  }
2433
- }
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];
2434
2390
 
2435
- if (config.type === 'scientific') {
2391
+ // Process padding zeros for coefficient
2436
2392
  let ret = processPaddingZeros(mask, temp, config.decimal);
2437
2393
  if (ret) {
2438
2394
  temp = ret;
2439
2395
  }
2440
2396
  expo[0] = temp;
2441
2397
 
2442
- mask = config.mask.toUpperCase().split('E+')[1];
2443
- 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);
2444
2401
  if (ret) {
2445
- expo[1] = ret;
2402
+ // Preserve the sign from the original exponent
2403
+ let sign = expo[1]?.charAt(0);
2404
+ expo[1] = (sign === '-' ? '-' : '+') + ret;
2446
2405
  }
2447
2406
 
2448
- 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
+ }
2449
2455
  }
2450
2456
 
2451
2457
  return temp;
@@ -3591,12 +3597,15 @@
3591
3597
  return value;
3592
3598
  };
3593
3599
 
3594
- Component.render = function(value, options, fullMask) {
3600
+ Component.render = function(value, options, fullMask, strict) {
3595
3601
  // Nothing to render
3596
3602
  if (value === '' || value === undefined || value === null) {
3597
3603
  return '';
3598
3604
  }
3599
3605
 
3606
+ // Store original value for strict mode
3607
+ const originalValue = value;
3608
+
3600
3609
  // Config
3601
3610
  const config = getConfig(options, value);
3602
3611
 
@@ -3617,6 +3626,11 @@
3617
3626
  value = value.toString();
3618
3627
  }
3619
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
+ }
3620
3634
  if (config.type === 'percentage') {
3621
3635
  if (typeof (value) === 'string' && value.indexOf('%') !== -1) {
3622
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.2",
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/",