forked from iuccio/csvToJson
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (142 loc) · 3.56 KB
/
index.js
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"use strict";
let csvToJson = require("./src/csvToJson.js");
const encodingOps = {
utf8: 'utf8',
ucs2: 'ucs2',
utf16le: 'utf16le',
latin1: 'latin1',
ascii: 'ascii',
base64: 'base64',
hex: 'hex'
};
/**
* Prints a digit as Number type (for example 32 instead of '32')
*/
exports.formatValueByType = function (active = true) {
csvToJson.formatValueByType(active);
return this;
};
/**
* If enabled, allows fields wrapped by quotation marks to be parsed correctly and not splitted
*/
exports.supportQuotedField = function (active = false) {
csvToJson.supportQuotedField(active);
return this;
};
/**
* Defines the field delimiter which will be used to split the fields
*/
exports.fieldDelimiter = function (delimiter) {
csvToJson.fieldDelimiter(delimiter);
return this;
};
/**
* Defines the index where the header is defined
*/
exports.indexHeader = function (index) {
csvToJson.indexHeader(index);
return this;
};
/**
* Defines how to match and parse a sub array
*/
exports.parseSubArray = function (delimiter, separator) {
csvToJson.parseSubArray(delimiter, separator);
return this;
};
/**
* Defines a custom encoding to decode a file
*/
exports.customEncoding = function (encoding) {
csvToJson.encoding = encoding;
return this;
};
/**
* Defines a custom encoding to decode a file
*/
exports.utf8Encoding = function utf8Encoding() {
csvToJson.encoding = encodingOps.utf8;
return this;
};
/**
* Defines ucs2 encoding to decode a file
*/
exports.ucs2Encoding = function () {
csvToJson.encoding = encodingOps.ucs2;
return this;
};
/**
* Defines utf16le encoding to decode a file
*/
exports.utf16leEncoding = function () {
csvToJson.encoding = encodingOps.utf16le;
return this;
};
/**
* Defines latin1 encoding to decode a file
*/
exports.latin1Encoding = function () {
csvToJson.encoding = encodingOps.latin1;
return this;
};
/**
* Defines ascii encoding to decode a file
*/
exports.asciiEncoding = function () {
csvToJson.encoding = encodingOps.ascii;
return this;
};
/**
* Defines base64 encoding to decode a file
*/
exports.base64Encoding = function () {
this.csvToJson = encodingOps.base64;
return this;
};
/**
* Defines hex encoding to decode a file
*/
exports.hexEncoding = function () {
this.csvToJson = encodingOps.hex;
return this;
};
/**
* Parses .csv file and put its content into a file in json format.
* @param {inputFileName} path/filename
* @param {outputFileName} path/filename
*
*/
exports.generateJsonFileFromCsv = function(inputFileName, outputFileName) {
if (!inputFileName) {
throw new Error("inputFileName is not defined!!!");
}
if (!outputFileName) {
throw new Error("outputFileName is not defined!!!");
}
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName);
};
/**
* Parses .csv file and put its content into an Array of Object in json format.
* @param {inputFileName} path/filename
* @return {Array} Array of Object in json format
*
*/
exports.getJsonFromCsv = function(inputFileName) {
if (!inputFileName) {
throw new Error("inputFileName is not defined!!!");
}
return csvToJson.getJsonFromCsv(inputFileName);
};
exports.csvStringToJson = function(csvString) {
return csvToJson.csvStringToJson(csvString);
};
/**
* Parses .csv file and put its content into a file in json format.
* @param {inputFileName} path/filename
* @param {outputFileName} path/filename
*
* @deprecated Use generateJsonFileFromCsv()
*/
exports.jsonToCsv = function(inputFileName, outputFileName) {
csvToJson.generateJsonFileFromCsv(inputFileName, outputFileName);
};