2018-11-09 18:40:19 +01:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2018
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
2020-07-29 16:27:55 +02:00
|
|
|
import * as flat from "flat";
|
|
|
|
const flatten = flat.default ? flat.default.flatten : flat.flatten;
|
2018-11-09 18:40:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* JSON to CSV operation
|
|
|
|
*/
|
|
|
|
class JSONToCSV extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JSONToCSV constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "JSON to CSV";
|
|
|
|
this.module = "Default";
|
2018-11-13 18:54:43 +01:00
|
|
|
this.description = "Converts JSON data to a CSV based on the definition in RFC 4180.";
|
2018-11-09 18:40:19 +01:00
|
|
|
this.infoURL = "https://wikipedia.org/wiki/Comma-separated_values";
|
|
|
|
this.inputType = "JSON";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
name: "Cell delimiter",
|
|
|
|
type: "binaryShortString",
|
|
|
|
value: ","
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Row delimiter",
|
|
|
|
type: "binaryShortString",
|
|
|
|
value: "\\r\\n"
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-07-29 16:27:55 +02:00
|
|
|
/**
|
2021-02-01 17:57:42 +01:00
|
|
|
* Converts JSON to a CSV equivalent.
|
2020-07-29 16:27:55 +02:00
|
|
|
*
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
2021-02-01 17:57:42 +01:00
|
|
|
toCSV() {
|
2020-07-29 16:27:55 +02:00
|
|
|
const self = this;
|
|
|
|
// If the JSON is an array of arrays, this is easy
|
|
|
|
if (this.flattened[0] instanceof Array) {
|
|
|
|
return this.flattened
|
|
|
|
.map(row => row
|
|
|
|
.map(self.escapeCellContents.bind(self))
|
|
|
|
.join(this.cellDelim)
|
|
|
|
)
|
|
|
|
.join(this.rowDelim) +
|
|
|
|
this.rowDelim;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If it's an array of dictionaries...
|
|
|
|
const header = Object.keys(this.flattened[0]);
|
|
|
|
return header
|
|
|
|
.map(self.escapeCellContents.bind(self))
|
|
|
|
.join(this.cellDelim) +
|
|
|
|
this.rowDelim +
|
|
|
|
this.flattened
|
|
|
|
.map(row => header
|
|
|
|
.map(h => row[h])
|
|
|
|
.map(self.escapeCellContents.bind(self))
|
|
|
|
.join(this.cellDelim)
|
|
|
|
)
|
|
|
|
.join(this.rowDelim) +
|
|
|
|
this.rowDelim;
|
|
|
|
}
|
|
|
|
|
2018-11-09 18:40:19 +01:00
|
|
|
/**
|
|
|
|
* @param {JSON} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
|
|
|
const [cellDelim, rowDelim] = args;
|
|
|
|
|
2018-11-13 19:05:52 +01:00
|
|
|
// Record values so they don't have to be passed to other functions explicitly
|
2018-11-13 18:54:43 +01:00
|
|
|
this.cellDelim = cellDelim;
|
|
|
|
this.rowDelim = rowDelim;
|
2020-07-29 16:27:55 +02:00
|
|
|
this.flattened = input;
|
|
|
|
if (!(this.flattened instanceof Array)) {
|
|
|
|
this.flattened = [input];
|
2019-04-28 22:29:15 +02:00
|
|
|
}
|
|
|
|
|
2018-11-09 18:40:19 +01:00
|
|
|
try {
|
2021-02-01 17:57:42 +01:00
|
|
|
return this.toCSV();
|
2018-11-09 18:40:19 +01:00
|
|
|
} catch (err) {
|
2020-07-29 16:27:55 +02:00
|
|
|
try {
|
|
|
|
this.flattened = flatten(input);
|
|
|
|
if (!(this.flattened instanceof Array)) {
|
|
|
|
this.flattened = [this.flattened];
|
|
|
|
}
|
2021-02-01 17:57:42 +01:00
|
|
|
return this.toCSV();
|
2020-07-29 16:27:55 +02:00
|
|
|
} catch (err) {
|
|
|
|
throw new OperationError("Unable to parse JSON to CSV: " + err.toString());
|
|
|
|
}
|
2018-11-13 18:54:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Correctly escapes a cell's contents based on the cell and row delimiters.
|
|
|
|
*
|
|
|
|
* @param {string} data
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
escapeCellContents(data) {
|
2019-04-28 22:29:15 +02:00
|
|
|
if (typeof data === "number") data = data.toString();
|
|
|
|
|
2018-11-13 18:54:43 +01:00
|
|
|
// Double quotes should be doubled up
|
|
|
|
data = data.replace(/"/g, '""');
|
|
|
|
|
|
|
|
// If the cell contains a cell or row delimiter or a double quote, it mut be enclosed in double quotes
|
|
|
|
if (
|
|
|
|
data.indexOf(this.cellDelim) >= 0 ||
|
|
|
|
data.indexOf(this.rowDelim) >= 0 ||
|
|
|
|
data.indexOf("\n") >= 0 ||
|
|
|
|
data.indexOf("\r") >= 0 ||
|
|
|
|
data.indexOf('"') >= 0
|
|
|
|
) {
|
|
|
|
data = `"${data}"`;
|
2018-11-09 18:40:19 +01:00
|
|
|
}
|
2018-11-13 18:54:43 +01:00
|
|
|
|
|
|
|
return data;
|
2018-11-09 18:40:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
export default JSONToCSV;
|