fixed fromCsv escaping quotes & letter headers
This commit is contained in:
parent
341ef37205
commit
ccf3fcb043
@ -10,10 +10,10 @@
|
||||
|
||||
const csv = '' +
|
||||
'_id,any,boolean,date,file,foreignKey,formula,javaScript,number,string,_createdBy,_createdDate,_updatedBy,_updatedDate\n' +
|
||||
'48,,,,,,,,,,system,2024-11-09T19:05:04.932Z,system,2024-11-09T19:05:04.932Z\n' +
|
||||
'48,"test,test,test",,,,,,,,,system,2024-11-09T19:05:04.932Z,system,2024-11-09T19:05:04.932Z\n' +
|
||||
'49,,,,,,,,,,system,2024-11-09T19:05:04.933Z,system,2024-11-09T19:05:04.933Z';
|
||||
|
||||
console.log(fromCsv(csv));
|
||||
console.log(fromCsv(csv, false));
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/utils",
|
||||
"version": "0.22.5",
|
||||
"version": "0.22.6",
|
||||
"description": "Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
29
src/csv.ts
29
src/csv.ts
@ -1,20 +1,39 @@
|
||||
import {ASet} from './aset.ts';
|
||||
import {dotNotation, flattenObj, JSONSanitize} from './objects.ts';
|
||||
import {LETTER_LIST} from './string.ts';
|
||||
|
||||
export function fromCsv<T = any>(csv: string, hasHeaders=true): T[] {
|
||||
const row = csv.split('\n');
|
||||
let headers: any = hasHeaders ? row.splice(0, 1)[0] : null;
|
||||
if(headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
|
||||
return <T[]>row.map(r => {
|
||||
const props: string[] = <any>r.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
|
||||
function parseLine(line: string): (string | null)[] {
|
||||
const parts = line.split(','), columns: string[] = [];
|
||||
let quoted = false;
|
||||
for(const p of parts) {
|
||||
if(quoted) columns[columns.length - 1] = columns.at(-1) + ',' + p;
|
||||
else columns.push(p);
|
||||
if(/[^"]"$/g.test(p)) {
|
||||
quoted = false;
|
||||
} else if(/^"[^"]/g.test(p)) {
|
||||
quoted = true;
|
||||
}
|
||||
}
|
||||
return columns;
|
||||
}
|
||||
|
||||
const props = parseLine(r);
|
||||
const h = headers || (Array(props.length).fill(null).map((r, i) => {
|
||||
let letter = '';
|
||||
const first = i / 26;
|
||||
if(first > 1) letter +=
|
||||
i % 26
|
||||
i
|
||||
if(first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
|
||||
letter += LETTER_LIST[i % 26];
|
||||
return letter;
|
||||
}));
|
||||
return h.reduce((acc: any, h: any, i: number) => ({...acc, [h]: props[i]}), {})
|
||||
return h.reduce((acc: any, h: any, i: number) => {
|
||||
dotNotation(acc, h, props[i]);
|
||||
return acc;
|
||||
}, {});
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user