UNPKG

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