Skip to content

Commit ad9f49b

Browse files
authored
Feat/onPreParse (evanplaice#130)
* Fix implementation - If defined, onPreParse should mutate the raw csv string. * Add tests * Update documentation
1 parent 368d1b4 commit ad9f49b

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

docs/hooks-callbacks.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ This hook is called before the parser starts processing the dataset. Useful if y
2323

2424
```javascript
2525
// strips empty (illegal) lines from the data before parsing
26-
var removeEmptyLines = function(csv) {
26+
var removeEmptyLines = function(csv, state) {
2727
var lines = $.csv.splitLines(csv);
2828
var output = [];
2929
for(var i=0, len=lines.length; i<len; i++) {

src/jquery.csv.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ RegExp.escape = function (s) {
714714

715715
// onPreParse hook
716716
if (options.onPreParse !== undefined) {
717-
options.onPreParse(csv, options.state);
717+
csv = options.onPreParse(csv, options.state);
718718
}
719719

720720
// parse the data
@@ -806,7 +806,7 @@ RegExp.escape = function (s) {
806806

807807
// onPreParse hook
808808
if (options.onPreParse !== undefined) {
809-
options.onPreParse(csv, options.state);
809+
csv = options.onPreParse(csv, options.state);
810810
}
811811

812812
// parse the csv

test/csv.on_pre_parse_hook.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 onPreParse hook callback - should be passed the raw csv and state', (t) => {
6+
let passedCSV, passedState;
7+
8+
csv.toObjects(fixtures.objects_csv, { onPreParse: (csv, state) => {
9+
passedCSV = csv;
10+
passedState = state;
11+
return csv;
12+
} });
13+
14+
t.isNot(passedCSV, 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 onPreParse hook callback - should be passed the raw csv and state', (t) => {
20+
let passedCSV, passedState;
21+
22+
csv.toArrays(fixtures.arrays1_csv, { onPreParse: (csv, state) => {
23+
passedCSV = csv;
24+
passedState = state;
25+
return csv;
26+
} });
27+
28+
t.isNot(passedCSV, 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 onPreParse hook callback - should affect return value', (t) => {
34+
const returnEmptyCSV = () => 'header\n""';
35+
const result = csv.toObjects(fixtures.objects_csv,{ onPreParse: returnEmptyCSV });
36+
t.deepEqual(result, [{ header: '' }], 'return value should reflect what was returned from callback');
37+
t.end();
38+
});
39+
40+
test('$.csv.toArrays onPreParse hook callback - should affect return value', (t) => {
41+
const returnEmptyCSV = () => '';
42+
const result = csv.toArrays(fixtures.arrays1_csv, { onPreParse: returnEmptyCSV });
43+
t.deepEqual(result, [], 'return value should reflect what was returned from callback');
44+
t.end();
45+
});

0 commit comments

Comments
 (0)