Skip to content

Commit

Permalink
Merge 5a5bf61 into 4539f9f
Browse files Browse the repository at this point in the history
  • Loading branch information
babruix committed Oct 14, 2016
2 parents 4539f9f + 5a5bf61 commit 67834a5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/officialValidators/week.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,29 @@
import { parseWeek } from '../utils';

// Example week: "2016-W33"
// TODO: this needs to be improved since some years have 52 weeks
const MAX_WEEK = 53
export default function weekValidator(control) {
function weekValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
}

const value = control.value
const [year, week] = parseWeek(value)
const value = control.value;
const [year, week] = parseWeek(value);

if (!year || !week) {
return true
return true;
}

return !(0 < year && 1 <= week && week <= MAX_WEEK)
return !(0 < year && 1 <= week && week <= weeksInYear(year));
}

function weeksInYear(year) {
const d = new Date(year, 0, 1);
const isLeap = new Date(year, 1, 29).getMonth() === 1;

// Check for a Jan 1 that's a Thursday or a leap year that has a
// Wednesday jan 1. Otherwise year has 52 weeks.
return d.getDay() === 4 || isLeap && d.getDay() === 3 ? 53 : 52
}

export {weekValidator as default, weeksInYear}
17 changes: 17 additions & 0 deletions src/officialValidators/week.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { weeksInYear, getWeekNumber } from './week';



describe('weeksInYear', () => {
it(`should return 52 for year 2014 `, () => {
expect(weeksInYear(2014)).toBe(52);
});

it(`should return 53 for year 2015 `, () => {
expect(weeksInYear(2015)).toBe(53);
});

it(`should return 52 for year 2016 `, () => {
expect(weeksInYear(2016)).toBe(52);
});
});

0 comments on commit 67834a5

Please sign in to comment.