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.0.6.5 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.6.4 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.6.3 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.6.2 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.6.1 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.6.0 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.18 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.17 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.16 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.15 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.14 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.13 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.12 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.11 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.10 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.9 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.8 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.7 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.6 tracks/ecmascript/exercises/meetup/example.js
trackler-2.0.5.5 tracks/ecmascript/exercises/meetup/example.js