Improves Splitter.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Wed, 24 Sep 2014 20:24:18 +0000 (21:24 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Fri, 10 Oct 2014 20:22:44 +0000 (21:22 +0100)
* Adds prototypal OO.

lib/utils/chunker.js

index 860a425..369e150 100644 (file)
@@ -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;