-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
index.js
148 lines (124 loc) · 3.69 KB
/
index.js
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
'use strict';
module.exports = (input, options = {}, fn) => {
if (typeof input !== 'string') throw new TypeError('expected a string');
if (typeof options === 'function') {
fn = options;
options = {};
}
let separator = options.separator || '.';
let ast = { type: 'root', nodes: [], stash: [''] };
let stack = [ast];
let state = { input, separator, stack };
let string = input;
let value, node;
let i = -1;
state.bos = () => i === 0;
state.eos = () => i === string.length;
state.prev = () => string[i - 1];
state.next = () => string[i + 1];
let quotes = options.quotes || [];
let openers = options.brackets || {};
if (options.brackets === true) {
openers = { '[': ']', '(': ')', '{': '}', '<': '>' };
}
if (options.quotes === true) {
quotes = ['"', '\'', '`'];
}
let closers = invert(openers);
let keep = options.keep || (value => value !== '\\');
const block = () => (state.block = stack[stack.length - 1]);
const peek = () => string[i + 1];
const next = () => string[++i];
const append = value => {
state.value = value;
if (value && keep(value, state) !== false) {
state.block.stash[state.block.stash.length - 1] += value;
}
};
const closeIndex = (value, startIdx) => {
let idx = string.indexOf(value, startIdx);
if (idx > -1 && string[idx - 1] === '\\') {
idx = closeIndex(value, idx + 1);
}
return idx;
};
for (; i < string.length - 1;) {
state.value = value = next();
state.index = i;
block();
// handle escaped characters
if (value === '\\') {
if (peek() === '\\') {
append(value + next());
} else {
// if the next char is not '\\', allow the "append" function
// to determine if the backslashes should be added
append(value);
append(next());
}
continue;
}
// handle quoted strings
if (quotes.includes(value)) {
let pos = i + 1;
let idx = closeIndex(value, pos);
if (idx > -1) {
append(value); // append opening quote
append(string.slice(pos, idx)); // append quoted string
append(string[idx]); // append closing quote
i = idx;
continue;
}
append(value);
continue;
}
// handle opening brackets, if not disabled
if (options.brackets !== false && openers[value]) {
node = { type: 'bracket', nodes: [] };
node.stash = keep(value) !== false ? [value] : [''];
node.parent = state.block;
state.block.nodes.push(node);
stack.push(node);
continue;
}
// handle closing brackets, if not disabled
if (options.brackets !== false && closers[value]) {
if (stack.length === 1) {
append(value);
continue;
}
append(value);
node = stack.pop();
block();
append(node.stash.join(''));
continue;
}
// push separator onto stash
if (value === separator && state.block.type === 'root') {
if (typeof fn === 'function' && fn(state) === false) {
append(value);
continue;
}
state.block.stash.push('');
continue;
}
// append value onto the last string on the stash
append(value);
}
node = stack.pop();
while (node !== ast) {
if (options.strict === true) {
let column = i - node.stash.length + 1;
throw new SyntaxError(`Unmatched: "${node.stash[0]}", at column ${column}`);
}
value = (node.parent.stash.pop() + node.stash.join('.'));
node.parent.stash = node.parent.stash.concat(value.split('.'));
node = stack.pop();
}
return node.stash;
};
function invert(obj) {
let inverted = {};
for (const key of Object.keys(obj)) inverted[obj[key]] = key;
return inverted;
}