transit-im
Version:
express-like framework for AIM bots creation (ICQ as well)
44 lines (40 loc) • 1.13 kB
JavaScript
// Generated by CoffeeScript 1.6.3
(function() {
var forEachPortion;
module.exports = function(limit) {
if (limit == null) {
limit = 2000;
}
return function(data, next) {
return forEachPortion(data, limit, function(dataPart) {
return next(dataPart);
});
};
};
forEachPortion = function(text, maxPortionLength, forPortion) {
var CRLF, buffer, index, length, lengths, lines, sum, _i, _len;
lines = text.split("\n");
lengths = lines.map(function(l) {
return l.length;
});
if (Math.max.apply(null, lengths) > maxPortionLength) {
throw "Cannot split text - there are lines larger than specified limit";
}
buffer = [];
sum = 0;
CRLF = 1;
for (index = _i = 0, _len = lengths.length; _i < _len; index = ++_i) {
length = lengths[index];
if (sum + length > maxPortionLength) {
forPortion(buffer.join("\n"));
buffer = [];
sum = 0;
}
buffer.push(lines[index]);
sum += length + CRLF;
}
if (buffer.length) {
return forPortion(buffer.join("\n"));
}
};
}).call(this);