Adds an option to Splitter helper to keep separators.
authorJakub Pawlowicz <contact@jakubpawlowicz.com>
Tue, 24 Mar 2015 19:41:25 +0000 (19:41 +0000)
committerJakub Pawlowicz <contact@jakubpawlowicz.com>
Sun, 12 Apr 2015 11:15:29 +0000 (12:15 +0100)
lib/utils/splitter.js
test/utils/splitter-test.js

index 839c6c6..2d3bb47 100644 (file)
@@ -2,11 +2,11 @@ function Splitter(separator) {
   this.separator = separator;
 }
 
-Splitter.prototype.split = function (value) {
+Splitter.prototype.split = function (value, withSeparator) {
   if (value.indexOf(this.separator) === -1)
     return [value];
 
-  if (value.indexOf('(') === -1)
+  if (value.indexOf('(') === -1 && !withSeparator)
     return value.split(this.separator);
 
   var level = 0;
@@ -21,7 +21,7 @@ Splitter.prototype.split = function (value) {
     } else if (value[cursor] == ')') {
       level--;
     } else if (value[cursor] == this.separator && level === 0) {
-      tokens.push(value.substring(lastStart, cursor));
+      tokens.push(value.substring(lastStart, cursor + (withSeparator ? 1 : 0)));
       lastStart = cursor + 1;
     }
   }
index ef6082a..88a2c79 100644 (file)
@@ -2,9 +2,9 @@ var vows = require('vows');
 var assert = require('assert');
 var Splitter = require('../../lib/utils/splitter');
 
-function split(value, expectedValue, separator) {
+function split(value, expectedValue, separator, withSeparator) {
   return function () {
-    assert.deepEqual(new Splitter(separator).split(value), expectedValue);
+    assert.deepEqual(new Splitter(separator).split(value, withSeparator), expectedValue);
   };
 }
 
@@ -19,4 +19,9 @@ vows.describe(Splitter)
     'space separated - level 1': split('rgb(0, 0, 0) #fff', ['rgb(0, 0, 0)', '#fff'], ' '),
     'space separated - level 2': split('linear-gradient(0, #fff, rgba(0, 0, 0)) red', ['linear-gradient(0, #fff, rgba(0, 0, 0))', 'red'], ' ')
   })
+  .addBatch({
+    'leading space and quote with separator': split(' "Font"', [' "Font"'], ' ', true),
+    'comma separated - level 2 - with separator': split('linear-gradient(0,#fff,rgba(0,0,0)),red', ['linear-gradient(0,#fff,rgba(0,0,0)),', 'red'], ',', true),
+    'space separated - level 2 - with separator': split('linear-gradient(0, #fff, rgba(0, 0, 0)) red', ['linear-gradient(0, #fff, rgba(0, 0, 0)) ', 'red'], ' ', true)
+  })
   .export(module);