From: Jakub Pawlowicz Date: Mon, 22 Sep 2014 01:58:25 +0000 (+0200) Subject: Extracts Chunker into a module. X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=29f9c299122f2bccaafdc18745ebbd0698b33dd2;p=clean-css.git Extracts Chunker into a module. * Adds some simple specs too. --- diff --git a/lib/selectors/tokenizer.js b/lib/selectors/tokenizer.js index ab57d320..ed074c95 100644 --- a/lib/selectors/tokenizer.js +++ b/lib/selectors/tokenizer.js @@ -1,3 +1,5 @@ +var Chunker = require('../utils/chunker'); + module.exports = function Tokenizer(data, minifyContext) { var chunker = new Chunker(data, 128); var chunk = chunker.next(); @@ -138,31 +140,3 @@ module.exports = function Tokenizer(data, minifyContext) { } }; }; - -// Divides `data` into chunks of `chunkSize` for faster processing -var Chunker = function(data, chunkSize) { - var chunks = []; - for (var cursor = 0, dataSize = data.length; cursor < dataSize;) { - var nextCursor = cursor + chunkSize > dataSize ? - dataSize - 1 : - cursor + chunkSize; - - if (data[nextCursor] != '}') - nextCursor = data.indexOf('}', nextCursor); - if (nextCursor == -1) - nextCursor = data.length - 1; - - chunks.push(data.substring(cursor, nextCursor + 1)); - cursor = nextCursor + 1; - } - - return { - isEmpty: function() { - return chunks.length === 0; - }, - - next: function() { - return chunks.shift() || ''; - } - }; -}; diff --git a/lib/utils/chunker.js b/lib/utils/chunker.js new file mode 100644 index 00000000..73dc70e6 --- /dev/null +++ b/lib/utils/chunker.js @@ -0,0 +1,27 @@ +module.exports = function Chunker(data, chunkSize) { +// Divides `data` into chunks of `chunkSize` for faster processing + var chunks = []; + for (var cursor = 0, dataSize = data.length; cursor < dataSize;) { + var nextCursor = cursor + chunkSize > dataSize ? + dataSize - 1 : + cursor + chunkSize; + + if (data[nextCursor] != '}') + nextCursor = data.indexOf('}', nextCursor); + if (nextCursor == -1) + nextCursor = data.length - 1; + + chunks.push(data.substring(cursor, nextCursor + 1)); + cursor = nextCursor + 1; + } + + return { + isEmpty: function() { + return chunks.length === 0; + }, + + next: function() { + return chunks.shift(); + } + }; +}; diff --git a/test/utils/chunker-test.js b/test/utils/chunker-test.js new file mode 100644 index 00000000..f56a38d8 --- /dev/null +++ b/test/utils/chunker-test.js @@ -0,0 +1,29 @@ +var vows = require('vows'); +var assert = require('assert'); +var Chunker = require('../../lib/utils/chunker'); + +vows.describe(Chunker) + .addBatch({ + 'empty string': { + topic: new Chunker('', 128), + 'is empty': function (chunker) { + assert.isTrue(chunker.isEmpty()); + }, + 'has no next': function (chunker) { + assert.isUndefined(chunker.next()); + } + }, + 'css': { + topic: new Chunker('a{color:red}p{}', 3), + 'is not empty': function (chunker) { + assert.isFalse(chunker.isEmpty()); + }, + 'breaks at first brace': function (chunker) { + assert.equal('a{color:red}', chunker.next()); + }, + 'breaks at second brace': function (chunker) { + assert.equal('p{}', chunker.next()); + } + } + }) + .export(module);