Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Bit of a cleanup

  • Loading branch information...
commit 86e992ddb5f262ed78ef34fecf31dc976822b20b 1 parent ca5c3a7
@chriso authored
Showing with 32 additions and 75 deletions.
  1. +32 −75 server.js
View
107 server.js
@@ -1,95 +1,52 @@
-var http = require('http')
- , fs = require('fs');
-
-var inc = fs.readFileSync('inc.csv').toString().split('\r\n');
-inc.shift();
-
-var cache = {};
-function csv(line, delim, quote, quote_escape) {
- if (typeof delim === 'undefined' || delim === 'csv') {
- delim = ',';
- } else if (delim === 'tsv') {
- delim = '\t';
- }
- quote = quote || '"';
- quote_escape = quote_escape || '"';
-
- //Check for cached regex patterns
- var match, clean, q;
- if (typeof cache[''+delim+quote+quote_escape] !== 'undefined') {
-
- var patterns = cache[''+delim+quote+quote_escape];
- match = patterns[0];
- clean = patterns[1];
- q = patterns[2];
-
- } else {
-
- //Escape special regex chars
- var d, escape = function (str) {
- return str.replace(new RegExp('[.*+?|()\\[\\]{}]', 'g'), '\\$&');
- }
-
- d = escape(delim);
- e = escape(quote_escape);
- q = escape(quote);
-
- match = new RegExp(
- '(' + d + '|^)' +
- '(?:' + q + '([^' + q + ']*(?:' + e + q + '[^' + q + ']*)*)' + q + '|' +
- '([^' + q + d + ']*))'
- , 'g');
-
- clean = new RegExp(e + q, 'g');
-
- //Cache the patterns for subsequent calls
- cache[delim+quote+quote_escape] = [match, clean, q];
- }
-
- var matches = null, value, csv = [];
+var parsed = [], total = 0, inc, current;
+function csv(line) {
+ var matches, value, csv = []
+ , match = /(,|^)(?:"([^"]*(?:""[^"]*)*)"|([^",]*))/g;
while (matches = match.exec(line)) {
if (!matches[1] && csv.length) {
break;
} else if (matches[2]) {
- value = matches[2].replace(clean, q);
+ value = matches[2].replace(/""/g, '"');
} else {
value = matches[3] || '';
}
csv.push(value);
}
-
return csv;
}
-var parsed = [], total;
+inc = require('fs').readFileSync('inc.csv').toString().split('\r\n');
+
+inc.shift(); //Header
+
inc.forEach(function (line) {
- parsed.push(csv(line));
+ current = csv(line);
+ parsed.push({
+ rank : current[0]
+ , company : current[1]
+ , growth3year : current[2] + '%'
+ , revenue2006 : current[10]
+ , revenue2009 : current[3]
+ , industry : current[4]
+ , industryrank : current[13].replace('#', '')
+ , city : current[5]
+ , state : current[6]
+ , profile : current[7]
+ , website : current[8]
+ , description : current[9]
+ , employees : current[11]
+ , founded : current[12]
+ });
+ ++total;
});
-total = parsed.length;
-function rand() {
- var listing = parsed[Math.round(total * Math.random())];
- return {
- rank : listing[0]
- , company : listing[1]
- , growth3year : listing[2] + '%'
- , revenue2009 : listing[3]
- , industry : listing[4]
- , city : listing[5]
- , state : listing[6]
- , profile : listing[7]
- , website : listing[8]
- , description : listing[9]
- , revenue2006 : listing[10]
- , employees : listing[11]
- , founded : listing[12]
- , industryrank : listing[13]
- };
-}
+require('http').createServer(function (request, response) {
-http.createServer(function (request, response) {
response.writeHead(200, {'content-type': 'application/json'});
- response.end(JSON.stringify(rand()));
+ response.end(JSON.stringify(
+ parsed[Math.round(total * Math.random())]
+ ));
+
}).listen(process.env.PORT || 3000);
Please sign in to comment.
Something went wrong with that request. Please try again.