-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.ts
More file actions
153 lines (144 loc) · 5.63 KB
/
index.ts
File metadata and controls
153 lines (144 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import {
JsonToStringTransformStream,
JsonEvent,
JsonEventType,
StringToCsvTransformStream,
CsvEvent,
E_START_OBJECT,
E_START_ARRAY,
E_END_ARRAY,
E_END_OBJECT
} from "@quadient/evolve-data-transformations";
export function getDescription(): ScriptDescription {
return {
description: "Reads CSV data and converts them to JSON format.",
input: [
{
id: "source",
displayName: "Source CSV file",
description: "Source CSV resource that will be processed. Must be encoded in UTF-8.",
type: "InputResource",
required: true,
},
{
id: "target",
displayName: "Target JSON file",
description: "File the JSON file will be written to.",
type: "OutputResource",
required: true,
},
],
output: [],
};
}
export async function execute(context: Context) {
// Open Input
console.log("Reading from " + (context.parameters.source as string));
const input = await context.openReadText(context.parameters.source as string);
// Delete output file if it exists
try {
const outputFile = context.getFile(context.parameters.target as string);
await outputFile.delete();
} catch (e) {
// no problem, output is probably not a file or does not exist
}
// Open output
console.log("Writing to " + (context.parameters.target as string));
const output = await context.openWriteText(context.parameters.target as string);
// Run the stream transformation
try {
await transform(input, output);
} catch (e) {
// In case of error in transformation delete the output file, so that
// no partial result remains.
try {
const outputFile = context.getFile(context.parameters.target as string);
await outputFile.delete();
} catch (e) {
// no problem, output is probably not a file or does not exist
}
throw e;
}
}
export async function transform(input: ReadableStream<string>, output: WritableStream<string>) {
await input
.pipeThrough(new StringToCsvTransformStream())
.pipeThrough(new CsvCustomTransformStream())
.pipeThrough(new JsonToStringTransformStream())
.pipeTo(output);
}
class CsvCustomTransformStream extends TransformStream<CsvEvent, JsonEvent> {
constructor() {
super({
start: async(controller) => {
// Begin the JSON with '{ "Clients": [' sequence.
controller.enqueue(E_START_OBJECT);
controller.enqueue({type: JsonEventType.PROPERTY_NAME, data: "Clients"});
controller.enqueue(E_START_ARRAY);
},
transform: async (event, controller) => {
switch (event.type) {
case "values":
const d = event.data;
// construct the javascript object from the CSV values array
const obj = {
"CustID": d[0],
"CustName": d[1],
"CustMid": d[2],
"CustSur": d[3],
"CustMail":d[4],
"FromMail": d[5],
"CustPhone": d[6],
"FromPhone": d[7],
"Subject": d[8],
"CustGen": d[9],
"CustCompany": d[10],
"CustStreet": d[11],
"CustCity": d[12],
"CustZIP": d[13],
"CustCountry": d[14],
"CustState": d[15],
"CountryLong": d[16],
"Manager": d[17],
"Internet": d[18],
"Phone": d[19],
"Consultant": d[20],
"CustOption": d[21],
"Date": d[22],
"Open": d[23],
"High": d[24],
"Low": d[25],
"Close": d[26],
"Change": d[27],
"LastDate": d[28],
"LastOpen": d[29],
"LastHigh": d[30],
"LastLow": d[31],
"LastClose": d[32],
"LastChange": d[33],
"Initial_Amount": d[34],
"Jan": d[35],
"Feb": d[36],
"Mar": d[37],
"Apr": d[38],
"May": d[39],
"Jun": d[40],
"Jul": d[41],
"Aug": d[42],
"Sep": d[43],
"Oct": d[44],
"Nov": d[45],
"Dec": d[46],
}
// output the JSON event with the constructed object
controller.enqueue({type: JsonEventType.ANY_VALUE, data: obj});
}
},
flush: async(controller) => {
// End the JSON with ']}' sequence.
controller.enqueue(E_END_ARRAY);
controller.enqueue(E_END_OBJECT);
}
});
}
}