11import { isNumberLike , isPlainObject , parseNumber } from '@douglasneuroinformatics/libjs' ;
22import type { AnyUnilingualFormInstrument , FormTypes } from '@opendatacapture/runtime-core' ;
3- import { unparse } from 'papaparse' ;
3+ import { parse , unparse } from 'papaparse' ;
44import { z } from 'zod' ;
55
66const ZOD_TYPE_NAMES = [
@@ -130,7 +130,7 @@ export function valueInterpreter(
130130 case 'ZodSet' :
131131 if ( entry . includes ( 'SET(' ) ) {
132132 let setData = entry . slice ( 4 , - 1 ) ;
133- let setDataList = setData . split ( '~~ ' ) ;
133+ let setDataList = setData . split ( ', ' ) ;
134134 return { success : true , value : new Set ( setDataList ) } ;
135135 }
136136 return { message : `Invalid ZodSet: ${ entry } ` , success : false } ;
@@ -161,8 +161,9 @@ export function ObjectValueInterpreter(
161161 if ( entry === '' && isOptional ) {
162162 return { success : true , value : undefined } ;
163163 }
164- if ( entry . includes ( 'recordArray (' ) && zList && zKeys ) {
164+ if ( entry . includes ( 'RECORD_ARRAY (' ) && zList && zKeys ) {
165165 let recordArray = [ ] ;
166+
166167 let recordArrayDataEntry = entry . slice ( 13 , - 2 ) ;
167168
168169 let recordArrayDataList = recordArrayDataEntry . split ( ';' ) ;
@@ -174,7 +175,8 @@ export function ObjectValueInterpreter(
174175
175176 for ( const listData of recordArrayDataList ) {
176177 let recordArrayObject : { [ key : string ] : any } = { } ;
177- let record = listData . split ( '++' ) ;
178+ let record = listData . split ( ',' ) ;
179+
178180 for ( let i = 0 ; i < record . length ; i ++ ) {
179181 let recordValue = record [ i ] ! . split ( ':' ) [ 1 ] ! ;
180182 const zListResult = zList [ i ] ! ;
@@ -213,7 +215,7 @@ export function applyLineTransformsSet(line: string) {
213215}
214216
215217export function applyLineTransformsArray ( line : string ) {
216- return line . replaceAll ( / r e c o r d A r r a y \( ( .* ?) \) / g, ( match ) => {
218+ return line . replaceAll ( / R E C O R D _ A R R A Y \( ( .* ?) \) / g, ( match ) => {
217219 return match . replaceAll ( ',' , '++' ) ;
218220 } ) ;
219221}
@@ -253,7 +255,7 @@ function sampleDataGenerator({
253255 case 'ZodArray' :
254256 case 'ZodObject' :
255257 try {
256- let multiString = 'recordArray ( ' ;
258+ let multiString = 'RECORD_ARRAY ( ' ;
257259 if ( multiValues && multiKeys ) {
258260 for ( let i = 0 ; i < multiValues . length ; i ++ ) {
259261 // eslint-disable-next-line max-depth
@@ -317,35 +319,32 @@ export async function processInstrumentCSV(
317319 const reader = new FileReader ( ) ;
318320 reader . onload = ( ) => {
319321 const text = reader . result as string ;
320- let [ headerLine , ...dataLines ] = text . split ( '\n' ) ;
322+ const parseResultCsv = parse < string [ ] > ( text , {
323+ header : false ,
324+ skipEmptyLines : true
325+ } ) ;
326+
327+ let [ headers , ...dataLines ] = parseResultCsv . data ;
321328
322329 //remove sample data if included
323- if ( dataLines [ 0 ] ?. includes ( INTERNAL_HEADERS_SAMPLE_DATA . join ( ',' ) ) ) {
324- dataLines = dataLines . slice ( 1 ) ;
330+ if ( dataLines [ 0 ] ?. [ 0 ] ?. startsWith ( MONGOLIAN_VOWEL_SEPARATOR ) ) {
331+ dataLines . shift ( ) ;
325332 }
326333
327- dataLines = dataLines . filter ( ( str ) => str !== '' ) ;
328-
329334 if ( dataLines . length === 0 ) {
330335 return resolve ( { message : 'data lines is empty array' , success : false } ) ;
331336 }
332337
333338 const result : FormTypes . Data [ ] = [ ] ;
334339
335- const headers : string [ ] = headerLine ! . split ( ',' ) ;
336-
337- if ( headers . length === 0 ) {
340+ if ( headers ?. length === 0 ) {
338341 return resolve ( { message : 'headers is empty array' , success : false } ) ;
339342 }
340343
341- for ( let line of dataLines ) {
342- line = applyLineTransformsSet ( line ) ;
343- line = applyLineTransformsArray ( line ) ;
344-
345- let elements = line . split ( ',' ) ;
344+ for ( let elements of dataLines ) {
346345 const jsonLine : { [ key : string ] : unknown } = { } ;
347- for ( let j = 0 ; j < headers . length ; j ++ ) {
348- const key = headers [ j ] ! ;
346+ for ( let j = 0 ; j < headers ! . length ; j ++ ) {
347+ const key = headers ! [ j ] ! ;
349348 const rawValue = elements [ j ] ! ;
350349
351350 if ( rawValue === '\n' ) {
@@ -379,7 +378,7 @@ export async function processInstrumentCSV(
379378 if ( ! interpreterResult . success ) {
380379 return resolve ( { message : interpreterResult . message , success : false } ) ;
381380 }
382- jsonLine [ headers [ j ] ! ] = interpreterResult . value ;
381+ jsonLine [ headers ! [ j ] ! ] = interpreterResult . value ;
383382 }
384383 const zodCheck = instrumentSchemaWithInternal . safeParse ( jsonLine ) ;
385384 if ( ! zodCheck . success ) {
0 commit comments