Help.js (3380B)
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.js"); 9 10 var _object = require("../utils/object.js"); 11 12 var _string = require("../utils/string.js"); 13 14 var _factory = require("../utils/factory.js"); 15 16 var name = 'Help'; 17 var dependencies = ['parse']; 18 var createHelpClass = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) { 19 var parse = _ref.parse; 20 21 /** 22 * Documentation object 23 * @param {Object} doc Object containing properties: 24 * {string} name 25 * {string} category 26 * {string} description 27 * {string[]} syntax 28 * {string[]} examples 29 * {string[]} seealso 30 * @constructor 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 * Attach type information 42 */ 43 44 45 Help.prototype.type = 'Help'; 46 Help.prototype.isHelp = true; 47 /** 48 * Generate a string representation of the Help object 49 * @return {string} Returns a string 50 * @private 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 // note: res can be undefined when `expr` is an empty string 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.mayThrow && doc.mayThrow.length) { 100 desc += 'Throws: ' + doc.mayThrow.join(', ') + '\n\n'; 101 } 102 103 if (doc.seealso && doc.seealso.length) { 104 desc += 'See also: ' + doc.seealso.join(', ') + '\n'; 105 } 106 107 return desc; 108 }; 109 /** 110 * Export the help object to JSON 111 */ 112 113 114 Help.prototype.toJSON = function () { 115 var obj = (0, _object.clone)(this.doc); 116 obj.mathjs = 'Help'; 117 return obj; 118 }; 119 /** 120 * Instantiate a Help object from a JSON object 121 * @param {Object} json 122 * @returns {Help} Returns a new Help object 123 */ 124 125 126 Help.fromJSON = function (json) { 127 var doc = {}; 128 Object.keys(json).filter(function (prop) { 129 return prop !== 'mathjs'; 130 }).forEach(function (prop) { 131 doc[prop] = json[prop]; 132 }); 133 return new Help(doc); 134 }; 135 /** 136 * Returns a string representation of the Help object 137 */ 138 139 140 Help.prototype.valueOf = Help.prototype.toString; 141 return Help; 142 }, { 143 isClass: true 144 }); 145 exports.createHelpClass = createHelpClass;