From 525e341e077946a840c6d424920842e6bf16564f Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Wed, 24 Sep 2014 21:24:18 +0100 Subject: [PATCH] Improves Splitter. * Adds prototypal OO. --- lib/utils/chunker.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/lib/utils/chunker.js b/lib/utils/chunker.js index 860a425c..369e1500 100644 --- a/lib/utils/chunker.js +++ b/lib/utils/chunker.js @@ -1,6 +1,7 @@ -module.exports = function Chunker(data, breakString, chunkSize) { // Divides `data` into chunks of `chunkSize` for faster processing - var chunks = []; +var Chunker = function Chunker(data, breakString, chunkSize) { + this.chunks = []; + for (var cursor = 0, dataSize = data.length; cursor < dataSize;) { var nextCursor = cursor + chunkSize > dataSize ? dataSize - 1 : @@ -11,17 +12,17 @@ module.exports = function Chunker(data, breakString, chunkSize) { if (nextCursor == -1) nextCursor = data.length - 1; - chunks.push(data.substring(cursor, nextCursor + breakString.length)); + this.chunks.push(data.substring(cursor, nextCursor + breakString.length)); cursor = nextCursor + breakString.length; } +}; - return { - isEmpty: function() { - return chunks.length === 0; - }, +Chunker.prototype.isEmpty = function () { + return this.chunks.length === 0; +}; - next: function() { - return chunks.shift(); - } - }; +Chunker.prototype.next = function () { + return this.chunks.shift(); }; + +module.exports = Chunker; -- 2.34.1