UNPKG

@albinberisha/codex-cli

Version:

Codex cli to scaffold plugins with Node

76 lines (66 loc) 2.83 kB
import { format } from 'date-fns' const helpers = { formatDateTime: isoDate => (isoDate ? format(new Date(`${isoDate}Z`), 'yyyy-MM-dd HH:mm:ss') : '-'), formatOffsetDateTime: isoDate => (isoDate ? format(new Date(`${isoDate}`), 'yyyy-MM-dd HH:mm:ss') : '-'), formatDateTimeDetailed: isoDate => (isoDate ? format(new Date(`${isoDate}Z`), 'yyyy-MM-dd HH:mm:ss') : '-'), formatOffsetDateTimeDetailed: isoDate => (isoDate ? format(new Date(`${isoDate}`), 'yyyy-MM-dd HH:mm:ss') : '-'), timeSince(dateAccepted) { const dateFormatted = dateAccepted.endsWith('Z') ? dateAccepted : `${dateAccepted}Z` const date = Date.parse(dateFormatted) const seconds = Math.floor((new Date() - date) / 1000) let interval = seconds / 31536000 if (interval > 1) { return `${Math.floor(interval)} year${Math.floor(interval) !== 1 ? 's' : '' }` } interval = seconds / 2592000 if (interval > 1) { return `${Math.floor(interval)} month${Math.floor(interval) !== 1 ? 's' : '' }` } interval = seconds / 86400 if (interval > 1) { return `${Math.floor(interval)} day${Math.floor(interval) !== 1 ? 's' : '' }` } interval = seconds / 3600 if (interval > 1) { return `${Math.floor(interval)} hour${Math.floor(interval) !== 1 ? 's' : '' }` } interval = seconds / 60 if (interval >= 1) { return `${Math.floor(interval)} minute${Math.floor(interval) !== 1 ? 's' : '' }` } return `${Math.floor(seconds)} second${Math.floor(seconds) !== 1 ? 's' : '' }` }, formatMilliseconds(milliseconds) { const ms = milliseconds let minutes = ms / 60000 if (minutes / 1440 >= 1) { return `${this.formatToInt((minutes / 1440).toFixed(1))} day${Math.round(minutes / 1440) !== 1 ? 's' : '' }` } if (minutes / 60 >= 1) { return `${this.formatToInt((minutes / 60).toFixed(1))} hour${Math.round(minutes / 60) !== 1 ? 's' : '' }` } if (minutes >= 1) { return `${this.formatToInt(minutes.toFixed(1))} minute${Math.round(minutes) !== 1 ? 's' : '' }` } if (minutes >= 0.016) { minutes *= 60 return `${this.formatToInt(minutes.toFixed(1))} second${Math.round(minutes) !== 1 ? 's' : '' }` } minutes *= 60000 return `${this.formatToInt(ms)} millisecond${Math.round(ms) !== 1 ? 's' : '' }` } } export default helpers