@trenskow/parse 0.1.0
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/.eslintrc.cjs +63 -0
- package/.vscode/launch.json +22 -0
- package/LICENSE +9 -0
- package/README.md +37 -0
- package/index.js +62 -0
- package/package.json +30 -0
- package/test.js +73 -0
package/.eslintrc.cjs
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
module.exports = {
|
|
2
|
+
'env': {
|
|
3
|
+
'es6': true,
|
|
4
|
+
'node': true,
|
|
5
|
+
'mocha': true
|
|
6
|
+
},
|
|
7
|
+
'parserOptions': {
|
|
8
|
+
'ecmaVersion': 2022,
|
|
9
|
+
'sourceType': 'module'
|
|
10
|
+
},
|
|
11
|
+
'extends': 'eslint:recommended',
|
|
12
|
+
'rules': {
|
|
13
|
+
'indent': [
|
|
14
|
+
'error',
|
|
15
|
+
'tab',
|
|
16
|
+
{ 'SwitchCase': 1 }
|
|
17
|
+
],
|
|
18
|
+
'linebreak-style': [
|
|
19
|
+
'error',
|
|
20
|
+
'unix'
|
|
21
|
+
],
|
|
22
|
+
'quotes': [
|
|
23
|
+
'error',
|
|
24
|
+
'single'
|
|
25
|
+
],
|
|
26
|
+
'semi': [
|
|
27
|
+
'error',
|
|
28
|
+
'always'
|
|
29
|
+
],
|
|
30
|
+
'no-console': [
|
|
31
|
+
'error', {
|
|
32
|
+
'allow': [
|
|
33
|
+
'warn',
|
|
34
|
+
'error',
|
|
35
|
+
'info'
|
|
36
|
+
]
|
|
37
|
+
}
|
|
38
|
+
],
|
|
39
|
+
'no-unused-vars': [
|
|
40
|
+
'error', {
|
|
41
|
+
'argsIgnorePattern': '^_'
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
'no-empty': [
|
|
45
|
+
'error', {
|
|
46
|
+
'allowEmptyCatch': true
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
'no-trailing-spaces': [
|
|
50
|
+
'error', {
|
|
51
|
+
'ignoreComments': true
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
'require-atomic-updates': 'off',
|
|
55
|
+
'no-implicit-globals': [
|
|
56
|
+
'error'
|
|
57
|
+
],
|
|
58
|
+
'eol-last': [
|
|
59
|
+
'error',
|
|
60
|
+
'always'
|
|
61
|
+
]
|
|
62
|
+
}
|
|
63
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Use IntelliSense to learn about possible attributes.
|
|
3
|
+
// Hover to view descriptions of existing attributes.
|
|
4
|
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
|
5
|
+
"version": "0.2.0",
|
|
6
|
+
"configurations": [
|
|
7
|
+
{
|
|
8
|
+
"type": "pwa-node",
|
|
9
|
+
"request": "launch",
|
|
10
|
+
"name": "Launch Program",
|
|
11
|
+
"skipFiles": [
|
|
12
|
+
"<node_internals>/**"
|
|
13
|
+
],
|
|
14
|
+
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
|
|
15
|
+
"args": [
|
|
16
|
+
"${workspaceFolder}/test.js",
|
|
17
|
+
"--timeout",
|
|
18
|
+
"0"
|
|
19
|
+
]
|
|
20
|
+
}
|
|
21
|
+
]
|
|
22
|
+
}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
Copyright 2021 Kristian Trenskow
|
|
2
|
+
|
|
3
|
+
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
|
4
|
+
|
|
5
|
+
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
|
6
|
+
|
|
7
|
+
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
8
|
+
|
|
9
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
package/README.md
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
@trenskow/parse
|
|
2
|
+
----
|
|
3
|
+
|
|
4
|
+
A small library for parsing a string into a tree.
|
|
5
|
+
|
|
6
|
+
# Usage
|
|
7
|
+
|
|
8
|
+
Below is an example on how to use the library.
|
|
9
|
+
|
|
10
|
+
````javascript
|
|
11
|
+
import parse from '@trenskow/parse';
|
|
12
|
+
|
|
13
|
+
parse('${', '}').do('This ${is ${my ${nested ${string}}}}');
|
|
14
|
+
````
|
|
15
|
+
|
|
16
|
+
The above example will return the following structure
|
|
17
|
+
|
|
18
|
+
````JSON
|
|
19
|
+
[
|
|
20
|
+
"This ",
|
|
21
|
+
[
|
|
22
|
+
"is ",
|
|
23
|
+
[
|
|
24
|
+
"my ",
|
|
25
|
+
[
|
|
26
|
+
"nested ",
|
|
27
|
+
"string"
|
|
28
|
+
]
|
|
29
|
+
]
|
|
30
|
+
]
|
|
31
|
+
]
|
|
32
|
+
````
|
|
33
|
+
|
|
34
|
+
# License
|
|
35
|
+
|
|
36
|
+
See license in LICENSE.
|
|
37
|
+
|
package/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// Created 02/05/22 by Kristian Trenskow
|
|
2
|
+
// See LICENSE for license.
|
|
3
|
+
|
|
4
|
+
export default (opening, closing) => {
|
|
5
|
+
|
|
6
|
+
if (typeof opening !== 'string' || typeof closing !== 'string') {
|
|
7
|
+
throw new TypeError('Opening and closing tokens must be strings.');
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
if (opening.length === 0 || closing.length === 0) {
|
|
11
|
+
throw new TypeError('Opening and closing tokens cannot be zero-length.');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (opening === closing) {
|
|
15
|
+
throw new TypeError('Opening and closing tokens cannot be the same.');
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
do: (text) => {
|
|
20
|
+
|
|
21
|
+
const next = (text, offset = 0) => {
|
|
22
|
+
|
|
23
|
+
let result = [];
|
|
24
|
+
|
|
25
|
+
let literal = '';
|
|
26
|
+
|
|
27
|
+
let idx = offset;
|
|
28
|
+
|
|
29
|
+
for (idx ; idx < text.length ; idx++) {
|
|
30
|
+
if (text[idx] === '\\') {
|
|
31
|
+
literal += text[++idx];
|
|
32
|
+
} else if (text.substring(idx, idx + opening.length) === opening) {
|
|
33
|
+
result.push(literal);
|
|
34
|
+
let value;
|
|
35
|
+
[idx, value] = next(text, idx + opening.length);
|
|
36
|
+
result.push(value);
|
|
37
|
+
literal = '';
|
|
38
|
+
} else if (text.substring(idx, idx + closing.length) === closing) {
|
|
39
|
+
|
|
40
|
+
if (literal.length > 0) result.push(literal);
|
|
41
|
+
|
|
42
|
+
if (result.length === 1 && typeof result[0] === 'string') {
|
|
43
|
+
result = result[0];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return [idx + closing.length - 1, result];
|
|
47
|
+
|
|
48
|
+
} else {
|
|
49
|
+
literal += text[idx];
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
throw new Error('Missing closing token.');
|
|
54
|
+
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
return next(text + closing)[1];
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@trenskow/parse",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "A small library for parsing a string into a tree.",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "node ./node_modules/mocha/bin/mocha ./test.js"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/trenskow/parse.git"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"parse",
|
|
16
|
+
"string",
|
|
17
|
+
"nested"
|
|
18
|
+
],
|
|
19
|
+
"author": "Kristian Trenskow",
|
|
20
|
+
"license": "BSD-2-Clause",
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/trenskow/parse/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/trenskow/parse#readme",
|
|
25
|
+
"devDependencies": {
|
|
26
|
+
"chai": "^4.3.6",
|
|
27
|
+
"eslint": "^8.14.0",
|
|
28
|
+
"mocha": "^10.0.0"
|
|
29
|
+
}
|
|
30
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Created 02/05/22 by Kristian Trenskow
|
|
2
|
+
// See LICENSE for license.
|
|
3
|
+
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
|
|
6
|
+
import parse from './index.js';
|
|
7
|
+
|
|
8
|
+
console.info(JSON.stringify(parse('[', ']').do('This [is [my [nested [string]]]]'), null, 4));
|
|
9
|
+
|
|
10
|
+
describe('parser', () => {
|
|
11
|
+
it ('should come back with parsed tree (with escapes).', () => {
|
|
12
|
+
expect(parse('[', ']').do('This [is [my [\\[nested\\]] string]].')).to.eql([
|
|
13
|
+
'This ',
|
|
14
|
+
[
|
|
15
|
+
'is ',
|
|
16
|
+
[
|
|
17
|
+
'my ',
|
|
18
|
+
'[nested]',
|
|
19
|
+
' string'
|
|
20
|
+
]
|
|
21
|
+
],
|
|
22
|
+
'.'
|
|
23
|
+
]);
|
|
24
|
+
});
|
|
25
|
+
it ('should come back with parsed tree.', () => {
|
|
26
|
+
expect(parse('{', '}').do('This {is {my {nested} string}}.')).to.eql([
|
|
27
|
+
'This ',
|
|
28
|
+
[
|
|
29
|
+
'is ',
|
|
30
|
+
[
|
|
31
|
+
'my ',
|
|
32
|
+
'nested',
|
|
33
|
+
' string'
|
|
34
|
+
]
|
|
35
|
+
],
|
|
36
|
+
'.'
|
|
37
|
+
]);
|
|
38
|
+
});
|
|
39
|
+
it ('should come back with parsed tree (long tokens).', () => {
|
|
40
|
+
expect(parse('hello', 'goodbye').do('This hello is hello my hello nested goodbye string goodbye goodbye.')).to.eql([
|
|
41
|
+
'This ',
|
|
42
|
+
[
|
|
43
|
+
' is ',
|
|
44
|
+
[
|
|
45
|
+
' my ',
|
|
46
|
+
' nested ',
|
|
47
|
+
' string '
|
|
48
|
+
],
|
|
49
|
+
' '
|
|
50
|
+
],
|
|
51
|
+
'.']);
|
|
52
|
+
});
|
|
53
|
+
it ('should throw an error if closing token is missing.', () => {
|
|
54
|
+
expect(() => {
|
|
55
|
+
parse('[', ']').do('[this');
|
|
56
|
+
}).to.throw('Missing closing token.');
|
|
57
|
+
});
|
|
58
|
+
it ('should throw an error if opening or closing tokens are not a string.', () => {
|
|
59
|
+
expect(() => {
|
|
60
|
+
parse(0, 1).do('[this');
|
|
61
|
+
}).to.throw('Opening and closing tokens must be strings.');
|
|
62
|
+
});
|
|
63
|
+
it ('should throw an error if opening or closing tokens are zero-length.', () => {
|
|
64
|
+
expect(() => {
|
|
65
|
+
parse('', '').do('[this');
|
|
66
|
+
}).to.throw('Opening and closing tokens cannot be zero-length.');
|
|
67
|
+
});
|
|
68
|
+
it ('should throw an error if opening or closing are the same.', () => {
|
|
69
|
+
expect(() => {
|
|
70
|
+
parse('123', '123').do('[this');
|
|
71
|
+
}).to.throw('Opening and closing tokens cannot be the same.');
|
|
72
|
+
});
|
|
73
|
+
});
|