mirror of
https://github.com/grafana/grafana.git
synced 2024-11-28 03:34:15 -06:00
Transformations: Convert fields transform fix, convert strings with commas to numbers (#59074)
convert strings with commas to numbers
This commit is contained in:
parent
560b595ef2
commit
84ec35a4ad
@ -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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user