Added Parsing to and from CSV

This commit is contained in:
Vimal-Raghubir 2018-03-25 19:49:17 -04:00
parent 2f5b0533d8
commit e069ae4991
6 changed files with 230 additions and 2 deletions

View file

@ -331,6 +331,8 @@ const Categories = [
"Extract EXIF",
"Numberwang",
"XKCD Random Number",
"Parse CSV to string",
"Parse string to CSV"
]
},
{

View file

@ -38,6 +38,7 @@ import StrUtils from "../operations/StrUtils.js";
import Tidy from "../operations/Tidy.js";
import Unicode from "../operations/Unicode.js";
import URL_ from "../operations/URL.js";
import CSVParser from "../operations/CSVParser.js";
/**
@ -4018,7 +4019,38 @@ const OperationConfig = {
inputType: "string",
outputType: "number",
args: []
}
},
"CSV To String": {
module: "CSVParser",
description: "Function used to parse CSV string to a regular string.",
inputType: "string",
outputType: "string",
args: [
{
name: "delimeter",
type: "string",
value: ","
},
{
name: "quotes",
type: "string",
value: '"'
}
]
},
"String to CSV": {
module: "CSVParser",
description: "Function used to parse a string array to a CSV string.",
inputType: "string",
outputType: "string",
args: [
{
name: "delimeter",
type: "string",
value: ","
}
]
}
};

View file

@ -0,0 +1,21 @@
import CSVParser from "../../operations/CSVParser.js";
/**
* CSVParser module.
*
* Libraries:
* - csv-string
*
* @author VimalRaghubir [vraghubir0418@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
let OpModules = typeof self === "undefined" ? {} : self.OpModules || {};
OpModules.CSVParser = {
"Parse from CSV": CSVParser.csvToString,
"Parse to CSV": CSVParser.stringToCSV,
};
export default OpModules;

View file

@ -21,6 +21,7 @@ import PublicKeyModule from "./PublicKey.js";
import RegexModule from "./Regex.js";
import ShellcodeModule from "./Shellcode.js";
import URLModule from "./URL.js";
import CSVModule from "./CSVParser.js"
Object.assign(
OpModules,
@ -37,7 +38,8 @@ Object.assign(
PublicKeyModule,
RegexModule,
ShellcodeModule,
URLModule
URLModule,
CSVModule
);
export default OpModules;

View file

@ -0,0 +1,48 @@
import { CSV } from 'csv-string';
/**
* @author VimalRaghubir [vraghubir0418@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
* @namespace
*/
const CSVParser = {
/**
* Parse from CSV
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
csvToString: function(input, args) {
var array = "";
if (input) {
var detectedDelimeter = CSV.detect(input);
if (detectedDelimeter != args[0]) {
args[0] = detectedDelimeter;
}
array = CSV.parse(input, args[0], args[1]);
} else {
array = "The passed in data is not a csv string. Please pass in a csv string.";
}
return array;
},
/**
* Parse to CSV
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
stringToCSV: function(input, args) {
var string = "";
if (input) {
string = CSV.stringify(input, args[0]);
} else {
string = "The passed in data is not a string that can be converted to a CSV.";
}
return string;
}
}
export default CSVParser;