From cd97a57871e9fa8cacce00c0a6568b3a333b3afd Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Mon, 24 Aug 2015 05:37:18 +0100 Subject: [PATCH] Improves splitting strings when keeping values. The last value in a string always included a separator while sometimes it should not. --- lib/utils/split.js | 10 ++++++++-- test/utils/split-test.js | 12 ++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/lib/utils/split.js b/lib/utils/split.js index 8a9e419d..0faf7459 100644 --- a/lib/utils/split.js +++ b/lib/utils/split.js @@ -34,8 +34,14 @@ function split(value, separator, includeSeparator, openLevel, closeLevel) { cursor++; } - if (lastStart < cursor + 1) - tokens.push(value.substring(lastStart)); + if (lastStart < cursor + 1) { + var lastValue = value.substring(lastStart); + var lastCharacter = lastValue[lastValue.length - 1]; + if (!includeSeparator && (withRegex ? separator.test(lastCharacter) : lastCharacter == separator)) + lastValue = lastValue.substring(0, lastValue.length - 1); + + tokens.push(lastValue); + } return tokens; } diff --git a/test/utils/split-test.js b/test/utils/split-test.js index 001cc751..93570914 100644 --- a/test/utils/split-test.js +++ b/test/utils/split-test.js @@ -52,6 +52,18 @@ vows.describe(split) assert.deepEqual(split(input, ' '), ['linear-gradient(0, #fff, rgba(0, 0, 0))', 'red']); } }, + 'semicolon separated - single': { + topic: 'apply(--var);', + split: function (input) { + assert.deepEqual(split(input, ';'), ['apply(--var)']); + } + }, + 'semicolon separated - double': { + topic: 'apply(--var);color:red;', + split: function (input) { + assert.deepEqual(split(input, ';'), ['apply(--var)', 'color:red']); + } + }, 'with regex': { topic: 'no-repeat,0/0', split: function (input) { -- 2.34.1