convert-csv-to-json2
Version:
Convert CSV to JSON
253 lines (250 loc) • 7.29 kB
JavaScript
// src/util/fileUtils.ts
import {
readFileSync,
writeFile as fsWriteFile
} from "fs";
function readFile(fileInputName, encoding = "utf8" /* utf8 */) {
return readFileSync(fileInputName, encoding).toString();
}
function writeFile(jsonString, fileOutputName) {
fsWriteFile(fileOutputName, jsonString, function(err) {
if (err) {
throw err;
} else {
console.log("File saved: " + fileOutputName);
}
});
}
// src/util/jsonUtils.ts
function validateJson(jsonString) {
try {
JSON.parse(jsonString);
} catch (err) {
throw Error("Parsed csv has generated an invalid json!!!\n" + err);
}
}
// src/util/stringUtils.ts
function getValueFormatByType(value) {
let retVal = value;
if (value === void 0 || value === "") {
retVal = "";
} else if (!isNaN(Number(value)) && typeof value === "string" && value.trim() !== "") {
retVal = Number(value);
} else if (value === "true" || value === "false") {
retVal = value === "true";
}
return retVal;
}
// src/index.ts
var ConvertCsvToJson = class {
constructor() {
this.encoding = "utf8" /* utf8 */;
}
formatValueByType(active = true) {
this.printValueFormatByType = active;
return this;
}
supportQuotedField(active = false) {
this.isSupportQuotedField = active;
return this;
}
fieldDelimiter(delimiter) {
this.delimiter = delimiter;
return this;
}
trimHeaderFieldWhiteSpace(active = false) {
this.isTrimHeaderFieldWhiteSpace = active;
return this;
}
indexHeader(indexHeader) {
if (isNaN(indexHeader)) {
throw new Error("The index Header must be a Number!");
}
this._indexHeader = indexHeader;
return this;
}
parseSubArray(delimiter = "*", separator = ",") {
this.parseSubArrayDelimiter = delimiter;
this.parseSubArraySeparator = separator;
return this;
}
customEncoding(encoding) {
this.encoding = encoding;
return this;
}
utf8Encoding() {
this.encoding = "utf8" /* utf8 */;
return this;
}
ucs2Encoding() {
this.encoding = "ucs2" /* ucs2 */;
return this;
}
utf16leEncoding() {
this.encoding = "utf16le" /* utf16le */;
return this;
}
latin1Encoding() {
this.encoding = "latin1" /* latin1 */;
return this;
}
asciiEncoding() {
this.encoding = "ascii" /* ascii */;
return this;
}
base64Encoding() {
this.encoding = "base64" /* base64 */;
return this;
}
hexEncoding() {
this.encoding = "hex" /* hex */;
return this;
}
generateJsonFileFromCsv(fileInputName, fileOutputName) {
const jsonStringified = this.getJsonFromCsvStringified(fileInputName);
writeFile(jsonStringified, fileOutputName);
}
getJsonFromCsvStringified(fileInputName) {
const json = this.getJsonFromCsv(fileInputName);
const jsonStringified = JSON.stringify(json, void 0, 1);
validateJson(jsonStringified);
return jsonStringified;
}
getJsonFromCsv(fileInputName) {
const parsedCsv = readFile(fileInputName, this.encoding);
return this.csvToJson(parsedCsv);
}
csvStringToJson(csvString) {
return this.csvToJson(csvString);
}
csvToJson(parsedCsv) {
var _a;
this.validateInputConfig();
const newLine = /\r?\n/;
const defaultFieldDelimiter = ",";
let lines = parsedCsv.split(newLine);
const fieldDelimiter = this.delimiter || defaultFieldDelimiter;
let index = (_a = this._indexHeader) != null ? _a : 0;
let headers = [];
lines = lines.filter((line) => line !== void 0 && line.trim() !== "");
if (this.isSupportQuotedField) {
headers = this.split(lines[index]);
} else {
headers = lines[index].split(fieldDelimiter);
}
while (headers.length === 0 && index <= lines.length) {
index = index + 1;
headers = lines[index].split(fieldDelimiter);
}
const jsonResult = [];
for (let i = index + 1; i < lines.length; i++) {
let currentLine = [];
if (this.isSupportQuotedField) {
currentLine = this.split(lines[i]);
} else {
currentLine = lines[i].split(fieldDelimiter);
}
if (currentLine.length > 0) {
jsonResult.push(this.buildJsonResult(headers, currentLine));
}
}
return jsonResult;
}
buildJsonResult(headers, currentLine) {
const jsonObject = {};
for (let j = 0; j < headers.length; j++) {
const propertyName = this.isTrimHeaderFieldWhiteSpace ? headers[j].replace(/\s/g, "") : headers[j].trim();
let value = currentLine[j];
if (this.parseSubArrayDelimiter && value && typeof value === "string" && value.indexOf(this.parseSubArrayDelimiter) === 0 && value.lastIndexOf(this.parseSubArrayDelimiter) === value.length - 1) {
value = value.substring(
value.indexOf(this.parseSubArrayDelimiter) + 1,
value.lastIndexOf(this.parseSubArrayDelimiter)
).trim().split(this.parseSubArraySeparator);
}
if (this.printValueFormatByType && !Array.isArray(value)) {
value = getValueFormatByType(value);
}
jsonObject[propertyName] = value;
}
return jsonObject;
}
hasQuotes(line) {
return line.includes('"');
}
split(line) {
if (line.length == 0) {
return [];
}
const delim = this.delimiter || ",";
const subSplits = [""];
if (this.hasQuotes(line)) {
const chars = line.split("");
let subIndex = 0;
let startQuote = false;
let isDouble = false;
chars.forEach((c, i, arr) => {
if (isDouble) {
subSplits[subIndex] += c;
isDouble = false;
return;
}
if (c != '"' && c != delim) {
subSplits[subIndex] += c;
} else if (c == delim && startQuote) {
subSplits[subIndex] += c;
} else if (c == delim) {
subIndex++;
subSplits[subIndex] = "";
return;
} else {
if (arr[i + 1] === '"') {
isDouble = true;
} else {
if (!startQuote) {
startQuote = true;
} else {
startQuote = false;
}
}
}
});
if (startQuote) {
throw new Error("Row contains mismatched quotes!");
}
return subSplits;
} else {
return line.split(delim);
}
}
validateInputConfig() {
if (this.isSupportQuotedField) {
if (this.delimiter === '"') {
throw new Error(
'When SupportQuotedFields is enabled you cannot defined the field delimiter as quote -> ["]'
);
}
if (this.parseSubArraySeparator === '"') {
throw new Error(
'When SupportQuotedFields is enabled you cannot defined the field parseSubArraySeparator as quote -> ["]'
);
}
if (this.parseSubArrayDelimiter === '"') {
throw new Error(
'When SupportQuotedFields is enabled you cannot defined the field parseSubArrayDelimiter as quote -> ["]'
);
}
}
}
/**
* @deprecated Use generateJsonFileFromCsv()
*/
jsonToCsv(inputFileName, outputFileName) {
this.generateJsonFileFromCsv(inputFileName, outputFileName);
}
};
var index_default = new ConvertCsvToJson();
export {
ConvertCsvToJson,
index_default as default
};
//# sourceMappingURL=index.js.map