Last active
May 15, 2018 10:12
-
-
Save christiaanwesterbeek/67afdaef3533b6e575016350eb95f32d to your computer and use it in GitHub Desktop.
Calculating and testing the last iso week of years
This file contains hidden or 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
import tape from 'tape' | |
import moment from 'moment' | |
const getLastIsoWeekOfYear = year => | |
moment() | |
.isoWeek(30) | |
.isoWeekYear(year + 1) | |
.isoWeek(1) | |
.isoWeekday(1) | |
.add(-1, 'days') | |
.isoWeek() | |
tape('correctly counting 52 and 53 iso weeks in years', t => { | |
// List from https://en.wikipedia.org/wiki/ISO_week_date | |
const yearsWith53Weeks = [ | |
4, 9, 15, 20, 26, | |
32, 37, 43, 48, 54, | |
60, 65, 71, 76, 82, | |
88, 93, 99, 105, 111, 116, 122, | |
128, 133, 139, 144, 150, | |
156, 161, 167, 172, 178, | |
184, 189, 195, 201, 207, 212, 218, | |
224, 229, 235, 240, 246, | |
252, 257, 263, 268, 274, | |
280, 285, 291, 296, 303, 308, 314, | |
320, 325, 331, 336, 342, | |
348, 353, 359, 364, 370, | |
376, 381, 387, 392, 398 | |
].map(year => year + 2000) | |
const yearsWith52Weeks = [...Array(400)] | |
.map((v, index) => index + 2000) | |
.filter(year => !yearsWith53Weeks.includes(year)) | |
t.plan(400) | |
yearsWith53Weeks.forEach(year => { | |
t.equal(getLastIsoWeekOfYear(year), 53, `Year ${year} has 53 weeks`) | |
}) | |
yearsWith52Weeks.forEach(year => { | |
t.equal(getLastIsoWeekOfYear(year), 52, `Year ${year} has 52 weeks`) | |
}) | |
t.end() | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment