@@ -9,42 +9,49 @@ export default function createCsvLoader(): ILoader<string, Record<string, string
99 async pull ( locale , _input ) {
1010 const input = parse ( _input , {
1111 columns : true ,
12+ skip_empty_lines : true ,
1213 } ) ;
1314
1415 const result : Record < string , string > = { } ;
1516
16- // Convert CSV rows to key-value pairs for the specified locale
1717 _ . forEach ( input , ( row ) => {
1818 const key = row . id ;
19- if ( key && row [ locale ] ) {
19+ if ( key && row [ locale ] && row [ locale ] . trim ( ) !== "" ) {
2020 result [ key ] = row [ locale ] ;
2121 }
2222 } ) ;
2323
2424 return result ;
2525 } ,
2626 async push ( locale , data , originalInput ) {
27- const input = parse ( originalInput || "" , { columns : true } ) as Record < string , any > [ ] ;
28- const columns = Object . keys ( input [ 0 ] || { id : "" } ) ;
27+ const input = parse ( originalInput || "" , {
28+ columns : true ,
29+ skip_empty_lines : true ,
30+ } ) as Record < string , any > [ ] ;
31+
32+ const columns = input . length > 0 ? Object . keys ( input [ 0 ] ) : [ "id" , locale ] ;
2933
30- // Update existing rows and collect new keys
3134 const updatedRows = input . map ( ( row ) => ( {
3235 ...row ,
3336 [ locale ] : data [ row . id ] || row [ locale ] || "" ,
3437 } ) ) ;
3538 const existingKeys = new Set ( input . map ( ( row ) => row . id ) ) ;
3639
37- // Add new keys
3840 Object . entries ( data ) . forEach ( ( [ key , value ] ) => {
3941 if ( ! existingKeys . has ( key ) ) {
40- updatedRows . push ( {
42+ const newRow : Record < string , string > = {
4143 id : key ,
42- ...Object . fromEntries ( columns . map ( ( column ) => [ column , column === locale ? value : "" ] ) ) ,
43- } ) ;
44+ ...Object . fromEntries ( columns . map ( ( column ) => [ column , "" ] ) ) ,
45+ } ;
46+ newRow [ locale ] = value ;
47+ updatedRows . push ( newRow ) ;
4448 }
4549 } ) ;
4650
47- return stringify ( updatedRows , { header : true } ) ;
51+ return stringify ( updatedRows , {
52+ header : true ,
53+ columns,
54+ } ) ;
4855 } ,
4956 } ) ;
5057}
0 commit comments