Skip to content

Commit

Permalink
add isValidDateArg
Browse files Browse the repository at this point in the history
  • Loading branch information
itmor committed May 29, 2020
1 parent a316171 commit 5117cf8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion build/calendar-array.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 35 additions & 1 deletion src/calendar-array.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
class CalendarArray {
getCalendar(...dates) {
if (this.#isValidDateArg(dates)) {
console.log('is valid');
}
}

/*
Validates getCalendar arguments.
- sample correct arguments
[[2020, 3], [2012], [2007]]
*/
#isValidDateArg(dates) {
if (dates.length > 0) {
for (let date of dates) {

if (!Array.isArray(date)) {
throw new TypeError(`type: ${typeof date} <- the argument is of the wrong type, array expected`);

} else if (date.length === 0) {
throw new Error(`length: ${date.length} <- array is empty`);
} else {

for (let value of date) {
if (typeof value !== 'number') {
throw new TypeError(`type: ${typeof value} <- invalid date type in this array of dates, expected number`);
}
}
}
}
} else {
throw new Error('Empty args array, pass an array with a date');
}

return true;
}
}

// export globals
if (typeof module !== 'undefined') {
module.exports = new CalendarArray();
} else {
window.CalendarArray = new CalendarArray();
window.calendarArray = new CalendarArray();
}

0 comments on commit 5117cf8

Please sign in to comment.