-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcustomBatchProcessor.js
More file actions
55 lines (51 loc) · 1.96 KB
/
Copy pathcustomBatchProcessor.js
File metadata and controls
55 lines (51 loc) · 1.96 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
/**
* Custom CSV batch processor
* @module mmc/customBatchProcessor
* @author Michael L. Hobbs {@link https://github.com/MichaelLeeHobbs}
* @license MIT
*/
/**
* A call back function that transforms the value.
* @typedef {function(key, value)} transformer
* @callback transformer
* @return {string} value
*/
/**
* Parses a single row into a JSON object
* @param {string} row one line of delimited text
* @param {string} [columnDelim=','] column deliminator
* @param {[string]} [headers] Optionally, the headers. If undefined parser will return an array of string to be used as the headers. This is useful when the first row is the headers
* @param {transformer} [transformer] An optional callback transformer
* @return {([string]|Object)} returns an array of strings that can be used if the headers param is undefined or the parsed and transformed Object if headers was a valid array
*/
const parser = (row, columnDelim, headers, transformer) => {
columnDelim = columnDelim || ','
const data = String(row).trim().split(columnDelim)
return !headers ? data : data.reduce((acc, value, i) => {
const key = headers[i]
acc[key] = typeof transformer === 'function' ? transformer(key, value) : value
return acc
}, {})
}
/**
* An example of a raw batch processor use in production. Reads one line at a time and parses it into a JSON string
* using the header row as the property keys. Uses the first line as the header.
* @return {string|''}
*/
function getMessage() {
var headers = $gc('headers')
var line = reader.readLine()
if (!line) {
$gc('headers', null)
return ''
}
if (!headers) {
$gc('headers', parser(line, '|', headers))
return getMessage()
}
const parsed = parser(line, '|', headers, (key, value) => {
return (key === 'reportText') ? value.split(/\\.br\\/).map(ele => ele.trim()) : value
})
return parsed ? JSON.stringify(parsed) : ''
}
return getMessage()