mirror of
				https://github.com/Djeeberjr/fw-anwesenheit.git
				synced 2025-11-03 23:24:10 +00:00 
			
		
		
		
	simplified csv generator
This commit is contained in:
		
							parent
							
								
									141c1aa9cb
								
							
						
					
					
						commit
						2e75ba2908
					
				@ -1,10 +1,9 @@
 | 
				
			|||||||
interface CSVOptions {
 | 
					interface CSVOptions {
 | 
				
			||||||
  delimiter?: string;             // default: ","
 | 
					  delimiter?: string;
 | 
				
			||||||
  includeHeader?: boolean;        // default: true for object input, false for array-of-arrays unless headers provided
 | 
					  headerOrder?: string[];
 | 
				
			||||||
  headerOrder?: string[];         // explicit ordering for columns that should come first
 | 
					  eol?: string;
 | 
				
			||||||
  eol?: string;                   // default: "\r\n"
 | 
					  includeBOM?: boolean;
 | 
				
			||||||
  includeBOM?: boolean;           // default: false (useful for Excel)
 | 
					  nullString?: string;
 | 
				
			||||||
  nullString?: string;            // default: "" (how to render null/undefined)
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type RowObject = Record<string, any>;
 | 
					type RowObject = Record<string, any>;
 | 
				
			||||||
@ -13,7 +12,6 @@ type InputRows = RowObject[];
 | 
				
			|||||||
export function generateCSVString(input: InputRows, opts: CSVOptions = {}): string {
 | 
					export function generateCSVString(input: InputRows, opts: CSVOptions = {}): string {
 | 
				
			||||||
  const {
 | 
					  const {
 | 
				
			||||||
    delimiter = ",",
 | 
					    delimiter = ",",
 | 
				
			||||||
    includeHeader,
 | 
					 | 
				
			||||||
    headerOrder,
 | 
					    headerOrder,
 | 
				
			||||||
    eol = "\r\n",
 | 
					    eol = "\r\n",
 | 
				
			||||||
    includeBOM = false,
 | 
					    includeBOM = false,
 | 
				
			||||||
@ -29,7 +27,7 @@ export function generateCSVString(input: InputRows, opts: CSVOptions = {}): stri
 | 
				
			|||||||
  const escapeCell = (raw: any): string => {
 | 
					  const escapeCell = (raw: any): string => {
 | 
				
			||||||
    if (raw === null || raw === undefined) return nullString;
 | 
					    if (raw === null || raw === undefined) return nullString;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let s = defaultStringify(raw);
 | 
					    let s = stringify(raw);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Replace quotes 
 | 
					    // Replace quotes 
 | 
				
			||||||
    if (s.includes('"')) s = s.replace(/"/g, '""');
 | 
					    if (s.includes('"')) s = s.replace(/"/g, '""');
 | 
				
			||||||
@ -38,7 +36,7 @@ export function generateCSVString(input: InputRows, opts: CSVOptions = {}): stri
 | 
				
			|||||||
  };
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Transform the value of a cell into a string
 | 
					  // Transform the value of a cell into a string
 | 
				
			||||||
  function defaultStringify(v: any): string {
 | 
					  function stringify(v: any): string {
 | 
				
			||||||
    if (v === null || v === undefined) return nullString;
 | 
					    if (v === null || v === undefined) return nullString;
 | 
				
			||||||
    if (v instanceof Date) return v.toLocaleDateString();
 | 
					    if (v instanceof Date) return v.toLocaleDateString();
 | 
				
			||||||
    if (typeof v === "boolean") return v ? "X" : "";
 | 
					    if (typeof v === "boolean") return v ? "X" : "";
 | 
				
			||||||
@ -79,10 +77,9 @@ export function generateCSVString(input: InputRows, opts: CSVOptions = {}): stri
 | 
				
			|||||||
    finalHeaders = first.concat(rest);
 | 
					    finalHeaders = first.concat(rest);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const shouldIncludeHeader = typeof includeHeader === "boolean" ? includeHeader : finalHeaders.length > 0;
 | 
					 | 
				
			||||||
  const rowsOut: string[] = [];
 | 
					  const rowsOut: string[] = [];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  if (shouldIncludeHeader && finalHeaders.length) {
 | 
					  if (finalHeaders.length > 0) {
 | 
				
			||||||
    rowsOut.push(finalHeaders.map(h => escapeCell(h)).join(delimiter));
 | 
					    rowsOut.push(finalHeaders.map(h => escapeCell(h)).join(delimiter));
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user