mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

Imported function help

Open dvd101x opened this issue 4 years ago • 3 comments

When using math.import and importing a function I would like for it to work with the help function

// define new functions and variables
math.import({
  myvalue: 42,
  hello: function (name) {
    /**
     * This function says hello
     *
     * Syntax:
     *
     *     math.hello("David")
     *     math.hello("Jos")
     *
     * Examples:
     *
     *     math.hello("David")               // returns "hello, David"
     *
     * See also:
     *
     *    compare, compareNatural
     *
     * @param {... *} args  A single string or a list of strings
     * @return {*} all strings
     */
    return 'hello, ' + name + '!'
  }
})

help(hello)

This function says hello

Syntax:

    math.hello("David")
    math.hello("Jos")

Examples:

    math.hello("David")               // returns "hello, David"

See also:

   compare, compareNatural

https://mathjs.org/docs/core/extension.html#extension

dvd101x avatar Nov 29 '21 22:11 dvd101x

Hm that is a good question, there isn't really an API for that right now.

I can think of two workarounds:

  1. When using the ES6 library, you can add your docs to the globally exported docs

    import { create, all, docs } from 'mathjs'
    const math = create(all)
    
    const myHelloDocs = {
      name: 'hello',
      category: '...',
      syntax: [
        '...'
      ],
      description: '....',
      examples: [
        '...'
      ],
      seealso: [
        '...'
      ]
    }
    
    // add custom docs to the globally exported docs
    docs.hello = myHelloDocs
    
    // now you can use help(hello)
    
  2. Override the function help with your own version which first checks whether the input is hello, if so return new Help(myHelloDocs), and otherwise call the original help function.

josdejong avatar Dec 01 '21 07:12 josdejong

Thank you, I will try both.

I'm working on an implementation of mathjs with some thermodynamics properties from coolprop. It benefits greatly from the physical units of mathjs.

dvd101x avatar Dec 14 '21 16:12 dvd101x

Thanks, good to hear 👍😄

josdejong avatar Dec 19 '21 16:12 josdejong

Closing as duplicate of #2047.

gwhitney avatar Oct 03 '23 02:10 gwhitney