main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
1.5 KB
|
Rev | Line | |
---|
[79a0317] | 1 | var Marker = require('../tokenizer/marker');
|
---|
| 2 |
|
---|
| 3 | function is(value, separator, isSeparatorRegex) {
|
---|
| 4 | return isSeparatorRegex
|
---|
| 5 | ? separator.test(value)
|
---|
| 6 | : value === separator;
|
---|
| 7 | }
|
---|
| 8 |
|
---|
| 9 | function split(value, separator) {
|
---|
| 10 | var openLevel = Marker.OPEN_ROUND_BRACKET;
|
---|
| 11 | var closeLevel = Marker.CLOSE_ROUND_BRACKET;
|
---|
| 12 | var level = 0;
|
---|
| 13 | var cursor = 0;
|
---|
| 14 | var lastStart = 0;
|
---|
| 15 | var lastValue;
|
---|
| 16 | var lastCharacter;
|
---|
| 17 | var len = value.length;
|
---|
| 18 | var parts = [];
|
---|
| 19 | var isSeparatorRegex = typeof (separator) == 'object' && 'exec' in separator;
|
---|
| 20 |
|
---|
| 21 | if (!isSeparatorRegex && value.indexOf(separator) == -1) {
|
---|
| 22 | return [value];
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | if (value.indexOf(openLevel) == -1) {
|
---|
| 26 | return value.split(separator);
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | while (cursor < len) {
|
---|
| 30 | if (value[cursor] == openLevel) {
|
---|
| 31 | level++;
|
---|
| 32 | } else if (value[cursor] == closeLevel) {
|
---|
| 33 | level--;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 | if (level === 0 && cursor > 0 && cursor + 1 < len && is(value[cursor], separator, isSeparatorRegex)) {
|
---|
| 37 | parts.push(value.substring(lastStart, cursor));
|
---|
| 38 |
|
---|
| 39 | if (isSeparatorRegex && separator.exec(value[cursor]).length > 1) {
|
---|
| 40 | parts.push(value[cursor]);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | lastStart = cursor + 1;
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | cursor++;
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | if (lastStart < cursor + 1) {
|
---|
| 50 | lastValue = value.substring(lastStart);
|
---|
| 51 | lastCharacter = lastValue[lastValue.length - 1];
|
---|
| 52 | if (is(lastCharacter, separator, isSeparatorRegex)) {
|
---|
| 53 | lastValue = lastValue.substring(0, lastValue.length - 1);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | parts.push(lastValue);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | return parts;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | module.exports = split;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.