fixed fromCsv escaping quotes & letter headers
All checks were successful
Build / Build NPM Project (push) Successful in 37s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 1m46s

This commit is contained in:
Zakary Timson 2024-11-09 17:27:05 -05:00
parent 341ef37205
commit ccf3fcb043
3 changed files with 27 additions and 8 deletions

View File

@ -10,10 +10,10 @@
const csv = '' + const csv = '' +
'_id,any,boolean,date,file,foreignKey,formula,javaScript,number,string,_createdBy,_createdDate,_updatedBy,_updatedDate\n' + '_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'; '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> </script>
</body> </body>
</html> </html>

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.22.5", "version": "0.22.6",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -1,20 +1,39 @@
import {ASet} from './aset.ts'; import {ASet} from './aset.ts';
import {dotNotation, flattenObj, JSONSanitize} from './objects.ts'; import {dotNotation, flattenObj, JSONSanitize} from './objects.ts';
import {LETTER_LIST} from './string.ts';
export function fromCsv<T = any>(csv: string, hasHeaders=true): T[] { export function fromCsv<T = any>(csv: string, hasHeaders=true): T[] {
const row = csv.split('\n'); const row = csv.split('\n');
let headers: any = hasHeaders ? row.splice(0, 1)[0] : null; let headers: any = hasHeaders ? row.splice(0, 1)[0] : null;
if(headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g); if(headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
return <T[]>row.map(r => { 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) => { const h = headers || (Array(props.length).fill(null).map((r, i) => {
let letter = ''; let letter = '';
const first = i / 26; const first = i / 26;
if(first > 1) letter += if(first > 1) letter += LETTER_LIST[Math.floor(first - 1)];
i % 26 letter += LETTER_LIST[i % 26];
i 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;
}, {});
}) })
} }