Improves splitting strings when keeping values.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 24 Aug 2015 04:37:18 +0000 (05:37 +0100)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Mon, 24 Aug 2015 05:51:27 +0000 (06:51 +0100)
The last value in a string always included a separator while sometimes
it should not.

lib/utils/split.js
test/utils/split-test.js

index 8a9e419..0faf745 100644 (file)
@@ -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;
 }
index 001cc75..9357091 100644 (file)
@@ -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) {