Skip to content

Commit

Permalink
Merge pull request #5882 from formio/FIO-9173-4.21.x
Browse files Browse the repository at this point in the history
FIO-9173: Allow Scientific Notations (#5878)
  • Loading branch information
edwinanciani authored Oct 30, 2024
2 parents cd4ba4b + e7d09a8 commit 0023bb4
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 17 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
"@formio/bootstrap3": "2.12.4-rc.1",
"@formio/choices.js": "10.2.1",
"@formio/semantic": "2.6.1",
"@formio/text-mask-addons": "^3.8.0-formio.2",
"@formio/text-mask-addons": "^3.8.0-formio.3",
"@formio/vanilla-text-mask": "^5.1.1-formio.1",
"abortcontroller-polyfill": "^1.7.5",
"autocompleter": "^7.0.1",
Expand Down
7 changes: 6 additions & 1 deletion src/components/number/Number.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,12 @@ export default class NumberComponent extends Input {
let value = parseFloat(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;
Expand Down
26 changes: 24 additions & 2 deletions src/components/number/Number.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
comp6,
comp7,
comp8,
comp9
comp9,
comp10,
comp11,
scientificNotation
} from './fixtures';
import CurrencyComponent from '../currency/Currency';

describe('Number Component', () => {
it('Should build an number component', () => {
Expand All @@ -25,6 +27,26 @@ 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'],
[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]) => {

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);
Expand Down
20 changes: 11 additions & 9 deletions src/components/number/fixtures/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export comp1 from './comp1';
export comp2 from './comp2';
export comp3 from './comp3';
export comp4 from './comp4';
export comp5 from './comp5';
export comp6 from './comp6';
export comp7 from './comp7';
export comp8 from './comp8';
export comp9 from './comp9';
import comp1 from './comp1';
import comp2 from './comp2';
import comp3 from './comp3';
import comp4 from './comp4';
import comp5 from './comp5';
import comp6 from './comp6';
import comp7 from './comp7';
import comp8 from './comp8';
import comp9 from './comp9';
import scientificNotation from './scientificNotation';
export { comp1, comp2, comp3, comp4, comp5, comp6, comp7, comp8, comp9, scientificNotation };
13 changes: 13 additions & 0 deletions src/components/number/fixtures/scientificNotation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export default {
'label': 'Number',
'mask': false,
'spellcheck': true,
'tableView': true,
'delimiter': false,
'requireDecimal': false,
'inputFormat': 'plain',
'key': 'number',
'type': 'number',
'decimalLimit': false,
'input': true
};
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1161,10 +1161,10 @@
resolved "https://registry.yarnpkg.com/@formio/semantic/-/semantic-2.6.1.tgz#dd224e276da2f27b272131ed6678847a52be299d"
integrity sha512-obp1BT5UnzD+uYBbqmnsTfO2hGxI2A2iR/cj3P5JUFLYSBpnr3TS2ShQ7Ee5GCRRtJPu0JnljuJj+YSKLCMuhg==

"@formio/text-mask-addons@^3.8.0-formio.2":
version "3.8.0-formio.2"
resolved "https://registry.yarnpkg.com/@formio/text-mask-addons/-/text-mask-addons-3.8.0-formio.2.tgz#b9489e68911c70a31997b97dd846d8a7cca6abdb"
integrity sha512-H4Sm+1Sx59jbrlKxtKbzethhp5OIcP8Oi4JBpsvH/SB8P/KCRmtjKbN5ACqURnXmYtBHLJC6Yr9KZibOVRGxpA==
"@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/vanilla-text-mask@^5.1.1-formio.1":
version "5.1.1-formio.1"
Expand Down

0 comments on commit 0023bb4

Please sign in to comment.