UNPKG

chunker

Version:

Chunk/split your stream without eating the splitter char.

116 lines (115 loc) 3.73 kB
// Generated by CoffeeScript 1.6.3 (function() { var Chunker, condition, fs, inspect, test; fs = require('fs'); test = require('tap').test; inspect = require('util').inspect; Chunker = require('./index.js'); test('exported interface', function(t) { var chunker; t.plan(3); chunker = new Chunker; t.type(chunker._transform, 'function', '_transform function overridden'); t.equivalent(chunker.matcher, new Buffer('\r\n'), 'default matcher supplied'); chunker = new Chunker({ matcher: 'asdf' }); return t.equivalent(chunker.matcher, new Buffer('asdf'), 'opts.matcher saved'); }); condition = function(description, opts) { return test(description, function(t) { var chunker, i, index, inputs, leftover, matcher, o, outputs, pump; inputs = opts.inputs, outputs = opts.outputs, matcher = opts.matcher, leftover = opts.leftover; if (typeof inputs[0] === 'string') { inputs = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = inputs.length; _i < _len; _i++) { i = inputs[_i]; _results.push(new Buffer(i)); } return _results; })(); } if (typeof outputs[0] === 'string') { outputs = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = outputs.length; _i < _len; _i++) { o = outputs[_i]; _results.push(new Buffer(o)); } return _results; })(); } if (typeof leftover === 'string') { leftover = new Buffer(leftover); } chunker = new Chunker({ matcher: matcher || '\r\n' }); outputs = (function() { var _i, _len, _results; _results = []; for (_i = 0, _len = outputs.length; _i < _len; _i++) { o = outputs[_i]; if (o.length) { _results.push(o); } } return _results; })(); t.plan(outputs.length + (leftover && 1 || 0)); index = 0; chunker.on('readable', function() { var buffer; while (index < outputs.length && (buffer = chunker.read())) { t.same(buffer, outputs[index++], "correct output #" + index); } if (index >= outputs.length) { chunker.removeAllListeners('readable'); if (leftover) { t.same(chunker.leftover, leftover, "leftover buffer correct"); } return t.end(); } }); pump = function() { while (inputs.length && chunker.write(inputs.shift())) {} if (!inputs.length) { chunker.end(); return chunker.removeListener('writable', pump); } }; chunker.on('writable', pump); return setImmediate(pump); }); }; condition('exact cut', { inputs: ['asdf\r\n'], outputs: ['asdf\r\n'] }); condition('inexact cut', { inputs: ['asdf\r\nzxcv'], outputs: ['asdf\r\n'], leftover: 'zxcv' }); condition('multiple matches in one chunk', { inputs: ['qwer\r\nasdf\r\nzxcv\r\n'], outputs: ['qwer\r\n', 'asdf\r\n', 'zxcv\r\n'] }); condition('chunks with matcher in-between', { inputs: ['asdf\r', '\nqwer\r', '\nzxcv\r', '\n', 'test\r\nlast'], outputs: ['asdf\r\n', 'qwer\r\n', 'zxcv\r\n', 'test\r\n'], leftover: 'last' }); condition('consecutive chunk without matcher', { inputs: ['asdf', 'qwer', 'zxcv', '\r\n'], outputs: ['asdfqwerzxcv\r\n'] }); return condition('large file input', { inputs: [fs.readFileSync('input.txt', 'utf-8')], outputs: (fs.readFileSync('output.txt', 'utf-8')).split('\n'), matcher: 'what ' }); })();