Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented JSDOC #41

Open
alamparelli opened this issue Sep 20, 2024 · 0 comments
Open

Implemented JSDOC #41

alamparelli opened this issue Sep 20, 2024 · 0 comments
Milestone

Comments

@alamparelli
Copy link
Owner

JSDoc for JavaScript Documentation

JSDoc is the go-to tool in JavaScript for generating inline documentation. It allows you to document functions, objects, methods, and variables directly in the code using special comments.

Basic Example with JSDoc

Here’s how you can write inline documentation with JSDoc:

/**
 * Adds two numbers together.
 *
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @return {number} The sum of the two numbers.
 */
function add(a, b) {
    return a + b;
}
  • @param: Documents the parameters of the function. It includes the type of the parameter ({number}), the name (a or b), and a short description.
  • @return: Documents what the function returns, specifying the return type and a description.

JSDoc Tags

JSDoc provides a wide variety of tags that you can use to describe different aspects of your code. Here are some common tags:

  • @param: Describes a function parameter.
  • @return: Describes the return value of a function.
  • @example: Provides a usage example for the documented code.
  • @throws: Describes any errors or exceptions a function might throw.
  • @typedef: Defines a custom type.
  • @callback: Documents a callback function type.

More Complex Example:

/**
 * Fetches user data from the API.
 *
 * @async
 * @function fetchUserData
 * @param {string} userId - The unique identifier for the user.
 * @return {Promise<Object>} A promise that resolves with user data.
 * @throws {Error} If the request fails.
 * @example
 * fetchUserData('12345')
 *   .then(user => console.log(user))
 *   .catch(error => console.error(error));
 */
async function fetchUserData(userId) {
    const response = await fetch(`/api/users/${userId}`);
    if (!response.ok) {
        throw new Error('User data fetch failed');
    }
    return await response.json();
}
  • @async: Indicates that the function is asynchronous.
  • @throws: Describes errors that the function might throw.
  • @example: Gives an example of how to use the function.

Generating Documentation with JSDoc

Once you've added inline documentation using JSDoc, you can generate HTML documentation by using the JSDoc command-line tool.

Steps:

  1. Install JSDoc globally or locally using npm:

    npm install -g jsdoc
  2. Run JSDoc on your JavaScript files:

    jsdoc yourfile.js

This will generate a docs/ folder containing HTML documentation that you can view in a web browser.


By using JSDoc, you can ensure your JavaScript code is well-documented, making it easier for others (and your future self) to understand and use the codebase effectively.

@alamparelli alamparelli added this to the MVP1 milestone Sep 20, 2024
@alamparelli alamparelli modified the milestones: MVP1, MVP2 Oct 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant