You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dist/csvtojson.js
+52-15Lines changed: 52 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -628,8 +628,10 @@ module.exports = function (params) {
628
628
maxRowLength: 0, //the max character a csv row could have. 0 means infinite. If max number exceeded, parser will emit "error" of "row_exceed". if a possibly corrupted csv data provided, give it a number like 65535 so the parser wont consume memory. default: 0
629
629
checkColumn: false, //whether check column number of a row is the same as headers. If column number mismatched headers number, an error of "mismatched_column" will be emitted.. default: false
630
630
escape: '"', //escape char for quoted column
631
+
colParser:{}, //flags on columns to alter field processing.
631
632
632
633
/**below are internal params */
634
+
_columnConv:[],
633
635
_headerType: [],
634
636
_headerTitle: [],
635
637
_headerFlag: [],
@@ -1004,19 +1006,27 @@ function convertRowToJson(row, headRow, param) {
1004
1006
if (!head || head === "") {
1005
1007
head = headRow[i] = "field" + (i + 1);
1006
1008
}
1007
-
var flag = getFlag(head, i, param);
1008
-
if (flag === 'omit') {
1009
-
continue;
1010
-
}
1011
-
if (param.checkType) {
1012
-
convertFunc = checkType(item, head, i, param);
1013
-
item = convertFunc(item);
1014
-
}
1015
-
var title = getTitle(head, i, param);
1016
-
if (flag === 'flat' || param.flatKeys) {
1017
-
resultRow[title] = item;
1009
+
var convFunc = getConvFunc(head, i, param);
1010
+
if (convFunc) {
1011
+
var convRes = convFunc(item, head, resultRow,row,i);
1012
+
if (convRes !== undefined) {
1013
+
setPath(resultRow, head, convRes);
1014
+
}
1018
1015
} else {
1019
-
setPath(resultRow, title, item);
1016
+
var flag = getFlag(head, i, param);
1017
+
if (flag === 'omit') {
1018
+
continue;
1019
+
}
1020
+
if (param.checkType) {
1021
+
convertFunc = checkType(item, head, i, param);
1022
+
item = convertFunc(item);
1023
+
}
1024
+
var title = getTitle(head, i, param);
1025
+
if (flag === 'flat' || param.flatKeys) {
1026
+
resultRow[title] = item;
1027
+
} else {
1028
+
setPath(resultRow, title, item);
1029
+
}
1020
1030
}
1021
1031
}
1022
1032
if (hasValue) {
@@ -1026,6 +1036,34 @@ function convertRowToJson(row, headRow, param) {
1026
1036
}
1027
1037
}
1028
1038
1039
+
var builtInConv={
1040
+
"string":stringType,
1041
+
"number":numberType,
1042
+
"omit":function(){}
1043
+
}
1044
+
function getConvFunc(head,i,param){
1045
+
if (param._columnConv[i] !== undefined){
1046
+
return param._columnConv[i];
1047
+
}else{
1048
+
var flag=param.colParser[head];
1049
+
if (flag === undefined){
1050
+
return param._columnConv[i]=false;
1051
+
}
1052
+
if (typeof flag ==="string"){
1053
+
flag=flag.trim().toLowerCase();
1054
+
var builtInFunc=builtInConv[flag];
1055
+
if (builtInFunc){
1056
+
return param._columnConv[i]=builtInFunc;
1057
+
}else{
1058
+
return param._columnConv[i]=false;
1059
+
}
1060
+
}else if (typeof flag ==="function"){
1061
+
return param._columnConv[i]=flag;
1062
+
}else{
1063
+
return param._columnConv[i]=false;
1064
+
}
1065
+
}
1066
+
}
1029
1067
function setPath(json, path, value) {
1030
1068
var _set = require('lodash/set');
1031
1069
var pathArr = path.split('.');
@@ -1054,8 +1092,7 @@ function getTitle(head, i, param) {
0 commit comments