1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createHelpClass = void 0;
|
7 |
|
8 | var _is = require("../utils/is");
|
9 |
|
10 | var _object = require("../utils/object");
|
11 |
|
12 | var _string = require("../utils/string");
|
13 |
|
14 | var _factory = require("../utils/factory");
|
15 |
|
16 | var name = 'Help';
|
17 | var dependencies = ['parse'];
|
18 | var createHelpClass = (0, _factory.factory)(name, dependencies, function (_ref) {
|
19 | var parse = _ref.parse;
|
20 |
|
21 | |
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
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 |
|
42 |
|
43 |
|
44 |
|
45 | Help.prototype.type = 'Help';
|
46 | Help.prototype.isHelp = true;
|
47 | |
48 |
|
49 |
|
50 |
|
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 |
|
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 |
|
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 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | Help.fromJSON = function (json) {
|
123 | var doc = {};
|
124 |
|
125 | for (var prop in json) {
|
126 | if (prop !== 'mathjs') {
|
127 |
|
128 | doc[prop] = json[prop];
|
129 | }
|
130 | }
|
131 |
|
132 | return new Help(doc);
|
133 | };
|
134 | |
135 |
|
136 |
|
137 |
|
138 |
|
139 | Help.prototype.valueOf = Help.prototype.toString;
|
140 | return Help;
|
141 | }, {
|
142 | isClass: true
|
143 | });
|
144 | exports.createHelpClass = createHelpClass; |
\ | No newline at end of file |