help.js 2.0 KB

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