print.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createPrint = void 0;
  6. var _string = require("../../utils/string.js");
  7. var _is = require("../../utils/is.js");
  8. var _factory = require("../../utils/factory.js");
  9. var name = 'print';
  10. var dependencies = ['typed'];
  11. var createPrint = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  12. var typed = _ref.typed;
  13. /**
  14. * Interpolate values into a string template.
  15. *
  16. * Syntax:
  17. *
  18. * math.print(template, values)
  19. * math.print(template, values, precision)
  20. * math.print(template, values, options)
  21. *
  22. * Example usage:
  23. *
  24. * // the following outputs: 'Lucy is 5 years old'
  25. * math.print('Lucy is $age years old', {age: 5})
  26. *
  27. * // the following outputs: 'The value of pi is 3.141592654'
  28. * math.print('The value of pi is $pi', {pi: math.pi}, 10)
  29. *
  30. * // the following outputs: 'hello Mary! The date is 2013-03-23'
  31. * math.print('Hello $user.name! The date is $date', {
  32. * user: {
  33. * name: 'Mary',
  34. * },
  35. * date: new Date(2013, 2, 23).toISOString().substring(0, 10)
  36. * })
  37. *
  38. * // the following outputs: 'My favorite fruits are apples and bananas !'
  39. * math.print('My favorite fruits are $0 and $1 !', [
  40. * 'apples',
  41. * 'bananas'
  42. * ])
  43. *
  44. * See also:
  45. *
  46. * format
  47. *
  48. * @param {string} template A string containing variable placeholders.
  49. * @param {Object | Array | Matrix} values An object or array containing variables
  50. * which will be filled in in the template.
  51. * @param {number | Object} [options] Formatting options,
  52. * or the number of digits to format numbers.
  53. * See function math.format for a description
  54. * of all options.
  55. * @return {string} Interpolated string
  56. */
  57. return typed(name, {
  58. // note: Matrix will be converted automatically to an Array
  59. 'string, Object | Array': _print,
  60. 'string, Object | Array, number | Object': _print
  61. });
  62. });
  63. /**
  64. * Interpolate values into a string template.
  65. * @param {string} template
  66. * @param {Object} values
  67. * @param {number | Object} [options]
  68. * @returns {string} Interpolated string
  69. * @private
  70. */
  71. exports.createPrint = createPrint;
  72. function _print(template, values, options) {
  73. return template.replace(/\$([\w.]+)/g, function (original, key) {
  74. var keys = key.split('.');
  75. var value = values[keys.shift()];
  76. while (keys.length && value !== undefined) {
  77. var k = keys.shift();
  78. value = k ? value[k] : value + '.';
  79. }
  80. if (value !== undefined) {
  81. if (!(0, _is.isString)(value)) {
  82. return (0, _string.format)(value, options);
  83. } else {
  84. return value;
  85. }
  86. }
  87. return original;
  88. });
  89. }