Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
ccf3fcb043 | |||
341ef37205 | |||
0eab2630ad |
@ -6,9 +6,14 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import {toCsv} from './dist/index.mjs';
|
import {fromCsv, toCsv} from './dist/index.mjs';
|
||||||
|
|
||||||
console.log(toCsv([{test:123, test2: {test: [1, 2, 3, 4], test3: true, test4: 'okay'}}]));
|
const csv = '' +
|
||||||
|
'_id,any,boolean,date,file,foreignKey,formula,javaScript,number,string,_createdBy,_createdDate,_updatedBy,_updatedDate\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, false));
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/utils",
|
"name": "@ztimson/utils",
|
||||||
"version": "0.22.3",
|
"version": "0.22.6",
|
||||||
"description": "Utility library",
|
"description": "Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
38
src/csv.ts
38
src/csv.ts
@ -1,5 +1,41 @@
|
|||||||
import {ASet} from './aset.ts';
|
import {ASet} from './aset.ts';
|
||||||
import {dotNotation, flattenObj, JSONAttemptParse, 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[] {
|
||||||
|
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 => {
|
||||||
|
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 += LETTER_LIST[Math.floor(first - 1)];
|
||||||
|
letter += LETTER_LIST[i % 26];
|
||||||
|
return letter;
|
||||||
|
}));
|
||||||
|
return h.reduce((acc: any, h: any, i: number) => {
|
||||||
|
dotNotation(acc, h, props[i]);
|
||||||
|
return acc;
|
||||||
|
}, {});
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert an object to a CSV string
|
* Convert an object to a CSV string
|
||||||
|
15
src/files.ts
15
src/files.ts
@ -52,6 +52,21 @@ export function fileBrowser(options: {accept?: string, multiple?: boolean} = {})
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract text from a file
|
||||||
|
*
|
||||||
|
* @param file File to extract text from
|
||||||
|
* @return {Promise<string | null>} File contents
|
||||||
|
*/
|
||||||
|
export function fileText(file: any): Promise<string | null> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = () => resolve(<string>reader.result);
|
||||||
|
reader.onerror = () => reject(reader.error);
|
||||||
|
reader.readAsText(file);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create timestamp intended for filenames from a date
|
* Create timestamp intended for filenames from a date
|
||||||
*
|
*
|
||||||
|
@ -1,22 +1,22 @@
|
|||||||
/**
|
/**
|
||||||
* String of all letters
|
* String of all letters
|
||||||
*/
|
*/
|
||||||
const LETTER_LIST = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
export const LETTER_LIST = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String of all numbers
|
* String of all numbers
|
||||||
*/
|
*/
|
||||||
const NUMBER_LIST = '0123456789';
|
export const NUMBER_LIST = '0123456789';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String of all symbols
|
* String of all symbols
|
||||||
*/
|
*/
|
||||||
const SYMBOL_LIST = '~`!@#$%^&*()_-+={[}]|\\:;"\'<,>.?/';
|
export const SYMBOL_LIST = '~`!@#$%^&*()_-+={[}]|\\:;"\'<,>.?/';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String of all letters, numbers & symbols
|
* String of all letters, numbers & symbols
|
||||||
*/
|
*/
|
||||||
const CHAR_LIST = LETTER_LIST + NUMBER_LIST + SYMBOL_LIST;
|
export const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert number of bytes into a human-readable size
|
* Convert number of bytes into a human-readable size
|
||||||
|
Reference in New Issue
Block a user