@doist/react-interpolate 0.2.0 → 0.3.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/README.md +49 -48
- package/dist/react-interpolate.cjs +56 -48
- package/dist/react-interpolate.min.cjs +1 -1
- package/dist/react-interpolate.min.mjs +1 -1
- package/dist/react-interpolate.mjs +33 -32
- package/package.json +73 -62
- package/src/constants.js +10 -10
- package/src/index.js +3 -8
- package/src/interpolate.js +13 -20
- package/src/lexer.js +8 -10
- package/src/node.js +11 -11
- package/src/parser.js +7 -8
- package/src/syntax.js +9 -14
- package/.eslintrc.js +0 -36
- package/.github/workflows/nodejs.yml +0 -32
- package/.prettierrc.json +0 -9
- package/CONTRIBUTING.md +0 -7
- package/__test__/Interpolate.test.js +0 -203
- package/__test__/parser.test.js +0 -45
- package/babel.config.js +0 -18
- package/dist/react-interpolate.min.cjs.gz +0 -0
- package/dist/react-interpolate.min.mjs.gz +0 -0
- package/rollup.config.js +0 -17
package/README.md
CHANGED
|
@@ -1,21 +1,19 @@
|
|
|
1
1
|
# react-interpolate
|
|
2
2
|
|
|
3
|
-
[](http://img.badgesize.io/https://cdn.jsdelivr.net/gh/Doist/react-interpolate/dist/react-interpolate.min.cjs?compression=gzip)
|
|
4
|
-
[](https://github.com/Doist/react-interpolate/actions)
|
|
5
|
-
|
|
3
|
+
[](http://img.badgesize.io/https://cdn.jsdelivr.net/gh/Doist/react-interpolate/dist/react-interpolate.min.cjs?compression=gzip) [](https://github.com/Doist/react-interpolate/actions)
|
|
6
4
|
|
|
7
5
|
A string interpolation component that formats and interpolates a template string in a safe way.
|
|
8
6
|
|
|
9
7
|
```jsx
|
|
10
|
-
import Interpolate from
|
|
8
|
+
import Interpolate from '@doist/react-interpolate'
|
|
11
9
|
|
|
12
10
|
function Greeting() {
|
|
13
11
|
return (
|
|
14
12
|
<Interpolate
|
|
15
13
|
string="<h1>Hello {name}. Here is <a>your order info</a></h1>"
|
|
16
14
|
mapping={{
|
|
17
|
-
name:
|
|
18
|
-
a: <a href="https://orderinfo.com"
|
|
15
|
+
name: 'William',
|
|
16
|
+
a: <a href="https://orderinfo.com" />,
|
|
19
17
|
}}
|
|
20
18
|
/>
|
|
21
19
|
)
|
|
@@ -28,21 +26,22 @@ Would render the following HTML
|
|
|
28
26
|
<h1>Hello William. Here is <a href="https://orderinfo.com">your order info</a></h1>
|
|
29
27
|
```
|
|
30
28
|
|
|
31
|
-
|
|
32
29
|
## Component API
|
|
33
30
|
|
|
34
31
|
`<Interpolate>` component accepts the following props
|
|
35
32
|
|
|
36
33
|
#### `string`
|
|
37
|
-
The template string to be interpolated. Required.
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
The template string to be interpolated. Required.
|
|
36
|
+
|
|
37
|
+
Please see the [Interpolation syntax](./#interpolation-syntax) section below for more detail.
|
|
40
38
|
|
|
41
|
-
#### `mapping`
|
|
42
|
-
An object that defines the values to be injected for placeholder and tags defined in the template string. Optional.
|
|
39
|
+
#### `mapping`
|
|
43
40
|
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
An object that defines the values to be injected for placeholder and tags defined in the template string. Optional.
|
|
42
|
+
|
|
43
|
+
- For placeholder or self-closing tag, the mapping value could be any valid element value
|
|
44
|
+
- For open & close tag, the mapping value could be either renderer function or an element.
|
|
46
45
|
|
|
47
46
|
```jsx
|
|
48
47
|
<Interpolate
|
|
@@ -50,40 +49,33 @@ An object that defines the values to be injected for placeholder and tags define
|
|
|
50
49
|
Please contact <supportLink>support</supportLink> for help"
|
|
51
50
|
mapping={{
|
|
52
51
|
// you can map placholder and self-closing tag to any valid element value
|
|
53
|
-
name:
|
|
52
|
+
name: 'William',
|
|
54
53
|
hr: <hr className="break" />,
|
|
55
54
|
|
|
56
55
|
// you can map open & close tag to a rendering function
|
|
57
|
-
orderLink: text =>
|
|
58
|
-
<a href="https://orderinfo.com">{text}</a>
|
|
59
|
-
),
|
|
56
|
+
orderLink: (text) => <a href="https://orderinfo.com">{text}</a>,
|
|
60
57
|
|
|
61
58
|
// or you can map open & close tag to an element
|
|
62
|
-
supportLink: <a href="https://orderinfo.com"
|
|
59
|
+
supportLink: <a href="https://orderinfo.com" />,
|
|
63
60
|
}}
|
|
64
61
|
/>
|
|
65
62
|
```
|
|
66
63
|
|
|
64
|
+
#### `graceful`
|
|
67
65
|
|
|
68
|
-
|
|
69
|
-
A boolean flag indicates how string syntax error or mapping error should be handled. When true, the raw string value from the prop `string` would be rendered as a fallback in the error scenario. When false, error would be thrown instead.
|
|
66
|
+
A boolean flag indicates how string syntax error or mapping error should be handled. When true, the raw string value from the prop `string` would be rendered as a fallback in the error scenario. When false, error would be thrown instead.
|
|
70
67
|
|
|
71
68
|
Optional. `true` by default.
|
|
72
69
|
|
|
73
|
-
|
|
74
70
|
```jsx
|
|
75
71
|
// would render "an invalid string with unclose tag <h1>"
|
|
76
|
-
<Interpolate
|
|
77
|
-
graceful
|
|
78
|
-
string="an invalid string with unclose tag <h1>"
|
|
79
|
-
/>
|
|
72
|
+
<Interpolate graceful string="an invalid string with unclose tag <h1>" />
|
|
80
73
|
```
|
|
81
74
|
|
|
82
|
-
#### `syntax`
|
|
75
|
+
#### `syntax`
|
|
83
76
|
|
|
84
77
|
Optional. `syntax` props allow use of react-Interpolate with different string formatting syntax. Please see the ["Custom syntax support"](#custom-syntax-support) section for more detail.
|
|
85
78
|
|
|
86
|
-
|
|
87
79
|
## Interpolation syntax
|
|
88
80
|
|
|
89
81
|
Here is interpolation syntax you can use in your `string`.
|
|
@@ -91,7 +83,7 @@ Here is interpolation syntax you can use in your `string`.
|
|
|
91
83
|
#### Placeholder
|
|
92
84
|
|
|
93
85
|
```jsx
|
|
94
|
-
|
|
86
|
+
'hello {user_name}'
|
|
95
87
|
```
|
|
96
88
|
|
|
97
89
|
Placeholder name should be alphanumeric (`[A-Za-z0-9_]`). Placeholders could be mapped to any valid element value.
|
|
@@ -99,18 +91,18 @@ Placeholder name should be alphanumeric (`[A-Za-z0-9_]`). Placeholders could be
|
|
|
99
91
|
#### Open & close tags
|
|
100
92
|
|
|
101
93
|
```jsx
|
|
102
|
-
|
|
94
|
+
'Here is <a>your order info</a>'
|
|
103
95
|
|
|
104
96
|
// tag name could be any alphanumeric string
|
|
105
|
-
|
|
97
|
+
'Here is <link>your order info</link>'
|
|
106
98
|
|
|
107
99
|
// you can nest tag and placeholder
|
|
108
|
-
|
|
100
|
+
'Here is <a><b>you order info {name}</b></a>'
|
|
109
101
|
```
|
|
110
102
|
|
|
111
|
-
Tag name should be alphanumeric (`[A-Za-z0-9_]`).
|
|
103
|
+
Tag name should be alphanumeric (`[A-Za-z0-9_]`).
|
|
112
104
|
|
|
113
|
-
Open & close tag could be mapped to an element value.
|
|
105
|
+
Open & close tag could be mapped to an element value.
|
|
114
106
|
|
|
115
107
|
```jsx
|
|
116
108
|
<Interpolate
|
|
@@ -135,64 +127,60 @@ Open & close tag could be mapped to an element value.
|
|
|
135
127
|
/>
|
|
136
128
|
```
|
|
137
129
|
|
|
138
|
-
Open & close tag could be mapped to a rendering function, which would take a single argument that contains the enclosing text.
|
|
130
|
+
Open & close tag could be mapped to a rendering function, which would take a single argument that contains the enclosing text.
|
|
139
131
|
|
|
140
132
|
```jsx
|
|
141
133
|
<Interpolate
|
|
142
134
|
string="Here is <a>your order info</a>"
|
|
143
135
|
mapping={{
|
|
144
|
-
a: text => (
|
|
136
|
+
a: (text) => (
|
|
145
137
|
<a href="https://orderinfo.com">
|
|
146
138
|
<b>{text}</b>
|
|
147
139
|
<br />
|
|
148
140
|
</a>
|
|
149
|
-
)
|
|
141
|
+
),
|
|
150
142
|
}}
|
|
151
143
|
/>
|
|
152
144
|
```
|
|
153
145
|
|
|
154
|
-
|
|
155
|
-
|
|
156
146
|
Unclosed tag or incorrect nesting of tag would result in syntax error.
|
|
157
147
|
|
|
158
148
|
```js
|
|
159
149
|
// bad: no close tag
|
|
160
|
-
|
|
150
|
+
'Here is <a>your order info'
|
|
161
151
|
|
|
162
152
|
// bad: incorrect tag structure
|
|
163
|
-
|
|
153
|
+
'Here is <a><b>your order info</a></b>'
|
|
164
154
|
```
|
|
165
155
|
|
|
166
|
-
#### Self closing tag
|
|
156
|
+
#### Self closing tag
|
|
167
157
|
|
|
168
158
|
```js
|
|
169
|
-
|
|
159
|
+
'Hello.<br/>Here is your order'
|
|
170
160
|
```
|
|
171
161
|
|
|
172
162
|
Tag name should be alphanumeric (`[A-Za-z0-9_]`). Self closing tags could be mapped to any valid element value.
|
|
173
163
|
|
|
174
|
-
|
|
175
164
|
## Auto tag element creation
|
|
176
165
|
|
|
177
|
-
When tags are used the string but there are no correponding mapped value, it would by default create the corresponding HTML element by default.
|
|
166
|
+
When tags are used the string but there are no correponding mapped value, it would by default create the corresponding HTML element by default.
|
|
178
167
|
|
|
179
168
|
```jsx
|
|
180
169
|
// would render: <h1>Hellow</h1><br/>World
|
|
181
|
-
<Interpolate string="<h1>Hello</h1><br/>world"/>
|
|
170
|
+
<Interpolate string="<h1>Hello</h1><br/>world" />
|
|
182
171
|
```
|
|
183
172
|
|
|
184
|
-
|
|
185
|
-
|
|
186
173
|
## Custom syntax support
|
|
187
174
|
|
|
188
175
|
You may already be using a formatting syntax in your string that is different than the built-in syntax support from Interpolate. You can configure Interpolate so that it could recognize the formatting syntax that you use.
|
|
189
176
|
|
|
190
177
|
For instance, you may be using [i18next](https://www.i18next.com/) which has a slightly different placeholder syntax.
|
|
178
|
+
|
|
191
179
|
```
|
|
192
180
|
hello {{name}}
|
|
193
181
|
```
|
|
194
182
|
|
|
195
|
-
You can define the formatting syntax of your string via
|
|
183
|
+
You can define the formatting syntax of your string via `syntax` props.
|
|
196
184
|
|
|
197
185
|
```jsx
|
|
198
186
|
import Interpolate, { TOKEN_PLACEHOLDER } from "react-interpolate"
|
|
@@ -225,3 +213,16 @@ import { SYNTAX_I18NEXT } from "react-interpolate"
|
|
|
225
213
|
/>
|
|
226
214
|
```
|
|
227
215
|
|
|
216
|
+
# Releasing
|
|
217
|
+
|
|
218
|
+
A new version of @doist/react-interpolate is published both on npm and GitHub Package Registry whenever a new release on GitHub is created.
|
|
219
|
+
|
|
220
|
+
To update the version in both `package.json` and `package-lock.json` run:
|
|
221
|
+
|
|
222
|
+
```sh
|
|
223
|
+
npm --no-git-tag-version version <major|minor|patch>
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
Once these changes have been pushed and merged, create a release on GitHub.
|
|
227
|
+
|
|
228
|
+
A GitHub Action will automatically perform all the necessary steps and will release the version number that's specified inside the `package.json`'s `version` field so make sure that the release tag reflects the version you want to publish.
|
|
@@ -2,32 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
var
|
|
14
|
-
var
|
|
15
|
-
var
|
|
16
|
-
var
|
|
17
|
-
var
|
|
18
|
-
|
|
19
|
-
var
|
|
20
|
-
var
|
|
21
|
-
var
|
|
22
|
-
var
|
|
5
|
+
var React = require('react');
|
|
6
|
+
var PropTypes = require('prop-types');
|
|
7
|
+
var _classCallCheck = require('@babel/runtime/helpers/classCallCheck');
|
|
8
|
+
var _createClass = require('@babel/runtime/helpers/createClass');
|
|
9
|
+
var _objectWithoutProperties = require('@babel/runtime/helpers/objectWithoutProperties');
|
|
10
|
+
|
|
11
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
12
|
+
|
|
13
|
+
var React__default = /*#__PURE__*/_interopDefaultLegacy(React);
|
|
14
|
+
var PropTypes__default = /*#__PURE__*/_interopDefaultLegacy(PropTypes);
|
|
15
|
+
var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
|
|
16
|
+
var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
|
|
17
|
+
var _objectWithoutProperties__default = /*#__PURE__*/_interopDefaultLegacy(_objectWithoutProperties);
|
|
18
|
+
|
|
19
|
+
var TOKEN_PLACEHOLDER = 'TOKEN_PLACEHOLDER';
|
|
20
|
+
var TOKEN_OPEN_TAG = 'TOKEN_OPEN_TAG';
|
|
21
|
+
var TOKEN_CLOSE_TAG = 'TOKEN_CLOSE_TAG';
|
|
22
|
+
var TOKEN_SELF_TAG = 'TOKEN_SELF_TAG';
|
|
23
|
+
var TOKEN_TEXT = 'TOKEN_TEXT';
|
|
24
|
+
var NODE_FRAGMENT = 'NODE_FRAGMENT';
|
|
25
|
+
var NODE_TAG_ELEMENT = 'NODE_TAG_ELEMENT';
|
|
26
|
+
var NODE_VOID_ELEMENT = 'NODE_VOID_ELEMENT';
|
|
27
|
+
var NODE_PLACEHOLDER = 'NODE_PLACEHOLDER';
|
|
28
|
+
var NODE_TEXT = 'NODE_TEXT';
|
|
29
|
+
|
|
30
|
+
var _excluded = ["type", "children"];
|
|
23
31
|
|
|
24
32
|
var Node = /*#__PURE__*/function () {
|
|
25
33
|
function Node(_ref) {
|
|
26
34
|
var type = _ref.type,
|
|
27
35
|
children = _ref.children,
|
|
28
|
-
fields =
|
|
36
|
+
fields = _objectWithoutProperties__default["default"](_ref, _excluded);
|
|
29
37
|
|
|
30
|
-
|
|
38
|
+
_classCallCheck__default["default"](this, Node);
|
|
31
39
|
|
|
32
40
|
this.type = type;
|
|
33
41
|
this.children = children || [];
|
|
@@ -41,7 +49,7 @@ var Node = /*#__PURE__*/function () {
|
|
|
41
49
|
}
|
|
42
50
|
}
|
|
43
51
|
|
|
44
|
-
|
|
52
|
+
_createClass__default["default"](Node, [{
|
|
45
53
|
key: "appendChild",
|
|
46
54
|
value: function appendChild(child) {
|
|
47
55
|
this.children.push(child);
|
|
@@ -123,9 +131,9 @@ var SYNTAX_I18NEXT = [{
|
|
|
123
131
|
regex: /<(\w+)\s*\/>/g
|
|
124
132
|
}];
|
|
125
133
|
|
|
126
|
-
function _createForOfIteratorHelper(o) {
|
|
134
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
127
135
|
|
|
128
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(
|
|
136
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
129
137
|
|
|
130
138
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
|
131
139
|
/*
|
|
@@ -185,7 +193,7 @@ function lexer(string, syntax) {
|
|
|
185
193
|
});
|
|
186
194
|
var text = string.substring(start);
|
|
187
195
|
|
|
188
|
-
if (text !==
|
|
196
|
+
if (text !== '') {
|
|
189
197
|
textTokens.push({
|
|
190
198
|
type: TOKEN_TEXT,
|
|
191
199
|
string: text,
|
|
@@ -209,10 +217,10 @@ function parser(string, syntax) {
|
|
|
209
217
|
var p = new Parser(tokens);
|
|
210
218
|
return p.parse();
|
|
211
219
|
}
|
|
212
|
-
var SYNTAX_ERROR =
|
|
220
|
+
var SYNTAX_ERROR = 'Syntax error. Please check if each open tag is closed correctly'; // A special token representing end of the tream
|
|
213
221
|
|
|
214
222
|
var EPSILON = {
|
|
215
|
-
type:
|
|
223
|
+
type: 'EPSILON'
|
|
216
224
|
};
|
|
217
225
|
/*
|
|
218
226
|
* A recursive descent parser that construct a syntax tree represeting
|
|
@@ -243,13 +251,13 @@ var EPSILON = {
|
|
|
243
251
|
|
|
244
252
|
var Parser = /*#__PURE__*/function () {
|
|
245
253
|
function Parser(tokens) {
|
|
246
|
-
|
|
254
|
+
_classCallCheck__default["default"](this, Parser);
|
|
247
255
|
|
|
248
256
|
this.tokens = [].concat(tokens);
|
|
249
257
|
this.tags = [];
|
|
250
258
|
}
|
|
251
259
|
|
|
252
|
-
|
|
260
|
+
_createClass__default["default"](Parser, [{
|
|
253
261
|
key: "parse",
|
|
254
262
|
value: function parse() {
|
|
255
263
|
var tree = this.document();
|
|
@@ -349,6 +357,11 @@ var Parser = /*#__PURE__*/function () {
|
|
|
349
357
|
|
|
350
358
|
this.match(TOKEN_CLOSE_TAG);
|
|
351
359
|
}
|
|
360
|
+
}, {
|
|
361
|
+
key: "lookahead",
|
|
362
|
+
get: function get() {
|
|
363
|
+
return this.tokens.length === 0 ? EPSILON : this.tokens[0];
|
|
364
|
+
}
|
|
352
365
|
}, {
|
|
353
366
|
key: "predict",
|
|
354
367
|
value: function predict() {
|
|
@@ -373,11 +386,6 @@ var Parser = /*#__PURE__*/function () {
|
|
|
373
386
|
value: function pushTag(token) {
|
|
374
387
|
this.tags.push(token);
|
|
375
388
|
}
|
|
376
|
-
}, {
|
|
377
|
-
key: "lookahead",
|
|
378
|
-
get: function get() {
|
|
379
|
-
return this.tokens.length === 0 ? EPSILON : this.tokens[0];
|
|
380
|
-
}
|
|
381
389
|
}]);
|
|
382
390
|
|
|
383
391
|
return Parser;
|
|
@@ -385,7 +393,7 @@ var Parser = /*#__PURE__*/function () {
|
|
|
385
393
|
|
|
386
394
|
var createElement = function createElement(node, mapping, keyPrefix) {
|
|
387
395
|
var children = node.children.map(function (c, i) {
|
|
388
|
-
return /*#__PURE__*/
|
|
396
|
+
return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, {
|
|
389
397
|
key: keyPrefix + i
|
|
390
398
|
}, createElement(c, mapping, keyPrefix));
|
|
391
399
|
});
|
|
@@ -398,18 +406,18 @@ var createElement = function createElement(node, mapping, keyPrefix) {
|
|
|
398
406
|
|
|
399
407
|
case NODE_FRAGMENT:
|
|
400
408
|
{
|
|
401
|
-
return
|
|
409
|
+
return /*#__PURE__*/React__default["default"].createElement(React__default["default"].Fragment, null, children);
|
|
402
410
|
}
|
|
403
411
|
|
|
404
412
|
case NODE_VOID_ELEMENT:
|
|
405
413
|
{
|
|
406
414
|
var val = mapping[node.name];
|
|
407
415
|
|
|
408
|
-
if (typeof val ===
|
|
416
|
+
if (typeof val === 'function') {
|
|
409
417
|
return val();
|
|
410
418
|
}
|
|
411
419
|
|
|
412
|
-
return val ||
|
|
420
|
+
return val || /*#__PURE__*/React__default["default"].createElement(node.name, null);
|
|
413
421
|
}
|
|
414
422
|
|
|
415
423
|
case NODE_TAG_ELEMENT:
|
|
@@ -417,19 +425,19 @@ var createElement = function createElement(node, mapping, keyPrefix) {
|
|
|
417
425
|
var _val = mapping[node.name];
|
|
418
426
|
|
|
419
427
|
if (_val === undefined) {
|
|
420
|
-
return
|
|
428
|
+
return /*#__PURE__*/React__default["default"].createElement(node.name, null, children);
|
|
421
429
|
}
|
|
422
430
|
|
|
423
|
-
if (typeof _val ===
|
|
431
|
+
if (typeof _val === 'function') {
|
|
424
432
|
return _val(children);
|
|
425
433
|
}
|
|
426
434
|
|
|
427
|
-
if (
|
|
428
|
-
if (
|
|
429
|
-
throw new Error(
|
|
435
|
+
if ( /*#__PURE__*/React__default["default"].isValidElement(_val)) {
|
|
436
|
+
if (React__default["default"].Children.count(_val.props.children) !== 0) {
|
|
437
|
+
throw new Error('when passing an element as value, the element should not contains children');
|
|
430
438
|
}
|
|
431
439
|
|
|
432
|
-
return
|
|
440
|
+
return /*#__PURE__*/React__default["default"].cloneElement(_val, {
|
|
433
441
|
children: children
|
|
434
442
|
});
|
|
435
443
|
}
|
|
@@ -446,7 +454,7 @@ var createElement = function createElement(node, mapping, keyPrefix) {
|
|
|
446
454
|
return node.string;
|
|
447
455
|
}
|
|
448
456
|
|
|
449
|
-
if (typeof _val2 ===
|
|
457
|
+
if (typeof _val2 === 'function') {
|
|
450
458
|
return _val2();
|
|
451
459
|
}
|
|
452
460
|
|
|
@@ -479,9 +487,9 @@ function Interpolate(_ref) {
|
|
|
479
487
|
}
|
|
480
488
|
}
|
|
481
489
|
Interpolate.propTypes = {
|
|
482
|
-
string:
|
|
483
|
-
mapping:
|
|
484
|
-
graceful:
|
|
490
|
+
string: PropTypes__default["default"].string.isRequired,
|
|
491
|
+
mapping: PropTypes__default["default"].object,
|
|
492
|
+
graceful: PropTypes__default["default"].bool
|
|
485
493
|
};
|
|
486
494
|
|
|
487
495
|
exports.SYNTAX_BUILT_IN = SYNTAX_BUILT_IN;
|
|
@@ -490,4 +498,4 @@ exports.TOKEN_CLOSE_TAG = TOKEN_CLOSE_TAG;
|
|
|
490
498
|
exports.TOKEN_OPEN_TAG = TOKEN_OPEN_TAG;
|
|
491
499
|
exports.TOKEN_PLACEHOLDER = TOKEN_PLACEHOLDER;
|
|
492
500
|
exports.TOKEN_SELF_TAG = TOKEN_SELF_TAG;
|
|
493
|
-
exports
|
|
501
|
+
exports["default"] = Interpolate;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),PropTypes=require("prop-types"),_classCallCheck=require("@babel/runtime/helpers/classCallCheck"),_createClass=require("@babel/runtime/helpers/createClass"),_objectWithoutProperties=require("@babel/runtime/helpers/objectWithoutProperties");function _interopDefaultLegacy(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var React__default=_interopDefaultLegacy(React),PropTypes__default=_interopDefaultLegacy(PropTypes),_classCallCheck__default=_interopDefaultLegacy(_classCallCheck),_createClass__default=_interopDefaultLegacy(_createClass),_objectWithoutProperties__default=_interopDefaultLegacy(_objectWithoutProperties),TOKEN_PLACEHOLDER="TOKEN_PLACEHOLDER",TOKEN_OPEN_TAG="TOKEN_OPEN_TAG",TOKEN_CLOSE_TAG="TOKEN_CLOSE_TAG",TOKEN_SELF_TAG="TOKEN_SELF_TAG",TOKEN_TEXT="TOKEN_TEXT",NODE_FRAGMENT="NODE_FRAGMENT",NODE_TAG_ELEMENT="NODE_TAG_ELEMENT",NODE_VOID_ELEMENT="NODE_VOID_ELEMENT",NODE_PLACEHOLDER="NODE_PLACEHOLDER",NODE_TEXT="NODE_TEXT",_excluded=["type","children"],Node=function(){function e(t){var r=t.type,n=t.children,a=_objectWithoutProperties__default.default(t,_excluded);for(var o in _classCallCheck__default.default(this,e),this.type=r,this.children=n||[],a)this[o]=a[o];a.token&&(this.string=a.token.string)}return _createClass__default.default(e,[{key:"appendChild",value:function(e){this.children.push(e)}},{key:"isLeaf",get:function(){return 0===this.children.length}}]),e}();Node.createTagNode=function(e,t){return new Node({type:NODE_TAG_ELEMENT,children:t,name:e.name,token:e})},Node.createFragmentNode=function(e){return new Node({type:NODE_FRAGMENT,children:e})},Node.createVoidNode=function(e){return new Node({type:NODE_VOID_ELEMENT,name:e.name,token:e})},Node.createTextNode=function(e){return new Node({type:NODE_TEXT,text:e.string,token:e})},Node.createPlaceholderNode=function(e){return new Node({type:NODE_PLACEHOLDER,name:e.name,token:e})};var SYNTAX_BUILT_IN=[{type:TOKEN_PLACEHOLDER,regex:/{\s*(\w+)\s*}/g},{type:TOKEN_OPEN_TAG,regex:/<(\w+)>/g},{type:TOKEN_CLOSE_TAG,regex:/<\/(\w+)>/g},{type:TOKEN_SELF_TAG,regex:/<(\w+)\s*\/>/g}],SYNTAX_I18NEXT=[{type:TOKEN_PLACEHOLDER,regex:/{{\s*(\w+)\s*}}/g},{type:TOKEN_OPEN_TAG,regex:/<(\w+)>/g},{type:TOKEN_CLOSE_TAG,regex:/<\/(\w+)>/g},{type:TOKEN_SELF_TAG,regex:/<(\w+)\s*\/>/g}];function _createForOfIteratorHelper(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,a=function(){};return{s:a,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,i=!0,u=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return i=e.done,e},e:function(e){u=!0,o=e},f:function(){try{i||null==r.return||r.return()}finally{if(u)throw o}}}}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function lexer(e,t){var r,n=[],a=_createForOfIteratorHelper(t);try{for(a.s();!(r=a.n()).done;)for(var o,i=r.value,u=i.type,l=i.regex;null!==(o=l.exec(e));)n.push({type:u,string:o[0],name:o[1],start:o.index,end:o.index+o[0].length})}catch(e){a.e(e)}finally{a.f()}n.sort(function(e,t){return e.start-t.start});var c=[],s=0;n.forEach(function(t){if(t.start!==s){var r=t.start,n=e.substring(s,r);c.push({type:TOKEN_TEXT,string:n,text:n,start:s,end:r}),s=t.end}else s=t.end});var _=e.substring(s);return""!==_&&c.push({type:TOKEN_TEXT,string:_,start:s,end:e.length}),[].concat(c,n).sort(function(e,t){return e.start-t.start})}function parser(e,t){t||(t=SYNTAX_BUILT_IN);var r=lexer(e,t);return new Parser(r).parse()}var SYNTAX_ERROR="Syntax error. Please check if each open tag is closed correctly",EPSILON={type:"EPSILON"},Parser=function(){function e(t){_classCallCheck__default.default(this,e),this.tokens=[].concat(t),this.tags=[]}return _createClass__default.default(e,[{key:"parse",value:function(){var e=this.document();if(!this.predict(EPSILON.type))throw new Error(SYNTAX_ERROR);return e}},{key:"document",value:function(){var e=this.elementOrData();return Node.createFragmentNode(e)}},{key:"element",value:function(){var e=this.openTag(),t=this.elementOrData();return this.endTag(),Node.createTagNode(e,t)}},{key:"openTag",value:function(){if(!this.predict(TOKEN_OPEN_TAG))throw new Error(SYNTAX_ERROR);return this.pushTag(this.lookahead),this.match(TOKEN_OPEN_TAG)}},{key:"elementOrData",value:function(){var e=[];if(this.predict(TOKEN_OPEN_TAG)&&(e.push(this.element()),e=e.concat(this.elementOrData())),this.predict(TOKEN_SELF_TAG,TOKEN_TEXT,TOKEN_PLACEHOLDER)){var t,r=this.match(this.lookahead.type);switch(r.type){case TOKEN_SELF_TAG:t=Node.createVoidNode(r);break;case TOKEN_TEXT:t=Node.createTextNode(r);break;case TOKEN_PLACEHOLDER:t=Node.createPlaceholderNode(r)}e.push(t),e=e.concat(this.elementOrData())}return e}},{key:"endTag",value:function(){if(!this.predict(TOKEN_CLOSE_TAG))throw new Error(SYNTAX_ERROR);if(this.tags.pop().name!==this.lookahead.name)throw new Error(SYNTAX_ERROR);this.match(TOKEN_CLOSE_TAG)}},{key:"lookahead",get:function(){return 0===this.tokens.length?EPSILON:this.tokens[0]}},{key:"predict",value:function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t.includes(this.lookahead.type)}},{key:"match",value:function(e){if(!this.predict(e))throw new Error(SYNTAX_ERROR);return this.tokens.shift()}},{key:"pushTag",value:function(e){this.tags.push(e)}}]),e}(),createElement=function e(t,r,n){var a=t.children.map(function(t,a){return React__default.default.createElement(React__default.default.Fragment,{key:n+a},e(t,r,n))});switch(t.type){case NODE_TEXT:return t.text;case NODE_FRAGMENT:return React__default.default.createElement(React__default.default.Fragment,null,a);case NODE_VOID_ELEMENT:var o=r[t.name];return"function"==typeof o?o():o||React__default.default.createElement(t.name,null);case NODE_TAG_ELEMENT:var i=r[t.name];if(void 0===i)return React__default.default.createElement(t.name,null,a);if("function"==typeof i)return i(a);if(React__default.default.isValidElement(i)){if(0!==React__default.default.Children.count(i.props.children))throw new Error("when passing an element as value, the element should not contains children");return React__default.default.cloneElement(i,{children:a})}throw new Error('Invalid mapping value for "'.concat(t.name,'". Only element or render function are accepted'));case NODE_PLACEHOLDER:var u=r[t.name];return void 0===u?(console.warn('missing "'.concat(t.name,'" in mapping')),t.string):"function"==typeof u?u():u;default:return}};function Interpolate(e){var t=e.string,r=e.syntax,n=e.mapping,a=void 0===n?{}:n,o=e.graceful,i=void 0===o||o;try{var u=parser(t,r);return createElement(u,a,t)}catch(e){if(i)return console.error(e),t;throw e}}Interpolate.propTypes={string:PropTypes__default.default.string.isRequired,mapping:PropTypes__default.default.object,graceful:PropTypes__default.default.bool},exports.SYNTAX_BUILT_IN=SYNTAX_BUILT_IN,exports.SYNTAX_I18NEXT=SYNTAX_I18NEXT,exports.TOKEN_CLOSE_TAG=TOKEN_CLOSE_TAG,exports.TOKEN_OPEN_TAG=TOKEN_OPEN_TAG,exports.TOKEN_PLACEHOLDER=TOKEN_PLACEHOLDER,exports.TOKEN_SELF_TAG=TOKEN_SELF_TAG,exports.default=Interpolate;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import React from"react";import PropTypes from"prop-types";import _classCallCheck from"@babel/runtime/helpers/classCallCheck";import _createClass from"@babel/runtime/helpers/createClass";import _objectWithoutProperties from"@babel/runtime/helpers/objectWithoutProperties";var TOKEN_PLACEHOLDER="TOKEN_PLACEHOLDER",TOKEN_OPEN_TAG="TOKEN_OPEN_TAG",TOKEN_CLOSE_TAG="TOKEN_CLOSE_TAG",TOKEN_SELF_TAG="TOKEN_SELF_TAG",TOKEN_TEXT="TOKEN_TEXT",NODE_FRAGMENT="NODE_FRAGMENT",NODE_TAG_ELEMENT="NODE_TAG_ELEMENT",NODE_VOID_ELEMENT="NODE_VOID_ELEMENT",NODE_PLACEHOLDER="NODE_PLACEHOLDER",NODE_TEXT="NODE_TEXT",Node=function(){function e(t){var r=t.type,n=t.children,a=_objectWithoutProperties(t,
|
|
1
|
+
import React from"react";import PropTypes from"prop-types";import _classCallCheck from"@babel/runtime/helpers/classCallCheck";import _createClass from"@babel/runtime/helpers/createClass";import _objectWithoutProperties from"@babel/runtime/helpers/objectWithoutProperties";var TOKEN_PLACEHOLDER="TOKEN_PLACEHOLDER",TOKEN_OPEN_TAG="TOKEN_OPEN_TAG",TOKEN_CLOSE_TAG="TOKEN_CLOSE_TAG",TOKEN_SELF_TAG="TOKEN_SELF_TAG",TOKEN_TEXT="TOKEN_TEXT",NODE_FRAGMENT="NODE_FRAGMENT",NODE_TAG_ELEMENT="NODE_TAG_ELEMENT",NODE_VOID_ELEMENT="NODE_VOID_ELEMENT",NODE_PLACEHOLDER="NODE_PLACEHOLDER",NODE_TEXT="NODE_TEXT",_excluded=["type","children"],Node=function(){function e(t){var r=t.type,n=t.children,a=_objectWithoutProperties(t,_excluded);for(var o in _classCallCheck(this,e),this.type=r,this.children=n||[],a)this[o]=a[o];a.token&&(this.string=a.token.string)}return _createClass(e,[{key:"appendChild",value:function(e){this.children.push(e)}},{key:"isLeaf",get:function(){return 0===this.children.length}}]),e}();Node.createTagNode=function(e,t){return new Node({type:NODE_TAG_ELEMENT,children:t,name:e.name,token:e})},Node.createFragmentNode=function(e){return new Node({type:NODE_FRAGMENT,children:e})},Node.createVoidNode=function(e){return new Node({type:NODE_VOID_ELEMENT,name:e.name,token:e})},Node.createTextNode=function(e){return new Node({type:NODE_TEXT,text:e.string,token:e})},Node.createPlaceholderNode=function(e){return new Node({type:NODE_PLACEHOLDER,name:e.name,token:e})};var SYNTAX_BUILT_IN=[{type:TOKEN_PLACEHOLDER,regex:/{\s*(\w+)\s*}/g},{type:TOKEN_OPEN_TAG,regex:/<(\w+)>/g},{type:TOKEN_CLOSE_TAG,regex:/<\/(\w+)>/g},{type:TOKEN_SELF_TAG,regex:/<(\w+)\s*\/>/g}],SYNTAX_I18NEXT=[{type:TOKEN_PLACEHOLDER,regex:/{{\s*(\w+)\s*}}/g},{type:TOKEN_OPEN_TAG,regex:/<(\w+)>/g},{type:TOKEN_CLOSE_TAG,regex:/<\/(\w+)>/g},{type:TOKEN_SELF_TAG,regex:/<(\w+)\s*\/>/g}];function _createForOfIteratorHelper(e,t){var r="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(!r){if(Array.isArray(e)||(r=_unsupportedIterableToArray(e))||t&&e&&"number"==typeof e.length){r&&(e=r);var n=0,a=function(){};return{s:a,n:function(){return n>=e.length?{done:!0}:{done:!1,value:e[n++]}},e:function(e){throw e},f:a}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var o,i=!0,c=!1;return{s:function(){r=r.call(e)},n:function(){var e=r.next();return i=e.done,e},e:function(e){c=!0,o=e},f:function(){try{i||null==r.return||r.return()}finally{if(c)throw o}}}}function _unsupportedIterableToArray(e,t){if(e){if("string"==typeof e)return _arrayLikeToArray(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(e):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_arrayLikeToArray(e,t):void 0}}function _arrayLikeToArray(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function lexer(e,t){var r,n=[],a=_createForOfIteratorHelper(t);try{for(a.s();!(r=a.n()).done;)for(var o,i=r.value,c=i.type,E=i.regex;null!==(o=E.exec(e));)n.push({type:c,string:o[0],name:o[1],start:o.index,end:o.index+o[0].length})}catch(e){a.e(e)}finally{a.f()}n.sort(function(e,t){return e.start-t.start});var s=[],u=0;n.forEach(function(t){if(t.start!==u){var r=t.start,n=e.substring(u,r);s.push({type:TOKEN_TEXT,string:n,text:n,start:u,end:r}),u=t.end}else u=t.end});var l=e.substring(u);return""!==l&&s.push({type:TOKEN_TEXT,string:l,start:u,end:e.length}),[].concat(s,n).sort(function(e,t){return e.start-t.start})}function parser(e,t){t||(t=SYNTAX_BUILT_IN);var r=lexer(e,t);return new Parser(r).parse()}var SYNTAX_ERROR="Syntax error. Please check if each open tag is closed correctly",EPSILON={type:"EPSILON"},Parser=function(){function e(t){_classCallCheck(this,e),this.tokens=[].concat(t),this.tags=[]}return _createClass(e,[{key:"parse",value:function(){var e=this.document();if(!this.predict(EPSILON.type))throw new Error(SYNTAX_ERROR);return e}},{key:"document",value:function(){var e=this.elementOrData();return Node.createFragmentNode(e)}},{key:"element",value:function(){var e=this.openTag(),t=this.elementOrData();return this.endTag(),Node.createTagNode(e,t)}},{key:"openTag",value:function(){if(!this.predict(TOKEN_OPEN_TAG))throw new Error(SYNTAX_ERROR);return this.pushTag(this.lookahead),this.match(TOKEN_OPEN_TAG)}},{key:"elementOrData",value:function(){var e=[];if(this.predict(TOKEN_OPEN_TAG)&&(e.push(this.element()),e=e.concat(this.elementOrData())),this.predict(TOKEN_SELF_TAG,TOKEN_TEXT,TOKEN_PLACEHOLDER)){var t,r=this.match(this.lookahead.type);switch(r.type){case TOKEN_SELF_TAG:t=Node.createVoidNode(r);break;case TOKEN_TEXT:t=Node.createTextNode(r);break;case TOKEN_PLACEHOLDER:t=Node.createPlaceholderNode(r)}e.push(t),e=e.concat(this.elementOrData())}return e}},{key:"endTag",value:function(){if(!this.predict(TOKEN_CLOSE_TAG))throw new Error(SYNTAX_ERROR);if(this.tags.pop().name!==this.lookahead.name)throw new Error(SYNTAX_ERROR);this.match(TOKEN_CLOSE_TAG)}},{key:"lookahead",get:function(){return 0===this.tokens.length?EPSILON:this.tokens[0]}},{key:"predict",value:function(){for(var e=arguments.length,t=new Array(e),r=0;r<e;r++)t[r]=arguments[r];return t.includes(this.lookahead.type)}},{key:"match",value:function(e){if(!this.predict(e))throw new Error(SYNTAX_ERROR);return this.tokens.shift()}},{key:"pushTag",value:function(e){this.tags.push(e)}}]),e}(),createElement=function e(t,r,n){var a=t.children.map(function(t,a){return React.createElement(React.Fragment,{key:n+a},e(t,r,n))});switch(t.type){case NODE_TEXT:return t.text;case NODE_FRAGMENT:return React.createElement(React.Fragment,null,a);case NODE_VOID_ELEMENT:var o=r[t.name];return"function"==typeof o?o():o||React.createElement(t.name,null);case NODE_TAG_ELEMENT:var i=r[t.name];if(void 0===i)return React.createElement(t.name,null,a);if("function"==typeof i)return i(a);if(React.isValidElement(i)){if(0!==React.Children.count(i.props.children))throw new Error("when passing an element as value, the element should not contains children");return React.cloneElement(i,{children:a})}throw new Error('Invalid mapping value for "'.concat(t.name,'". Only element or render function are accepted'));case NODE_PLACEHOLDER:var c=r[t.name];return void 0===c?(console.warn('missing "'.concat(t.name,'" in mapping')),t.string):"function"==typeof c?c():c;default:return}};function Interpolate(e){var t=e.string,r=e.syntax,n=e.mapping,a=void 0===n?{}:n,o=e.graceful,i=void 0===o||o;try{var c=parser(t,r);return createElement(c,a,t)}catch(e){if(i)return console.error(e),t;throw e}}Interpolate.propTypes={string:PropTypes.string.isRequired,mapping:PropTypes.object,graceful:PropTypes.bool};export{SYNTAX_BUILT_IN,SYNTAX_I18NEXT,TOKEN_CLOSE_TAG,TOKEN_OPEN_TAG,TOKEN_PLACEHOLDER,TOKEN_SELF_TAG,Interpolate as default};
|