namespace import { Map } from 'immutable' ; import { AggregateTransform , array , Datum , Format , FormatJSON , FormatSV } from 'vega' ; import { store } from '../store' ; import { Column , ColumnRecord , DatasetRecord , Schema , SourceDatasetRecord } from '../store/factory/Dataset' ; import { NOMINAL , QUANTITATIVE , TEMPORAL } from 'vega-lite/src/type' ; const MTYPES = { 'boolean' : NOMINAL , 'number' : QUANTITATIVE , 'integer' : QUANTITATIVE , 'date' : TEMPORAL , 'string' : NOMINAL } ; const dl = require ( 'datalib' ) ; const promisify = require ( 'es6-promisify' ) ; const imutils = require ( './immutable-utils' ) ; const getInVis = imutils . getInVis ; function def ( id ) { return getInVis ( store . getState ( ) , 'datasets.' + id ) ; } let _values = { } ; param { Object } action returns { void } export function init ( ds , values ) { const id = ds . _id ; if ( _values [ id ] ) { return _values [ id ] ; } if ( values ) { _values [ id ] = values ; } else if ( ( ds as SourceDatasetRecord ) . source !== undefined ) { _values [ id ] = _values [ ( ds as SourceDatasetRecord ) . source as string ] ; } else { _values [ id ] = null ; } } export function reset ( ) { _values = { } ; } export function persist ( vals ? ) { return arguments . length ? ( _values = vals ) : _values ; } param { number } id returns { Array | string } export function input ( id ) { return _values [ id ] || [ ] ; } param { number } id returns { Object [ ] } export function output ( id ) { const ctrl = require ( '../ctrl' ) ; const ds = def ( id ) ; const data = ds && ctrl . view && ctrl . view . data ( ds . name ) ; return data || input ( id ) || [ ] ; } export interface ParsedValues { format : Format ; values : Datum [ ] ; } param { string } raw returns { ParsedValues } export function parseRaw ( raw ) { const format = { parse : 'auto' , type : null } ; let parsed ; try { format . type = 'json' ; return { format : format as FormatJSON , values : dl . read ( raw , format ) } ; } catch ( error ) { console . error ( error ) ; format . type = 'csv' ; parsed = dl . read ( raw , format ) ; if ( Object . keys ( parsed [ 0 ] ) . length > 1 ) { return { format : format as FormatSV , values : parsed } ; } format . type = 'tsv' ; parsed = dl . read ( raw , format ) ; if ( Object . keys ( parsed [ 0 ] ) . length > 1 ) { return { format : format as FormatSV , values : parsed } ; } throw Error ( 'Raw data is in an unsupported format. ' + 'Only JSON, CSV, or TSV may be imported.' ) ; } } param { number | Array } arg returns { Object } export function schema ( arg ) { if ( dl . isNumber ( arg ) ) { throw Error ( 'Dataset schemas are now available in the store.' ) ; } else if ( dl . isArray ( arg ) ) { const types = ( arg as any ) . __types__ ; return dl . keys ( types ) . reduce ( function ( s , k ) { return s . set ( k , Column ( { name : k , type : types [ k ] , mtype : MTYPES [ types [ k ] ] , source : true } ) ) ; } , Map ( ) ) ; } throw Error ( 'Expected an array of raw values.' ) ; } param { Object } src param { Object } aggregate returns { Object } export function aggregateSchema ( src , aggregate ) { const groupby = ( aggregate . groupby as string [ ] ) ; const aggSchema = groupby . reduce ( ( s , gb ) => s . set ( gb , src . get ( gb ) ) , Map < string , ColumnRecord > ( ) ) ; return array ( aggregate . as ) . reduce ( ( s , k ) => { return s . set ( k , Column ( { name : k , type : 'number' , mtype : MTYPES . number , source : false } ) ) ; } , aggSchema ) ; } module . exports = { init , reset , persist , input , output , parseRaw , schema , aggregateSchema } ;