Sha256: 19036d82908ac385bf800868a08a289301ea3e6118118a82d663ae7336c464df

Contents?: true

Size: 1.31 KB

Versions: 181

Compression:

Stored size: 1.31 KB

Contents

export default function meetupDay (year, month, dayOfWeek, which) {

  let candidates = getCandidates(year, month, dayOfWeek);
  let d, i, res;

  which = which.toLowerCase();

  if (which === 'teenth') {
    res = find(candidates, 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, dayOfWeek) {
  let 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(dayOfWeek)) {
      res.push(d);
    }
  }

  return res;
}

function getDayIndex (day) {
  const 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 (let i = 0; i < ary.length; i++) {
    if (callback(ary[i], i, ary)) { return ary[i]; }
  }
}

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

Version data entries

181 entries across 181 versions & 1 rubygems

Version Path
trackler-2.1.0.25 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.24 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.23 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.22 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.21 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.20 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.19 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.18 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.17 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.16 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.15 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.14 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.13 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.12 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.11 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.10 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.9 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.8 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.7 tracks/ecmascript/exercises/meetup/example.js
trackler-2.1.0.6 tracks/ecmascript/exercises/meetup/example.js