-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature :: define a basic dataset type (#120)
DataSet type is defined as two properties: - `headers`: a list of objects defining columns - each column is itself defined by a `name` and an optional `type`. We'll probably add extra metadata later, - `data`: a list of list of values Use this type in the `DataViewer` component. Related to ToucanToco/weaverbird#26
- Loading branch information
Showing
6 changed files
with
290 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* This module contains helpers and definitions related to datasets. | ||
*/ | ||
|
||
export type DataSetColumn = { | ||
name: string; | ||
type?: 'integer' | 'float' | 'boolean' | 'string' | 'date' | 'object'; | ||
}; | ||
|
||
/** | ||
* Here's a dataset example: | ||
* ```javascript | ||
* { | ||
* headers: [ | ||
* { name: 'col1' }, | ||
* { name: 'col2' }, | ||
* { name: 'col3' } | ||
* ], | ||
* data: [ | ||
* [ 'ab', 12, true ], | ||
* [ 'cd', 13, null ] | ||
* ] | ||
* } | ||
* | ||
* NOTE that we use `null` to represent empty values. | ||
* ``` | ||
*/ | ||
export type DataSet = { | ||
headers: Array<DataSetColumn>; | ||
data: Array<Array<any>>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* mongo specific helpers for dataset manipulation. | ||
*/ | ||
|
||
import { DataSet } from '@/lib/dataset'; | ||
|
||
type MongoDocument = { [k: string]: any }; | ||
export type MongoResults = Array<MongoDocument>; | ||
|
||
/** | ||
* transform a mongo resultset (i.e. a list of json documents) into | ||
* a `DataSet` structure | ||
*/ | ||
export function mongoResultsToDataset(results: MongoResults): DataSet { | ||
const dataset: DataSet = { headers: [], data: [] }; | ||
if (results.length) { | ||
const colnames = results | ||
// get list of list of key | ||
.map(row => Object.keys(row)) | ||
// then flatten them | ||
.flat() | ||
// and remove duplicates by keeping the _first_ occurence | ||
.filter((col, colidx, array) => array.indexOf(col) === colidx); | ||
// transform set of names to list of DataSetColumn objects | ||
dataset.headers = colnames.map(name => ({ name })); | ||
for (const row of results) { | ||
dataset.data.push( | ||
dataset.headers.map(coldef => (row.hasOwnProperty(coldef.name) ? row[coldef.name] : null)), | ||
); | ||
} | ||
} | ||
return dataset; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,44 @@ | ||
import { storiesOf } from '@storybook/vue'; | ||
import {storiesOf} from '@storybook/vue'; | ||
|
||
import { DataViewer } from '../dist/storybook/components'; | ||
import {DataViewer} from '../dist/storybook/components'; | ||
|
||
const stories = storiesOf('DataViewer', module); | ||
|
||
stories.add('empty', () => ({ | ||
components: { DataViewer }, | ||
template: ` | ||
components: {DataViewer}, | ||
template: ` | ||
<data-viewer> | ||
</data-viewer> | ||
`, | ||
})); | ||
})); | ||
|
||
stories.add('simple', () => ({ | ||
components: { DataViewer }, | ||
props: { | ||
dataset: { | ||
default() { | ||
return [ | ||
{ columnA: 'value1', columnB: 'value2', columnC: 'value3' }, | ||
{ columnA: 'value4', columnB: 'value5', columnC: 'value6' }, | ||
{ columnA: 'value7', columnB: 'value8', columnC: 'value9' }, | ||
{ columnA: 'value10', columnB: 'value11', columnC: 'value12' }, | ||
{ columnA: 'value13', columnB: { obj: 'value14' }, columnC: undefined }, | ||
]; | ||
}, | ||
}, | ||
}, | ||
template: ` | ||
components: {DataViewer}, | ||
props: { | ||
dataset: { | ||
default() { | ||
return { | ||
headers: | ||
[ | ||
{name: 'columnA'}, | ||
{name: 'columnB'}, | ||
{name: 'columnC'}, | ||
], | ||
data: [ | ||
['value1', 'value2', 'value3'], | ||
['value4', 'value5', 'value6'], | ||
['value7', 'value8', 'value9'], | ||
['value10', 'value11', 'value12'], | ||
['value10', {obj: 'value14'}, null], | ||
] | ||
} | ||
}, | ||
}, | ||
}, | ||
template: ` | ||
<data-viewer | ||
:dataset="dataset" | ||
> | ||
</data-viewer> | ||
`, | ||
})); | ||
})); |
Oops, something went wrong.