f0-content-parser 1.0.3-dev
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/LICENSE +21 -0
- package/README.md +29 -0
- package/dist/hasDynamicData.js +34 -0
- package/dist/index.js +29 -0
- package/dist/parseContent.js +98 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License Copyright (c) 2021
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free
|
|
4
|
+
of charge, to any person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the Software without
|
|
6
|
+
restriction, including without limitation the rights to use, copy, modify, merge,
|
|
7
|
+
publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to the
|
|
9
|
+
following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice
|
|
12
|
+
(including the next paragraph) shall be included in all copies or substantial
|
|
13
|
+
portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
16
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
|
|
18
|
+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
|
19
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
20
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# f0-content-parser
|
|
2
|
+
|
|
3
|
+
F0 content parser
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- ES6 syntax, managed with Prettier
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
yarn add f0-content-parser
|
|
13
|
+
// or
|
|
14
|
+
npm i f0-content-parser
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
### Requirements
|
|
18
|
+
|
|
19
|
+
- keymirror
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### Usage
|
|
23
|
+
|
|
24
|
+
```js
|
|
25
|
+
import { parseContent } from 'f0-content-parser';
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
console.log(parseContent(`YOUR_CONTENT_HERE`));
|
|
29
|
+
```
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.hasDynamicData = hasDynamicData;
|
|
7
|
+
/**
|
|
8
|
+
* Test whether a content string references dynamic data
|
|
9
|
+
* @param {String} content - The content to check for dynamic data
|
|
10
|
+
*
|
|
11
|
+
* @return {Boolean} - Returns true if the content contains dynamic data
|
|
12
|
+
*/
|
|
13
|
+
function hasDynamicData(content) {
|
|
14
|
+
var escapedCharacter = false;
|
|
15
|
+
var character = void 0;
|
|
16
|
+
|
|
17
|
+
if (typeof content !== 'string') {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
for (var index = 0; index < content.length; index++) {
|
|
22
|
+
character = content[index];
|
|
23
|
+
|
|
24
|
+
if (escapedCharacter) {
|
|
25
|
+
escapedCharacter = false;
|
|
26
|
+
} else if (character === '{') {
|
|
27
|
+
return true;
|
|
28
|
+
} else if (character === '\\') {
|
|
29
|
+
escapedCharacter = true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return false;
|
|
34
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _parseContent = require('./parseContent');
|
|
8
|
+
|
|
9
|
+
Object.keys(_parseContent).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
Object.defineProperty(exports, key, {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
get: function get() {
|
|
14
|
+
return _parseContent[key];
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
var _hasDynamicData = require('./hasDynamicData');
|
|
20
|
+
|
|
21
|
+
Object.keys(_hasDynamicData).forEach(function (key) {
|
|
22
|
+
if (key === "default" || key === "__esModule") return;
|
|
23
|
+
Object.defineProperty(exports, key, {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
get: function get() {
|
|
26
|
+
return _hasDynamicData[key];
|
|
27
|
+
}
|
|
28
|
+
});
|
|
29
|
+
});
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.parseContent = parseContent;
|
|
7
|
+
|
|
8
|
+
var _lodash = require('lodash.get');
|
|
9
|
+
|
|
10
|
+
var _lodash2 = _interopRequireDefault(_lodash);
|
|
11
|
+
|
|
12
|
+
var _keymirror = require('keymirror');
|
|
13
|
+
|
|
14
|
+
var _keymirror2 = _interopRequireDefault(_keymirror);
|
|
15
|
+
|
|
16
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
17
|
+
|
|
18
|
+
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
19
|
+
|
|
20
|
+
// Create an object of states with values equal to its keys
|
|
21
|
+
var STATES = (0, _keymirror2.default)({
|
|
22
|
+
default: true,
|
|
23
|
+
escaped_character: true,
|
|
24
|
+
parsing_object: true,
|
|
25
|
+
nested_brackets: true
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Given a content string, replace occurences of dynamic data referenced within `{...}`
|
|
30
|
+
* @param {String} content - The string to parse
|
|
31
|
+
* @param {Object} data - An object containing data that can be referenced from the string
|
|
32
|
+
* @return {String} - The string with variables replaced with their values
|
|
33
|
+
*/
|
|
34
|
+
function parseContent(content, data) {
|
|
35
|
+
var _stateHandlers;
|
|
36
|
+
|
|
37
|
+
var state = STATES.default,
|
|
38
|
+
objectPaths = ['', ''],
|
|
39
|
+
bracketsCount = 0,
|
|
40
|
+
parsedContent = '',
|
|
41
|
+
index = void 0,
|
|
42
|
+
character = void 0;
|
|
43
|
+
|
|
44
|
+
// State handlers for each state the parser can be in
|
|
45
|
+
var stateHandlers = (_stateHandlers = {}, _defineProperty(_stateHandlers, STATES.default, function () {
|
|
46
|
+
if (character === '{') {
|
|
47
|
+
bracketsCount++;
|
|
48
|
+
state = STATES.parsing_object;
|
|
49
|
+
} else if (character === '}') {
|
|
50
|
+
throw new Error('"' + content + '" has an unmatched curly brace at index ' + index);
|
|
51
|
+
} else if (character === '\\') {
|
|
52
|
+
state = STATES.escaped_character;
|
|
53
|
+
} else {
|
|
54
|
+
parsedContent += character;
|
|
55
|
+
}
|
|
56
|
+
}), _defineProperty(_stateHandlers, STATES.escaped_character, function () {
|
|
57
|
+
parsedContent += character;
|
|
58
|
+
state = STATES.default;
|
|
59
|
+
}), _defineProperty(_stateHandlers, STATES.parsing_object, function () {
|
|
60
|
+
if (character === '{') {
|
|
61
|
+
// if during the PARSING state we find another `{`, that means
|
|
62
|
+
// that there are nested dynamic content
|
|
63
|
+
bracketsCount++;
|
|
64
|
+
objectPaths[bracketsCount] = objectPaths[bracketsCount] || '';
|
|
65
|
+
state = STATES.nested_brackets;
|
|
66
|
+
} else if (character === '}') {
|
|
67
|
+
parsedContent += (0, _lodash2.default)(data, objectPaths[bracketsCount], '');
|
|
68
|
+
state = STATES.default;
|
|
69
|
+
objectPaths[bracketsCount] = '';
|
|
70
|
+
bracketsCount--;
|
|
71
|
+
} else {
|
|
72
|
+
objectPaths[bracketsCount] += character;
|
|
73
|
+
}
|
|
74
|
+
}), _defineProperty(_stateHandlers, STATES.nested_brackets, function () {
|
|
75
|
+
if (character === '{') {
|
|
76
|
+
bracketsCount++;
|
|
77
|
+
objectPaths[bracketsCount] = objectPaths[bracketsCount] || '';
|
|
78
|
+
} else if (character === '}') {
|
|
79
|
+
objectPaths[bracketsCount - 1] += (0, _lodash2.default)(data, objectPaths[bracketsCount], '');
|
|
80
|
+
objectPaths[bracketsCount] = '';
|
|
81
|
+
bracketsCount--;
|
|
82
|
+
// only break out of the nested state if we're back down to a count of 1
|
|
83
|
+
// aka the original/first bracket
|
|
84
|
+
if (bracketsCount <= 1) {
|
|
85
|
+
state = STATES.parsing_object;
|
|
86
|
+
}
|
|
87
|
+
} else {
|
|
88
|
+
objectPaths[bracketsCount] += character;
|
|
89
|
+
}
|
|
90
|
+
}), _stateHandlers);
|
|
91
|
+
|
|
92
|
+
for (index = 0; index < content.length; index++) {
|
|
93
|
+
character = content[index];
|
|
94
|
+
stateHandlers[state]();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return parsedContent;
|
|
98
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "f0-content-parser",
|
|
3
|
+
"version": "1.0.3-dev",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "F0 content parser",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "hppcont",
|
|
8
|
+
"main": "dist/index.js",
|
|
9
|
+
"scripts": {
|
|
10
|
+
"build": "vite",
|
|
11
|
+
"test": "exit 0"
|
|
12
|
+
},
|
|
13
|
+
"repository": "https://www.gitlab.com/hppcont/f0-content-parser",
|
|
14
|
+
"publishConfig": {
|
|
15
|
+
"access": "public"
|
|
16
|
+
},
|
|
17
|
+
"dependencies": {
|
|
18
|
+
"lodash.get": "^4.4.2",
|
|
19
|
+
"keymirror": "^0.1.1"
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"vite": "^4.0.4",
|
|
23
|
+
"vitest": "^0.27.1"
|
|
24
|
+
}
|
|
25
|
+
}
|