1+ import { readTXT , writeJSON } from 'https://deno.land/x/flat@0.0.15/mod.ts' ;
2+ import { csvParse , autoType } from "https://unpkg.com/d3-dsv@3.0.1/src/index.js" ;
3+ import { config_path } from "./config.js" ;
4+
5+ const keys = [ "authors" , "groups" , "layers" , "sources" , "statuses" , "translations" ] ;
6+ const parseForBoolean = ( val ) => val === "TRUE" ? true : val === "FALSE" ? false : val ;
7+
8+ const config = { } ;
9+
10+ for ( const key of keys ) {
11+ const path = `raw-data/${ key } .csv` ;
12+ console . log ( `Reading ${ path } ` ) ;
13+ const data = csvParse ( await readTXT ( path ) , autoType ) ;
14+
15+ config [ key ] = data . map ( d => {
16+ const row = { } ;
17+ for ( const col of data . columns ) {
18+ const arr = col . match ( / (?< = \[ ) \d * (? = \] ) / ) ;
19+ if ( Array . isArray ( arr ) ) {
20+ const key = col . split ( "[" ) [ 0 ] ;
21+ if ( arr [ 0 ] === "" ) row [ key ] = d [ col ] . split ( ", " ) ;
22+ else {
23+ if ( ! row [ key ] ) row [ key ] = [ ] ;
24+ row [ key ] [ arr [ 0 ] ] = parseForBoolean ( d [ col ] ) ;
25+ }
26+ } else row [ col ] = parseForBoolean ( d [ col ] ) ;
27+ }
28+ const newCols = Object . keys ( row ) ;
29+ for ( const col of newCols ) {
30+ if ( Array . isArray ( row [ col ] ) && row [ col ] . every ( d => ! d ) ) row [ col ] = null ;
31+ }
32+ return row ;
33+ } ) ;
34+ }
35+
36+ writeJSON ( config_path , config ) ;
37+ console . log ( `Wrote ${ config_path } ` ) ;
0 commit comments