@ztimson/utils 0.22.2 → 0.22.4
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 +1 -1
- package/dist/files.d.ts +7 -0
- package/dist/index.cjs +16 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +16 -14
- package/dist/index.mjs.map +1 -1
- package/dist/objects.d.ts +2 -2
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -53,8 +53,9 @@ function encodeQuery(data) {
|
|
|
53
53
|
function flattenObj(obj, parent, result = {}) {
|
|
54
54
|
if (typeof obj === "object" && !Array.isArray(obj)) {
|
|
55
55
|
for (const key of Object.keys(obj)) {
|
|
56
|
-
const propName = parent ? parent
|
|
57
|
-
if (typeof obj[key] === "object") {
|
|
56
|
+
const propName = parent ? `${parent}.${key}` : key;
|
|
57
|
+
if (typeof obj[key] === "object" && obj[key] != null && !Array.isArray(obj[key])) {
|
|
58
|
+
console.log(propName);
|
|
58
59
|
flattenObj(obj[key], propName, result);
|
|
59
60
|
} else {
|
|
60
61
|
result[propName] = obj[key];
|
|
@@ -407,21 +408,13 @@ class Cache {
|
|
|
407
408
|
return this;
|
|
408
409
|
}
|
|
409
410
|
}
|
|
410
|
-
function
|
|
411
|
-
const headers = target.reduce((acc, row) =>
|
|
412
|
-
Object.keys(flatten ? flattenObj(row) : row).forEach((key) => {
|
|
413
|
-
if (!acc.includes(key)) acc.push(key);
|
|
414
|
-
});
|
|
415
|
-
return acc;
|
|
416
|
-
}, []);
|
|
411
|
+
function toCsv(target, flatten = true) {
|
|
412
|
+
const headers = new ASet(target.reduce((acc, row) => [...acc, ...Object.keys(flatten ? flattenObj(row) : row)], []));
|
|
417
413
|
return [
|
|
418
414
|
headers.join(","),
|
|
419
415
|
...target.map((row) => headers.map((h) => {
|
|
420
416
|
const value = dotNotation(row, h);
|
|
421
|
-
|
|
422
|
-
if (type == "string" && value.includes(",")) return `"${value}"`;
|
|
423
|
-
if (type == "object") return `"${JSON.stringify(value)}"`;
|
|
424
|
-
return value;
|
|
417
|
+
return typeof value == "object" && value != null ? '"' + JSONSanitize(value).replaceAll('"', '""') + '"' : value;
|
|
425
418
|
}).join(","))
|
|
426
419
|
].join("\n");
|
|
427
420
|
}
|
|
@@ -496,6 +489,14 @@ function fileBrowser(options = {}) {
|
|
|
496
489
|
input.click();
|
|
497
490
|
});
|
|
498
491
|
}
|
|
492
|
+
function fileText(file) {
|
|
493
|
+
return new Promise((resolve, reject) => {
|
|
494
|
+
const reader = new FileReader();
|
|
495
|
+
reader.onload = () => resolve(reader.result);
|
|
496
|
+
reader.onerror = () => reject(reader.error);
|
|
497
|
+
reader.readAsText(file);
|
|
498
|
+
});
|
|
499
|
+
}
|
|
499
500
|
function timestampFilename(name, date = /* @__PURE__ */ new Date()) {
|
|
500
501
|
if (typeof date == "number" || typeof date == "string") date = new Date(date);
|
|
501
502
|
const timestamp = `${date.getFullYear()}-${(date.getMonth() + 1).toString().padStart(2, "0")}-${date.getDate().toString().padStart(2, "0")}_${date.getHours().toString().padStart(2, "0")}-${date.getMinutes().toString().padStart(2, "0")}-${date.getSeconds().toString().padStart(2, "0")}`;
|
|
@@ -1515,7 +1516,6 @@ export {
|
|
|
1515
1516
|
arrayDiff,
|
|
1516
1517
|
caseInsensitiveSort,
|
|
1517
1518
|
clean,
|
|
1518
|
-
csv,
|
|
1519
1519
|
dec2Frac,
|
|
1520
1520
|
deepCopy,
|
|
1521
1521
|
deepMerge,
|
|
@@ -1526,6 +1526,7 @@ export {
|
|
|
1526
1526
|
errorFromCode,
|
|
1527
1527
|
escapeRegex,
|
|
1528
1528
|
fileBrowser,
|
|
1529
|
+
fileText,
|
|
1529
1530
|
findByProp,
|
|
1530
1531
|
flattenArr,
|
|
1531
1532
|
flattenObj,
|
|
@@ -1555,6 +1556,7 @@ export {
|
|
|
1555
1556
|
strSplice,
|
|
1556
1557
|
timeUntil,
|
|
1557
1558
|
timestampFilename,
|
|
1559
|
+
toCsv,
|
|
1558
1560
|
typeKeys,
|
|
1559
1561
|
uploadWithProgress,
|
|
1560
1562
|
validateEmail
|