Compare commits

..

2 Commits

Author SHA1 Message Date
49a2df8cd4 Fixed timezone format
All checks were successful
Build / Build NPM Project (push) Successful in 1m15s
Build / Tag Version (push) Successful in 13s
Build / Publish Documentation (push) Successful in 1m57s
2024-11-14 22:58:24 -05:00
ccf3fcb043 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
2024-11-09 17:27:05 -05:00
4 changed files with 34 additions and 41 deletions

View File

@ -8,10 +8,10 @@
<script type="module">
import {fromCsv, toCsv} from './dist/index.mjs';
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' +
'49,,,,,,,,,,system,2024-11-09T19:05:04.933Z,system,2024-11-09T19:05:04.933Z';
const csv = '_id,any,boolean,date,file,foreignKey,formula,javaScript,number,string,_createdBy,_createdDate,_updatedBy,_updatedDate\n' +
'34,,true,,,,,,,,system,2024-11-09T12:06:50.023Z,system,2024-11-09T17:44:42.512Z\n' +
'37,,,,,,,,,,system,2024-11-09T18:53:21.793Z,system,2024-11-09T18:53:21.793Z\n' +
'38,,,,,,,,,,system,2024-11-09T18:53:21.796Z,system,2024-11-09T18:53:21.796Z';
console.log(fromCsv(csv));
</script>

View File

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

View File

@ -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;
}, {});
})
}

View File

@ -84,36 +84,10 @@ export function formatDate(date: Date | number | string, format = 'YYYY-MM-DD H:
return (offset > 0 ? '-' : '') + `${hours}:${minutes.toString().padStart(2, '0')}`;
}
function timezone(offset: number) {
const hours = offset / 60;
switch (hours) {
case -12: return "IDLW";
case -11: return "SST";
case -10: return "HST";
case -9: return "AKST";
case -8: return "PST";
case -7: return "MST";
case -6: return "CST";
case -5: return "EST";
case -4: return "AST";
case -3: return "ART";
case -2: return "FNT";
case -1: return "AZOT";
case 0: return "UTC";
case 1: return "CET";
case 2: return "EET";
case 3: return "MSK";
case 4: return "SAMT";
case 5: return "YEKT";
case 6: return "OMST";
case 7: return "KRAT";
case 8: return "CST";
case 9: return "JST";
case 10: return "AEST";
case 11: return "SBT";
case 12: return "NZST";
default: return '';
}
function timezone(date: Date): string {
const formatter = new Intl.DateTimeFormat('en-US', {timeZoneName: 'short'});
const formattedDate = formatter.format(date);
return formattedDate.split(' ').pop() || '';
}
return format
@ -155,7 +129,7 @@ export function formatDate(date: Date | number | string, format = 'YYYY-MM-DD H:
// Timezone
.replaceAll('ZZ', tzOffset(date.getTimezoneOffset()).replace(':', ''))
.replaceAll('Z', tzOffset(date.getTimezoneOffset()))
.replaceAll('z', timezone(date.getTimezoneOffset()));
.replaceAll('z', timezone(date));
}
/**