|
import DateFormattingStrategy from './strategies/DateFormattingStrategy'; |
|
import MomentFormatFormattingStrategy from './strategies/MomentFormatFormattingStrategy'; |
|
import WeekRangeFormattingStrategy from './strategies/WeekRangeFormattingStrategy'; |
|
|
|
export default class DateFormatter() { |
|
static FULL_HOUR = new MomentFormatFormattingStrategy('HH:00'); |
|
static HOUR_MINUTE = new MomentFormatFormattingStrategy('HH:mm'); |
|
|
|
static DAY_MONTH = new MomentFormatFormattingStrategy('DD.MM'); |
|
static LONG_DAY_MONTH = new MomentFormatFormattingStrategy('dddd DD.MM'); |
|
|
|
static WEEK_DAY = new MomentFormatFormattingStrategy('dddd'); |
|
static LONG_WEEK_DAY = new MomentFormatFormattingStrategy('dddd'); |
|
static SHORT_WEEK_DAY = new MomentFormatFormattingStrategy('ddd'); |
|
|
|
static WEEK_NUMBER = new MomentFormatFormattingStrategy('[KW]W'); |
|
static WEEK_RANGE = new WeekRangeFormattingStrategy(); |
|
|
|
static MONTH_YEAR = new MomentFormatFormattingStrategy('MMM YY'); |
|
|
|
/** |
|
* @param {*} [date] |
|
* @return {moment} |
|
*/ |
|
_parseDate(date) { |
|
return moment(date || new Date()); |
|
}; |
|
|
|
/** |
|
* @param {string} format |
|
* @return {function} |
|
*/ |
|
createCallback(format) { |
|
return this.format.bind(this, format); |
|
}; |
|
|
|
/** |
|
* @param {DateFormattingStrategy} strategy |
|
* @return {function} |
|
*/ |
|
createCallbackUsing(strategy) { |
|
return this.formatUsing.bind(this, strategy); |
|
}; |
|
|
|
/** |
|
* @param {string} formatString |
|
* @param {Date|string|moment} date |
|
* @return {string} |
|
*/ |
|
format(formatString, date) { |
|
return this._parseDate(date).format(formatString); |
|
}; |
|
|
|
/** |
|
* @param {DateFormattingStrategy} strategy |
|
* @param {moment} date |
|
* @return {string} |
|
* @throws {TypeError} |
|
*/ |
|
formatUsing(strategy, date) { |
|
if (strategy instanceof DateFormattingStrategy) { |
|
return strategy.format(this._parseDate(date)); |
|
} |
|
|
|
throw new TypeError( |
|
'`strategy` must be an instance of DateFormattingStrategy' |
|
); |
|
}; |
|
} |