server for my chess game
jsdoc utils.js
bendn 2022-10-12
parent 43d835c · commit 4121e5a
-rw-r--r--src/utils.js29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/utils.js b/src/utils.js
index 6924fe7..d9a5491 100644
--- a/src/utils.js
+++ b/src/utils.js
@@ -1,17 +1,46 @@
+/**
+ * Stringifies a object
+ * @param {Object} obj The thing to be stringed
+ * @return {String} The string representation of obj
+ * @exports
+ */
export function str_obj(obj) {
return JSON.stringify(obj, null, 0);
}
+/**
+ * Flips a color: `w` -> `b`
+ *
+ * @param {String} color
+ * @returns {String} !color
+ * @exports
+ */
export function flip_color(color) {
return color === "w" ? "b" : "w";
}
+/**
+ * Sends a packet to multiple clients
+ *
+ * @param {Object} data The stuff to send
+ * @param {String} header The header to use
+ * @param {WebSocket[]} clients The clients to sendd it to
+ * @exports
+ */
export function send_group_packet(data, header, clients) {
clients.forEach((client) => {
if (client) client.send_packet(data, header);
});
}
+/**
+ * Picks properties from a object
+ *
+ * @param {Object} o The object
+ * @param {...String} props The properties
+ * @returns {(undefined|Object)} undefined if o === undefined
+ * @exports
+ */
export function pick(o, ...props) {
if (o === undefined) return undefined;
return Object.assign({}, ...props.map((prop) => ({ [prop]: o[prop] })));