Skip to content

Commit eabfd24

Browse files
committed
Add Hooks & Callbacks documentation
1 parent 7b6ee08 commit eabfd24

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

docs/hooks-callbacks.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
2+
## Why hook?
3+
4+
CSV as a data format is very loosely implemented. Part of what makes it so versatile is the lack of specific rules to enforce how it should be used. Unfortunately, without such rules it becomes a lot more difficult to define how the data should be interpreted.
5+
6+
To keep the library size small and avoid baking in a ton of domain-specific 'magic', an extensible architecture is provided whereby users can define their own 'magic'.
7+
8+
## How it works
9+
10+
Hooks work by defining a user-defined function that will be used inline during the parsing process. The point where it's called during parsing depends on the hook used.
11+
12+
To use a hook, simply attach a function callback (ie anonymous function) of your design to the hook in the parser options object.
13+
14+
No really... it's that easy.
15+
16+
## Available Hooks
17+
18+
### onPreParse()
19+
20+
This hook is called before the parser starts processing the dataset. Useful if you need to make a pass to 'clean' the data first.
21+
22+
*Usage:*
23+
24+
```javascript
25+
// strips empty (illegal) lines from the data before parsing
26+
var removeEmptyLines = function(csv) {
27+
var lines = $.csv.splitLines(csv);
28+
var output = [];
29+
for(var i=0, len=lines.length; i<len; i++) {
30+
if(lines[i] !== '') {
31+
output.push[lines[i]);
32+
}
33+
}
34+
return output.join('\n');
35+
};
36+
$.csv.toArrays(csv, { onPreParse: removeEmptyLines )};
37+
```
38+
39+
### onParseEntry()
40+
41+
This hook is called each time an entry is parsed and before the entry is broken down further into values. Useful if you need to modify specific entries, or process-by-row.
42+
43+
*Usage:*
44+
45+
```javascript
46+
// signals to only parse rows 3 and 4
47+
var rowRange = function(entry, state) {
48+
var start = 3;
49+
var end = 4;
50+
if(state.rowNum >= start && state.rowNum <= end) {
51+
return entry;
52+
}
53+
return false;
54+
}
55+
$.csv.toArrays(testHook3, { onParseEntry: rowRange });
56+
```
57+
58+
### onParseValue()
59+
60+
This hook is called each time a value is parsed. Useful if you want to modify the output values inline, or process-by-column.
61+
62+
*Usage:*
63+
64+
```javascript
65+
// signals to only parse columns 4 and 5
66+
var columnRange = function(value, state) {
67+
var start = 4;
68+
var end = 5;
69+
if(state.colNum >= start && state.colNum <= end) {
70+
return value;
71+
}
72+
return false;
73+
}
74+
$.csv.toArrays(testHook2, { onParseValue: columnRange });
75+
```
76+
77+
### onPostParse()
78+
79+
This hook is called after the parser is complete. Useful if you want to modify the output data.
80+
81+
*Usage:*
82+
83+
```javascript
84+
// sort the 2D array by the value of the second column
85+
var sortByCol2 = function(data) {
86+
data.sort(function(a,b){
87+
return a[1] - b[1];
88+
});
89+
return data;
90+
}
91+
$.csv.toArrays(csv, { onPostParse: sortByCol2 });
92+
```
93+
94+
## Available User-Defined Callbacks
95+
96+
So... I lied when I said that the library won't provide any user-defined hook callbacks. Some are just too common and valuable to leave out.
97+
98+
The following will outline the user-defined callbacks that will be provided with the library by defaults.
99+
100+
### $.csv.hooks.castToScalar(value)
101+
102+
This is a onParseValue callback that detects and casts all output values to the correct type.
103+
104+
*Usage:*
105+
106+
```javascript
107+
var csv = '734,4.5,sda,"555","4523.35","af323"';
108+
109+
var data = $.csv2Array(csv, {
110+
onParseValue: $.csv.hooks.castToScalar
111+
});
112+
console.log(data);
113+
Output:
114+
115+
[
116+
734,
117+
4.5,
118+
"sda",
119+
555,
120+
4523.35,
121+
"af323"]
122+
]
123+
```
124+
125+
Aside: There are plenty more use-cases to be discovered for the plethora of hooks. If you feel like you have one that is common enough to justify adding it to the library we'd like to see it. Just create a new 'feature request' in the 'issues' section outlining the details.

0 commit comments

Comments
 (0)