-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
36 lines (30 loc) · 855 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var through = require('through2');
var choppa = function(chunkSize) {
chunkSize = chunkSize === undefined ? 1 : chunkSize;
var prev = new Buffer(0);
var transform = function(chunk, enc, cb) {
if (typeof chunk === 'string') chunk = new Buffer(chunk);
chunk = Buffer.concat([prev, chunk]);
var self = this;
if (chunkSize > 0) {
while (chunk.length >= chunkSize) {
self.push(chunk.slice(0, chunkSize));
chunk = chunk.slice(chunkSize);
}
prev = chunk;
} else {
while (chunk.length) {
var size = Math.floor(Math.random() * chunk.length) + 1;
self.push(chunk.slice(0, size));
chunk = chunk.slice(size);
}
}
cb();
};
var flush = function(cb) {
this.push(prev);
cb();
};
return through.obj(transform, flush);
};
module.exports = choppa;