Skip to content

Commit da93632

Browse files
committed
fix indexHeader is not a function, close #39
1 parent 220539b commit da93632

File tree

5 files changed

+89
-5
lines changed

5 files changed

+89
-5
lines changed

src/csvToJson.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ class CsvToJson {
2929
return this;
3030
}
3131

32-
indexHeader(indexHeader) {
33-
if(isNaN(indexHeader)){
32+
indexHeader(indexHeaderValue) {
33+
if(isNaN(indexHeaderValue)){
3434
throw new Error('The index Header must be a Number!');
3535
}
36-
this.indexHeader = indexHeader;
36+
this.indexHeaderValue = indexHeaderValue;
3737
return this;
3838
}
3939

@@ -118,8 +118,8 @@ class CsvToJson {
118118
}
119119

120120
getIndexHeader(){
121-
if(this.indexHeader !== null && !isNaN(this.indexHeader)){
122-
return this.indexHeader;
121+
if(this.indexHeaderValue !== null && !isNaN(this.indexHeaderValue)){
122+
return this.indexHeaderValue;
123123
}
124124
return 0;
125125
}

test/index.spec.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,5 +242,77 @@ describe('API testing', function () {
242242

243243
});
244244

245+
describe('indexHeader() multiple files test', function () {
246+
beforeEach(function () {
247+
// Reset all configurations before each test using chaining pattern
248+
index
249+
.formatValueByType(false)
250+
.fieldDelimiter(';')
251+
.indexHeader(0)
252+
.supportQuotedField(false)
253+
.trimHeaderFieldWhiteSpace(false);
254+
});
255+
256+
it('should handle multiple files with different indexHeader values without throwing error', function () {
257+
//given
258+
const files = [
259+
{ path: 'test/resource/input_header_row0.csv', headerIndex: 0, expectedLength: 2 },
260+
{ path: 'test/resource/input_header_row1.csv', headerIndex: 1, expectedLength: 2 },
261+
{ path: 'test/resource/input_header_row2.csv', headerIndex: 2, expectedLength: 2 }
262+
];
263+
264+
//when & then - process each file with different indexHeader
265+
files.forEach(file => {
266+
const result = index
267+
.fieldDelimiter(',')
268+
.indexHeader(file.headerIndex)
269+
.getJsonFromCsv(file.path);
270+
271+
expect(result).toBeDefined();
272+
expect(Array.isArray(result)).toBe(true);
273+
expect(result.length).toEqual(file.expectedLength);
274+
275+
// Reset state after each file to ensure clean state using chaining pattern
276+
index
277+
.fieldDelimiter(';')
278+
.indexHeader(0);
279+
});
280+
});
281+
282+
it('should process same file multiple times with different indexHeader values', function () {
283+
//given
284+
const file = 'test/resource/input_header_row0.csv';
285+
286+
//when & then - process same file multiple times using chaining pattern
287+
const result1 = index
288+
.fieldDelimiter(',')
289+
.indexHeader(0)
290+
.getJsonFromCsv(file);
291+
292+
expect(result1.length).toEqual(2);
293+
expect(result1[0]).toHaveProperty('name');
294+
expect(result1[0]).toHaveProperty('age');
295+
expect(result1[0]).toHaveProperty('city');
296+
297+
// Reset and process the same file again - this should not throw an error using chaining pattern
298+
const result2 = index
299+
.fieldDelimiter(',')
300+
.indexHeader(0)
301+
.getJsonFromCsv(file);
302+
303+
expect(result2.length).toEqual(2);
304+
expect(result2[0]).toHaveProperty('name');
305+
});
306+
307+
afterEach(function () {
308+
// Reset all configurations after each test using chaining pattern
309+
index
310+
.formatValueByType(false)
311+
.fieldDelimiter(';')
312+
.indexHeader(0)
313+
.supportQuotedField(false)
314+
.trimHeaderFieldWhiteSpace(false);
315+
});
316+
});
245317

246318
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
name,age,city
2+
John,30,NewYork
3+
Jane,25,Boston
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
skipped row
2+
product,price,quantity
3+
Apple,1.50,100
4+
Banana,0.75,200
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
skip1
2+
skip2
3+
color,hex,rgb
4+
Red,#FF0000,255,0,0
5+
Blue,#0000FF,0,0,255

0 commit comments

Comments
 (0)