timetable
Version:
Using GTFS and other sources to produce beautiful and usable timetables for public transport.
180 lines (148 loc) • 4.7 kB
JavaScript
// Generated by CoffeeScript 1.7.1
(function() {
var $, $split_and_skip, P, TEXT, TRM, TYPES, alert, badge, debug, echo, help, info, log, njs_fs, rainbow, route, rpr, urge, warn, whisper;
njs_fs = require('fs');
TYPES = require('coffeenode-types');
TEXT = require('coffeenode-text');
TRM = require('coffeenode-trm');
rpr = TRM.rpr.bind(TRM);
badge = 'TIMETABLE/read-gtfs-data';
log = TRM.get_logger('plain', badge);
info = TRM.get_logger('info', badge);
whisper = TRM.get_logger('whisper', badge);
alert = TRM.get_logger('alert', badge);
debug = TRM.get_logger('debug', badge);
warn = TRM.get_logger('warn', badge);
help = TRM.get_logger('help', badge);
urge = TRM.get_logger('urge', badge);
echo = TRM.echo.bind(TRM);
rainbow = TRM.rainbow.bind(TRM);
/* http://c2fo.github.io/fast-csv/index.html, https://github.com/C2FO/fast-csv */
P = require('pipedreams');
$ = P.$.bind(P);
this.$count = function(input_stream, title) {
var count;
count = 0;
input_stream.on('end', function() {
return info((title != null ? title : 'Count') + ':', count);
});
return $((function(_this) {
return function(record, handler) {
count += 1;
return handler(null, record);
};
})(this));
};
this.$parse_csv = function() {
var field_names;
field_names = null;
return $((function(_this) {
return function(record, handler) {
var idx, value, values, _i, _len;
values = (S(record)).parseCSV(',', '"', '\\');
if (field_names === null) {
field_names = values;
return handler();
}
record = {};
for (idx = _i = 0, _len = values.length; _i < _len; idx = ++_i) {
value = values[idx];
record[field_names[idx]] = value;
}
return handler(null, record);
};
})(this));
};
this.$skip_empty = function() {
return $((function(_this) {
return function(record, handler) {
if (record.length === 0) {
return handler();
}
return handler(null, record);
};
})(this));
};
this.$show = function() {
return $((function(_this) {
return function(record, handler) {
urge(rpr(record));
return handler(null, record);
};
})(this));
};
this.read_trips = function(route, handler) {
var input;
input = P.create_readstream(route);
input.on('end', function() {
log('ok: trips');
return handler(null);
});
input.setMaxListeners(100);
input.pipe(P.$split()).pipe(P.$skip_empty()).pipe(P.$parse_csv()).pipe(P.$count(input, 'trips A')).pipe(P.$show_table(input));
return null;
};
this.test_batch = function(route, handler) {
var input;
input = P.create_readstream(route);
return input.pipe(P.$split()).pipe(P.$sample(1 / 5)).pipe(P.$signal_end()).pipe(P.$batch(3)).pipe($(function(record, handler) {
if (record === P.eos) {
warn('over');
} else {
help(record.length);
}
return handler(null, record);
})).on('end', function() {
return handler(null);
});
};
this.test_batch_with_handler = function(route, handler) {
var input;
input = P.create_readstream(route);
return input.pipe(P.$split()).pipe(P.$sample(1 / 5)).pipe(P.$signal_end()).pipe(P.$batch(3, function(error, batch) {
if (error != null) {
throw error;
}
if (batch === P.eos) {
return warn('handler:', 'over');
} else {
return help('handler:', batch.length);
}
})).pipe($(function(record, handler) {
if (record === P.eos) {
warn('pipe:', 'over');
} else {
whisper('pipe:', record.length);
}
return handler(null, record);
})).on('end', function() {
return handler(null);
});
};
$split_and_skip = function() {
return P.$chain(P.$split(), P.$skip_empty());
};
this.test_show_sample = function(route, handler) {
var input;
input = P.create_readstream(route);
return input.pipe($split_and_skip()).pipe(P.$collect_sample(input, 3, {
headers: true,
seed: 4
}, function(error, sample) {
return urge(sample);
})).pipe(P.$count(function(_, count) {
return help(count);
})).pipe(P.$count()).pipe(P.$show(input)).on('end', function() {
return handler(null);
});
};
if (!module.parent) {
route = '/Volumes/Storage/cnd/node_modules/timetable-data/germany-berlin-2014/routes.txt';
this.test_show_sample(route, function(error) {
if (error != null) {
throw error;
}
return log('ok');
});
}
}).call(this);