From f73bf612e67665c45857e8e9a687ef3d8359f727 Mon Sep 17 00:00:00 2001 From: Edwin Anciani Date: Thu, 24 Oct 2024 08:54:15 -0500 Subject: [PATCH 1/3] FIO-9173: Allow Scientific Notations --- src/components/number/Number.js | 16 +++++++++++++++- .../number/editForm/Number.edit.data.js | 8 ++++++++ test/unit/Number.unit.js | 18 +++++++++++++++++- .../unit/fixtures/number/scientificNotation.js | 13 +++++++++++++ 4 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 test/unit/fixtures/number/scientificNotation.js diff --git a/src/components/number/Number.js b/src/components/number/Number.js index 6db0b51237..b6559dae91 100644 --- a/src/components/number/Number.js +++ b/src/components/number/Number.js @@ -94,6 +94,7 @@ export default class NumberComponent extends Input { decimalLimit: _.get(this.component, 'decimalLimit', this.decimalLimit), allowNegative: _.get(this.component, 'allowNegative', true), allowDecimal: this.isDecimalAllowed(), + allowScientificNotation: _.get(this.component, 'allowScientificNotation', false), }); } @@ -132,6 +133,11 @@ export default class NumberComponent extends Input { // Remove delimiters and convert decimal separator to dot. value = value.split(this.delimiter).join('').replace(this.decimalSeparator, '.'); + // Add support for scientific notation + if (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/.test(value)) { + return Number(value); + } + if (this.component.validate && this.component.validate.integer) { return parseInt(value, 10); } @@ -193,9 +199,17 @@ export default class NumberComponent extends Input { input = input.split(this.delimiter).join('').replace(this.decimalSeparator, '.'); } let value = parseFloat(input); + if (this.component.allowScientificNotation) { + value = Number(input); + } if (!_.isNaN(value)) { - value = String(value).replace('.', this.decimalSeparator); + // Format scientific notation + if (/e/i.test(String(value))) { + value = value.toExponential(this.decimalLimit); + } else { + value = String(value).replace('.', this.decimalSeparator); + } } else { value = null; diff --git a/src/components/number/editForm/Number.edit.data.js b/src/components/number/editForm/Number.edit.data.js index 1c937e491b..edee014209 100644 --- a/src/components/number/editForm/Number.edit.data.js +++ b/src/components/number/editForm/Number.edit.data.js @@ -23,6 +23,14 @@ export default [ label: 'Require Decimal', tooltip: 'Always show decimals, even if trailing zeros.' }, + { + type: 'checkbox', + input: true, + weight: 100, + key: 'allowScientificNotation', + label: 'Allow Scientific Notation', + tooltip: 'Allow scientific notation for numbers.' + }, { key: 'case', ignore: true, diff --git a/test/unit/Number.unit.js b/test/unit/Number.unit.js index 35cc8beb2c..c57713dee5 100644 --- a/test/unit/Number.unit.js +++ b/test/unit/Number.unit.js @@ -15,7 +15,8 @@ import { comp7, comp8, comp9, - comp10 + comp10, + scientificNotation } from './fixtures/number'; import CurrencyComponent from "../../src/components/currency/Currency"; @@ -26,6 +27,21 @@ describe('Number Component', () => { }); }); + it('Should correctly handle scientific notation', () => { + return Harness.testCreate(NumberComponent, scientificNotation, { allowScientificNotation: true }).then((component) => { + const testCases = [ + [6.54635E+12, 6546350000000, '6546350000000'], + ]; + + 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}`); + }); + }); + }); + it('Should format submissions for table view for French locale', () => { return Harness.testCreate(NumberComponent, comp4, { language: 'fr' }).then((component) => { const value1 = component.getValueAsString(1); diff --git a/test/unit/fixtures/number/scientificNotation.js b/test/unit/fixtures/number/scientificNotation.js new file mode 100644 index 0000000000..efdf080f5d --- /dev/null +++ b/test/unit/fixtures/number/scientificNotation.js @@ -0,0 +1,13 @@ +export default { + 'label': 'Number', + 'mask': false, + 'spellcheck': true, + 'tableView': true, + 'delimiter': true, + 'requireDecimal': true, + 'inputFormat': 'plain', + 'key': 'number', + 'type': 'number', + 'decimalLimit': 2, + 'input': true +}; From d6b667adf280e5dffec43007d7084e106580f903 Mon Sep 17 00:00:00 2001 From: Edwin Anciani Date: Thu, 24 Oct 2024 09:11:53 -0500 Subject: [PATCH 2/3] FIO-9173: Fixing index --- test/unit/fixtures/number/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/fixtures/number/index.js b/test/unit/fixtures/number/index.js index 461e633cda..b2b89ceadf 100644 --- a/test/unit/fixtures/number/index.js +++ b/test/unit/fixtures/number/index.js @@ -9,4 +9,4 @@ import comp8 from './comp8'; import comp9 from './comp9'; import comp10 from './comp10'; import comp11 from './comp11'; -export { comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp9, comp10, comp11 }; +export { comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp9, comp10, comp11, scientificNotation }; From b02d5b7fba59e7c7f972aaabef17ebfa07d56d00 Mon Sep 17 00:00:00 2001 From: Edwin Anciani Date: Sun, 27 Oct 2024 11:47:50 -0500 Subject: [PATCH 3/3] FIO-9173: Adding more tests & using parseFloat instead of Number --- src/components/number/Number.js | 9 --------- src/components/number/editForm/Number.edit.data.js | 8 -------- test/unit/Number.unit.js | 5 +++++ 3 files changed, 5 insertions(+), 17 deletions(-) diff --git a/src/components/number/Number.js b/src/components/number/Number.js index b6559dae91..812793a282 100644 --- a/src/components/number/Number.js +++ b/src/components/number/Number.js @@ -94,7 +94,6 @@ export default class NumberComponent extends Input { decimalLimit: _.get(this.component, 'decimalLimit', this.decimalLimit), allowNegative: _.get(this.component, 'allowNegative', true), allowDecimal: this.isDecimalAllowed(), - allowScientificNotation: _.get(this.component, 'allowScientificNotation', false), }); } @@ -133,11 +132,6 @@ export default class NumberComponent extends Input { // Remove delimiters and convert decimal separator to dot. value = value.split(this.delimiter).join('').replace(this.decimalSeparator, '.'); - // Add support for scientific notation - if (/^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/.test(value)) { - return Number(value); - } - if (this.component.validate && this.component.validate.integer) { return parseInt(value, 10); } @@ -199,9 +193,6 @@ export default class NumberComponent extends Input { input = input.split(this.delimiter).join('').replace(this.decimalSeparator, '.'); } let value = parseFloat(input); - if (this.component.allowScientificNotation) { - value = Number(input); - } if (!_.isNaN(value)) { // Format scientific notation diff --git a/src/components/number/editForm/Number.edit.data.js b/src/components/number/editForm/Number.edit.data.js index edee014209..1c937e491b 100644 --- a/src/components/number/editForm/Number.edit.data.js +++ b/src/components/number/editForm/Number.edit.data.js @@ -23,14 +23,6 @@ export default [ label: 'Require Decimal', tooltip: 'Always show decimals, even if trailing zeros.' }, - { - type: 'checkbox', - input: true, - weight: 100, - key: 'allowScientificNotation', - label: 'Allow Scientific Notation', - tooltip: 'Allow scientific notation for numbers.' - }, { key: 'case', ignore: true, diff --git a/test/unit/Number.unit.js b/test/unit/Number.unit.js index 6497a1e58d..84843d7ed8 100644 --- a/test/unit/Number.unit.js +++ b/test/unit/Number.unit.js @@ -31,6 +31,11 @@ describe('Number Component', () => { return Harness.testCreate(NumberComponent, scientificNotation, { allowScientificNotation: true }).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'], ]; testCases.forEach(([input, expectedValue, expectedDisplayValue]) => {