UNPKG

generator-begcode

Version:

Spring Boot + Angular/React/Vue in one handy generator

60 lines (59 loc) 2.38 kB
export function formatDateForChangelog(now) { const nowUTC = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds()); const year = `${nowUTC.getFullYear()}`; let month = `${nowUTC.getMonth() + 1}`; if (month.length === 1) { month = `0${month}`; } let day = `${nowUTC.getDate()}`; if (day.length === 1) { day = `0${day}`; } let hour = `${nowUTC.getHours()}`; if (hour.length === 1) { hour = `0${hour}`; } let minute = `${nowUTC.getMinutes()}`; if (minute.length === 1) { minute = `0${minute}`; } let second = `${nowUTC.getSeconds()}`; if (second.length === 1) { second = `0${second}`; } return `${year}${month}${day}${hour}${minute}${second}`; } export function parseChangelog(changelogDate) { if (!changelogDate) { throw new Error('changelogDate is required.'); } if (typeof changelogDate !== 'string') { throw new Error(`changelogDate ${changelogDate} must be a string.`); } if (changelogDate.length !== 14) { throw new Error(`changelogDate ${changelogDate} is not a valid changelogDate.`); } const zeroFallback = (val, fallback) => (/^0+$/.test(val) ? fallback : val); const year = zeroFallback(changelogDate.substring(0, 4), '2024'); const month = zeroFallback(changelogDate.substring(4, 6), '01'); const day = zeroFallback(changelogDate.substring(6, 8), '01'); const formattedDate = `${year}-${month}-${day}T${changelogDate.substring(8, 10)}:${changelogDate.substring(10, 12)}:${changelogDate.substring(12, 14)}+00:00`; const parsedTimestamp = Date.parse(formattedDate); if (isNaN(parsedTimestamp)) { throw new Error(`changelogDate ${changelogDate} is not a valid date.`); } return new Date(parsedTimestamp); } export const parseCreationTimestamp = (creationTimestampOption) => { let creationTimestamp; if (creationTimestampOption) { creationTimestamp = Date.parse(creationTimestampOption); if (!creationTimestamp) { return undefined; } if (creationTimestamp > new Date().getTime()) { throw new Error(`Creation timestamp should not be in the future: ${creationTimestampOption}.`); } } return creationTimestamp; };