Skip to content

Instantly share code, notes, and snippets.

@tweinfeld
Created June 14, 2015 21:01
Show Gist options
  • Save tweinfeld/a56459b324585966d921 to your computer and use it in GitHub Desktop.
Save tweinfeld/a56459b324585966d921 to your computer and use it in GitHub Desktop.
define(["jquery", "lodash", "bacon"], function($, _, Bacon){
return function(url, cb) {
var parseTimeCode = function (timeCodeStr) {
return _(timeCodeStr.split(/[:,]/))
.map(Number)
.zipWith([60 * 60 * 1000, 60 * 1000, 1000, 1], function(a, b){ return a * b; })
.reduce(_.add, 0);
};
var stream = Bacon
.fromPromise($.get(url))
.map(function(srtPackets) {
return _(srtPackets)
.split(/^\s+$/m)
.map(_.trim)
.compact()
.map(function (setPacket) {
return _.extend({
serial: +_(setPacket.match(/^\s*([0-9]+)/)).last(),
text: _(setPacket).split('\n').drop(2).join('\n')
}, _(setPacket.match(/([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})\s-->\s([0-9]{2}:[0-9]{2}:[0-9]{2},[0-9]{3})/)).rest().map(parseTimeCode).zipObject(["in", "out"]).invert().value());
}).value();
});
stream.onValue(_.partial(cb, null));
stream.onError(cb);
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment