From 6957bc71feaf76819dfa3ae53320992d87bd6a5b Mon Sep 17 00:00:00 2001 From: Jakub Pawlowicz Date: Tue, 24 Mar 2015 19:41:25 +0000 Subject: [PATCH] Adds an option to Splitter helper to keep separators. --- lib/utils/splitter.js | 6 +++--- test/utils/splitter-test.js | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/utils/splitter.js b/lib/utils/splitter.js index 839c6c6e..2d3bb47a 100644 --- a/lib/utils/splitter.js +++ b/lib/utils/splitter.js @@ -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; } } diff --git a/test/utils/splitter-test.js b/test/utils/splitter-test.js index ef6082a7..88a2c796 100644 --- a/test/utils/splitter-test.js +++ b/test/utils/splitter-test.js @@ -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); -- 2.34.1