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