You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

42 lines
843 B

const parseMessageFromString = (message) => {
try {
return JSON.parse(message);
} catch {
return {};
}
}
/**
* @typedef {Object} MessageLike
* @property {string} action - every string valid for JS function name.
* @property {Object} data - The user's last name.
*/
/**
* @param {string|MessageLike} data - Message itself of JSON representation of Message
*/
function Message(data = {}) {
switch(Object.prototype.toString.call(data)) {
case "[object String]":
const parsedMessage = parseMessageFromString(data);
this.action = parsedMessage?.action;
this.data = parsedMessage?.data;
break;
case "[object Object]":
this.action = data?.action;
this.data = data?.data;
break;
}
}
Message.prototype.isValid = function() {
return (
this?.action !== undefined
);
}
export { Message }