UNPKG

1.23 kBJavaScriptView Raw
1'use strict'
2
3let list = {
4 comma(string) {
5 return list.split(string, [','], true)
6 },
7
8 space(string) {
9 let spaces = [' ', '\n', '\t']
10 return list.split(string, spaces)
11 },
12
13 split(string, separators, last) {
14 let array = []
15 let current = ''
16 let split = false
17
18 let func = 0
19 let inQuote = false
20 let prevQuote = ''
21 let escape = false
22
23 for (let letter of string) {
24 if (escape) {
25 escape = false
26 } else if (letter === '\\') {
27 escape = true
28 } else if (inQuote) {
29 if (letter === prevQuote) {
30 inQuote = false
31 }
32 } else if (letter === '"' || letter === "'") {
33 inQuote = true
34 prevQuote = letter
35 } else if (letter === '(') {
36 func += 1
37 } else if (letter === ')') {
38 if (func > 0) func -= 1
39 } else if (func === 0) {
40 if (separators.includes(letter)) split = true
41 }
42
43 if (split) {
44 if (current !== '') array.push(current.trim())
45 current = ''
46 split = false
47 } else {
48 current += letter
49 }
50 }
51
52 if (last || current !== '') array.push(current.trim())
53 return array
54 }
55}
56
57module.exports = list
58list.default = list