Skip to content

Instantly share code, notes, and snippets.

@libetl libetl/fakemoment.ts
Created Nov 30, 2019

Embed
What would you like to do?
Fake moment.js
const moment = function moment(textValue: string | number, format: string) {
const timeOfTheDay = /^([0-9]{1,2}):([0-9]{1,2})$/.exec((textValue || "").trim())
const onlyTime = timeOfTheDay && new Date()
if (onlyTime && timeOfTheDay) onlyTime.setHours(parseInt(timeOfTheDay[1]), parseInt(timeOfTheDay[2]))
const date = !textValue ? new Date() :
!isNaN(textValue as number) ? new Date(textValue) :
!(textValue as string).trim() ? new Date() :
onlyTime ? onlyTime : new Date(textValue)
return {
date,
humanizedDiff: '',
diffDays: 0,
diffHours: 0,
diffMinutes: 0,
add: function (quantity: number, unit: 'day' | 'days' | 'month' | 'months' | 'year' | 'years') {
const oneDay = 24 * 60 * 60 * 1000
if (unit === 'day' || unit === 'days')
this.date = new Date(this.date.getTime() + quantity * oneDay)
return this
},
utcOffset: function (offset: any) {
const oneHour = 60 * 60 * 1000
const oneMinute = 60 * 60 * 1000
if (!isNaN(offset)) {
return this
}
const offsetText = (offset || "").toString()
const regex = /^([+-]?)([0-9]{1,2})(?::([0-9]{1,2}))?$/
const match = regex.exec(offsetText)
if (!match) return this
const sign = match[1] === "-" ? -1 : 1
const hours = parseInt(match[2])
const minutes = !match[3] ? 0 : parseInt(match[3])
this.date = new Date(this.date.getTime() + sign * hours * oneHour
+ minutes * oneMinute)
return this
},
toObject: function () {
return {
year: this.date.getFullYear(), month: this.date.getMonth() + 1, day: this.date.getDay(),
hours: this.date.getHours(), minutes: this.date.getMinutes(), seconds: this.date.getSeconds()
}
},
format: function (format: string) {
const table = format.split(/(?=[^a-zA-Z])/)
return table.map(
(token, i) => {
const rightPadding = table[i + 1] &&
/^[a-zA-Z]/.exec(table[i + 1].trim()) ? ' ' : ''
switch (token.trim()) {
case 'LT': return `${this.date.getHours()}:${this.date.getMinutes()}${rightPadding}`
case 'DD': return this.date.getDate() + rightPadding
case 'YYYY': return this.date.getFullYear() + rightPadding
case 'ddd': switch (this.date.getDay()) {
case 0: return 'Sun' + rightPadding
case 1: return 'Mon' + rightPadding
case 2: return 'Tue' + rightPadding
case 3: return 'Wed' + rightPadding
case 4: return 'Thi' + rightPadding
case 5: return 'Fri' + rightPadding
case 6: return 'Sat' + rightPadding
default: return "" + rightPadding
}
case 'MMM': switch (this.date.getMonth()) {
case 0: return 'Jan' + rightPadding
case 1: return 'Feb' + rightPadding
case 2: return 'Mar' + rightPadding
case 3: return 'Apr' + rightPadding
case 4: return 'May' + rightPadding
case 5: return 'Jun' + rightPadding
case 6: return 'Jul' + rightPadding
case 7: return 'Aug' + rightPadding
case 8: return 'Sep' + rightPadding
case 9: return 'Oct' + rightPadding
case 10: return 'Nov' + rightPadding
case 11: return 'Dec' + rightPadding
default: return "" + rightPadding
}
default: return token + rightPadding;
}
}
).join('')
},
diff: function (otherMoment?: any, unit: 'day' | 'days' | 'month' | 'months' | 'year' | 'years') {
if (!otherMoment || !otherMoment.date) return ""
const oDate = otherMoment.date as Date;
const diff = this.date.getTime() - oDate.getTime()
this.diffDays = Math.floor(diff / 86400000)
this.diffHours = Math.floor((diff - this.diffDays * 86400000) / 3600000)
this.diffMinutes = (diff - this.diffDays * 86400000 - this.diffHours * 3600000) / 60000
this.humanizedDiff = `${this.diffDays > 0 ? this.diffDays + ' days' : ''}${
this.diffDays > 0 && this.diffHours > 0 ? ' ' : ''}${
this.diffHours > 0 ? this.diffHours + ' hours' : ''}${this.diffHours > 0 && this.diffMinutes > 0 ? ' ' : ''}${
this.diffMinutes > 0 ? this.diffMinutes + ' minutes' : ''}`;
return unit === 'day' || unit === 'days' ? this.diffDays : this
},
humanize: function () {
return this.humanizedDiff
}
}
}
const momentModule = Object.assign(moment, {
duration: function (thisMoment?: any) {
return thisMoment
}
})
export default momentModule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.