UNPKG

953 BJavaScriptView Raw
1import { hasOwn } from 'element-ui/src/utils/util';
2
3const RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;
4/**
5 * String format template
6 * - Inspired:
7 * https://github.com/Matt-Esch/string-template/index.js
8 */
9export default function(Vue) {
10
11 /**
12 * template
13 *
14 * @param {String} string
15 * @param {Array} ...args
16 * @return {String}
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}