UNPKG

@skyra/discord-components-core

Version:

Web components to easily build and display fake Discord messages on your webpages.

32 lines (31 loc) 1.5 kB
const padZeroes = (value) => { const [month, day, year] = value.split('/'); return `${month.padStart(2, '0')}/${day.padStart(2, '0')}/${year}`; }; const formatDate = (value) => { if (!(value instanceof Date)) return value; return padZeroes(`${value.getMonth() + 1}/${value.getDate()}/${value.getFullYear()}`); }; const formatTime = (value, hour24 = false) => { if (!(value instanceof Date)) return value; if (hour24) return `${value.getHours()}:${value.getMinutes().toString().padStart(2, '0')}`; const hour = value.getHours() % 12 || 12; const meridiem = value.getHours() < 12 ? 'AM' : 'PM'; return `${hour}:${value.getMinutes().toString().padStart(2, '0')} ${meridiem}`; }; export const handleTimestamp = (value, useTime = false, hour24 = false) => { if (!(value instanceof Date) && typeof value !== 'string') { throw new TypeError('Timestamp prop must be a Date object or a string.'); } return useTime ? formatTime(value, hour24) : formatDate(value); }; export const IMAGE_EXTENSION = /\.(bmp|jpe?g|png|gif|webp|tiff)$/i; export const validateImageExtension = (url) => { if (!IMAGE_EXTENSION.test(url)) throw new Error(`The url of an image for discord-attachment should match the regex ${IMAGE_EXTENSION}`); }; export const getGlobalEmojiUrl = (emojiName) => { var _a, _b; return (_b = (_a = window.$discordMessage) === null || _a === void 0 ? void 0 : _a.emojis) === null || _b === void 0 ? void 0 : _b[emojiName]; }; //# sourceMappingURL=util.js.map