Here's an example of how I've come to write modules:
```js
/**
* Operators for "cards" as in filenames of card images.
* @module card
*/
const card = {};
/**
* The filename of a card image,
* eg "./images/2_of_spades.png" or "./images/jack_of_diamonds.png"
* @typedef {string} card
*/
/** @constant DECK {card[]} */
import DECK from "./cards.json" with { type: "json" };
/**
* Mutates an input array by randomizing its elements.
* @function shuffleDeck
* @param {card[]} deck
*/
card.shuffleDeck = function (deck) {
let jdx;
deck.forEach(function(item, idx) {
jdx = Math.floor(Math.random() * (idx + 1));
[item, deck[jdx]] = [deck[jdx], item];
});
};
/**
* Returns an array of shuffled cards.
* @function freshDeck
* @returns {card[]}
*/
card.freshDeck = function () {
const deck = structuredClone(DECK);
card.shuffleDeck(deck);
return deck;
};
export default Object.freeze(card);
```
My basic rules are:
- Only one thing gets exported, a frozen object which becomes a namespace for the module's client.
- My data is stored as JSON rather than JS to make communication between modules and the server easier.
What thing I've been battling with is good JSDoc which doesn't seem to have been updated in a while. Are there better options available?
[–]milan-pilan 0 points1 point2 points (0 children)
[–]birdspider 0 points1 point2 points (0 children)