Help.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.createHelpClass = void 0;
  6. var _is = require("../utils/is.js");
  7. var _object = require("../utils/object.js");
  8. var _string = require("../utils/string.js");
  9. var _factory = require("../utils/factory.js");
  10. var name = 'Help';
  11. var dependencies = ['parse'];
  12. var createHelpClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
  13. var parse = _ref.parse;
  14. /**
  15. * Documentation object
  16. * @param {Object} doc Object containing properties:
  17. * {string} name
  18. * {string} category
  19. * {string} description
  20. * {string[]} syntax
  21. * {string[]} examples
  22. * {string[]} seealso
  23. * @constructor
  24. */
  25. function Help(doc) {
  26. if (!(this instanceof Help)) {
  27. throw new SyntaxError('Constructor must be called with the new operator');
  28. }
  29. if (!doc) throw new Error('Argument "doc" missing');
  30. this.doc = doc;
  31. }
  32. /**
  33. * Attach type information
  34. */
  35. Help.prototype.type = 'Help';
  36. Help.prototype.isHelp = true;
  37. /**
  38. * Generate a string representation of the Help object
  39. * @return {string} Returns a string
  40. * @private
  41. */
  42. Help.prototype.toString = function () {
  43. var doc = this.doc || {};
  44. var desc = '\n';
  45. if (doc.name) {
  46. desc += 'Name: ' + doc.name + '\n\n';
  47. }
  48. if (doc.category) {
  49. desc += 'Category: ' + doc.category + '\n\n';
  50. }
  51. if (doc.description) {
  52. desc += 'Description:\n ' + doc.description + '\n\n';
  53. }
  54. if (doc.syntax) {
  55. desc += 'Syntax:\n ' + doc.syntax.join('\n ') + '\n\n';
  56. }
  57. if (doc.examples) {
  58. desc += 'Examples:\n';
  59. var scope = {};
  60. for (var i = 0; i < doc.examples.length; i++) {
  61. var expr = doc.examples[i];
  62. desc += ' ' + expr + '\n';
  63. var res = void 0;
  64. try {
  65. // note: res can be undefined when `expr` is an empty string
  66. res = parse(expr).compile().evaluate(scope);
  67. } catch (e) {
  68. res = e;
  69. }
  70. if (res !== undefined && !(0, _is.isHelp)(res)) {
  71. desc += ' ' + (0, _string.format)(res, {
  72. precision: 14
  73. }) + '\n';
  74. }
  75. }
  76. desc += '\n';
  77. }
  78. if (doc.mayThrow && doc.mayThrow.length) {
  79. desc += 'Throws: ' + doc.mayThrow.join(', ') + '\n\n';
  80. }
  81. if (doc.seealso && doc.seealso.length) {
  82. desc += 'See also: ' + doc.seealso.join(', ') + '\n';
  83. }
  84. return desc;
  85. };
  86. /**
  87. * Export the help object to JSON
  88. */
  89. Help.prototype.toJSON = function () {
  90. var obj = (0, _object.clone)(this.doc);
  91. obj.mathjs = 'Help';
  92. return obj;
  93. };
  94. /**
  95. * Instantiate a Help object from a JSON object
  96. * @param {Object} json
  97. * @returns {Help} Returns a new Help object
  98. */
  99. Help.fromJSON = function (json) {
  100. var doc = {};
  101. Object.keys(json).filter(function (prop) {
  102. return prop !== 'mathjs';
  103. }).forEach(function (prop) {
  104. doc[prop] = json[prop];
  105. });
  106. return new Help(doc);
  107. };
  108. /**
  109. * Returns a string representation of the Help object
  110. */
  111. Help.prototype.valueOf = Help.prototype.toString;
  112. return Help;
  113. }, {
  114. isClass: true
  115. });
  116. exports.createHelpClass = createHelpClass;