Transformations: Convert fields transform fix, convert strings with commas to numbers (#59074)

convert strings with commas to numbers
This commit is contained in:
Brendan O'Handley 2022-11-28 13:16:02 -05:00 committed by GitHub
parent 560b595ef2
commit 84ec35a4ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -101,6 +101,26 @@ describe('field convert type', () => {
});
});
it('can convert strings with commas to numbers', () => {
const options = { targetField: 'stringy nums', destinationType: FieldType.number };
const stringyNumbers = {
name: 'stringy nums',
type: FieldType.string,
values: new ArrayVector(['1,000', '1,000,000']),
config: {},
};
const numbers = convertFieldType(stringyNumbers, options);
expect(numbers).toEqual({
name: 'stringy nums',
type: FieldType.number,
values: new ArrayVector([1000, 1000000]),
config: {},
});
});
describe('field convert types transformer', () => {
beforeAll(() => {
mockTransformationsRegistry([convertFieldTypeTransformer]);

View File

@ -142,7 +142,9 @@ function fieldToNumberField(field: Field): Field {
const numValues = field.values.toArray().slice();
for (let n = 0; n < numValues.length; n++) {
const number = +numValues[n];
// some numbers returned from datasources have commas
// strip the commas, coerce the string to a number
const number = +numValues[n].replace(/,/g, '');
numValues[n] = Number.isFinite(number) ? number : null;
}