From e4dfd5950ae8a99b6f1b3546af505dde7f0c553c Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Fri, 20 Jan 2017 10:53:20 +0100 Subject: [PATCH] Simplifies `split` helper code. Why: * Much of its complexity was due to old tokenizer which is no more; * currently it serves as a way to splitting strings where commas inside round brackets are ignored. --- lib/utils/split.js | 49 +++++++++++------------ test/utils/split-test.js | 84 ---------------------------------------- 2 files changed, 23 insertions(+), 110 deletions(-) diff --git a/lib/utils/split.js b/lib/utils/split.js index 899ce299..c9162550 100644 --- a/lib/utils/split.js +++ b/lib/utils/split.js @@ -1,23 +1,23 @@ -function split(value, separator, includeSeparator, openLevel, closeLevel, firstOnly) { - var withRegex = typeof separator != 'string'; - var hasSeparator = withRegex ? - separator.test(value) : - value.indexOf(separator) > -1; - - if (!hasSeparator) - return [value]; - - openLevel = openLevel || '('; - closeLevel = closeLevel || ')'; - - if (value.indexOf(openLevel) == -1 && !includeSeparator && !firstOnly) - return value.split(separator); +var Marker = require('../tokenizer/marker'); +function split(value, separator) { + var openLevel = Marker.OPEN_ROUND_BRACKET; + var closeLevel = Marker.CLOSE_ROUND_BRACKET; var level = 0; var cursor = 0; var lastStart = 0; + var lastValue; + var lastCharacter; var len = value.length; - var tokens = []; + var parts = []; + + if (value.indexOf(separator) == -1) { + return [value]; + } + + if (value.indexOf(openLevel) == -1) { + return value.split(separator); + } while (cursor < len) { if (value[cursor] == openLevel) { @@ -26,28 +26,25 @@ function split(value, separator, includeSeparator, openLevel, closeLevel, firstO level--; } - if (level === 0 && cursor > 0 && cursor + 1 < len && (withRegex ? separator.test(value[cursor]) : value[cursor] == separator)) { - tokens.push(value.substring(lastStart, cursor + (includeSeparator ? 1 : 0))); + if (level === 0 && cursor > 0 && cursor + 1 < len && value[cursor] == separator) { + parts.push(value.substring(lastStart, cursor)); lastStart = cursor + 1; - - if (firstOnly && tokens.length == 1) { - break; - } } cursor++; } if (lastStart < cursor + 1) { - var lastValue = value.substring(lastStart); - var lastCharacter = lastValue[lastValue.length - 1]; - if (!includeSeparator && (withRegex ? separator.test(lastCharacter) : lastCharacter == separator)) + lastValue = value.substring(lastStart); + lastCharacter = lastValue[lastValue.length - 1]; + if (lastCharacter == separator) { lastValue = lastValue.substring(0, lastValue.length - 1); + } - tokens.push(lastValue); + parts.push(lastValue); } - return tokens; + return parts; } module.exports = split; diff --git a/test/utils/split-test.js b/test/utils/split-test.js index ab0835e9..2eb6567a 100644 --- a/test/utils/split-test.js +++ b/test/utils/split-test.js @@ -63,90 +63,6 @@ vows.describe(split) split: function (input) { assert.deepEqual(split(input, ';'), ['apply(--var)', 'color:red']); } - }, - 'with regex': { - topic: 'no-repeat,0/0', - split: function (input) { - assert.deepEqual(split(input, /[ ,\/]/), ['no-repeat', '0', '0']); - } - } - }) - .addBatch({ - 'including separator - leading space and quote': { - topic: ' "Font"', - split: function (input) { - assert.deepEqual(split(input, ' ', true), [' "Font"']); - } - }, - 'including separator - comma separated - level 2': { - topic: 'linear-gradient(0,#fff,rgba(0,0,0)),red', - split: function (input) { - assert.deepEqual(split(input, ',', true), ['linear-gradient(0,#fff,rgba(0,0,0)),', 'red']); - } - }, - 'including separator - space separated - level 2 with spaces': { - topic: 'linear-gradient(0, #fff, rgba(0, 0, 0)) red', - split: function (input) { - assert.deepEqual(split(input, ' ', true), ['linear-gradient(0, #fff, rgba(0, 0, 0)) ', 'red']); - } - }, - 'including separator - with regex': { - topic: 'no-repeat,0/0', - split: function (input) { - assert.deepEqual(split(input, /[ ,\/]/, true), ['no-repeat,', '0/', '0']); - } - } - }) - .addBatch({ - 'with custom wrappers - level 1': { - topic: 'a{ color:red; width:100% } p{ color:red }', - split: function (input) { - assert.deepEqual(split(input, ' ', true, '{', '}'), [ 'a{ color:red; width:100% } ', 'p{ color:red }' ]); - } - }, - 'with custom wrappers - level 2': { - topic: 'a{ color:red; --var { color:red; display: none } } p{ color:red }', - split: function (input) { - assert.deepEqual(split(input, ' ', true, '{', '}'), [ 'a{ color:red; --var { color:red; display: none } } ', 'p{ color:red }' ]); - } - }, - 'semicolon separated - variable list': { - topic: '--my-toolbar:{color:red;width:100%}', - split: function (input) { - assert.deepEqual(split(input, ';', false, '{', '}'), ['--my-toolbar:{color:red;width:100%}']); - } - }, - 'with custom wrappers - on close brace': { - topic: 'a{ color:red; --var { color:red; display: none } } p{ color:red }', - split: function (input) { - assert.deepEqual(split(input, '}', true, '{', '}'), [ 'a{ color:red; --var { color:red; display: none } }', ' p{ color:red }' ]); - } - }, - 'with custom wrappers - one block on close brace': { - topic: '{ color:red; --var { color:red; display: none } color:blue }', - split: function (input) { - assert.deepEqual(split(input, '}', true, '{', '}'), [ '{ color:red; --var { color:red; display: none } color:blue }' ]); - } - } - }) - .addBatch({ - 'just first one': { - topic: 'linear-gradient(0, #fff, rgba(0, 0, 0)) red', - split: function (input) { - assert.deepEqual(split(input, ' ', false, '(', ')', true), ['linear-gradient(0, #fff, rgba(0, 0, 0))']); - } - }, - 'just first one when no opening token': { - topic: 'red blue', - split: function (input) { - assert.deepEqual(split(input, ' ', false, '(', ')', true), ['red']); - } - }, - 'just first one when no closing token in last token': { - topic: 'red linear-gradient(0 0', - split: function (input) { - assert.deepEqual(split(input, ' ', false, '(', ')', true), ['red']); - } } }) .export(module); -- 2.34.1