writer
writer copied to clipboard
JSDoc `@returns` tag explains function
It's my first time using this extension, and I'm impressed by how well it works.
I wrote a function to help generate complex class names in JavaScript. The project uses React, so I have states to dynamically generate the classes for my elements. It gave me everything I need, but I noticed the@returns tag in the JSDoc explains the function again.
This is what it returns
/**
* It takes an array of strings, arrays, and objects, and returns a string of all the truthy values
* @param {CX[]} args - CX[]
* @returns A function that takes in an array of strings and returns a string of class names.
*/
So I just trimmed the text to use a string of class names. alone.
Here are the function and types
export function cx(...args: CX[]) {
const classes = args.reduce<Array<string | string[]>>((acc, cur) => {
let item: string | string[] = [];
if (!cur) return acc;
if (typeof cur === "string") item = cur;
else if (Array.isArray(cur)) item = (cur[0] ? cur[1] : cur[2]) ?? [];
else item = Object.keys(Object.fromEntries(Object.entries(cur ?? {}).filter((i) => i[1])));
return item ? [...acc, item] : acc;
}, []);
return [...new Set(classes.flat())].join(" ").replace(/\s{2,}/g, " ");
}
type CX = undefined | null | false | string | [unknown, string, string?] | Record<string, unknown>;
This is a really good extension and I'm already happy with how it works. Thanks for your effort
I started using it for other functions in my project. I use them to upload and get images with the Google Drive API.
It doesn't explain the function again here
/**
* It takes a base64 encoded image, uploads it to Google Drive, and returns the file ID
* @param {string} [dataURL] - The base64 encoded image data.
* @returns The ID of the uploaded file
*/
export async function uploadImage(dataURL?: string) {
const [, base64] = dataURL.split(dataURLRegex);
const client = drive({ auth });
const { data: upload } = await client.files.create({
media: {
mimeType: "image/webp",
body: sharp(Buffer.from(base64, "base64")).toFormat("webp"),
},
requestBody: { name: `name.webp` },
});
return upload.id;
}
But it does here
/**
* If the id is a data URL, return it, otherwise return a Google Drive URL
* @param {string} [id] - The id of the image.
* @returns A function that takes an id and returns a dataURL if the id is a dataURL, otherwise it
* returns a google drive url.
*/
export const getImage = (id?: string) => {
if (!id) return;
return dataURLRegex.test(id) ? id : `https://drive.google.com/uc?id=${id}&export=download`;
};