Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -564,13 +564,14 @@ Joe, 1970-01-01
*/
csv({
colParser:{
"birthday":function(item, head, resultRow, row , colIdx){
"birthday":function(item, head, resultRow, row , colIdx, rowIdx){
/*
item - "1970-01-01"
head - "birthday"
resultRow - {name:"Joe"}
row - ["Joe","1970-01-01"]
colIdx - 1
rowIdx - 0
*/
return new Date(item);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export interface CSVParseParam {
needEmitAll: boolean;
}

export type CellParser = (item: string, head: string, resultRow: any, row: string[], columnIndex: number) => any;
export type CellParser = (item: string, head: string, resultRow: any, row: string[], columnIndex: number, rowIndex: number) => any;

export interface ColumnParam {
flat?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions src/lineToJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ export type JSONResult = {
[key: string]: any
}

function processRow(row: string[], conv: Converter, index): JSONResult | null {
function processRow(row: string[], conv: Converter, index: number): JSONResult | null {

if (conv.parseParam.checkColumn && conv.parseRuntime.headers && row.length !== conv.parseRuntime.headers.length) {
throw (CSVError.column_mismatched(conv.parseRuntime.parsedLineNumber + index))
}

const headRow = conv.parseRuntime.headers || [];
const resultRow = convertRowToJson(row, headRow, conv);
const resultRow = convertRowToJson(row, headRow, conv,index);
if (resultRow) {
return resultRow;
} else {
return null;
}
}

function convertRowToJson(row: string[], headRow: string[], conv: Converter): { [key: string]: any } | null {
function convertRowToJson(row: string[], headRow: string[], conv: Converter, rowIndex: number): { [key: string]: any } | null {
let hasValue = false;
const resultRow = {};

Expand All @@ -53,7 +53,7 @@ function convertRowToJson(row: string[], headRow: string[], conv: Converter): {
}
const convFunc = getConvFunc(head, i, conv);
if (convFunc) {
const convRes = convFunc(item, head, resultRow, row, i);
const convRes = convFunc(item, head, resultRow, row, i, rowIndex);
if (convRes !== undefined) {
setPath(resultRow, head, convRes, conv,i);
}
Expand Down
34 changes: 18 additions & 16 deletions test/testCSVConverter3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,34 @@ describe("testCSVConverter3", function () {
});
rs.pipe(csvConverter);
});
it("should setup customise type convert function", function (done) {
csv({

it("should setup customise type convert function", async function () {
const json = await csv({
checkType: true,
colParser: {
"column1": "string",
"column5": function (item, head, resultRow, row, i) {
"column5": function (item, head, resultRow, row, i, rowIndex) {
assert.equal(item, '{"hello":"world"}');
assert.equal(head, "column5"),
assert(resultRow);
assert.equal(head, "column5");
assert(resultRow);
assert(row);
assert.equal(i, 5);

// this assumes we are parsing a file with a single row
assert.equal(rowIndex, 0);

return "hello world";
}
}
})
.fromFile(__dirname + "/data/dataWithType")
.subscribe(function (json) {
assert.equal(typeof json.column1, "string");
assert.equal(json.column5, "hello world");
assert.strictEqual(json["name#!"], false);
assert.strictEqual(json["column9"], true);
})
.on('done', function () {
done()
});
})
.fromFile(__dirname + "/data/dataWithType");

assert.equal(typeof json[0].column1, "string");
assert.equal(json[0].column5, "hello world");
assert.strictEqual(json[0]["name#!"], false);
assert.strictEqual(json[0]["column9"], true);
});

it("should accept pipe as quote", function (done) {
csv({
quote: "|",
Expand Down