UNPKG

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