Skip to content

Commit

Permalink
fixed calculation of dates
Browse files Browse the repository at this point in the history
  • Loading branch information
pjnovas committed Sep 11, 2016
1 parent b8c4c42 commit 03fa18c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
11 changes: 6 additions & 5 deletions lib/loaders/calculateDays.js
@@ -1,12 +1,13 @@
import moment from 'moment';

const week = [,'monday','tuesday','wednesday','thursday','friday','saturday','sunday'];
const positions = ['first', 'second', 'third'];

const getDate = (key, month, year) => {
const date = moment(`01/${month}/${year}`, 'DD/MM/YYYY');
var date = new Date(year, month, 7);
let [ pos, weekDay ] = key.split('_');
return date.day(week.indexOf(weekDay) + (7 * positions.indexOf(pos))).date();
date.setDate(
(7 * positions.indexOf(pos)) + (week.indexOf(weekDay) - date.getDay())
);
return date.getDate();
};

export default function(year, yearNumber) {
Expand All @@ -15,7 +16,7 @@ export default function(year, yearNumber) {
if (month.calculated){ // has calculations

Object.keys(month.calculated).forEach( key => {
const day = getDate(key, mIdx+1, yearNumber);
const day = getDate(key, mIdx, yearNumber);
const value = month.calculated[key];

if (month[day]){
Expand Down
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -32,8 +32,7 @@
"install": "^0.8.1",
"joi": "^9.0.4",
"lodash": "^4.15.0",
"boom": "^3.2.2",
"moment": "^2.14.1"
"boom": "^3.2.2"
},
"devDependencies": {
"babel-eslint": "^6.1.2",
Expand All @@ -45,6 +44,7 @@
"mocha": "^3.0.2",
"nyc": "^8.1.0",
"sinon": "^1.17.5",
"sinon-chai": "^2.8.0"
"sinon-chai": "^2.8.0",
"moment": "^2.14.1"
}
}
9 changes: 4 additions & 5 deletions test/loaders/calculateDays.js
Expand Up @@ -6,18 +6,17 @@ const expect = chai.expect;
describe('#calculateDays', () => {

it('must calculate days and fill dynamic dates', () => {
// TODO: Check "third_thursday" > not working!
const year = 2016;
const from = [{},{},{},{
// 'abril',
'calculated': {
'first_sunday': 'Día de la locura (primer domingo de abril)',
'second_saturday': 'Día de la vida (segundo sabado de abril)',
'third_saturday': 'Día de la magía misma (tercer sabado de abril)'
'third_thursday': 'Día de la magía misma (tercer jueves de abril)'
},
'3': 'Día epico magico',
'7': 'Día mundial de la fantasia',
'16': [
'14': [
'Día A',
'Día B'
]
Expand All @@ -30,10 +29,10 @@ describe('#calculateDays', () => {
],
'7': 'Día mundial de la fantasia',
'9': 'Día de la vida (segundo sabado de abril)',
'16': [
'14': [
'Día A',
'Día B',
'Día de la magía misma (tercer sabado de abril)'
'Día de la magía misma (tercer jueves de abril)'
]
}];

Expand Down

0 comments on commit 03fa18c

Please sign in to comment.