Skip to content

Commit

Permalink
Tweaks to week validator
Browse files Browse the repository at this point in the history
  • Loading branch information
franleplant committed Oct 15, 2016
1 parent 35d4dd6 commit 31bb727
Showing 1 changed file with 2 additions and 4 deletions.
6 changes: 2 additions & 4 deletions src/officialValidators/week.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { parseWeek } from '../utils';

// Example week: "2016-W33"
function weekValidator(control) {
export default function weekValidator(control) {
// Special case for empty values. This is the job of the `required` validator
if (!control.value) {
return false;
Expand All @@ -19,13 +19,11 @@ function weekValidator(control) {
return !(0 < year && 1 <= week && week <= weeksInYear(year));
}

function weeksInYear(year) {
export 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}

0 comments on commit 31bb727

Please sign in to comment.