Last active
August 6, 2019 05:47
-
-
Save brookjordan/a531880354201facb760f3e26ec6f6b3 to your computer and use it in GitHub Desktop.
Appends the Number prototype with date related functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* let a = 1; | |
* let an = 1; | |
* let some = 2; | |
* | |
* Currently supports: | |
* - a.day.ago | |
* - some.days.ago | |
* - a.week.ago | |
* - some.weeks.ago | |
* - a.month.ago | |
* - some.months.ago | |
* - a.year.ago | |
* - some.years.ago | |
* - a.decade.ago | |
* - some.decades.ago | |
* - a.centry.ago | |
* - some.centuries.ago | |
* - a.millennium.ago | |
* - some.millennia.ago | |
* - some.millenniums.ago | |
* - an.age.ago | |
* - some.ages.ago | |
* | |
* - a.day.ago | |
* - a.day.in.the.past | |
* - a.day.from.now | |
* - a.day.in.the.future | |
* - a.day.before(date) // where date is any parameters that creating a Date would take | |
* - a.day.after(date) // where date is any parameters that creating a Date would take | |
*/ | |
class Period { | |
value; | |
func; | |
constructor({ | |
value, | |
func, | |
}) { | |
this.value = value; | |
this.func = func; | |
} | |
get in() { | |
let obj = this; | |
return { | |
the: { | |
get future() { | |
return obj.after(new Date()); | |
}, | |
get past() { | |
return obj.ago; | |
}, | |
}, | |
} | |
} | |
get from() { | |
let obj = this; | |
return { | |
get now() { | |
return obj.after(new Date()); | |
}, | |
}; | |
} | |
get ago(){ | |
return this.before(new Date()); | |
} | |
before(...args) { | |
let date = new Date(...args); | |
date[`set${this.func}`](date[`get${this.func}`]() - this.value); | |
return date; | |
} | |
after(...args) { | |
let date = new Date(...args); | |
date[`set${this.func}`](date[`get${this.func}`]() + this.value); | |
return date; | |
} | |
} | |
Object.defineProperties(Number.prototype, { | |
days: { | |
get() { | |
return new Period({ | |
value: this, | |
func: 'Date', | |
}); | |
}, | |
}, | |
day: { | |
get() { | |
return this.days; | |
}, | |
}, | |
weeks: { | |
get() { | |
return (this * 7).days; | |
}, | |
}, | |
week: { | |
get() { | |
return this.weeks; | |
}, | |
}, | |
months: { | |
get() { | |
return new Period({ | |
value: this, | |
func: 'Month', | |
}); | |
}, | |
}, | |
month: { | |
get() { | |
return this.months; | |
}, | |
}, | |
years: { | |
get() { | |
return new Period({ | |
value: this, | |
func: 'FullYear', | |
}); | |
}, | |
}, | |
year: { | |
get() { | |
return this.years; | |
}, | |
}, | |
decades: { | |
get() { | |
return (this * 10).years; | |
}, | |
}, | |
decade: { | |
get() { | |
return this.decades; | |
}, | |
}, | |
centuries: { | |
get() { | |
return (this * 100).years; | |
}, | |
}, | |
century: { | |
get() { | |
return this.centuries; | |
}, | |
}, | |
millennia: { | |
get() { | |
return (this * 1000).years; | |
}, | |
}, | |
millenniums: { | |
get() { | |
return this.millennia; | |
}, | |
}, | |
millennium: { | |
get() { | |
return this.millennia; | |
}, | |
}, | |
ages: { | |
get() { | |
return (this * 2160).years; | |
}, | |
}, | |
age: { | |
get() { | |
return this.ages; | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment