@trenskow/parse 0.1.7 → 0.1.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.vscode/launch.json +1 -1
- package/.vscode/settings.json +6 -0
- package/index.js +25 -0
- package/package.json +1 -1
- package/test.js +20 -0
package/.vscode/launch.json
CHANGED
package/index.js
CHANGED
|
@@ -37,6 +37,14 @@ export default (opening, closing, options) => {
|
|
|
37
37
|
throw new TypeError('Max depth must be a number.');
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
+
let boundaries = options.boundaries;
|
|
41
|
+
|
|
42
|
+
if (typeof boundaries === 'undefined') boundaries = 'exclude';
|
|
43
|
+
|
|
44
|
+
if (!['exclude', 'include'].includes(boundaries)) {
|
|
45
|
+
throw new TypeError('Boundaries must be either `\'exclude\'` (default) or `\'include\'`.');
|
|
46
|
+
}
|
|
47
|
+
|
|
40
48
|
if (maxDepth < 0) {
|
|
41
49
|
throw new TypeError('Max depth must be greater than zero.');
|
|
42
50
|
}
|
|
@@ -77,11 +85,17 @@ export default (opening, closing, options) => {
|
|
|
77
85
|
if (text.substring(idx, idx + opening.length) === opening) {
|
|
78
86
|
|
|
79
87
|
if (maxDepth > depth) {
|
|
88
|
+
|
|
80
89
|
result.push(literal);
|
|
90
|
+
|
|
81
91
|
let value;
|
|
92
|
+
|
|
82
93
|
[idx, value] = next(text, idx + opening.length, depth + 1);
|
|
94
|
+
|
|
83
95
|
result.push(value);
|
|
96
|
+
|
|
84
97
|
literal = '';
|
|
98
|
+
|
|
85
99
|
} else {
|
|
86
100
|
literal += text[idx];
|
|
87
101
|
ignoredDepths++;
|
|
@@ -97,6 +111,17 @@ export default (opening, closing, options) => {
|
|
|
97
111
|
result = result[0];
|
|
98
112
|
}
|
|
99
113
|
|
|
114
|
+
if (depth > 0 && boundaries === 'include') {
|
|
115
|
+
if (Array.isArray(result)) {
|
|
116
|
+
if (Array.isArray(result[0])) result[0] = [opening].concat(result[0]);
|
|
117
|
+
else result[0] = `${opening}${result[0]}`;
|
|
118
|
+
if (Array.isArray(result[result.length - 1])) result.push(closing);
|
|
119
|
+
else result[result.length - 1] = `${result[result.length - 1]}${closing}`;
|
|
120
|
+
} else {
|
|
121
|
+
result = `${opening}${result}${closing}`;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
100
125
|
return [idx + closing.length - 1, result];
|
|
101
126
|
|
|
102
127
|
} else {
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -34,6 +34,21 @@ describe('parser', () => {
|
|
|
34
34
|
'.'
|
|
35
35
|
]);
|
|
36
36
|
});
|
|
37
|
+
it ('should come back with parsed tree (with escapes and boundaries).', () => {
|
|
38
|
+
expect(parse('[', ']', { boundaries: 'include' }).do('This [is [my [\\[nested\\]] string]].')).to.eql([
|
|
39
|
+
'This ',
|
|
40
|
+
[
|
|
41
|
+
'[is ',
|
|
42
|
+
[
|
|
43
|
+
'[my ',
|
|
44
|
+
'[[nested]]',
|
|
45
|
+
' string]'
|
|
46
|
+
],
|
|
47
|
+
']'
|
|
48
|
+
],
|
|
49
|
+
'.'
|
|
50
|
+
]);
|
|
51
|
+
});
|
|
37
52
|
it ('should come back with parsed tree (long tokens).', () => {
|
|
38
53
|
expect(parse('hello', 'goodbye').do('This hello is hello my hello nested goodbye string goodbye goodbye.')).to.eql([
|
|
39
54
|
'This ',
|
|
@@ -109,4 +124,9 @@ describe('parser', () => {
|
|
|
109
124
|
parse('[', ']', { maxDepth: -1 });
|
|
110
125
|
}).to.throw('Max depth must be greater than zero.');
|
|
111
126
|
});
|
|
127
|
+
it ('should throw an error if boundaries is unknown value.', () => {
|
|
128
|
+
expect(() => {
|
|
129
|
+
parse('[', ']', { boundaries: 'wrong' });
|
|
130
|
+
}).to.throw('Boundaries must be either `\'exclude\'` (default) or `\'include\'`.');
|
|
131
|
+
});
|
|
112
132
|
});
|