-function split(value, separator, includeSeparator) {
+function split(value, separator, includeSeparator, openLevel, closeLevel) {
var withRegex = typeof separator != 'string';
var hasSeparator = withRegex ?
separator.test(value) :
if (!hasSeparator)
return [value];
- if (value.indexOf('(') === -1 && !includeSeparator)
+ openLevel = openLevel || '(';
+ closeLevel = closeLevel || ')';
+
+ if (value.indexOf(openLevel) == -1 && !includeSeparator)
return value.split(separator);
var level = 0;
var tokens = [];
while (cursor++ < len) {
- if (value[cursor] == '(') {
+ if (value[cursor] == openLevel) {
level++;
- } else if (value[cursor] == ')') {
+ } else if (value[cursor] == closeLevel) {
level--;
} else if ((withRegex ? separator.test(value[cursor]) : value[cursor] == separator) && level === 0) {
tokens.push(value.substring(lastStart, cursor + (includeSeparator ? 1 : 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%}']);
+ }
+ }
+ })
.export(module);