Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aupajo/9776832 to your computer and use it in GitHub Desktop.
Save Aupajo/9776832 to your computer and use it in GitHub Desktop.
CSV regex experiments
// Kids, this is a bad idea. Don't try this at home.
//
// npm install csv-spectrum
//
var CSV_REGEX = /^(([^,"]+)|("([^"]|"")*"))(,(([^,"]+)|("([^"]|"")*")))*$/;
var spectrum = require('csv-spectrum'),
passed = 0,
failed = 0;
var testEntry = function(entry) {
var hasFailure = false,
string = entry.csv.toString();
var lines = string.split("\n");
var testLine = function(line) {
if(line.match(CSV_REGEX)) {
passed ++;
} else {
if(!hasFailure) {
displayBanner(entry.name);
hasFailure = true;
}
console.log("Failed:", line);
failed ++;
}
}
lines.forEach(testLine);
if(hasFailure) {
// Print a newline
console.log();
}
};
var displayBanner = function(title) {
console.log(title);
console.log('----------');
};
spectrum(function(err, data) {
data.forEach(testEntry);
console.log("Total:", passed + "/" + (passed + failed), "(" + failed, "failed)");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment