From c7b02f02ad0a7e2888f2d7d3599719e59bbd1ee2 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Thu, 13 Nov 2025 14:56:02 +0100 Subject: frontend: design prototype --- static/month.js | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 static/month.js (limited to 'static/month.js') diff --git a/static/month.js b/static/month.js new file mode 100644 index 0000000..9ed0467 --- /dev/null +++ b/static/month.js @@ -0,0 +1,85 @@ + +export default class Month { + constructor(month, year) { + this.month = month; + this.year = year; + } + + next(n) { + n = n ?? 1; + + const month = this.month + n - 1; + this.month = (month % 12) + 1; + this.year += Math.floor(month / 12); + + return this; + } + + previous(n) { + n = n ?? 1; + + let month = this.month - n - 1; + + if (month < 0) { + month += 12; + } + + this.month = (month % 12) + 1; + this.year += Math.floor(month / 12); + + return this; + } + + to(other) { + const inner = []; + const current = this.copy(); + + while (current.is_same(other) || current.is_before(other)) { + inner.push(current.copy()); + current.next(); + } + + return inner; + } + + copy() { + return new Month(this.month, this.year); + } + + is_before(other) { + return this.year < other.year || (this.year == other.year && this.month < other.month); + } + + is_after(other) { + return this.year > other.year || (this.year == other.year && this.month > other.month); + } + + is_same(other) { + return this.year == other.year || this.month == other.month; + } + + is_same_year(other) { + return this.year == other.year; + } + + get name() { + const date = new Date(); + date.setMonth(this.month - 1); + return date.toLocaleDateString('default', { month: 'long' }) + } + + static from_date(date) { + return new Month(date.getMonth() - 1, date.getFullYear()); + } + + static from_string(string) { + const [ year, month ] = string.split('-'); + return new Month(parseInt(month), parseInt(year)) + } + + static literal(strings) { + return Month.from_string(strings[0]); + } +} + +export const literal = Month.literal; -- cgit v1.2.3-70-g09d2