@doist/react-interpolate 0.1.0 → 0.3.7
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.js +1 -2
- package/.github/workflows/nodejs.yml +18 -26
- package/.github/workflows/publish.yml +53 -0
- package/.nvmrc +1 -0
- package/README.md +38 -35
- package/dist/react-interpolate.cjs +47 -29
- package/dist/react-interpolate.min.cjs +1 -1
- package/dist/react-interpolate.min.mjs +1 -1
- package/dist/react-interpolate.mjs +27 -16
- package/jest.config.js +3 -0
- package/package.json +64 -61
- package/dist/react-interpolate.min.cjs.gz +0 -0
- package/dist/react-interpolate.min.mjs.gz +0 -0
package/.eslintrc.js
CHANGED
|
@@ -3,7 +3,6 @@ const config = {
|
|
|
3
3
|
"eslint:recommended",
|
|
4
4
|
"plugin:react/recommended",
|
|
5
5
|
"prettier",
|
|
6
|
-
"prettier/react",
|
|
7
6
|
"plugin:compat/recommended"
|
|
8
7
|
],
|
|
9
8
|
env: {
|
|
@@ -18,7 +17,7 @@ const config = {
|
|
|
18
17
|
sourceType: "module"
|
|
19
18
|
},
|
|
20
19
|
plugins: ["react", "prettier", "react-hooks", "jest"],
|
|
21
|
-
parser: "babel-
|
|
20
|
+
parser: "@babel/eslint-parser",
|
|
22
21
|
rules: {
|
|
23
22
|
semi: ["error", "never"],
|
|
24
23
|
"arrow-parens": ["error", "as-needed"],
|
|
@@ -1,32 +1,24 @@
|
|
|
1
|
-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
|
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
|
3
|
-
|
|
4
1
|
name: CI
|
|
5
2
|
|
|
6
|
-
on:
|
|
7
|
-
push:
|
|
8
|
-
branches: [ master ]
|
|
9
|
-
pull_request:
|
|
10
|
-
branches: [ master ]
|
|
3
|
+
on: [push, pull_request]
|
|
11
4
|
|
|
12
5
|
jobs:
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
runs-on: ubuntu-latest
|
|
6
|
+
build:
|
|
7
|
+
runs-on: ubuntu-latest
|
|
16
8
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
strategy:
|
|
10
|
+
matrix:
|
|
11
|
+
node-version: [16.x]
|
|
20
12
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
13
|
+
steps:
|
|
14
|
+
- uses: actions/checkout@v2
|
|
15
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
16
|
+
uses: actions/setup-node@v1
|
|
17
|
+
with:
|
|
18
|
+
node-version: ${{ matrix.node-version }}
|
|
19
|
+
- run: npm ci
|
|
20
|
+
- run: npm run build
|
|
21
|
+
- run: npm test
|
|
22
|
+
env:
|
|
23
|
+
CI: true
|
|
24
|
+
- run: npm run lint
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
name: Release @doist/react-interpolate package
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [created]
|
|
6
|
+
workflow_dispatch:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v2
|
|
13
|
+
|
|
14
|
+
- name: Read Node.js version from '.nvmrc'
|
|
15
|
+
id: nvmrc
|
|
16
|
+
run: |
|
|
17
|
+
echo "::set-output name=NODE_VERSION::$(cat .nvmrc)"
|
|
18
|
+
|
|
19
|
+
- name: Setup Node
|
|
20
|
+
uses: actions/setup-node@v1
|
|
21
|
+
with:
|
|
22
|
+
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}
|
|
23
|
+
|
|
24
|
+
- name: Install dependencies
|
|
25
|
+
run: npm ci
|
|
26
|
+
|
|
27
|
+
- name: Lint
|
|
28
|
+
run: npm run lint
|
|
29
|
+
env:
|
|
30
|
+
CI: true
|
|
31
|
+
|
|
32
|
+
- name: Build
|
|
33
|
+
run: npm run build
|
|
34
|
+
|
|
35
|
+
- name: Publish to GitHub Package Registry
|
|
36
|
+
uses: actions/setup-node@v1
|
|
37
|
+
with:
|
|
38
|
+
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}
|
|
39
|
+
registry-url: https://npm.pkg.github.com/
|
|
40
|
+
scope: "@doist"
|
|
41
|
+
- run: npm publish
|
|
42
|
+
env:
|
|
43
|
+
NODE_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
|
|
44
|
+
|
|
45
|
+
- name: Publish to npm registry
|
|
46
|
+
uses: actions/setup-node@v1
|
|
47
|
+
with:
|
|
48
|
+
node-version: ${{ steps.nvmrc.outputs.NODE_VERSION }}
|
|
49
|
+
registry-url: https://registry.npmjs.org/
|
|
50
|
+
scope: "@doist"
|
|
51
|
+
- run: npm publish
|
|
52
|
+
env:
|
|
53
|
+
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
16.13.0
|
package/README.md
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
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
|
|
|
@@ -25,24 +23,27 @@ function Greeting() {
|
|
|
25
23
|
Would render the following HTML
|
|
26
24
|
|
|
27
25
|
```html
|
|
28
|
-
<h1>
|
|
26
|
+
<h1>
|
|
27
|
+
Hello William. Here is <a href="https://orderinfo.com">your order info</a>
|
|
28
|
+
</h1>
|
|
29
29
|
```
|
|
30
30
|
|
|
31
|
-
|
|
32
31
|
## Component API
|
|
33
32
|
|
|
34
33
|
`<Interpolate>` component accepts the following props
|
|
35
34
|
|
|
36
35
|
#### `string`
|
|
37
|
-
The template string to be interpolated. Required.
|
|
38
36
|
|
|
39
|
-
|
|
37
|
+
The template string to be interpolated. Required.
|
|
38
|
+
|
|
39
|
+
Please see the [Interpolation syntax](./#interpolation-syntax) section below for more detail.
|
|
40
40
|
|
|
41
|
-
#### `mapping`
|
|
42
|
-
An object that defines the values to be injected for placeholder and tags defined in the template string. Optional.
|
|
41
|
+
#### `mapping`
|
|
43
42
|
|
|
44
|
-
|
|
45
|
-
|
|
43
|
+
An object that defines the values to be injected for placeholder and tags defined in the template string. Optional.
|
|
44
|
+
|
|
45
|
+
- For placeholder or self-closing tag, the mapping value could be any valid element value
|
|
46
|
+
- For open & close tag, the mapping value could be either renderer function or an element.
|
|
46
47
|
|
|
47
48
|
```jsx
|
|
48
49
|
<Interpolate
|
|
@@ -54,9 +55,7 @@ An object that defines the values to be injected for placeholder and tags define
|
|
|
54
55
|
hr: <hr className="break" />,
|
|
55
56
|
|
|
56
57
|
// you can map open & close tag to a rendering function
|
|
57
|
-
orderLink: text =>
|
|
58
|
-
<a href="https://orderinfo.com">{text}</a>
|
|
59
|
-
),
|
|
58
|
+
orderLink: text => <a href="https://orderinfo.com">{text}</a>,
|
|
60
59
|
|
|
61
60
|
// or you can map open & close tag to an element
|
|
62
61
|
supportLink: <a href="https://orderinfo.com" />
|
|
@@ -64,26 +63,21 @@ An object that defines the values to be injected for placeholder and tags define
|
|
|
64
63
|
/>
|
|
65
64
|
```
|
|
66
65
|
|
|
66
|
+
#### `graceful`
|
|
67
67
|
|
|
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.
|
|
68
|
+
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
69
|
|
|
71
70
|
Optional. `true` by default.
|
|
72
71
|
|
|
73
|
-
|
|
74
72
|
```jsx
|
|
75
73
|
// would render "an invalid string with unclose tag <h1>"
|
|
76
|
-
<Interpolate
|
|
77
|
-
graceful
|
|
78
|
-
string="an invalid string with unclose tag <h1>"
|
|
79
|
-
/>
|
|
74
|
+
<Interpolate graceful string="an invalid string with unclose tag <h1>" />
|
|
80
75
|
```
|
|
81
76
|
|
|
82
|
-
#### `syntax`
|
|
77
|
+
#### `syntax`
|
|
83
78
|
|
|
84
79
|
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
80
|
|
|
86
|
-
|
|
87
81
|
## Interpolation syntax
|
|
88
82
|
|
|
89
83
|
Here is interpolation syntax you can use in your `string`.
|
|
@@ -108,9 +102,9 @@ Placeholder name should be alphanumeric (`[A-Za-z0-9_]`). Placeholders could be
|
|
|
108
102
|
"Here is <a><b>you order info {name}</b></a>"
|
|
109
103
|
```
|
|
110
104
|
|
|
111
|
-
Tag name should be alphanumeric (`[A-Za-z0-9_]`).
|
|
105
|
+
Tag name should be alphanumeric (`[A-Za-z0-9_]`).
|
|
112
106
|
|
|
113
|
-
Open & close tag could be mapped to an element value.
|
|
107
|
+
Open & close tag could be mapped to an element value.
|
|
114
108
|
|
|
115
109
|
```jsx
|
|
116
110
|
<Interpolate
|
|
@@ -135,7 +129,7 @@ Open & close tag could be mapped to an element value.
|
|
|
135
129
|
/>
|
|
136
130
|
```
|
|
137
131
|
|
|
138
|
-
Open & close tag could be mapped to a rendering function, which would take a single argument that contains the enclosing text.
|
|
132
|
+
Open & close tag could be mapped to a rendering function, which would take a single argument that contains the enclosing text.
|
|
139
133
|
|
|
140
134
|
```jsx
|
|
141
135
|
<Interpolate
|
|
@@ -151,8 +145,6 @@ Open & close tag could be mapped to a rendering function, which would take a sin
|
|
|
151
145
|
/>
|
|
152
146
|
```
|
|
153
147
|
|
|
154
|
-
|
|
155
|
-
|
|
156
148
|
Unclosed tag or incorrect nesting of tag would result in syntax error.
|
|
157
149
|
|
|
158
150
|
```js
|
|
@@ -163,7 +155,7 @@ Unclosed tag or incorrect nesting of tag would result in syntax error.
|
|
|
163
155
|
"Here is <a><b>your order info</a></b>"
|
|
164
156
|
```
|
|
165
157
|
|
|
166
|
-
#### Self closing tag
|
|
158
|
+
#### Self closing tag
|
|
167
159
|
|
|
168
160
|
```js
|
|
169
161
|
"Hello.<br/>Here is your order"
|
|
@@ -171,28 +163,26 @@ Unclosed tag or incorrect nesting of tag would result in syntax error.
|
|
|
171
163
|
|
|
172
164
|
Tag name should be alphanumeric (`[A-Za-z0-9_]`). Self closing tags could be mapped to any valid element value.
|
|
173
165
|
|
|
174
|
-
|
|
175
166
|
## Auto tag element creation
|
|
176
167
|
|
|
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.
|
|
168
|
+
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
169
|
|
|
179
170
|
```jsx
|
|
180
171
|
// would render: <h1>Hellow</h1><br/>World
|
|
181
|
-
<Interpolate string="<h1>Hello</h1><br/>world"/>
|
|
172
|
+
<Interpolate string="<h1>Hello</h1><br/>world" />
|
|
182
173
|
```
|
|
183
174
|
|
|
184
|
-
|
|
185
|
-
|
|
186
175
|
## Custom syntax support
|
|
187
176
|
|
|
188
177
|
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
178
|
|
|
190
179
|
For instance, you may be using [i18next](https://www.i18next.com/) which has a slightly different placeholder syntax.
|
|
180
|
+
|
|
191
181
|
```
|
|
192
182
|
hello {{name}}
|
|
193
183
|
```
|
|
194
184
|
|
|
195
|
-
You can define the formatting syntax of your string via
|
|
185
|
+
You can define the formatting syntax of your string via `syntax` props.
|
|
196
186
|
|
|
197
187
|
```jsx
|
|
198
188
|
import Interpolate, { TOKEN_PLACEHOLDER } from "react-interpolate"
|
|
@@ -225,3 +215,16 @@ import { SYNTAX_I18NEXT } from "react-interpolate"
|
|
|
225
215
|
/>
|
|
226
216
|
```
|
|
227
217
|
|
|
218
|
+
# Releasing
|
|
219
|
+
|
|
220
|
+
A new version of @doist/react-interpolate is published both on npm and GitHub Package Registry whenever a new release on GitHub is created.
|
|
221
|
+
|
|
222
|
+
To update the version in both `package.json` and `package-lock.json` run:
|
|
223
|
+
|
|
224
|
+
```sh
|
|
225
|
+
npm --no-git-tag-version version <major|minor|patch>
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
Once these changes have been pushed and merged, create a release on GitHub.
|
|
229
|
+
|
|
230
|
+
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,13 +2,19 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
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');
|
|
6
10
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var
|
|
10
|
-
var
|
|
11
|
-
var
|
|
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);
|
|
12
18
|
|
|
13
19
|
var TOKEN_PLACEHOLDER = "TOKEN_PLACEHOLDER";
|
|
14
20
|
var TOKEN_OPEN_TAG = "TOKEN_OPEN_TAG";
|
|
@@ -21,13 +27,15 @@ var NODE_VOID_ELEMENT = "NODE_VOID_ELEMENT";
|
|
|
21
27
|
var NODE_PLACEHOLDER = "NODE_PLACEHOLDER";
|
|
22
28
|
var NODE_TEXT = "NODE_TEXT";
|
|
23
29
|
|
|
30
|
+
var _excluded = ["type", "children"];
|
|
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
|
/*
|
|
@@ -209,7 +217,7 @@ 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
223
|
type: "EPSILON"
|
|
@@ -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,7 +406,7 @@ 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:
|
|
@@ -409,22 +417,32 @@ var createElement = function createElement(node, mapping, keyPrefix) {
|
|
|
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:
|
|
416
424
|
{
|
|
417
425
|
var _val = mapping[node.name];
|
|
418
426
|
|
|
427
|
+
if (_val === undefined) {
|
|
428
|
+
return /*#__PURE__*/React__default["default"].createElement(node.name, null, children);
|
|
429
|
+
}
|
|
430
|
+
|
|
419
431
|
if (typeof _val === "function") {
|
|
420
432
|
return _val(children);
|
|
421
433
|
}
|
|
422
434
|
|
|
423
|
-
if (_val
|
|
424
|
-
|
|
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");
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
return /*#__PURE__*/React__default["default"].cloneElement(_val, {
|
|
441
|
+
children: children
|
|
442
|
+
});
|
|
425
443
|
}
|
|
426
444
|
|
|
427
|
-
|
|
445
|
+
throw new Error("Invalid mapping value for \"".concat(node.name, "\". Only element or render function are accepted"));
|
|
428
446
|
}
|
|
429
447
|
|
|
430
448
|
case NODE_PLACEHOLDER:
|
|
@@ -469,9 +487,9 @@ function Interpolate(_ref) {
|
|
|
469
487
|
}
|
|
470
488
|
}
|
|
471
489
|
Interpolate.propTypes = {
|
|
472
|
-
string:
|
|
473
|
-
mapping:
|
|
474
|
-
graceful:
|
|
490
|
+
string: PropTypes__default["default"].string.isRequired,
|
|
491
|
+
mapping: PropTypes__default["default"].object,
|
|
492
|
+
graceful: PropTypes__default["default"].bool
|
|
475
493
|
};
|
|
476
494
|
|
|
477
495
|
exports.SYNTAX_BUILT_IN = SYNTAX_BUILT_IN;
|
|
@@ -480,4 +498,4 @@ exports.TOKEN_CLOSE_TAG = TOKEN_CLOSE_TAG;
|
|
|
480
498
|
exports.TOKEN_OPEN_TAG = TOKEN_OPEN_TAG;
|
|
481
499
|
exports.TOKEN_PLACEHOLDER = TOKEN_PLACEHOLDER;
|
|
482
500
|
exports.TOKEN_SELF_TAG = TOKEN_SELF_TAG;
|
|
483
|
-
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};
|
|
@@ -15,11 +15,13 @@ var NODE_VOID_ELEMENT = "NODE_VOID_ELEMENT";
|
|
|
15
15
|
var NODE_PLACEHOLDER = "NODE_PLACEHOLDER";
|
|
16
16
|
var NODE_TEXT = "NODE_TEXT";
|
|
17
17
|
|
|
18
|
+
var _excluded = ["type", "children"];
|
|
19
|
+
|
|
18
20
|
var Node = /*#__PURE__*/function () {
|
|
19
21
|
function Node(_ref) {
|
|
20
22
|
var type = _ref.type,
|
|
21
23
|
children = _ref.children,
|
|
22
|
-
fields = _objectWithoutProperties(_ref,
|
|
24
|
+
fields = _objectWithoutProperties(_ref, _excluded);
|
|
23
25
|
|
|
24
26
|
_classCallCheck(this, Node);
|
|
25
27
|
|
|
@@ -117,9 +119,9 @@ var SYNTAX_I18NEXT = [{
|
|
|
117
119
|
regex: /<(\w+)\s*\/>/g
|
|
118
120
|
}];
|
|
119
121
|
|
|
120
|
-
function _createForOfIteratorHelper(o) {
|
|
122
|
+
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; } } }; }
|
|
121
123
|
|
|
122
|
-
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(
|
|
124
|
+
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); }
|
|
123
125
|
|
|
124
126
|
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; }
|
|
125
127
|
/*
|
|
@@ -203,7 +205,7 @@ function parser(string, syntax) {
|
|
|
203
205
|
var p = new Parser(tokens);
|
|
204
206
|
return p.parse();
|
|
205
207
|
}
|
|
206
|
-
var SYNTAX_ERROR = "
|
|
208
|
+
var SYNTAX_ERROR = "Syntax error. Please check if each open tag is closed correctly"; // A special token representing end of the tream
|
|
207
209
|
|
|
208
210
|
var EPSILON = {
|
|
209
211
|
type: "EPSILON"
|
|
@@ -343,6 +345,11 @@ var Parser = /*#__PURE__*/function () {
|
|
|
343
345
|
|
|
344
346
|
this.match(TOKEN_CLOSE_TAG);
|
|
345
347
|
}
|
|
348
|
+
}, {
|
|
349
|
+
key: "lookahead",
|
|
350
|
+
get: function get() {
|
|
351
|
+
return this.tokens.length === 0 ? EPSILON : this.tokens[0];
|
|
352
|
+
}
|
|
346
353
|
}, {
|
|
347
354
|
key: "predict",
|
|
348
355
|
value: function predict() {
|
|
@@ -367,11 +374,6 @@ var Parser = /*#__PURE__*/function () {
|
|
|
367
374
|
value: function pushTag(token) {
|
|
368
375
|
this.tags.push(token);
|
|
369
376
|
}
|
|
370
|
-
}, {
|
|
371
|
-
key: "lookahead",
|
|
372
|
-
get: function get() {
|
|
373
|
-
return this.tokens.length === 0 ? EPSILON : this.tokens[0];
|
|
374
|
-
}
|
|
375
377
|
}]);
|
|
376
378
|
|
|
377
379
|
return Parser;
|
|
@@ -392,7 +394,7 @@ var createElement = function createElement(node, mapping, keyPrefix) {
|
|
|
392
394
|
|
|
393
395
|
case NODE_FRAGMENT:
|
|
394
396
|
{
|
|
395
|
-
return React.createElement(React.Fragment, null, children);
|
|
397
|
+
return /*#__PURE__*/React.createElement(React.Fragment, null, children);
|
|
396
398
|
}
|
|
397
399
|
|
|
398
400
|
case NODE_VOID_ELEMENT:
|
|
@@ -403,22 +405,32 @@ var createElement = function createElement(node, mapping, keyPrefix) {
|
|
|
403
405
|
return val();
|
|
404
406
|
}
|
|
405
407
|
|
|
406
|
-
return val || React.createElement(node.name, null);
|
|
408
|
+
return val || /*#__PURE__*/React.createElement(node.name, null);
|
|
407
409
|
}
|
|
408
410
|
|
|
409
411
|
case NODE_TAG_ELEMENT:
|
|
410
412
|
{
|
|
411
413
|
var _val = mapping[node.name];
|
|
412
414
|
|
|
415
|
+
if (_val === undefined) {
|
|
416
|
+
return /*#__PURE__*/React.createElement(node.name, null, children);
|
|
417
|
+
}
|
|
418
|
+
|
|
413
419
|
if (typeof _val === "function") {
|
|
414
420
|
return _val(children);
|
|
415
421
|
}
|
|
416
422
|
|
|
417
|
-
if (_val
|
|
418
|
-
|
|
423
|
+
if ( /*#__PURE__*/React.isValidElement(_val)) {
|
|
424
|
+
if (React.Children.count(_val.props.children) !== 0) {
|
|
425
|
+
throw new Error("when passing an element as value, the element should not contains children");
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
return /*#__PURE__*/React.cloneElement(_val, {
|
|
429
|
+
children: children
|
|
430
|
+
});
|
|
419
431
|
}
|
|
420
432
|
|
|
421
|
-
|
|
433
|
+
throw new Error("Invalid mapping value for \"".concat(node.name, "\". Only element or render function are accepted"));
|
|
422
434
|
}
|
|
423
435
|
|
|
424
436
|
case NODE_PLACEHOLDER:
|
|
@@ -468,5 +480,4 @@ Interpolate.propTypes = {
|
|
|
468
480
|
graceful: PropTypes.bool
|
|
469
481
|
};
|
|
470
482
|
|
|
471
|
-
export default
|
|
472
|
-
export { SYNTAX_BUILT_IN, SYNTAX_I18NEXT, TOKEN_CLOSE_TAG, TOKEN_OPEN_TAG, TOKEN_PLACEHOLDER, TOKEN_SELF_TAG };
|
|
483
|
+
export { SYNTAX_BUILT_IN, SYNTAX_I18NEXT, TOKEN_CLOSE_TAG, TOKEN_OPEN_TAG, TOKEN_PLACEHOLDER, TOKEN_SELF_TAG, Interpolate as default };
|
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,63 +1,66 @@
|
|
|
1
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
|
-
|
|
2
|
+
"name": "@doist/react-interpolate",
|
|
3
|
+
"version": "0.3.7",
|
|
4
|
+
"description": "A string interpolation component that formats and interpolates a template string in a safe way",
|
|
5
|
+
"main": "dist/react-interpolate.cjs",
|
|
6
|
+
"module": "dist/react-interpolate.mjs",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": "16.13",
|
|
9
|
+
"npm": "8.1.0"
|
|
10
|
+
},
|
|
11
|
+
"scripts": {
|
|
12
|
+
"test": "jest",
|
|
13
|
+
"lint": "eslint ./src ./__test__",
|
|
14
|
+
"build": "del dist && rollup -c && npm run mini-cjs && npm run mini-mjs",
|
|
15
|
+
"mini-cjs": "uglifyjs dist/react-interpolate.cjs --compress --mangle --enclose --output dist/react-interpolate.min.cjs",
|
|
16
|
+
"mini-mjs": "uglifyjs dist/react-interpolate.mjs --compress --mangle --enclose --output dist/react-interpolate.min.mjs"
|
|
17
|
+
},
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "https://github.com/Doist/react-interpolate.git"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"react",
|
|
24
|
+
"interpolate",
|
|
25
|
+
"template",
|
|
26
|
+
"format",
|
|
27
|
+
"text",
|
|
28
|
+
"string"
|
|
29
|
+
],
|
|
30
|
+
"author": "Steven Kao",
|
|
31
|
+
"license": "ISC",
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": "^17.0.2",
|
|
34
|
+
"react-dom": "^17.0.2"
|
|
35
|
+
},
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"@babel/cli": "^7.16.0",
|
|
38
|
+
"@babel/core": "^7.16.0",
|
|
39
|
+
"@babel/eslint-parser": "^7.16.3",
|
|
40
|
+
"@babel/plugin-transform-runtime": "^7.16.4",
|
|
41
|
+
"@babel/preset-env": "^7.16.4",
|
|
42
|
+
"@babel/preset-react": "^7.16.0",
|
|
43
|
+
"@testing-library/react": "^12.1.2",
|
|
44
|
+
"babel-jest": "^27.3.1",
|
|
45
|
+
"core-js": "^3.19.1",
|
|
46
|
+
"del-cli": "^4.0.1",
|
|
47
|
+
"eslint": "^8.2.0",
|
|
48
|
+
"eslint-config-prettier": "^8.3.0",
|
|
49
|
+
"eslint-plugin-compat": "^4.0.0",
|
|
50
|
+
"eslint-plugin-jest": "^25.2.4",
|
|
51
|
+
"eslint-plugin-prettier": "^4.0.0",
|
|
52
|
+
"eslint-plugin-react": "^7.27.0",
|
|
53
|
+
"eslint-plugin-react-hooks": "^4.3.0",
|
|
54
|
+
"gzip-cli": "^1.2.0",
|
|
55
|
+
"jest": "^27.3.1",
|
|
56
|
+
"prettier": "^2.4.1",
|
|
57
|
+
"react": "^17.0.2",
|
|
58
|
+
"react-dom": "^17.0.2",
|
|
59
|
+
"rollup": "^2.60.0",
|
|
60
|
+
"rollup-plugin-babel": "^4.4.0",
|
|
61
|
+
"uglify-es": "^3.3.9"
|
|
62
|
+
},
|
|
63
|
+
"dependencies": {
|
|
64
|
+
"@babel/runtime": "^7.16.3"
|
|
65
|
+
}
|
|
63
66
|
}
|
|
Binary file
|
|
Binary file
|