From: Jakub Pawlowicz Date: Wed, 24 Sep 2014 20:24:18 +0000 (+0100) Subject: Improves Splitter. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=525e341e077946a840c6d424920842e6bf16564f;p=clean-css.git Improves Splitter. * Adds prototypal OO. --- 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;