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