Skip to content

Commit

Permalink
feat(data): auto detect data shape
Browse files Browse the repository at this point in the history
  • Loading branch information
hatemhosny committed Sep 1, 2024
1 parent 544c39d commit 5ede0f7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/lib/options/options.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface OptionsAction extends Action {
}

export interface Options {
dataShape: 'long' | 'wide';
dataShape: 'long' | 'wide' | 'auto';
dataType: 'json' | 'csv' | 'tsv' | 'xml' | 'auto';
dataTransform: null | ((data: Data[] | WideData[]) => Data[] | WideData[]);
fillDateGapsInterval: null | 'year' | 'month' | 'day';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/options/options.reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Options, OptionsAction } from './options.models';
import { actionTypes } from './options.actions';

export const defaultOptions: Options = {
dataShape: 'long',
dataShape: 'auto',
dataType: 'auto',
dataTransform: null,
fillDateGapsInterval: null,
Expand Down
13 changes: 12 additions & 1 deletion src/lib/worker/prepare-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,20 @@ function calculateLastValues(makeCumulative = false) {
};
}

function detectDataShape(data: Data[] | WideData[], dataShape: Options['dataShape']) {
if (dataShape === 'long' || dataShape === 'wide') return dataShape;
const firstRow = data[0];
if ('date' in firstRow && 'name' in firstRow && 'value' in firstRow) {
return 'long';
}
return 'wide';
}

function wideDataToLong(dataShape: Options['dataShape'], nested = false) {
return function (data: WideData[]) {
if (dataShape === 'long') return data as Data[];
if (dataShape === 'long' || detectDataShape(data, dataShape) === 'long') {
return data as Data[];
}

const long = [] as Data[];
data.forEach((row) => {
Expand Down

0 comments on commit 5ede0f7

Please sign in to comment.