react-csv-autopilot 1.0.0 → 1.1.0
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/index.cjs +1 -6
- package/dist/index.js +1 -6
- package/dist/worker.js +114 -0
- package/package.json +4 -3
- package/dist/index.d.cts +0 -59
- package/dist/index.d.ts +0 -59
package/dist/index.cjs
CHANGED
|
@@ -71,12 +71,7 @@ var _WorkerManager = class _WorkerManager {
|
|
|
71
71
|
constructor() {
|
|
72
72
|
__privateAdd(this, _WorkerManager_instances);
|
|
73
73
|
__privateAdd(this, _worker);
|
|
74
|
-
|
|
75
|
-
try {
|
|
76
|
-
workerUrl = new URL("./worker.js", import_meta.url);
|
|
77
|
-
} catch {
|
|
78
|
-
workerUrl = "/worker.js";
|
|
79
|
-
}
|
|
74
|
+
const workerUrl = new URL("./worker.js", import_meta.url);
|
|
80
75
|
__privateSet(this, _worker, new Worker(workerUrl, {
|
|
81
76
|
name: WEB_WORKER_NAME,
|
|
82
77
|
type: "module"
|
package/dist/index.js
CHANGED
|
@@ -44,12 +44,7 @@ var _WorkerManager = class _WorkerManager {
|
|
|
44
44
|
constructor() {
|
|
45
45
|
__privateAdd(this, _WorkerManager_instances);
|
|
46
46
|
__privateAdd(this, _worker);
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
workerUrl = new URL("./worker.js", import.meta.url);
|
|
50
|
-
} catch {
|
|
51
|
-
workerUrl = "/worker.js";
|
|
52
|
-
}
|
|
47
|
+
const workerUrl = new URL("./worker.js", import.meta.url);
|
|
53
48
|
__privateSet(this, _worker, new Worker(workerUrl, {
|
|
54
49
|
name: WEB_WORKER_NAME,
|
|
55
50
|
type: "module"
|
package/dist/worker.js
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// src/core/utils.ts
|
|
2
|
+
function objectsToCSV(jsonArray, columns, includeHeaders) {
|
|
3
|
+
if (!jsonArray.length || !columns.length) return "";
|
|
4
|
+
const rows = [];
|
|
5
|
+
if (includeHeaders) {
|
|
6
|
+
rows.push(columns.map((col) => col.label).join(","));
|
|
7
|
+
}
|
|
8
|
+
jsonArray.forEach((row) => {
|
|
9
|
+
rows.push(
|
|
10
|
+
columns.map((col) => {
|
|
11
|
+
const val = getNested(row, col.key);
|
|
12
|
+
const normalizedValue = normalisedValue(val);
|
|
13
|
+
const maybeFormattedValue = getFormatter(col, normalizedValue);
|
|
14
|
+
return maybeFormattedValue;
|
|
15
|
+
}).join(",")
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
return `${rows.join("\n")}
|
|
19
|
+
`;
|
|
20
|
+
}
|
|
21
|
+
function getNested(obj, keyPath) {
|
|
22
|
+
return keyPath.split(".").reduce((acc, key) => {
|
|
23
|
+
if (acc && typeof acc === "object") {
|
|
24
|
+
return acc[key];
|
|
25
|
+
}
|
|
26
|
+
return void 0;
|
|
27
|
+
}, obj);
|
|
28
|
+
}
|
|
29
|
+
function normalisedValue(value) {
|
|
30
|
+
let result = value;
|
|
31
|
+
if (typeof result === "string") {
|
|
32
|
+
result = `"${result.replace(/"/g, '""')}"`;
|
|
33
|
+
}
|
|
34
|
+
if (result === void 0 || result === null) {
|
|
35
|
+
result = "";
|
|
36
|
+
}
|
|
37
|
+
return result;
|
|
38
|
+
}
|
|
39
|
+
function makeFormatters(locale = "en-US", timeZone = "UTC", currency = "USD") {
|
|
40
|
+
return {
|
|
41
|
+
dateFull: new Intl.DateTimeFormat(locale, { dateStyle: "full", timeZone }),
|
|
42
|
+
dateMediumTime: new Intl.DateTimeFormat(locale, {
|
|
43
|
+
dateStyle: "medium",
|
|
44
|
+
timeStyle: "short",
|
|
45
|
+
timeZone
|
|
46
|
+
}),
|
|
47
|
+
numCompact: new Intl.NumberFormat(locale, {
|
|
48
|
+
maximumFractionDigits: 1,
|
|
49
|
+
notation: "compact"
|
|
50
|
+
}),
|
|
51
|
+
numCurrency: new Intl.NumberFormat(locale, {
|
|
52
|
+
currency,
|
|
53
|
+
maximumFractionDigits: 2,
|
|
54
|
+
style: "currency"
|
|
55
|
+
}),
|
|
56
|
+
numDecimal: new Intl.NumberFormat(locale, { maximumFractionDigits: 2 }),
|
|
57
|
+
numPercent: new Intl.NumberFormat(locale, {
|
|
58
|
+
maximumFractionDigits: 1,
|
|
59
|
+
style: "percent"
|
|
60
|
+
}),
|
|
61
|
+
timeShort: new Intl.DateTimeFormat(locale, {
|
|
62
|
+
timeStyle: "short",
|
|
63
|
+
timeZone
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
function getFormatter(col, value) {
|
|
68
|
+
const formatters = makeFormatters();
|
|
69
|
+
const availableFormatters = Object.keys(formatters);
|
|
70
|
+
if ("formatType" in col && availableFormatters.includes(col.formatType)) {
|
|
71
|
+
const enhance = formatters[col.formatType];
|
|
72
|
+
return enhance.format(Number(value));
|
|
73
|
+
} else {
|
|
74
|
+
return value;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// src/core/worker.ts
|
|
79
|
+
var headersWritten = /* @__PURE__ */ new Map();
|
|
80
|
+
self.onmessage = (event) => {
|
|
81
|
+
const msg = event.data;
|
|
82
|
+
try {
|
|
83
|
+
switch (msg.type) {
|
|
84
|
+
case "to_csv_chunk": {
|
|
85
|
+
const { columns, data, id } = msg;
|
|
86
|
+
const csvChunk = objectsToCSV(data, columns, !headersWritten.get(id));
|
|
87
|
+
const out = {
|
|
88
|
+
id,
|
|
89
|
+
result: csvChunk,
|
|
90
|
+
type: "csv_chunk"
|
|
91
|
+
};
|
|
92
|
+
headersWritten.set(id, true);
|
|
93
|
+
self.postMessage(out);
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
case "completed": {
|
|
97
|
+
const out = { id: msg.id, type: "done" };
|
|
98
|
+
self.postMessage(out);
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
default: {
|
|
102
|
+
console.warn(`Unsupported for worker message:: ${JSON.stringify(msg)}`);
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
} catch (error) {
|
|
107
|
+
const _error = error instanceof Error ? error : new Error(String(error));
|
|
108
|
+
self.postMessage({
|
|
109
|
+
error: _error,
|
|
110
|
+
id: msg.id,
|
|
111
|
+
type: "error"
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
};
|
package/package.json
CHANGED
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
},
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
|
+
"types": "./dist/index.d.ts",
|
|
16
17
|
"import": "./dist/index.js",
|
|
17
|
-
"require": "./dist/index.cjs"
|
|
18
|
-
"types": "./dist/index.d.ts"
|
|
18
|
+
"require": "./dist/index.cjs"
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
@@ -55,10 +55,11 @@
|
|
|
55
55
|
},
|
|
56
56
|
"scripts": {
|
|
57
57
|
"build": "tsup",
|
|
58
|
+
"build:watch": "npm run build --watch",
|
|
58
59
|
"dev": "tsup --watch"
|
|
59
60
|
},
|
|
60
61
|
"sideEffects": false,
|
|
61
62
|
"type": "module",
|
|
62
63
|
"types": "dist/index.d.ts",
|
|
63
|
-
"version": "1.
|
|
64
|
+
"version": "1.1.0"
|
|
64
65
|
}
|
package/dist/index.d.cts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
type formatterTypes = "dateFull" | "dateMediumTime" | "timeShort" | "numDecimal" | "numCompact" | "numCurrency" | "numPercent";
|
|
2
|
-
type Column = {
|
|
3
|
-
key: string;
|
|
4
|
-
label: string;
|
|
5
|
-
timezone?: "UTC" | string;
|
|
6
|
-
formatType?: formatterTypes;
|
|
7
|
-
};
|
|
8
|
-
type ExportParams<T> = {
|
|
9
|
-
fileName: string;
|
|
10
|
-
columns: Column[];
|
|
11
|
-
getNextPage: (offset: number) => Promise<{
|
|
12
|
-
rows: T[];
|
|
13
|
-
total: number;
|
|
14
|
-
}>;
|
|
15
|
-
};
|
|
16
|
-
type ExportResponse = {
|
|
17
|
-
finished: boolean;
|
|
18
|
-
totalRowsLoaded: number;
|
|
19
|
-
};
|
|
20
|
-
interface ExportStrategy {
|
|
21
|
-
export<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
declare class BolbExportStrategy implements ExportStrategy {
|
|
25
|
-
private workerManager;
|
|
26
|
-
constructor();
|
|
27
|
-
export<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
28
|
-
private downloadBlob;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
declare class FsAccessExportStrategy implements ExportStrategy {
|
|
32
|
-
private workerManager;
|
|
33
|
-
constructor();
|
|
34
|
-
export<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type ExportControllerDeps = {
|
|
38
|
-
fsAccessStrategy: FsAccessExportStrategy;
|
|
39
|
-
blobExportStrategy: BolbExportStrategy;
|
|
40
|
-
};
|
|
41
|
-
declare class ExportController {
|
|
42
|
-
#private;
|
|
43
|
-
private readonly deps;
|
|
44
|
-
constructor(deps: ExportControllerDeps);
|
|
45
|
-
start<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
declare function useExportCSV(): {
|
|
49
|
-
handler: ExportController;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
type Payload = {
|
|
53
|
-
total: number;
|
|
54
|
-
loadedItemsCount: number;
|
|
55
|
-
state: "progress" | "failed" | "done";
|
|
56
|
-
};
|
|
57
|
-
declare function useMessageExportCSV(cb: (payload: Payload) => void): void;
|
|
58
|
-
|
|
59
|
-
export { type Column, ExportController, type ExportParams, useExportCSV, useMessageExportCSV };
|
package/dist/index.d.ts
DELETED
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
type formatterTypes = "dateFull" | "dateMediumTime" | "timeShort" | "numDecimal" | "numCompact" | "numCurrency" | "numPercent";
|
|
2
|
-
type Column = {
|
|
3
|
-
key: string;
|
|
4
|
-
label: string;
|
|
5
|
-
timezone?: "UTC" | string;
|
|
6
|
-
formatType?: formatterTypes;
|
|
7
|
-
};
|
|
8
|
-
type ExportParams<T> = {
|
|
9
|
-
fileName: string;
|
|
10
|
-
columns: Column[];
|
|
11
|
-
getNextPage: (offset: number) => Promise<{
|
|
12
|
-
rows: T[];
|
|
13
|
-
total: number;
|
|
14
|
-
}>;
|
|
15
|
-
};
|
|
16
|
-
type ExportResponse = {
|
|
17
|
-
finished: boolean;
|
|
18
|
-
totalRowsLoaded: number;
|
|
19
|
-
};
|
|
20
|
-
interface ExportStrategy {
|
|
21
|
-
export<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
declare class BolbExportStrategy implements ExportStrategy {
|
|
25
|
-
private workerManager;
|
|
26
|
-
constructor();
|
|
27
|
-
export<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
28
|
-
private downloadBlob;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
declare class FsAccessExportStrategy implements ExportStrategy {
|
|
32
|
-
private workerManager;
|
|
33
|
-
constructor();
|
|
34
|
-
export<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
type ExportControllerDeps = {
|
|
38
|
-
fsAccessStrategy: FsAccessExportStrategy;
|
|
39
|
-
blobExportStrategy: BolbExportStrategy;
|
|
40
|
-
};
|
|
41
|
-
declare class ExportController {
|
|
42
|
-
#private;
|
|
43
|
-
private readonly deps;
|
|
44
|
-
constructor(deps: ExportControllerDeps);
|
|
45
|
-
start<T>(params: ExportParams<T>): Promise<ExportResponse>;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
declare function useExportCSV(): {
|
|
49
|
-
handler: ExportController;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
type Payload = {
|
|
53
|
-
total: number;
|
|
54
|
-
loadedItemsCount: number;
|
|
55
|
-
state: "progress" | "failed" | "done";
|
|
56
|
-
};
|
|
57
|
-
declare function useMessageExportCSV(cb: (payload: Payload) => void): void;
|
|
58
|
-
|
|
59
|
-
export { type Column, ExportController, type ExportParams, useExportCSV, useMessageExportCSV };
|