RowsToFields: Fix issue with field names that are numbers (#40580)
* RowsToFields: Fix issue with field names that are numbers * Only add the index accessor if field name does not conflict with it * fix lint Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
This commit is contained in:
co-authored by
Ryan McKinley
parent
7e59b92c8c
commit
25813db334
@@ -81,4 +81,19 @@ describe('dataFrameView', () => {
|
|||||||
value: 1,
|
value: 1,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Can handle fields with number name', () => {
|
||||||
|
const view = new DataFrameView<MySpecialObject>(
|
||||||
|
new MutableDataFrame({
|
||||||
|
fields: [
|
||||||
|
{ name: '1', type: FieldType.string, values: ['a'] },
|
||||||
|
{ name: '2', type: FieldType.string, values: ['b'] },
|
||||||
|
],
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
const obj = view.get(0) as any;
|
||||||
|
expect(obj['1']).toEqual('a');
|
||||||
|
expect(obj['2']).toEqual('b');
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -32,10 +32,12 @@ export class DataFrameView<T = any> extends FunctionalVector<T> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.defineProperty(obj, i, {
|
if (!(obj as any).hasOwnProperty(i.toString())) {
|
||||||
enumerable: false, // Don't enumerate array index
|
Object.defineProperty(obj, i, {
|
||||||
get: getter,
|
enumerable: false, // Don't enumerate array index
|
||||||
});
|
get: getter,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.obj = obj;
|
this.obj = obj;
|
||||||
|
|||||||
@@ -151,4 +151,26 @@ describe('Rows to fields', () => {
|
|||||||
expect(result.fields[0].name).toEqual('Stockholm');
|
expect(result.fields[0].name).toEqual('Stockholm');
|
||||||
expect(result.fields[0].values.get(0)).toEqual(20);
|
expect(result.fields[0].values.get(0)).toEqual(20);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('Can handle number fields as name field', () => {
|
||||||
|
const input = toDataFrame({
|
||||||
|
fields: [
|
||||||
|
{ name: 'SensorID', type: FieldType.number, values: [10, 20, 30] },
|
||||||
|
{ name: 'Value', type: FieldType.number, values: [1, 2, 3] },
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = rowsToFields(
|
||||||
|
{
|
||||||
|
mappings: [
|
||||||
|
{ fieldName: 'SensorID', handlerKey: 'field.name' },
|
||||||
|
{ fieldName: 'Value', handlerKey: 'field.value' },
|
||||||
|
],
|
||||||
|
},
|
||||||
|
input
|
||||||
|
);
|
||||||
|
|
||||||
|
expect(result.fields[0].name).toEqual('10');
|
||||||
|
expect(result.fields[0].values.get(0)).toEqual(1);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ export function rowsToFields(options: RowToFieldsTransformOptions, data: DataFra
|
|||||||
const labels = getLabelsFromRow(data, index, mappingResult);
|
const labels = getLabelsFromRow(data, index, mappingResult);
|
||||||
|
|
||||||
const field: Field = {
|
const field: Field = {
|
||||||
name: name,
|
name: `${name}`,
|
||||||
type: valueField.type,
|
type: valueField.type,
|
||||||
values: new ArrayVector([value]),
|
values: new ArrayVector([value]),
|
||||||
config: config,
|
config: config,
|
||||||
|
|||||||
Reference in New Issue
Block a user