Here's a JSDoc for the hasClass function:
/**
* Checks if an element has a class that matches the given name or pattern.
*
* @async
* @param {object} locator - The element locator.
* @param {string|RegExp} classNameOrPattern - The class name or pattern to match.
* @returns {Promise<boolean>} A promise that resolves to `true` if the element has a matching class, `false` otherwise.
*/
const hasClass = async (locator, classNameOrPattern) => {
const classAttribute = await locator.getAttribute('class');
return classAttribute?.split(' ').some(cls =>
classNameOrPattern instanceof RegExp
? classNameOrPattern.test(cls)
: cls === classNameOrPattern
) ?? false;
};
However, to make it more informative and follow JSDoc best practices, you can add more details:
/**
* Checks if an element has a class that matches the given name or pattern.
*
* @async
* @param {object} locator - The element locator.
* @param {string|RegExp} classNameOrPattern - The class name or pattern to match.
* If a string, it will be matched exactly. If a RegExp, it will be tested against each class.
* @returns {Promise<boolean>} A promise that resolves to `true` if the element has a matching class, `false` otherwise.
* @example
* await hasClass(element, 'active'); // true if element has class "active"
* await hasClass(element, /^prefix-/); // true if element has a class starting with "prefix-"
*/
const hasClass = async (locator, classNameOrPattern) => {
const classAttribute = await locator.getAttribute('class');
return classAttribute?.split(' ').some(cls =>
classNameOrPattern instanceof RegExp
? classNameOrPattern.test(cls)
: cls === classNameOrPattern
) ?? false;
};
This JSDoc includes:
- A brief description of the function's purpose
@async to indicate it's an asynchronous function
@param tags to describe the function's parameters
@returns to describe the return value
@example to provide usage examples
This should make it easier for others (and yourself) to understand how to use the hasClass function.