Commit 88ec7e09 by Ryan McKinley Committed by GitHub

toDataFrame: detect field properties using in rather than hasOwnProperty (#23673)

parent 806dd3f6
......@@ -10,6 +10,7 @@ import {
import { DataFrameDTO, FieldType, TableData, TimeSeries } from '../types/index';
import { dateTime } from '../datetime/moment_wrapper';
import { MutableDataFrame } from './MutableDataFrame';
import { ArrayDataFrame } from './ArrayDataFrame';
describe('toDataFrame', () => {
it('converts timeseries to series', () => {
......@@ -73,6 +74,19 @@ describe('toDataFrame', () => {
expect(again).toBe(input);
});
it('Make sure ArrayDataFrame is used as a DataFrame without modification', () => {
const orig = [
{ a: 1, b: 2 },
{ a: 3, b: 4 },
];
const array = new ArrayDataFrame(orig);
const frame = toDataFrame(array);
expect(frame).toEqual(array);
expect(frame instanceof ArrayDataFrame).toEqual(true);
expect(frame.length).toEqual(orig.length);
expect(frame.fields.map(f => f.name)).toEqual(['a', 'b']);
});
it('throws when table rows is not array', () => {
expect(() =>
toDataFrame({
......
......@@ -261,9 +261,9 @@ export const isTableData = (data: any): data is DataFrame => data && data.hasOwn
export const isDataFrame = (data: any): data is DataFrame => data && data.hasOwnProperty('fields');
export const toDataFrame = (data: any): DataFrame => {
if (data.hasOwnProperty('fields')) {
if ('fields' in data) {
// DataFrameDTO does not have length
if (data.hasOwnProperty('length')) {
if ('length' in data) {
return data as DataFrame;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment