simple-squiggle

A restricted subset of Squiggle
Log | Files | Refs | README

Help.js (3137B)


      1 import { isHelp } from '../utils/is.js';
      2 import { clone } from '../utils/object.js';
      3 import { format } from '../utils/string.js';
      4 import { factory } from '../utils/factory.js';
      5 var name = 'Help';
      6 var dependencies = ['parse'];
      7 export var createHelpClass = /* #__PURE__ */factory(name, dependencies, _ref => {
      8   var {
      9     parse
     10   } = _ref;
     11 
     12   /**
     13    * Documentation object
     14    * @param {Object} doc  Object containing properties:
     15    *                      {string} name
     16    *                      {string} category
     17    *                      {string} description
     18    *                      {string[]} syntax
     19    *                      {string[]} examples
     20    *                      {string[]} seealso
     21    * @constructor
     22    */
     23   function Help(doc) {
     24     if (!(this instanceof Help)) {
     25       throw new SyntaxError('Constructor must be called with the new operator');
     26     }
     27 
     28     if (!doc) throw new Error('Argument "doc" missing');
     29     this.doc = doc;
     30   }
     31   /**
     32    * Attach type information
     33    */
     34 
     35 
     36   Help.prototype.type = 'Help';
     37   Help.prototype.isHelp = true;
     38   /**
     39    * Generate a string representation of the Help object
     40    * @return {string} Returns a string
     41    * @private
     42    */
     43 
     44   Help.prototype.toString = function () {
     45     var doc = this.doc || {};
     46     var desc = '\n';
     47 
     48     if (doc.name) {
     49       desc += 'Name: ' + doc.name + '\n\n';
     50     }
     51 
     52     if (doc.category) {
     53       desc += 'Category: ' + doc.category + '\n\n';
     54     }
     55 
     56     if (doc.description) {
     57       desc += 'Description:\n    ' + doc.description + '\n\n';
     58     }
     59 
     60     if (doc.syntax) {
     61       desc += 'Syntax:\n    ' + doc.syntax.join('\n    ') + '\n\n';
     62     }
     63 
     64     if (doc.examples) {
     65       desc += 'Examples:\n';
     66       var scope = {};
     67 
     68       for (var i = 0; i < doc.examples.length; i++) {
     69         var expr = doc.examples[i];
     70         desc += '    ' + expr + '\n';
     71         var res = void 0;
     72 
     73         try {
     74           // note: res can be undefined when `expr` is an empty string
     75           res = parse(expr).compile().evaluate(scope);
     76         } catch (e) {
     77           res = e;
     78         }
     79 
     80         if (res !== undefined && !isHelp(res)) {
     81           desc += '        ' + format(res, {
     82             precision: 14
     83           }) + '\n';
     84         }
     85       }
     86 
     87       desc += '\n';
     88     }
     89 
     90     if (doc.mayThrow && doc.mayThrow.length) {
     91       desc += 'Throws: ' + doc.mayThrow.join(', ') + '\n\n';
     92     }
     93 
     94     if (doc.seealso && doc.seealso.length) {
     95       desc += 'See also: ' + doc.seealso.join(', ') + '\n';
     96     }
     97 
     98     return desc;
     99   };
    100   /**
    101    * Export the help object to JSON
    102    */
    103 
    104 
    105   Help.prototype.toJSON = function () {
    106     var obj = clone(this.doc);
    107     obj.mathjs = 'Help';
    108     return obj;
    109   };
    110   /**
    111    * Instantiate a Help object from a JSON object
    112    * @param {Object} json
    113    * @returns {Help} Returns a new Help object
    114    */
    115 
    116 
    117   Help.fromJSON = function (json) {
    118     var doc = {};
    119     Object.keys(json).filter(prop => prop !== 'mathjs').forEach(prop => {
    120       doc[prop] = json[prop];
    121     });
    122     return new Help(doc);
    123   };
    124   /**
    125    * Returns a string representation of the Help object
    126    */
    127 
    128 
    129   Help.prototype.valueOf = Help.prototype.toString;
    130   return Help;
    131 }, {
    132   isClass: true
    133 });