Sha256: d2b78a6e13a9fd9889ac17f441ac1616433ecce38ea0901caf927e76745f04fa

Contents?: true

Size: 1.37 KB

Versions: 255

Compression:

Stored size: 1.37 KB

Contents

function MeetupDayException(message) {
  this.message = message;
  this.name = 'MeetupDayException';
}

function meetupDay (year, month, day_of_week, which) {
  'use strict';

  var candidates = _getCandidates(year, month, day_of_week),
      d,
      i,
      res;
  which = which.toLowerCase();

  if (which === 'teenth') {
    res = _find(candidates, function (d) {
      return 13 <= d.getDate() && d.getDate() <= 19;
    });
  }
  else if (which === 'last') {
    res = candidates.pop();
  }
  else {
    which = parseInt(which) - 1;
    res = candidates.slice(which, which + 1).pop();
  }

  if (!res) { throw new MeetupDayException('Day not found! ;_;'); }

  return res;
}

function _getCandidates (year, month, day_of_week) {
  var d,
      i,
      numDaysInMonth = new Date(year, month + 1, 0).getDate(),
      res = [];

  for (i = 0; i < numDaysInMonth; i++) {
    d = new Date(year, month, i + 1);

    if (d.getDay() === _getDayIndex(day_of_week)) {
      res.push(d);
    }
  }

  return res;
}

function _getDayIndex (day) {
  var daysInd = {
    'sunday': 0,
    'monday': 1,
    'tuesday': 2,
    'wednesday': 3,
    'thursday': 4,
    'friday': 5,
    'saturday': 6
  };

  day = day.toLowerCase();

  return daysInd[day];
}

function _find(ary, callback) {
  for (var i = 0; i < ary.length; i++) {
    if (callback(ary[i], i, ary)) { return ary[i]; }
  }
}

module.exports = meetupDay;

Version data entries

255 entries across 255 versions & 1 rubygems

Version Path
trackler-2.2.1.37 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.36 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.35 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.34 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.33 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.32 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.31 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.30 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.29 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.28 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.27 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.26 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.25 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.24 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.23 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.22 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.21 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.20 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.19 tracks/javascript/exercises/meetup/example.js
trackler-2.2.1.18 tracks/javascript/exercises/meetup/example.js