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 =
|
19 |
|
20 | (0, _factory.factory)(name, dependencies, function (_ref) {
|
21 | var parse = _ref.parse;
|
22 |
|
23 | |
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
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 |
|
44 |
|
45 |
|
46 |
|
47 | Help.prototype.type = 'Help';
|
48 | Help.prototype.isHelp = true;
|
49 | |
50 |
|
51 |
|
52 |
|
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 |
|
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 |
|
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 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 | Help.fromJSON = function (json) {
|
125 | var doc = {};
|
126 |
|
127 | for (var prop in json) {
|
128 | if (prop !== 'mathjs') {
|
129 |
|
130 | doc[prop] = json[prop];
|
131 | }
|
132 | }
|
133 |
|
134 | return new Help(doc);
|
135 | };
|
136 | |
137 |
|
138 |
|
139 |
|
140 |
|
141 | Help.prototype.valueOf = Help.prototype.toString;
|
142 | return Help;
|
143 | }, {
|
144 | isClass: true
|
145 | });
|
146 | exports.createHelpClass = createHelpClass; |
\ | No newline at end of file |