Skip to content

Commit 41379c7

Browse files
committed
update
1 parent 2b677ac commit 41379c7

2 files changed

Lines changed: 59 additions & 22 deletions

File tree

dist/csvtojson.js

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,10 @@ module.exports = function (params) {
628628
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
629629
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
630630
escape: '"', //escape char for quoted column
631+
colParser:{}, //flags on columns to alter field processing.
631632

632633
/**below are internal params */
634+
_columnConv:[],
633635
_headerType: [],
634636
_headerTitle: [],
635637
_headerFlag: [],
@@ -1004,19 +1006,27 @@ function convertRowToJson(row, headRow, param) {
10041006
if (!head || head === "") {
10051007
head = headRow[i] = "field" + (i + 1);
10061008
}
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+
}
10181015
} 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+
}
10201030
}
10211031
}
10221032
if (hasValue) {
@@ -1026,6 +1036,34 @@ function convertRowToJson(row, headRow, param) {
10261036
}
10271037
}
10281038

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+
}
10291067
function setPath(json, path, value) {
10301068
var _set = require('lodash/set');
10311069
var pathArr = path.split('.');
@@ -1054,8 +1092,7 @@ function getTitle(head, i, param) {
10541092
}
10551093

10561094
var flag = getFlag(head, i, param);
1057-
var str = head.replace(flag, '');
1058-
str = str.replace('string#!', '').replace('number#!', '');
1095+
var str = head.replace('*flat*', '').replace('string#!', '').replace('number#!', '');
10591096
return param._headerTitle[i] = str;
10601097
}
10611098

@@ -28173,7 +28210,7 @@ module.exports={
2817328210
"hireable": true
2817428211
}
2817528212
],
28176-
"version": "1.1.6",
28213+
"version": "1.1.7",
2817728214
"keywords": [
2817828215
"csv",
2817928216
"csv parser",

0 commit comments

Comments
 (0)