Created
April 28, 2021 15:26
-
-
Save icetbr/5896f7776ba1c67ed26e1f002c302274 to your computer and use it in GitHub Desktop.
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
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()) |
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
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()) |
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
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