@ztimson/utils 0.23.14 → 0.23.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/csv.d.ts +9 -2
- package/dist/index.cjs +37 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +37 -19
- package/dist/index.mjs.map +1 -1
- package/dist/string.d.ts +4 -0
- package/package.json +1 -1
package/dist/csv.d.ts
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a CSV string into an array of objects
|
|
3
|
+
*
|
|
4
|
+
* @param csv String with CSV
|
|
5
|
+
* @param hasHeaders First line of CSV contains headers
|
|
6
|
+
* @return {T[]} Array of parsed objects
|
|
7
|
+
*/
|
|
1
8
|
export declare function fromCsv<T = any>(csv: string, hasHeaders?: boolean): T[];
|
|
2
9
|
/**
|
|
3
|
-
* Convert an
|
|
10
|
+
* Convert an array of objects to a CSV string
|
|
4
11
|
*
|
|
5
12
|
* @param {any[]} target Array of objects to create CSV from
|
|
6
13
|
* @param {boolean} flatten Should nested object be flattened or treated as values
|
|
7
14
|
* @return {string} CSV string
|
|
8
15
|
*/
|
|
9
|
-
export declare function toCsv(target: any
|
|
16
|
+
export declare function toCsv(target: any, flatten?: boolean): string;
|
package/dist/index.cjs
CHANGED
|
@@ -543,6 +543,14 @@ ${opts.message || this.desc}`;
|
|
|
543
543
|
const NUMBER_LIST = "0123456789";
|
|
544
544
|
const SYMBOL_LIST = "~`!@#$%^&*()_-+={[}]|\\:;\"'<,>.?/";
|
|
545
545
|
const CHAR_LIST = LETTER_LIST + LETTER_LIST.toLowerCase() + NUMBER_LIST + SYMBOL_LIST;
|
|
546
|
+
function camelCase(text) {
|
|
547
|
+
if (!text) return "";
|
|
548
|
+
text = text.replaceAll(/^[0-9]+/g, "").replaceAll(/[^a-zA-Z0-9]+(\w?)/g, (...args) => {
|
|
549
|
+
var _a;
|
|
550
|
+
return ((_a = args[1]) == null ? void 0 : _a.toUpperCase()) || "";
|
|
551
|
+
});
|
|
552
|
+
return text[0].toLowerCase() + text.slice(1);
|
|
553
|
+
}
|
|
546
554
|
function formatBytes(bytes, decimals = 2) {
|
|
547
555
|
if (bytes === 0) return "0 Bytes";
|
|
548
556
|
const k = 1024;
|
|
@@ -680,24 +688,29 @@ ${opts.message || this.desc}`;
|
|
|
680
688
|
return /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/.test(email);
|
|
681
689
|
}
|
|
682
690
|
function fromCsv(csv, hasHeaders = true) {
|
|
683
|
-
|
|
691
|
+
function parseLine(line) {
|
|
692
|
+
const columns = [];
|
|
693
|
+
let current = "", inQuotes = false;
|
|
694
|
+
for (let i = 0; i < line.length; i++) {
|
|
695
|
+
const char = line[i];
|
|
696
|
+
const nextChar = line[i + 1];
|
|
697
|
+
if (char === '"') {
|
|
698
|
+
if (inQuotes && nextChar === '"') {
|
|
699
|
+
current += '"';
|
|
700
|
+
i++;
|
|
701
|
+
} else inQuotes = !inQuotes;
|
|
702
|
+
} else if (char === "," && !inQuotes) {
|
|
703
|
+
columns.push(current);
|
|
704
|
+
current = "";
|
|
705
|
+
} else current += char;
|
|
706
|
+
}
|
|
707
|
+
columns.push(current);
|
|
708
|
+
return columns.map((col) => col.replace(/^"|"$/g, "").replace(/""/g, '"'));
|
|
709
|
+
}
|
|
710
|
+
const row = csv.split(/\r?\n/);
|
|
684
711
|
let headers = hasHeaders ? row.splice(0, 1)[0] : null;
|
|
685
|
-
if (headers) headers = headers.match(/(?:[^,"']+|"[^"]*"|'[^']*')+/g);
|
|
712
|
+
if (headers) headers = headers.match(/(?:[^,"']+|"(?:[^"]|"")*"|'(?:[^']|'')*')+/g);
|
|
686
713
|
return row.map((r2) => {
|
|
687
|
-
function parseLine(line) {
|
|
688
|
-
const parts = line.split(","), columns = [];
|
|
689
|
-
let quoted = false;
|
|
690
|
-
for (const p2 of parts) {
|
|
691
|
-
if (quoted) columns[columns.length - 1] = columns.at(-1) + "," + p2;
|
|
692
|
-
else columns.push(p2);
|
|
693
|
-
if (/[^"]"$/g.test(p2)) {
|
|
694
|
-
quoted = false;
|
|
695
|
-
} else if (/^"[^"]/g.test(p2)) {
|
|
696
|
-
quoted = true;
|
|
697
|
-
}
|
|
698
|
-
}
|
|
699
|
-
return columns;
|
|
700
|
-
}
|
|
701
714
|
const props = parseLine(r2);
|
|
702
715
|
const h = headers || Array(props.length).fill(null).map((r22, i) => {
|
|
703
716
|
let letter = "";
|
|
@@ -713,12 +726,16 @@ ${opts.message || this.desc}`;
|
|
|
713
726
|
});
|
|
714
727
|
}
|
|
715
728
|
function toCsv(target, flatten = true) {
|
|
716
|
-
const
|
|
729
|
+
const t = makeArray(target);
|
|
730
|
+
const headers = new ASet(t.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
|
|
717
731
|
return [
|
|
718
732
|
headers.join(","),
|
|
719
|
-
...
|
|
733
|
+
...t.map((row) => headers.map((h) => {
|
|
720
734
|
const value2 = dotNotation(row, h);
|
|
721
|
-
|
|
735
|
+
if (value2 == null) return "";
|
|
736
|
+
if (typeof value2 == "object") return `"${JSONSanitize(value2).replaceAll("`", '""')}"`;
|
|
737
|
+
if (typeof value2 == "string" && /[\n"]/g.test(value2)) return `"${value2.replaceAll('"', '""')}"`;
|
|
738
|
+
return value2;
|
|
722
739
|
}).join(","))
|
|
723
740
|
].join("\n");
|
|
724
741
|
}
|
|
@@ -1701,6 +1718,7 @@ ${opts.message || this.desc}`;
|
|
|
1701
1718
|
exports.adjustedInterval = adjustedInterval;
|
|
1702
1719
|
exports.arrayDiff = arrayDiff;
|
|
1703
1720
|
exports.blackOrWhite = blackOrWhite;
|
|
1721
|
+
exports.camelCase = camelCase;
|
|
1704
1722
|
exports.caseInsensitiveSort = caseInsensitiveSort;
|
|
1705
1723
|
exports.clean = clean;
|
|
1706
1724
|
exports.dec2Frac = dec2Frac;
|