Help.js 3.0 KB

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