Skip to content

Commit 7b6ee08

Browse files
committed
Add API documentation
1 parent f1e4e4f commit 7b6ee08

File tree

1 file changed

+183
-0
lines changed

1 file changed

+183
-0
lines changed

docs/api.md

+183
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
## Introduction
2+
3+
The following will cover the core utilities provided by `jquery-csv`.
4+
5+
The project's home page outlines the most general use cases but this library is capable of much more. If you're interested in learning some of the more advanced capabilities of this library, then you're in the right place.
6+
7+
Note: To avoid confusion, assume that the terms row/entry and column/value as both will be used interchangeably.
8+
9+
## Namespaces
10+
11+
To keep the library out of the global namespace, everything contained in this library has been organized into a hierarchy of namespaces.
12+
13+
### $.csv
14+
15+
The top-level namespace where you will find the primary parser methods and the utility functions needed by them.
16+
17+
### $.options
18+
19+
A namespace containing the default option values for the library. If you're looking to change the defaults (as opposed to overriding them) this is where you may do so.
20+
21+
### $.config
22+
23+
Created after parser initialization. This is where the settings can be read/modified for an active parser instance.
24+
25+
### $.state
26+
27+
Created after parser initialization. This is where the current state of the parser can be read/modified.
28+
29+
### $.hooks
30+
31+
A namespace containing helpful user-defined hook callbacks. For more information on what's available refer to the Hooks? page.
32+
33+
### $.parsers
34+
35+
Where the parser methods live. They're useful if you just need access to a CSV-specific split-lines function, or a single-entry parser. You can also override the default parser by changing these values inline.
36+
37+
## Core Methods
38+
39+
The following methods make up the core of the jquery-csv library.
40+
41+
### $.csv.toArray()
42+
Useful for parsing a single entry of CSV data into an array of `[values]`.
43+
44+
*Structure:*
45+
46+
```javascript
47+
$.csv.toArray(csv, options, callback)
48+
```
49+
50+
*Parameters:*
51+
52+
- csv (required)
53+
- a string of CSV data
54+
- options (optional)
55+
- contains a list of user-configurable options
56+
- callback (optional)
57+
- used for node.js and/or asynchronous processing of data
58+
- used to define the callback that is executed when parsing is complete
59+
- the callbacks take the standard form [function(err, data){}]
60+
61+
*Options:*
62+
63+
- separator
64+
- an override for the separator character
65+
- defaults to a comma(,)
66+
- delimiter
67+
- an override for the delimiter character
68+
- defaults to a double-quote(")
69+
70+
### $.csv.toArrays()
71+
72+
Useful for parsing multi-line CSV data into a two-dimensional array of `[record][values].`
73+
74+
*Structure:*
75+
76+
```javascript
77+
$.csv.toArrays(csv, options, callback)
78+
```
79+
80+
*Parameters:*
81+
82+
- csv (required)
83+
- a string of CSV data
84+
- options (optional)
85+
- contains a list of user-configurable options
86+
- callback (optional)
87+
- used for node.js and/or asynchronous processing of data
88+
- used to define the callback that is executed when parsing is complete
89+
- the callbacks take the standard form [function(err, data){}]
90+
91+
*Options:*
92+
93+
- separator
94+
- an override for the separator character
95+
- defaults to a comma(,)
96+
- delimiter
97+
- an override for the delimiter character
98+
- defaults to a double-quote(")
99+
- startIndex (not implemented)
100+
- the line where the parser should start processing
101+
- defaults to 1 (non-zero based counting)
102+
- endIndex (not implemented)
103+
- the line the parser should stop after
104+
- defaults to EOF
105+
106+
### $.csv.toObjects()
107+
108+
Useful for parsing multi-line CSV data into an array of objects representing data in the form `[record][{header:value}]`.
109+
110+
Unless overridden, the first line of data is assumed to contain the headers.
111+
112+
*Structure:*
113+
114+
```javascript
115+
$.csv.toObjects(csv, options, callback)
116+
```
117+
118+
*Parameters:*
119+
120+
- csv (required)
121+
- a string of CSV data
122+
- options (optional)
123+
- contains a list of user-configurable options
124+
- callback (optional)
125+
- used for node.js and/or asynchronous processing of data
126+
- used to define the callback that is executed when parsing is complete
127+
- the callbacks take the standard form [function(err, data){}]
128+
129+
*Options:*
130+
131+
- separator
132+
- an override for the separator character
133+
- defaults to a comma(,)
134+
- delimiter
135+
- an override for the delimiter character
136+
- defaults to a double-quote(")
137+
- headerIndex (not implemented)
138+
- the line containing the headers
139+
- defaults to 1 (non-zero based counting)
140+
- startIndex (not implemented)
141+
- the line where the parser should start processing
142+
- defaults to 2 (non-zero based counting)
143+
- endIndex (not implemented)
144+
- the line the parser should stop after
145+
- defaults to EOF
146+
147+
## Helper Method(s)
148+
149+
The following methods may or may not be useful unless you're trying to tackle a specific use case. These methods make up some of the magic that makes the library work.
150+
151+
Warning: Replace and/or modify these at your own risk. Their purpose is to override the internal behavior of the parser. Don't expect support if your custom implementation introduces bugs.
152+
153+
```javascript
154+
$.csv.splitLines(csv)
155+
```
156+
157+
An advanced line splitter that converts a CSV data string into an array of CSV entries represented in string form.
158+
159+
As opposed to the commonly used string.split() function, this is a more advanced line splitting implementation capable of ignoring new-lines contained in the value data.
160+
161+
Splitting data by lines may sound trivial (no split('\n) does not work) but under the covers it required the development a special lexer tailored specifically to handle CSV data.
162+
163+
*Structure:*
164+
165+
```javascript```
166+
$.csv.parsers.splitLines(csv, options)
167+
```
168+
169+
*Parameters:*
170+
171+
- csv (required)
172+
- a string of CSV data
173+
- options (optional)
174+
- contains a list of user-configurable options
175+
176+
*Options:*
177+
178+
- separator
179+
- an override for the separator character
180+
- defaults to a comma(,)
181+
- delimiter
182+
- an override for the delimiter character
183+
- defaults to a double-quote(")

0 commit comments

Comments
 (0)