From a787da82ba1c9e52c086813cd390043e95bc1b80 Mon Sep 17 00:00:00 2001 From: Edwin Anciani Date: Fri, 15 Nov 2024 10:38:49 -0600 Subject: [PATCH 1/4] Fix Scientific notation allowing it by default --- src/components/number/Number.js | 15 +++++++++++---- test/unit/Number.unit.js | 16 +++++++--------- test/unit/fixtures/number/scientificNotation.js | 2 +- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/components/number/Number.js b/src/components/number/Number.js index 812793a282..b667355c4d 100644 --- a/src/components/number/Number.js +++ b/src/components/number/Number.js @@ -192,13 +192,20 @@ export default class NumberComponent extends Input { if (typeof input === 'string') { input = input.split(this.delimiter).join('').replace(this.decimalSeparator, '.'); } - let value = parseFloat(input); + let value; - if (!_.isNaN(value)) { + if (!_.isNaN(input)) { // Format scientific notation - if (/e/i.test(String(value))) { - value = value.toExponential(this.decimalLimit); + if (/[eE]/.test(String(input))) { + // Convert to exponential notation will depend on the decimal limit set in the component + // Example: 1.23e-5 will be converted to 1.23e-5 if decimal limit is set to 2 + // Example: 1.23e5 will be converted to 1.23e+5 if decimal limit is set to 2 + // if decimal limit is 3, 1.23e5 will be converted to 1.230e+5 + // if decimal limit is not set, 1.23e5 will be converted to 1.23000000000000000000e+5 + value = parseFloat(input); + value = value.toExponential(this.decimalLimit); } else { + value = parseFloat(input); value = String(value).replace('.', this.decimalSeparator); } } diff --git a/test/unit/Number.unit.js b/test/unit/Number.unit.js index 84843d7ed8..6a835e6b20 100644 --- a/test/unit/Number.unit.js +++ b/test/unit/Number.unit.js @@ -28,18 +28,16 @@ describe('Number Component', () => { }); it('Should correctly handle scientific notation', () => { - return Harness.testCreate(NumberComponent, scientificNotation, { allowScientificNotation: true }).then((component) => { + return Harness.testCreate(NumberComponent, scientificNotation).then((component) => { const testCases = [ - [6.54635E+12, 6546350000000, '6546350000000'], - [1.23e-5, 0.0000123, '0.0000123'], - [3.14159e2, 314.159, '314.159'], - [2e-3, 0.002, '0.002'], - [7.5e5, 750000, '750000'], - [1.2345e10, 12345000000, '12345000000'], + ['6.54e+12', 6.54e+12, '6.54e+12'], + ['1.23e-5', 1.23e-5, '1.23e-5'], + ['3.14e+2', 3.14e+2, '3.14e+2'], + ['2e-3', 2e-3, '2e-3'], + ['7.5e+5', 7.5e+5, '7.5e+5'], + ['1.23e+10', 1.23e+10, '1.23e+10'], ]; - testCases.forEach(([input, expectedValue, expectedDisplayValue]) => { - component.setValue(input); assert.equal(component.dataValue, expectedValue, `setValue: ${input} should result in ${expectedValue}`); assert.equal(component.getValueAsString(input), expectedDisplayValue, `getValueAsString: ${input} should result in ${expectedDisplayValue}`); diff --git a/test/unit/fixtures/number/scientificNotation.js b/test/unit/fixtures/number/scientificNotation.js index efdf080f5d..e87ebe2b7e 100644 --- a/test/unit/fixtures/number/scientificNotation.js +++ b/test/unit/fixtures/number/scientificNotation.js @@ -4,7 +4,7 @@ export default { 'spellcheck': true, 'tableView': true, 'delimiter': true, - 'requireDecimal': true, + 'requireDecimal': false, 'inputFormat': 'plain', 'key': 'number', 'type': 'number', From acff1001b9aca93f4a161e6c25a1a6d9720660b7 Mon Sep 17 00:00:00 2001 From: lane-formio Date: Fri, 15 Nov 2024 16:06:29 -0600 Subject: [PATCH 2/4] Update text-mask-addons to formio.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index dd333b3f62..ab00a00ac1 100644 --- a/package.json +++ b/package.json @@ -83,7 +83,7 @@ "@formio/bootstrap": "3.0.0-dev.98.17ba6ea", "@formio/choices.js": "^10.2.1", "@formio/core": "v2.1.0-dev.174.9a3c6ec", - "@formio/text-mask-addons": "^3.8.0-formio.3", + "@formio/text-mask-addons": "^3.8.0-formio.4", "@formio/vanilla-text-mask": "^5.1.1-formio.1", "abortcontroller-polyfill": "^1.7.5", "autocompleter": "^8.0.4", From 93dbbe7b8d73a58ef6da518b567f4137d59689a3 Mon Sep 17 00:00:00 2001 From: lane-formio Date: Fri, 15 Nov 2024 16:13:39 -0600 Subject: [PATCH 3/4] Add yarn.lock to fix/FIO-9173-notation --- yarn.lock | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/yarn.lock b/yarn.lock index 2ab2a0c002..4bc9119a45 100644 --- a/yarn.lock +++ b/yarn.lock @@ -398,10 +398,10 @@ lodash "^4.17.21" moment "^2.29.4" -"@formio/text-mask-addons@^3.8.0-formio.3": - version "3.8.0-formio.3" - resolved "https://registry.yarnpkg.com/@formio/text-mask-addons/-/text-mask-addons-3.8.0-formio.3.tgz#9da71371a2cb6f781e5e9f90246f25b3d053f75d" - integrity sha512-izNhggJTaCEJNOhtdCkouLxBP3byl+ObsHTdp9RpKwNz0k/ZIy/0xuelk1NY4vAjKojnJPiAjtxn+rrcZnEkpw== +"@formio/text-mask-addons@^3.8.0-formio.4": + version "3.8.0-formio.4" + resolved "https://registry.yarnpkg.com/@formio/text-mask-addons/-/text-mask-addons-3.8.0-formio.4.tgz#f3ce4bf978c6c5e8f671641d96f6fd116f7c92a3" + integrity sha512-vhkeIyuL+1rtC9S4IW8O3JCwroPtvJrkrcMO4wyELNqMIgQRKbiyBAitZfUP4tY04xdB5lxAinbzdwb+NMdX6w== "@formio/vanilla-text-mask@^5.1.1-formio.1": version "5.1.1-formio.1" From 1adbfb0172c0aa27614a930bf4a49685b63cdc3a Mon Sep 17 00:00:00 2001 From: Edwin Anciani Date: Tue, 19 Nov 2024 11:59:47 -0600 Subject: [PATCH 4/4] Fix tests on NaN inputs like {} [] --- src/components/number/Number.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/number/Number.js b/src/components/number/Number.js index b667355c4d..4aba32c3cc 100644 --- a/src/components/number/Number.js +++ b/src/components/number/Number.js @@ -196,7 +196,7 @@ export default class NumberComponent extends Input { if (!_.isNaN(input)) { // Format scientific notation - if (/[eE]/.test(String(input))) { + if (/[0-9]+[eE]/.test(String(input))) { // Convert to exponential notation will depend on the decimal limit set in the component // Example: 1.23e-5 will be converted to 1.23e-5 if decimal limit is set to 2 // Example: 1.23e5 will be converted to 1.23e+5 if decimal limit is set to 2 @@ -206,7 +206,7 @@ export default class NumberComponent extends Input { value = value.toExponential(this.decimalLimit); } else { value = parseFloat(input); - value = String(value).replace('.', this.decimalSeparator); + value = !_.isNaN(value) ? String(value).replace('.', this.decimalSeparator) : null; } } else {