1 | 'use strict';
|
2 |
|
3 | var object = require('../utils/object');
|
4 | var string = require('../utils/string');
|
5 |
|
6 | function factory(type, config, load, typed) {
|
7 | var parser = load(require('./function/parser'))();
|
8 |
|
9 | |
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function Help(doc) {
|
21 | if (!(this instanceof Help)) {
|
22 | throw new SyntaxError('Constructor must be called with the new operator');
|
23 | }
|
24 |
|
25 | if (!doc) throw new Error('Argument "doc" missing');
|
26 |
|
27 | this.doc = doc;
|
28 | }
|
29 |
|
30 | |
31 |
|
32 |
|
33 | Help.prototype.type = 'Help';
|
34 | Help.prototype.isHelp = true;
|
35 |
|
36 | |
37 |
|
38 |
|
39 |
|
40 |
|
41 | Help.prototype.toString = function () {
|
42 | var doc = this.doc || {};
|
43 | var desc = '\n';
|
44 |
|
45 | if (doc.name) {
|
46 | desc += 'Name: ' + doc.name + '\n\n';
|
47 | }
|
48 | if (doc.category) {
|
49 | desc += 'Category: ' + doc.category + '\n\n';
|
50 | }
|
51 | if (doc.description) {
|
52 | desc += 'Description:\n ' + doc.description + '\n\n';
|
53 | }
|
54 | if (doc.syntax) {
|
55 | desc += 'Syntax:\n ' + doc.syntax.join('\n ') + '\n\n';
|
56 | }
|
57 | if (doc.examples) {
|
58 | desc += 'Examples:\n';
|
59 | for (var i = 0; i < doc.examples.length; i++) {
|
60 | var expr = doc.examples[i];
|
61 | desc += ' ' + expr + '\n';
|
62 |
|
63 | var res = void 0;
|
64 | try {
|
65 |
|
66 | res = parser.eval(expr);
|
67 | } catch (e) {
|
68 | res = e;
|
69 | }
|
70 | if (res !== undefined && !type.isHelp(res)) {
|
71 | desc += ' ' + string.format(res, { precision: 14 }) + '\n';
|
72 | }
|
73 | }
|
74 | desc += '\n';
|
75 | }
|
76 | if (doc.seealso && doc.seealso.length) {
|
77 | desc += 'See also: ' + doc.seealso.join(', ') + '\n';
|
78 | }
|
79 |
|
80 | return desc;
|
81 | };
|
82 |
|
83 | |
84 |
|
85 |
|
86 | Help.prototype.toJSON = function () {
|
87 | var obj = object.clone(this.doc);
|
88 | obj.mathjs = 'Help';
|
89 | return obj;
|
90 | };
|
91 |
|
92 | |
93 |
|
94 |
|
95 |
|
96 |
|
97 | Help.fromJSON = function (json) {
|
98 | var doc = {};
|
99 | for (var prop in json) {
|
100 | if (prop !== 'mathjs') {
|
101 |
|
102 | doc[prop] = json[prop];
|
103 | }
|
104 | }
|
105 | return new Help(doc);
|
106 | };
|
107 |
|
108 | |
109 |
|
110 |
|
111 | Help.prototype.valueOf = Help.prototype.toString;
|
112 |
|
113 | return Help;
|
114 | }
|
115 |
|
116 | exports.name = 'Help';
|
117 | exports.path = 'type';
|
118 | exports.factory = factory; |
\ | No newline at end of file |