Skip to content

Commit 5dc5cde

Browse files
authored
Feat/onParseValue (evanplaice#131)
* Fix Implementation - previously onParseValue was being applied to the object headers. * Add tests/fixtures * Test using state to locate the correct value
1 parent ad9f49b commit 5dc5cde

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

src/jquery.csv.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ RegExp.escape = function (s) {
138138
if (options.onParseValue === undefined) {
139139
// onParseValue hook not set
140140
entry.push(value);
141+
} else if (options.headers && options.state.rowNum === 1) {
142+
// don't onParseValue object headers
143+
entry.push(value);
141144
} else {
142145
var hook = options.onParseValue(value, options.state); // onParseValue Hook
143146
// false skips the row, configurable through a hook
@@ -801,7 +804,8 @@ RegExp.escape = function (s) {
801804
state: {
802805
rowNum: 1,
803806
colNum: 1
804-
}
807+
},
808+
headers: true
805809
};
806810

807811
// onPreParse hook
@@ -811,7 +815,7 @@ RegExp.escape = function (s) {
811815

812816
// parse the csv
813817
var headerLine = $.csv.parsers.splitLines(csv, headerOptions);
814-
var headers = $.csv.toArray(headerLine[0], options);
818+
var headers = $.csv.toArray(headerLine[0], headerOptions);
815819

816820
// fetch the data
817821
lines = $.csv.parsers.splitLines(csv, options);

test/csv.on_parse_value_hook.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const test = require('tape');
2+
const csv = require('../src/jquery.csv.js');
3+
const fixtures = require('./fixtures/fixtures.js');
4+
5+
test('$.csv.toObjects onParseValue hook callback - should be passed the data and state', (t) => {
6+
let passedData, passedState;
7+
8+
csv.toObjects(fixtures.objects_csv, { onParseValue: (data, state) => {
9+
passedData = data;
10+
passedState = state;
11+
return data;
12+
} });
13+
14+
t.isNot(passedData, null, 'data argument should not be null');
15+
t.isNot(passedState, null, 'state argument should not be null');
16+
t.end();
17+
});
18+
19+
test('$.csv.toArrays onParseValue hook callback - should be passed the data and state', (t) => {
20+
let passedData, passedState;
21+
22+
csv.toArrays(fixtures.arrays1_csv, { onParseValue: (data, state) => {
23+
passedData = data;
24+
passedState = state;
25+
return data;
26+
} });
27+
28+
t.isNot(passedData, null, 'data argument should not be null');
29+
t.isNot(passedState, null, 'state argument should not be null');
30+
t.end();
31+
});
32+
33+
test('$.csv.toObjects onParseValue hook callback - should affect return value', (t) => {
34+
const returnMutatedValues = (value) => value + 'a';
35+
const result = csv.toObjects(fixtures.value_objects_csv, { onParseValue: returnMutatedValues });
36+
t.deepEqual(result, fixtures.value_objects_obj, 'return value should reflect what was returned from callback');
37+
t.end();
38+
});
39+
40+
test('$.csv.toObjects onParseValue hook callback - should have correct state', (t) => {
41+
const checkValueState = (value, state) => {
42+
if (state.rowNum === 2, state.colNum === 3) {
43+
t.equal(value, 'some');
44+
}
45+
};
46+
csv.toObjects(fixtures.value_objects_csv, { onParseValue: checkValueState });
47+
t.end();
48+
});
49+
50+
test('$.csv.toArrays onParseValue hook callback - should have correct state', (t) => {
51+
const checkValueState = (value, state) => {
52+
if (state.rowNum === 1, state.colNum === 3) {
53+
t.equal(value, 'some');
54+
}
55+
};
56+
csv.toArrays(fixtures.value_arrays_csv, { onParseValue: checkValueState });
57+
t.end();
58+
});

test/fixtures/fixtures.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,11 @@ var fs = require('fs');
6464
backslash_obj: jsonFixture('backslash'),
6565
objects_csv: csvFixture('objects'),
6666
objects_obj: jsonFixture('objects'),
67-
objects2_csv: csvFixture('objects2')
68-
67+
objects2_csv: csvFixture('objects2'),
68+
value_arrays_csv: csvFixture('value_arrays'),
69+
value_arrays_obj: jsonFixture('value_arrays'),
70+
value_objects_csv: csvFixture('value_objects'),
71+
value_objects_obj: jsonFixture('value_objects')
6972
};
7073

7174
// CommonJS module is defined

test/fixtures/value_arrays.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
this,is,some,data

test/fixtures/value_arrays.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
[ "thisa", "isa", "somea", "dataa" ]
3+
]

test/fixtures/value_objects.csv

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
h1,h2,h3,h4
2+
this,is,some,data

test/fixtures/value_objects.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[
2+
{ "h1": "thisa", "h2": "isa", "h3": "somea", "h4": "dataa" }
3+
]

0 commit comments

Comments
 (0)