UNPKG

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