[v9.3.x] Transformations: Convert fields transform fix, convert strings with commas to numbers (#59433)

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

convert strings with commas to numbers

(cherry picked from commit 84ec35a4ad)

Co-authored-by: Brendan O'Handley <brendan.ohandley@grafana.com>
This commit is contained in:
Grot (@grafanabot)
2022-11-28 13:39:26 -05:00
committed by GitHub
co-authored by Brendan O'Handley
parent dc1e759ce5
commit bfe04d4d1a
2 changed files with 23 additions and 1 deletions
@@ -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]);
@@ -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;
}