1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.createHelpClass = void 0;
|
7 | var _is = require("../utils/is.js");
|
8 | var _object = require("../utils/object.js");
|
9 | var _string = require("../utils/string.js");
|
10 | var _factory = require("../utils/factory.js");
|
11 | var name = 'Help';
|
12 | var dependencies = ['parse'];
|
13 | var createHelpClass = (0, _factory.factory)(name, dependencies, function (_ref) {
|
14 | var parse = _ref.parse;
|
15 | |
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
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 |
|
36 |
|
37 | Help.prototype.type = 'Help';
|
38 | Help.prototype.isHelp = true;
|
39 |
|
40 | |
41 |
|
42 |
|
43 |
|
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 |
|
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 |
|
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 |
|
101 |
|
102 |
|
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 |
|
116 |
|
117 | Help.prototype.valueOf = Help.prototype.toString;
|
118 | return Help;
|
119 | }, {
|
120 | isClass: true
|
121 | });
|
122 | exports.createHelpClass = createHelpClass; |
\ | No newline at end of file |