1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | var CSVError_1 = __importDefault(require("./CSVError"));
|
7 | var set_1 = __importDefault(require("lodash/set"));
|
8 | var numReg = /^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$/;
|
9 | function default_1(csvRows, conv) {
|
10 | var res = [];
|
11 | for (var i = 0, len = csvRows.length; i < len; i++) {
|
12 | var r = processRow(csvRows[i], conv, i);
|
13 | if (r) {
|
14 | res.push(r);
|
15 | }
|
16 | }
|
17 | return res;
|
18 | }
|
19 | exports.default = default_1;
|
20 | ;
|
21 | function processRow(row, conv, index) {
|
22 | if (conv.parseParam.checkColumn && conv.parseRuntime.headers && row.length !== conv.parseRuntime.headers.length) {
|
23 | throw (CSVError_1.default.column_mismatched(conv.parseRuntime.parsedLineNumber + index));
|
24 | }
|
25 | var headRow = conv.parseRuntime.headers || [];
|
26 | var resultRow = convertRowToJson(row, headRow, conv);
|
27 | if (resultRow) {
|
28 | return resultRow;
|
29 | }
|
30 | else {
|
31 | return null;
|
32 | }
|
33 | }
|
34 | function convertRowToJson(row, headRow, conv) {
|
35 | var hasValue = false;
|
36 | var resultRow = {};
|
37 | for (var i = 0, len = row.length; i < len; i++) {
|
38 | var item = row[i];
|
39 | if (conv.parseParam.ignoreEmpty && item === '') {
|
40 | continue;
|
41 | }
|
42 | hasValue = true;
|
43 | var head = headRow[i];
|
44 | if (!head || head === "") {
|
45 | head = headRow[i] = "field" + (i + 1);
|
46 | }
|
47 | var convFunc = getConvFunc(head, i, conv);
|
48 | if (convFunc) {
|
49 | var convRes = convFunc(item, head, resultRow, row, i);
|
50 | if (convRes !== undefined) {
|
51 | setPath(resultRow, head, convRes, conv, i);
|
52 | }
|
53 | }
|
54 | else {
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | if (conv.parseParam.checkType) {
|
60 | var convertFunc = checkType(item, head, i, conv);
|
61 | item = convertFunc(item);
|
62 | }
|
63 | if (item !== undefined) {
|
64 | setPath(resultRow, head, item, conv, i);
|
65 | }
|
66 | }
|
67 | }
|
68 | if (hasValue) {
|
69 | return resultRow;
|
70 | }
|
71 | else {
|
72 | return null;
|
73 | }
|
74 | }
|
75 | var builtInConv = {
|
76 | "string": stringType,
|
77 | "number": numberType,
|
78 | "omit": function () { }
|
79 | };
|
80 | function getConvFunc(head, i, conv) {
|
81 | if (conv.parseRuntime.columnConv[i] !== undefined) {
|
82 | return conv.parseRuntime.columnConv[i];
|
83 | }
|
84 | else {
|
85 | var flag = conv.parseParam.colParser[head];
|
86 | if (flag === undefined) {
|
87 | return conv.parseRuntime.columnConv[i] = null;
|
88 | }
|
89 | if (typeof flag === "object") {
|
90 | flag = flag.cellParser || "string";
|
91 | }
|
92 | if (typeof flag === "string") {
|
93 | flag = flag.trim().toLowerCase();
|
94 | var builtInFunc = builtInConv[flag];
|
95 | if (builtInFunc) {
|
96 | return conv.parseRuntime.columnConv[i] = builtInFunc;
|
97 | }
|
98 | else {
|
99 | return conv.parseRuntime.columnConv[i] = null;
|
100 | }
|
101 | }
|
102 | else if (typeof flag === "function") {
|
103 | return conv.parseRuntime.columnConv[i] = flag;
|
104 | }
|
105 | else {
|
106 | return conv.parseRuntime.columnConv[i] = null;
|
107 | }
|
108 | }
|
109 | }
|
110 | function setPath(resultJson, head, value, conv, headIdx) {
|
111 | if (!conv.parseRuntime.columnValueSetter[headIdx]) {
|
112 | if (conv.parseParam.flatKeys) {
|
113 | conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
|
114 | }
|
115 | else {
|
116 | if (head.indexOf(".") > -1) {
|
117 | var headArr = head.split(".");
|
118 | var jsonHead = true;
|
119 | while (headArr.length > 0) {
|
120 | var headCom = headArr.shift();
|
121 | if (headCom.length === 0) {
|
122 | jsonHead = false;
|
123 | break;
|
124 | }
|
125 | }
|
126 | if (!jsonHead || conv.parseParam.colParser[head] && conv.parseParam.colParser[head].flat) {
|
127 | conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
|
128 | }
|
129 | else {
|
130 | conv.parseRuntime.columnValueSetter[headIdx] = jsonSetter;
|
131 | }
|
132 | }
|
133 | else {
|
134 | conv.parseRuntime.columnValueSetter[headIdx] = flatSetter;
|
135 | }
|
136 | }
|
137 | }
|
138 | if (conv.parseParam.nullObject === true && value === "null") {
|
139 | value = null;
|
140 | }
|
141 | conv.parseRuntime.columnValueSetter[headIdx](resultJson, head, value);
|
142 |
|
143 | }
|
144 | function flatSetter(resultJson, head, value) {
|
145 | resultJson[head] = value;
|
146 | }
|
147 | function jsonSetter(resultJson, head, value) {
|
148 | set_1.default(resultJson, head, value);
|
149 | }
|
150 | function checkType(item, head, headIdx, conv) {
|
151 | if (conv.parseRuntime.headerType[headIdx]) {
|
152 | return conv.parseRuntime.headerType[headIdx];
|
153 | }
|
154 | else if (head.indexOf('number#!') > -1) {
|
155 | return conv.parseRuntime.headerType[headIdx] = numberType;
|
156 | }
|
157 | else if (head.indexOf('string#!') > -1) {
|
158 | return conv.parseRuntime.headerType[headIdx] = stringType;
|
159 | }
|
160 | else if (conv.parseParam.checkType) {
|
161 | return conv.parseRuntime.headerType[headIdx] = dynamicType;
|
162 | }
|
163 | else {
|
164 | return conv.parseRuntime.headerType[headIdx] = stringType;
|
165 | }
|
166 | }
|
167 | function numberType(item) {
|
168 | var rtn = parseFloat(item);
|
169 | if (isNaN(rtn)) {
|
170 | return item;
|
171 | }
|
172 | return rtn;
|
173 | }
|
174 | function stringType(item) {
|
175 | return item.toString();
|
176 | }
|
177 | function dynamicType(item) {
|
178 | var trimed = item.trim();
|
179 | if (trimed === "") {
|
180 | return stringType(item);
|
181 | }
|
182 | if (numReg.test(trimed)) {
|
183 | return numberType(item);
|
184 | }
|
185 | else if (trimed.length === 5 && trimed.toLowerCase() === "false" || trimed.length === 4 && trimed.toLowerCase() === "true") {
|
186 | return booleanType(item);
|
187 | }
|
188 | else if (trimed[0] === "{" && trimed[trimed.length - 1] === "}" || trimed[0] === "[" && trimed[trimed.length - 1] === "]") {
|
189 | return jsonType(item);
|
190 | }
|
191 | else {
|
192 | return stringType(item);
|
193 | }
|
194 | }
|
195 | function booleanType(item) {
|
196 | var trimed = item.trim();
|
197 | if (trimed.length === 5 && trimed.toLowerCase() === "false") {
|
198 | return false;
|
199 | }
|
200 | else {
|
201 | return true;
|
202 | }
|
203 | }
|
204 | function jsonType(item) {
|
205 | try {
|
206 | return JSON.parse(item);
|
207 | }
|
208 | catch (e) {
|
209 | return item;
|
210 | }
|
211 | }
|
212 |
|
\ | No newline at end of file |