simple-squiggle

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

help.js (2235B)


      1 "use strict";
      2 
      3 Object.defineProperty(exports, "__esModule", {
      4   value: true
      5 });
      6 exports.createHelp = void 0;
      7 
      8 var _factory = require("../../utils/factory.js");
      9 
     10 var _customs = require("../../utils/customs.js");
     11 
     12 var _embeddedDocs = require("../embeddedDocs/embeddedDocs.js");
     13 
     14 var _object = require("../../utils/object.js");
     15 
     16 var name = 'help';
     17 var dependencies = ['typed', 'mathWithTransform', 'Help'];
     18 var createHelp = /* #__PURE__ */(0, _factory.factory)(name, dependencies, function (_ref) {
     19   var typed = _ref.typed,
     20       mathWithTransform = _ref.mathWithTransform,
     21       Help = _ref.Help;
     22 
     23   /**
     24    * Retrieve help on a function or data type.
     25    * Help files are retrieved from the embedded documentation in math.docs.
     26    *
     27    * Syntax:
     28    *
     29    *    math.help(search)
     30    *
     31    * Examples:
     32    *
     33    *    console.log(math.help('sin').toString())
     34    *    console.log(math.help(math.add).toString())
     35    *    console.log(math.help(math.add).toJSON())
     36    *
     37    * @param {Function | string | Object} search   A function or function name
     38    *                                              for which to get help
     39    * @return {Help} A help object
     40    */
     41   return typed(name, {
     42     any: function any(search) {
     43       var prop;
     44       var searchName = search;
     45 
     46       if (typeof search !== 'string') {
     47         for (prop in mathWithTransform) {
     48           // search in functions and constants
     49           if ((0, _object.hasOwnProperty)(mathWithTransform, prop) && search === mathWithTransform[prop]) {
     50             searchName = prop;
     51             break;
     52           }
     53         }
     54         /* TODO: implement help for data types
     55          if (!text) {
     56          // search data type
     57          for (prop in math.type) {
     58          if (hasOwnProperty(math, prop)) {
     59          if (search === math.type[prop]) {
     60          text = prop
     61          break
     62          }
     63          }
     64          }
     65          }
     66          */
     67 
     68       }
     69 
     70       var doc = (0, _customs.getSafeProperty)(_embeddedDocs.embeddedDocs, searchName);
     71 
     72       if (!doc) {
     73         var searchText = typeof searchName === 'function' ? searchName.name : searchName;
     74         throw new Error('No documentation found on "' + searchText + '"');
     75       }
     76 
     77       return new Help(doc);
     78     }
     79   });
     80 });
     81 exports.createHelp = createHelp;