Updated to be more efficient
This commit is contained in:
@ -6,8 +6,9 @@ efficient and no dependencies.
|
||||
Only one method is provided;
|
||||
```js
|
||||
const csv = 'Name,Age,Location\nDom,26,Sydney\nSteve,30,New York';
|
||||
parseCSV(csv, function(line) {
|
||||
console.log(line.name);//Dom then Steve
|
||||
parseCSV(csv, function(headers, line, index) {
|
||||
console.log(index, '-', line[headers.indexOf('Name')]);//1 - Dom
|
||||
return true;//Return false to break loop
|
||||
})
|
||||
```
|
||||
To stay efficient we ask for a callback that is fired for every line parsed.
|
64
src/index.js
64
src/index.js
@ -1,43 +1,53 @@
|
||||
function parseCSV(csv, onLine) {
|
||||
const lines = csv.split(/\r?\n/);
|
||||
//Split lines by delim, don't worry about \r here
|
||||
const lines = csv.split('\n');
|
||||
let headers = null;
|
||||
let index = 0;
|
||||
|
||||
//For each line...
|
||||
for(let i = 0; i < lines.length; i++) {
|
||||
//Shave off trailing whitespaces
|
||||
let line = lines[i].trim();
|
||||
if(!line.length) continue;
|
||||
if(!line.replace(/\s/g, '').length) continue;//Skip blank lines
|
||||
index++;//Count real lines
|
||||
|
||||
let lineArray = [];
|
||||
let currentValue = '';
|
||||
let encapsulated = false;
|
||||
//Setup line array
|
||||
let lineArray;
|
||||
if(line.indexOf('"') !== -1) {//Only escape lines with strings
|
||||
let currentValue = '';//Store current value
|
||||
let encapsulated = false;//Track when we're inside a "
|
||||
lineArray = [];
|
||||
|
||||
for(let j = 0; j < line.length; j++) {
|
||||
let c = line[j];
|
||||
|
||||
if(!encapsulated && c == ',') {
|
||||
//Comma, make new cell
|
||||
lineArray.push(currentValue);
|
||||
currentValue = '';
|
||||
continue;
|
||||
} else if(c == '"') {
|
||||
//Quote, begin string encapsulation
|
||||
encapsulated = !encapsulated;
|
||||
continue;
|
||||
//Foreach char in the line...
|
||||
for(let j = 0; j < line.length; j++) {
|
||||
let c = line[j];
|
||||
if(!encapsulated && c == ',') {
|
||||
//Comma, make new cell
|
||||
lineArray.push(currentValue);
|
||||
currentValue = '';
|
||||
continue;
|
||||
} else if(c == '"') {
|
||||
//Quote, begin string encapsulation
|
||||
encapsulated = !encapsulated;
|
||||
continue;
|
||||
}
|
||||
currentValue += `${c}`;
|
||||
}
|
||||
currentValue += `${c}`;
|
||||
}
|
||||
lineArray.push(currentValue);
|
||||
|
||||
//Is this the headers?
|
||||
//Add current value
|
||||
lineArray.push(currentValue);
|
||||
} else {
|
||||
lineArray = line.split(',');
|
||||
}
|
||||
|
||||
//Is this the headers line?
|
||||
if(!headers) {
|
||||
headers = lineArray;
|
||||
continue;
|
||||
}
|
||||
|
||||
//This is a line
|
||||
let x = {};
|
||||
for(let j = 0; j < headers.length; j++) {
|
||||
x[headers[j]] = lineArray[j] || '';
|
||||
}
|
||||
onLine(x);
|
||||
//Callback
|
||||
let r = onLine(headers, lineArray, index);
|
||||
if(!r) break;//If r != true then break loop
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user