source: trip-planner-front/node_modules/split-string/index.js@ ceaed42

Last change on this file since ceaed42 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 3.6 KB
Line 
1/*!
2 * split-string <https://github.com/jonschlinkert/split-string>
3 *
4 * Copyright (c) 2015-2017, Jon Schlinkert.
5 * Released under the MIT License.
6 */
7
8'use strict';
9
10var extend = require('extend-shallow');
11
12module.exports = function(str, options, fn) {
13 if (typeof str !== 'string') {
14 throw new TypeError('expected a string');
15 }
16
17 if (typeof options === 'function') {
18 fn = options;
19 options = null;
20 }
21
22 // allow separator to be defined as a string
23 if (typeof options === 'string') {
24 options = { sep: options };
25 }
26
27 var opts = extend({sep: '.'}, options);
28 var quotes = opts.quotes || ['"', "'", '`'];
29 var brackets;
30
31 if (opts.brackets === true) {
32 brackets = {
33 '<': '>',
34 '(': ')',
35 '[': ']',
36 '{': '}'
37 };
38 } else if (opts.brackets) {
39 brackets = opts.brackets;
40 }
41
42 var tokens = [];
43 var stack = [];
44 var arr = [''];
45 var sep = opts.sep;
46 var len = str.length;
47 var idx = -1;
48 var closeIdx;
49
50 function expected() {
51 if (brackets && stack.length) {
52 return brackets[stack[stack.length - 1]];
53 }
54 }
55
56 while (++idx < len) {
57 var ch = str[idx];
58 var next = str[idx + 1];
59 var tok = { val: ch, idx: idx, arr: arr, str: str };
60 tokens.push(tok);
61
62 if (ch === '\\') {
63 tok.val = keepEscaping(opts, str, idx) === true ? (ch + next) : next;
64 tok.escaped = true;
65 if (typeof fn === 'function') {
66 fn(tok);
67 }
68 arr[arr.length - 1] += tok.val;
69 idx++;
70 continue;
71 }
72
73 if (brackets && brackets[ch]) {
74 stack.push(ch);
75 var e = expected();
76 var i = idx + 1;
77
78 if (str.indexOf(e, i + 1) !== -1) {
79 while (stack.length && i < len) {
80 var s = str[++i];
81 if (s === '\\') {
82 s++;
83 continue;
84 }
85
86 if (quotes.indexOf(s) !== -1) {
87 i = getClosingQuote(str, s, i + 1);
88 continue;
89 }
90
91 e = expected();
92 if (stack.length && str.indexOf(e, i + 1) === -1) {
93 break;
94 }
95
96 if (brackets[s]) {
97 stack.push(s);
98 continue;
99 }
100
101 if (e === s) {
102 stack.pop();
103 }
104 }
105 }
106
107 closeIdx = i;
108 if (closeIdx === -1) {
109 arr[arr.length - 1] += ch;
110 continue;
111 }
112
113 ch = str.slice(idx, closeIdx + 1);
114 tok.val = ch;
115 tok.idx = idx = closeIdx;
116 }
117
118 if (quotes.indexOf(ch) !== -1) {
119 closeIdx = getClosingQuote(str, ch, idx + 1);
120 if (closeIdx === -1) {
121 arr[arr.length - 1] += ch;
122 continue;
123 }
124
125 if (keepQuotes(ch, opts) === true) {
126 ch = str.slice(idx, closeIdx + 1);
127 } else {
128 ch = str.slice(idx + 1, closeIdx);
129 }
130
131 tok.val = ch;
132 tok.idx = idx = closeIdx;
133 }
134
135 if (typeof fn === 'function') {
136 fn(tok, tokens);
137 ch = tok.val;
138 idx = tok.idx;
139 }
140
141 if (tok.val === sep && tok.split !== false) {
142 arr.push('');
143 continue;
144 }
145
146 arr[arr.length - 1] += tok.val;
147 }
148
149 return arr;
150};
151
152function getClosingQuote(str, ch, i, brackets) {
153 var idx = str.indexOf(ch, i);
154 if (str.charAt(idx - 1) === '\\') {
155 return getClosingQuote(str, ch, idx + 1);
156 }
157 return idx;
158}
159
160function keepQuotes(ch, opts) {
161 if (opts.keepDoubleQuotes === true && ch === '"') return true;
162 if (opts.keepSingleQuotes === true && ch === "'") return true;
163 return opts.keepQuotes;
164}
165
166function keepEscaping(opts, str, idx) {
167 if (typeof opts.keepEscaping === 'function') {
168 return opts.keepEscaping(str, idx);
169 }
170 return opts.keepEscaping === true || str[idx + 1] === '\\';
171}
Note: See TracBrowser for help on using the repository browser.