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.1.0.19 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.18 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.17 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.16 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.15 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.14 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.13 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.12 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.11 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.10 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.9 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.8 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.7 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.6 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.5 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.4 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.3 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.2 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.1 tracks/javascript/exercises/meetup/example.js
trackler-2.1.0.0 tracks/javascript/exercises/meetup/example.js