Repository: xenova/transformers.js
File: src/utils/core.js (JavaScript)

Code before:

/**
 * @file Core utility functions/classes for Transformers.js.
 * 
 * These are only used internally, meaning an end-user shouldn't
 * need to access anything here.
 * 
 * @module utils/core
 */

/**
 * @typedef {Object} ProgressInfo
 * @property {'initiate' | 'download' | 'progress' | 'done'} status
 * @property {string} name - This can be either:
 *   - a string, the *model id* of a model repo on huggingface.co.
 *   - a path to a *directory* potentially containing the file.
 * @property {string} file - The name of the file
 * @property {number} [progress] - A number between 0 and 100. Only available for the 'progress' status.
 * @property {number} [loaded] - The number of bytes loaded. Only available for the 'progress' status.
 * @property {number} [total] - The total number of bytes to be loaded. Only available for the 'progress' status.
 */

/**
 * A callback function that is called with progress information.
 * @callback ProgressCallback
 * @param {ProgressInfo} progressInfo
 * @returns {void}
 */

/**
 * Helper function to dispatch progress callbacks.
 *
 * @param {ProgressCallback | null | undefined} progress_callback The progress callback function to dispatch.
 * @param {ProgressInfo} data The data to pass to the progress callback function.
 * @returns {void}
 * @private
 */
export function dispatchCallback(progress_callback, data) {
    if (progress_callback) progress_callback(data);
}

/**
 * Reverses the keys and values of an object.
 *
 * @param {Object} data The object to reverse.
 * @returns {Object} The reversed object.

Review comment (suggestion):
```suggestion
/**
 * @typedef {Object} ProgressInfo
 * @property {'initiate' | 'download' | 'progress' | 'done'} status The status of the progress item.
 * @property {string} name This can be either:
 * - a string, the *model id* of a model repo on huggingface.co.
 * - a path to a *directory* potentially containing the file.
 * @property {string} file The name of the file
 * @property {number} [progress] A number between 0 and 100. Only available for the 'progress' status.
 * @property {number} [loaded] The number of bytes loaded. Only available for the 'progress' status.
 * @property {number} [total] The total number of bytes to be loaded. Only available for the 'progress' status.
 */
```


Before:
![image](https://github.com/user-attachments/assets/d336f9c1-acce-4b44-a522-d75ca005af21)

After:
![image](https://github.com/user-attachments/assets/835f0a45-9cfb-4b96-b7f2-c5ca0ce532d3)


Code after:

/**
 * @file Core utility functions/classes for Transformers.js.
 * 
 * These are only used internally, meaning an end-user shouldn't
 * need to access anything here.
 * 
 * @module utils/core
 */

/**
 * @typedef {Object} ProgressInfo
 * @property {'initiate' | 'download' | 'progress' | 'done'} status The status of the progress item.
 * @property {string} name This can be either:
 * - a string, the *model id* of a model repo on huggingface.co.
 * - a path to a *directory* potentially containing the file.
 * @property {string} file The name of the file
 * @property {number} [progress] A number between 0 and 100. Only available for the 'progress' status.
 * @property {number} [loaded] The number of bytes loaded. Only available for the 'progress' status.
 * @property {number} [total] The total number of bytes to be loaded. Only available for the 'progress' status.
 */

/**
 * A callback function that is called with progress information.
 * @callback ProgressCallback
 * @param {ProgressInfo} progressInfo
 * @returns {void}
 */

/**
 * Helper function to dispatch progress callbacks.
 *
 * @param {ProgressCallback | null | undefined} progress_callback The progress callback function to dispatch.
 * @param {ProgressInfo} data The data to pass to the progress callback function.
 * @returns {void}
 * @private
 */
export function dispatchCallback(progress_callback, data) {
    if (progress_callback) progress_callback(data);
}

/**
 * Reverses the keys and values of an object.
 *
 * @param {Object} data The object to reverse.
 * @returns {Object} The reversed object.