Skip to content

Instantly share code, notes, and snippets.

@icetbr
Created April 28, 2021 15:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icetbr/5896f7776ba1c67ed26e1f002c302274 to your computer and use it in GitHub Desktop.
Save icetbr/5896f7776ba1c67ed26e1f002c302274 to your computer and use it in GitHub Desktop.
class Period {
constructor(year, month) {
this.year = year;
this.month = month;
}
next() {
return this.month + 1 > 11
? new Period(this.year + 1, 0)
: new Period(this.year, this.month + 1)
}
previous() {
return this.month - 1 < 0
? new Period(this.year - 1, 11)
: new Period(this.year, this.month - 1)
}
static parse(YYYY_MM) {
const [year, month] = YYYY_MM.split('-').map(Number);
return new Period(year, month - 1);
}
}
// const Period = require('./Period')
// const p1 = new Period(2017, 10)
// const p2 = new Period(2018, 9)
// console.log(p1.next())
const Period = (year, month) => ({
year,
month,
parse(YYYY_MM) {
const [year, month] = YYYY_MM.split('-').map(Number);
return Period(year, month - 1);
},
next: () => month + 1 > 11
? Period(year + 1, 0)
: Period(year, month + 1),
previous: () => month - 1 < 0
? Period(year - 1, 11)
: Period(year, month - 1),
});
// const Period = require('./Period')
// const p1 = Period(2017, 10)
// const p2 = Period(2018, 9)
// console.log(p1.next())
const Period = (year, month) => ({ year, month });
const Period1 = {
next: ({ year, month }) => month + 1 > 11
? Period(year + 1, 0)
: Period(year, month + 1),
previous: ({ year, month }) => month - 1 < 0
? Period(year - 1, 11)
: Period(year, month - 1),
parse(YYYY_MM) {
const [year, month] = YYYY_MM.split('-').map(Number);
return Period(year, month - 1);
},
};
// const { nextPeriod: next } = require('./Period')
// const p1 = Period(2017, 10)
// const p2 = Period(2018, 9)
// console.log(nextPeriod(p1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment