format.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. exports.__esModule = true;
  3. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
  4. exports.default = function (Vue) {
  5. /**
  6. * template
  7. *
  8. * @param {String} string
  9. * @param {Array} ...args
  10. * @return {String}
  11. */
  12. function template(string) {
  13. for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  14. args[_key - 1] = arguments[_key];
  15. }
  16. if (args.length === 1 && _typeof(args[0]) === 'object') {
  17. args = args[0];
  18. }
  19. if (!args || !args.hasOwnProperty) {
  20. args = {};
  21. }
  22. return string.replace(RE_NARGS, function (match, prefix, i, index) {
  23. var result = void 0;
  24. if (string[index - 1] === '{' && string[index + match.length] === '}') {
  25. return i;
  26. } else {
  27. result = (0, _util.hasOwn)(args, i) ? args[i] : null;
  28. if (result === null || result === undefined) {
  29. return '';
  30. }
  31. return result;
  32. }
  33. });
  34. }
  35. return template;
  36. };
  37. var _util = require('element-ui/lib/utils/util');
  38. var RE_NARGS = /(%|)\{([0-9a-zA-Z_]+)\}/g;
  39. /**
  40. * String format template
  41. * - Inspired:
  42. * https://github.com/Matt-Esch/string-template/index.js
  43. */