UNPKG

md2

Version:

Angular2 based Material Design components, directives and services are Accordion, Autocomplete, Chips(Tags), Collapse, Colorpicker, Data Table, Datepicker, Dialog(Modal), Menu, Multiselect, Select, Tabs, Tags(Chips), Toast and Tooltip.

614 lines 25.4 kB
import { DateLocale } from './date-locale'; var DateUtil = (function () { function DateUtil() { this._locale = new DateLocale(); this.parseDateMap = { 'y': 0, 'Y': [0, -2000], 'M': [1, 1], 'n': [1, this._locale.getMonthNames('short')], 'N': [1, this._locale.getMonthNames('long')], 'd': 2, 'm': 4, 'H': 3, 'h': 3, 'K': [3, 1], 'k': [3, 1], 's': 5, 'S': 6, 'a': [3, ['am', 'pm']], 'A': [3, ['AM', 'PM']] }; } DateUtil.prototype.replace = function (s, regexp, sub) { return (s != null ? '' + s : '').replace(regexp, sub != null ? sub : ''); }; DateUtil.prototype.startsWith = function (base, start) { return start != null && base.substr(0, start.length) == start; }; DateUtil.prototype.isType = function (s, o) { return typeof s == o; }; DateUtil.prototype.isFunction = function (f) { return this.isType(f, 'function'); }; DateUtil.prototype.isList = function (v) { return !!v && v.length != null && !this.isString(v) && !this.isNode(v) && !this.isFunction(v); }; DateUtil.prototype.isString = function (s) { return this.isType(s, 'string'); }; DateUtil.prototype.isObject = function (f) { return !!f && this.isType(f, 'object'); }; DateUtil.prototype.isNode = function (n) { return n && n['nodeType']; }; DateUtil.prototype.isNumber = function (n) { return this.isType(n, 'number'); }; DateUtil.prototype.getFindFunc = function (findFunc) { return this.isFunction(findFunc) ? findFunc : function (obj, index) { if (findFunc === obj) { return index; } }; }; DateUtil.prototype.getFindIndex = function (list, index, defaultIndex) { return index == null ? defaultIndex : index < 0 ? Math.max(list.length + index, 0) : Math.min(list.length, index); }; DateUtil.prototype.find = function (list, findFunc, startIndex, endIndex) { var f = this.getFindFunc(findFunc); var e = this.getFindIndex(list, endIndex, list.length); var r; for (var i = this.getFindIndex(list, startIndex, 0); i < e; i++) { if ((r = f.call(list, list[i], i)) != null) { return r; } } }; DateUtil.prototype.parseDate = function (date, fmt) { var _this = this; var indexMap = {}; // contains reGroupPosition -> typeLetter or [typeLetter, value array] var reIndex = 1; var timezoneOffsetMatch; var timezoneIndex; var match; var format = this.replace(fmt, /^\?/); if (format != fmt && !this.replace(date, /^\s+|\s+$/g)) { return null; } if (match = /^\[([+-])(\d\d)(\d\d)\]\s*(.*)/.exec(format)) { timezoneOffsetMatch = match; format = match[4]; } var parser = new RegExp(format.replace(/(.)(\1*)(?:\[([^\]]*)\])?/g, function (wholeMatch, placeholderChar, placeholderDigits, param) { if (/[dmhkyhs]/i.test(placeholderChar)) { indexMap[reIndex++] = placeholderChar; var plen = placeholderDigits.length + 1; return '(\\d' + (plen < 2 ? '+' : ('{1,' + plen + '}')) + ')'; } else if (placeholderChar == 'z') { timezoneIndex = reIndex; reIndex += 3; return '([+-])(\\d\\d)(\\d\\d)'; } else if (/[NnaA]/.test(placeholderChar)) { indexMap[reIndex++] = [placeholderChar, param && param.split(',')]; return '([a-zA-Z\\u0080-\\u1fff]+)'; } else if (/w/i.test(placeholderChar)) { return '[a-zA-Z\\u0080-\\u1fff]+'; } else if (/\s/.test(placeholderChar)) { return '\\s+'; } else { return _this.replace(wholeMatch, /[\\\[\]\/{}()*+?.$|^-]/g, '\\$&'); } })); if (!(match = parser.exec(date))) { return undefined; } var ctorArgs = [0, 0, 0, 0, 0, 0, 0]; var _loop_1 = function (i) { var matchVal = match[i]; var indexEntry = indexMap[i]; if (this_1.isList(indexEntry)) { var placeholderChar = indexEntry[0]; var mapEntry = this_1.parseDateMap[placeholderChar]; var ctorIndex = mapEntry[0]; var valList = indexEntry[1] || mapEntry[1]; var listValue = this_1.find(valList, function (v, index) { if (_this.startsWith(matchVal.toLowerCase(), v.toLowerCase())) { return index; } }); if (listValue == null) { return { value: undefined }; } if (placeholderChar == 'a' || placeholderChar == 'A') { ctorArgs[ctorIndex] += listValue * 12; } else { ctorArgs[ctorIndex] = listValue; } } else if (indexEntry) { var value = parseFloat(matchVal); var mapEntry = this_1.parseDateMap[indexEntry]; if (this_1.isList(mapEntry)) { ctorArgs[mapEntry[0]] += value - mapEntry[1]; } else { ctorArgs[mapEntry] += value; } } }; var this_1 = this; for (var i = 1; i < reIndex; i++) { var state_1 = _loop_1(i); if (typeof state_1 === "object") return state_1.value; } var d = new Date(ctorArgs[0], ctorArgs[1], ctorArgs[2], ctorArgs[3], ctorArgs[4], ctorArgs[5], ctorArgs[6]); return d; }; DateUtil.prototype.today = function () { return new Date(); }; DateUtil.prototype.parse = function (value) { var timestamp = typeof value == 'number' ? value : Date.parse(value); return isNaN(timestamp) ? null : new Date(timestamp); }; DateUtil.prototype.getYear = function (date) { return date.getFullYear(); }; DateUtil.prototype.getMonth = function (date) { return date.getMonth(); }; DateUtil.prototype.getDate = function (date) { return date.getDate(); }; DateUtil.prototype.getHours = function (date) { return date.getHours(); }; DateUtil.prototype.getMinutes = function (date) { return date.getMinutes(); }; DateUtil.prototype.getSeconds = function (date) { return date.getSeconds(); }; DateUtil.prototype.createDate = function (year, month, date, hours, minutes, seconds) { // Check for invalid month and date (except upper bound on date which we have to check after // creating the Date). if (month < 0 || month > 11 || date < 1) { return null; } var result = this._createDateWithOverflow(year, month, date, hours, minutes, seconds); // Check that the date wasn't above the upper bound for the month, causing the month to // overflow. if (result.getMonth() != month) { return null; } return result; }; DateUtil.prototype.clone = function (date) { return this.createDate(this.getYear(date), this.getMonth(date), this.getDate(date), this.getHours(date), this.getMinutes(date), this.getSeconds(date)); }; DateUtil.prototype.getNumDaysInMonth = function (date) { return this.getDate(this._createDateWithOverflow(this.getYear(date), this.getMonth(date) + 1, 0, 0, 0, 0)); }; DateUtil.prototype.addCalendarYears = function (date, years) { return this.addCalendarMonths(date, years * 12); }; DateUtil.prototype.addCalendarMonths = function (date, months) { var newDate = this._createDateWithOverflow(this.getYear(date), this.getMonth(date) + months, this.getDate(date), this.getHours(date), this.getMinutes(date), this.getSeconds(date)); // It's possible to wind up in the wrong month if the original month has more days than the new // month. In this case we want to go to the last day of the desired month. // Note: the additional + 12 % 12 ensures we end up with a positive number, since JS % doesn't // guarantee this. if (this.getMonth(newDate) != ((this.getMonth(date) + months) % 12 + 12) % 12) { newDate = this._createDateWithOverflow(this.getYear(newDate), this.getMonth(newDate), 0, this.getHours(newDate), this.getMinutes(newDate), this.getSeconds(newDate)); } return newDate; }; DateUtil.prototype.addCalendarDays = function (date, days) { return this._createDateWithOverflow(this.getYear(date), this.getMonth(date), this.getDate(date) + days, this.getHours(date), this.getMinutes(date), this.getSeconds(date)); }; DateUtil.prototype.addCalendarHours = function (date, hours) { return this._createDateWithOverflow(this.getYear(date), this.getMonth(date), this.getDate(date), this.getHours(date) + hours, this.getMinutes(date), this.getSeconds(date)); }; DateUtil.prototype.addCalendarMinutes = function (date, minutes) { return this._createDateWithOverflow(this.getYear(date), this.getMonth(date), this.getDate(date), this.getHours(date), this.getMinutes(date) + minutes, this.getSeconds(date)); }; DateUtil.prototype.getISODateString = function (date) { return [ date.getUTCFullYear(), this._2digit(date.getUTCMonth() + 1), this._2digit(date.getUTCDate()) ].join('-'); }; /** Creates a date but allows the month and date to overflow. */ DateUtil.prototype._createDateWithOverflow = function (year, month, date, hours, minutes, seconds) { var result = new Date(year, month, date, hours, minutes, seconds); // We need to correct for the fact that JS native Date treats years in range [0, 99] as // abbreviations for 19xx. if (year >= 0 && year < 100) { result.setFullYear(this.getYear(result) - 1900); } return result; }; /** * Pads a number to make it two digits. * @param n The number to pad. * @returns The padded number. */ DateUtil.prototype._2digit = function (n) { return ('00' + n).slice(-2); }; DateUtil.prototype.compareDate = function (first, second) { return this.getYear(first) - this.getYear(second) || this.getMonth(first) - this.getMonth(second) || this.getDate(first) - this.getDate(second); }; /** * Gets the first day of the month for the given date's month. * @param {Date} date * @returns {Date} */ DateUtil.prototype.getFirstDateOfWeek = function (date, firstDayOfWeek) { var day = date.getDate() - ((7 + date.getDay() - firstDayOfWeek) % 7); return new Date(date.getFullYear(), date.getMonth(), day, date.getHours(), date.getMinutes()); }; /** * Gets the first day of the month for the given date's month. * @param {Date} date * @returns {Date} */ DateUtil.prototype.getFirstDateOfMonth = function (date) { return new Date(date.getFullYear(), date.getMonth(), 1); }; /** * Gets the number of days in the month for the given date's month. * @param date * @returns {number} */ DateUtil.prototype.getNumberOfDaysInMonth = function (date) { return new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate(); }; /** * Get an arbitrary date in the month after the given date's month. * @param date * @returns {Date} */ DateUtil.prototype.getDateInNextMonth = function (date) { return new Date(date.getFullYear(), date.getMonth() + 1, 1, date.getHours(), date.getMinutes()); }; /** * Get an arbitrary date in the month before the given date's month. * @param date * @returns {Date} */ DateUtil.prototype.getDateInPreviousMonth = function (date) { return new Date(date.getFullYear(), date.getMonth() - 1, 1, date.getHours(), date.getMinutes()); }; /** * Gets whether two dates have the same year. * @param {Date} d1 * @param {Date} d2 * @returns {boolean} */ DateUtil.prototype.isSameYear = function (d1, d2) { return d1 && d2 && d1.getFullYear() === d2.getFullYear(); }; /** * Gets whether two dates have the same month and year. * @param {Date} d1 * @param {Date} d2 * @returns {boolean} */ DateUtil.prototype.isSameMonthAndYear = function (d1, d2) { return d1 && d2 && d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth(); }; /** * Gets whether two dates are the same day (not not necesarily the same time). * @param {Date} d1 * @param {Date} d2 * @returns {boolean} */ DateUtil.prototype.isSameDay = function (d1, d2) { return d1 && d2 && d1.getDate() == d2.getDate() && this.isSameMonthAndYear(d1, d2); }; /** * Gets whether two dates are the same hours. * @param {Date} d1 * @param {Date} d2 * @returns {boolean} */ DateUtil.prototype.isSameHour = function (d1, d2) { return d1 && d2 && d1.getHours() == d2.getHours() && this.isSameDay(d1, d2); }; /** * Gets whether two dates are the same minutes. * @param {Date} d1 * @param {Date} d2 * @returns {boolean} */ DateUtil.prototype.isSameMinute = function (d1, d2) { return d1 && d2 && d1.getMinutes() == d2.getMinutes() && this.isSameHour(d1, d2); }; /** * Gets whether a date is in the month immediately after some date. * @param {Date} startDate The date from which to compare. * @param {Date} endDate The date to check. * @returns {boolean} */ DateUtil.prototype.isInNextMonth = function (startDate, endDate) { var nextMonth = this.getDateInNextMonth(startDate); return this.isSameMonthAndYear(nextMonth, endDate); }; /** * Gets whether a date is in the month immediately before some date. * @param {Date} startDate The date from which to compare. * @param {Date} endDate The date to check. * @returns {boolean} */ DateUtil.prototype.isInPreviousMonth = function (startDate, endDate) { var previousMonth = this.getDateInPreviousMonth(startDate); return this.isSameMonthAndYear(endDate, previousMonth); }; /** * Gets the midpoint between two dates. * @param {Date} d1 * @param {Date} d2 * @returns {Date} */ DateUtil.prototype.getDateMidpoint = function (d1, d2) { return this.createDateAtMidnight((d1.getTime() + d2.getTime()) / 2); }; /** * Gets the week of the month that a given date occurs in. * @param {Date} date * @returns {number} Index of the week of the month (zero-based). */ DateUtil.prototype.getWeekOfMonth = function (date) { var firstDayOfMonth = this.getFirstDateOfMonth(date); return Math.floor((firstDayOfMonth.getDay() + date.getDate() - 1) / 7); }; /** * Gets a new date incremented by the given number of minutes. Number of minutes can be negative. * @param {Date} date * @param {number} numberOfMinutes * @returns {Date} */ DateUtil.prototype.incrementMinutes = function (date, numberOfMinutes) { return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours(), date.getMinutes() + numberOfMinutes); }; /** * Gets a new date incremented by the given number of hours. Number of hours can be negative. * @param {Date} date * @param {number} numberOfHours * @returns {Date} */ DateUtil.prototype.incrementHours = function (date, numberOfHours) { return new Date(date.getFullYear(), date.getMonth(), date.getDate(), date.getHours() + numberOfHours, date.getMinutes()); }; /** * Gets a new date incremented by the given number of days. Number of days can be negative. * @param {Date} date * @param {number} numberOfDays * @returns {Date} */ DateUtil.prototype.incrementDays = function (date, numberOfDays) { return new Date(date.getFullYear(), date.getMonth(), date.getDate() + numberOfDays, date.getHours(), date.getMinutes()); }; /** * Gets a new date incremented by the given number of months. Number of months can be negative. * If the date of the given month does not match the target month, the date will be set to the * last day of the month. * @param {Date} date * @param {number} numberOfMonths * @returns {Date} */ DateUtil.prototype.incrementMonths = function (date, numberOfMonths) { // If the same date in the target month does not actually exist, the Date object will // automatically advance *another* month by the number of missing days. // For example, if you try to go from Jan. 30 to Feb. 30, you'll end up on March 2. // So, we check if the month overflowed and go to the last day of the target month instead. var dateInTargetMonth = new Date(date.getFullYear(), date.getMonth() + numberOfMonths, 1, date.getHours(), date.getMinutes()); var numberOfDaysInMonth = this.getNumberOfDaysInMonth(dateInTargetMonth); if (numberOfDaysInMonth < date.getDate()) { dateInTargetMonth.setDate(numberOfDaysInMonth); } else { dateInTargetMonth.setDate(date.getDate()); } return dateInTargetMonth; }; /** * Get the integer distance between two months. This *only* considers the month and year * portion of the Date instances. * * @param {Date} start * @param {Date} end * @returns {number} Number of months between `start` and `end`. If `end` is before `start` * chronologically, this number will be negative. */ DateUtil.prototype.getMonthDistance = function (start, end) { return (12 * (end.getFullYear() - start.getFullYear())) + (end.getMonth() - start.getMonth()); }; /** * Gets the last day of the month for the given date. * @param {Date} date * @returns {Date} */ DateUtil.prototype.getLastDateOfMonth = function (date) { return new Date(date.getFullYear(), date.getMonth(), this.getNumberOfDaysInMonth(date), date.getHours(), date.getMinutes()); }; /** * Checks whether a date is valid. * @param {Date} date * @return {boolean} Whether the date is a valid Date. */ DateUtil.prototype.isValidDate = function (date) { return date != null && date.getTime && !isNaN(date.getTime()); }; /** * Sets a date's time to midnight. * @param {Date} date */ DateUtil.prototype.setDateTimeToMidnight = function (date) { if (this.isValidDate(date)) { date.setHours(0, 0, 0, 0); } }; /** * Creates a date with the time set to midnight. * Drop-in replacement for two forms of the Date constructor: * 1. No argument for Date representing now. * 2. Single-argument value representing number of seconds since Unix Epoch * or a Date object. * @param {number|Date=} value * @return {Date} New date with time set to midnight. */ DateUtil.prototype.createDateAtMidnight = function (value) { var date; if (!value) { date = new Date(); } else { date = new Date(value); } this.setDateTimeToMidnight(date); return date; }; /** * Checks if a date is within a min and max range, ignoring the time component. * If minDate or maxDate are not dates, they are ignored. * @param {Date} date * @param {Date} minDate * @param {Date} maxDate */ DateUtil.prototype.isDateWithinRange = function (date, minDate, maxDate) { var dateAtMidnight = this.createDateAtMidnight(date); var minDateAtMidnight = this.isValidDate(minDate) ? this.createDateAtMidnight(minDate) : null; var maxDateAtMidnight = this.isValidDate(maxDate) ? this.createDateAtMidnight(maxDate) : null; return (!minDateAtMidnight || minDateAtMidnight <= dateAtMidnight) && (!maxDateAtMidnight || maxDateAtMidnight >= dateAtMidnight); }; /** * Checks if a date is within a min and max range. * If minDate or maxDate are not dates, they are ignored. * @param {Date} date * @param {Date} minDate * @param {Date} maxDate */ DateUtil.prototype.isFullDateWithinRange = function (date, minDate, maxDate) { minDate = this.isValidDate(minDate) ? minDate : null; maxDate = this.isValidDate(maxDate) ? maxDate : null; return (!minDate || minDate <= date) && (!maxDate || maxDate >= date); }; /** * Gets a new date incremented by the given number of years. Number of years can be negative. * See `incrementMonths` for notes on overflow for specific dates. * @param {Date} date * @param {number} numberOfYears * @returns {Date} */ DateUtil.prototype.incrementYears = function (date, numberOfYears) { return this.incrementMonths(date, numberOfYears * 12); }; /** * Get the integer distance between two years. This *only* considers the year portion of the * Date instances. * * @param {Date} start * @param {Date} end * @returns {number} Number of months between `start` and `end`. If `end` is before `start` * chronologically, this number will be negative. */ DateUtil.prototype.getYearDistance = function (start, end) { return end.getFullYear() - start.getFullYear(); }; /** * Clamps a date between a minimum and a maximum date. * @param {Date} date Date to be clamped * @param {Date=} minDate Minimum date * @param {Date=} maxDate Maximum date * @return {Date} */ DateUtil.prototype.clampDate = function (date, minDate, maxDate) { var boundDate = date; if (minDate && date < minDate) { boundDate = new Date(minDate.getTime()); } if (maxDate && date > maxDate) { boundDate = new Date(maxDate.getTime()); } return boundDate; }; /** * Extracts and parses the timestamp from a DOM node. * @param {HTMLElement} node Node from which the timestamp will be extracted. * @return {number} Time since epoch. */ DateUtil.prototype.getTimestampFromNode = function (node) { if (node && node.hasAttribute('data-timestamp')) { return Number(node.getAttribute('data-timestamp')); } }; /** * Checks if a month is within a min and max range, ignoring the date and time components. * If minDate or maxDate are not dates, they are ignored. * @param {Date} date * @param {Date} minDate * @param {Date} maxDate */ DateUtil.prototype.isMonthWithinRange = function (date, minDate, maxDate) { var month = date.getMonth(); var year = date.getFullYear(); return (!minDate || minDate.getFullYear() < year || minDate.getMonth() <= month) && (!maxDate || maxDate.getFullYear() > year || maxDate.getMonth() >= month); }; /** * Compares two dates. * @param first The first date to compare. * @param second The second date to compare. * @returns 0 if the dates are equal, a number less than 0 if the first date is earlier, * a number greater than 0 if the first date is later. */ DateUtil.prototype.compareDateAndTime = function (first, second) { return this.getYear(first) - this.getYear(second) || this.getMonth(first) - this.getMonth(second) || this.getDate(first) - this.getDate(second) || this.getHours(first) - this.getDate(second) || this.getMinutes(first) - this.getDate(second) || this.getSeconds(first) - this.getDate(second); }; /** * Checks if two dates are equal. * @param first The first date to check. * @param second The second date to check. * @returns {boolean} Whether the two dates are equal. * Null dates are considered equal to other null dates. */ DateUtil.prototype.sameDate = function (first, second) { return first && second ? !this.compareDate(first, second) : first == second; }; /** * Checks if two dates are equal. * @param first The first date to check. * @param second The second date to check. * @returns {boolean} Whether the two dates are equal. * Null dates are considered equal to other null dates. */ DateUtil.prototype.sameDateAndTime = function (first, second) { return first && second ? !this.compareDateAndTime(first, second) : first == second; }; return DateUtil; }()); export { DateUtil }; //# sourceMappingURL=date-util.js.map