Last active
August 29, 2015 13:57
-
-
Save Aupajo/9776832 to your computer and use it in GitHub Desktop.
CSV regex experiments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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