-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'CSV to JSON' and 'JSON to CSV' operations. Closes #277
- Loading branch information
Showing
5 changed files
with
160 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* @author n1474335 [[email protected]] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
import OperationError from "../errors/OperationError"; | ||
import Utils from "../Utils"; | ||
|
||
/** | ||
* CSV to JSON operation | ||
*/ | ||
class CSVToJSON extends Operation { | ||
|
||
/** | ||
* CSVToJSON constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "CSV to JSON"; | ||
this.module = "Default"; | ||
this.description = "Converts a CSV file to JSON format."; | ||
this.infoURL = "https://wikipedia.org/wiki/Comma-separated_values"; | ||
this.inputType = "string"; | ||
this.outputType = "JSON"; | ||
this.args = [ | ||
{ | ||
name: "Cell delimiters", | ||
type: "binaryShortString", | ||
value: "," | ||
}, | ||
{ | ||
name: "Row delimiters", | ||
type: "binaryShortString", | ||
value: "\\r\\n" | ||
}, | ||
{ | ||
name: "Format", | ||
type: "option", | ||
value: ["Array of dictionaries", "Array of arrays"] | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {string} input | ||
* @param {Object[]} args | ||
* @returns {JSON} | ||
*/ | ||
run(input, args) { | ||
const [cellDelims, rowDelims, format] = args; | ||
let json, header; | ||
|
||
try { | ||
json = Utils.parseCSV(input, cellDelims.split(""), rowDelims.split("")); | ||
} catch (err) { | ||
throw new OperationError("Unable to parse CSV: " + err); | ||
} | ||
|
||
switch (format) { | ||
case "Array of dictionaries": | ||
header = json[0]; | ||
return json.slice(1).map(row => { | ||
const obj = {}; | ||
header.forEach((h, i) => { | ||
obj[h] = row[i]; | ||
}); | ||
return obj; | ||
}); | ||
case "Array of arrays": | ||
default: | ||
return json; | ||
} | ||
} | ||
|
||
} | ||
|
||
export default CSVToJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @author n1474335 [[email protected]] | ||
* @copyright Crown Copyright 2018 | ||
* @license Apache-2.0 | ||
*/ | ||
|
||
import Operation from "../Operation"; | ||
import OperationError from "../errors/OperationError"; | ||
|
||
/** | ||
* JSON to CSV operation | ||
*/ | ||
class JSONToCSV extends Operation { | ||
|
||
/** | ||
* JSONToCSV constructor | ||
*/ | ||
constructor() { | ||
super(); | ||
|
||
this.name = "JSON to CSV"; | ||
this.module = "Default"; | ||
this.description = "Converts JSON data to a CSV."; | ||
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" | ||
} | ||
]; | ||
} | ||
|
||
/** | ||
* @param {JSON} input | ||
* @param {Object[]} args | ||
* @returns {string} | ||
*/ | ||
run(input, args) { | ||
const [cellDelim, rowDelim] = args; | ||
|
||
try { | ||
// If the JSON is an array of arrays, this is easy | ||
if (input[0] instanceof Array) { | ||
return input.map(row => row.join(cellDelim)).join(rowDelim) + rowDelim; | ||
} | ||
|
||
// If it's an array of dictionaries... | ||
const header = Object.keys(input[0]); | ||
return header.join(cellDelim) + | ||
rowDelim + | ||
input.map( | ||
row => header.map( | ||
h => row[h] | ||
).join(cellDelim) | ||
).join(rowDelim) + | ||
rowDelim; | ||
} catch (err) { | ||
throw new OperationError("Unable to parse JSON to CSV: " + err); | ||
} | ||
} | ||
|
||
} | ||
|
||
export default JSONToCSV; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters