1 | import { hasOwn } from 'element-ui/src/utils/util';
|
2 |
|
3 | const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | export default function(Vue) {
|
10 |
|
11 | |
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function template(string, ...args) {
|
20 | if (args.length === 1 && typeof args[0] === 'object') {
|
21 | args = args[0];
|
22 | }
|
23 |
|
24 | if (!args || !args.hasOwnProperty) {
|
25 | args = {};
|
26 | }
|
27 |
|
28 | return string.replace(RE_NARGS, (match, prefix, i, index) => {
|
29 | let result;
|
30 |
|
31 | if (string[index - 1] === '{' &&
|
32 | string[index + match.length] === '}') {
|
33 | return i;
|
34 | } else {
|
35 | result = hasOwn(args, i) ? args[i] : null;
|
36 | if (result === null || result === undefined) {
|
37 | return '';
|
38 | }
|
39 |
|
40 | return result;
|
41 | }
|
42 | });
|
43 | }
|
44 |
|
45 | return template;
|
46 | }
|