@woocommerce/date
Version:
WooCommerce date utilities.
298 lines • 11.7 kB
TypeScript
/**
* External dependencies
*/
import moment from 'moment';
type Query = {
[key: string]: string | undefined;
};
export declare const isoDateFormat = "YYYY-MM-DD";
export declare const defaultDateTimeFormat = "YYYY-MM-DDTHH:mm:ss";
/**
* DateValue Object
*
* @typedef {Object} DateValue - DateValue data about the selected period.
* @property {moment.Moment} primaryStart - Primary start of the date range.
* @property {moment.Moment} primaryEnd - Primary end of the date range.
* @property {moment.Moment} secondaryStart - Secondary start of the date range.
* @property {moment.Moment} secondaryEnd - Secondary End of the date range.
*/
export type DateValue = {
primaryStart: moment.Moment;
primaryEnd: moment.Moment;
secondaryStart: moment.Moment;
secondaryEnd: moment.Moment;
};
/**
* DataPickerOptions Object
*
* @typedef {Object} DataPickerOptions - Describes the date range supplied by the date picker.
* @property {string} label - The translated value of the period.
* @property {string} range - The human readable value of a date range.
* @property {moment.Moment} after - Start of the date range.
* @property {moment.Moment} before - End of the date range.
*/
export type DataPickerOptions = {
label: string;
range: string;
after: moment.Moment;
before: moment.Moment;
};
/**
* DateParams Object
*
* @typedef {Object} DateParams - date parameters derived from query parameters.
* @property {string} period - period value, ie `last_week`
* @property {string} compare - compare valuer, ie previous_year
* @param {moment.Moment|null} after - If the period supplied is "custom", this is the after date
* @param {moment.Moment|null} before - If the period supplied is "custom", this is the before date
*/
export type DateParams = {
period: string;
compare: string;
after: moment.Moment | null;
before: moment.Moment | null;
};
export declare const presetValues: {
value: string;
label: string;
}[];
export declare const periods: {
value: string;
label: string;
}[];
/**
* Adds timestamp to a string date.
*
* @param {moment.Moment} date - Date as a moment object.
* @param {string} timeOfDay - Either `start`, `now` or `end` of the day.
* @return {string} - String date with timestamp attached.
*/
export declare const appendTimestamp: (date: moment.Moment, timeOfDay: string) => string;
/**
* Convert a string to Moment object
*
* @param {string} format - localized date string format
* @param {unknown} str - date string or moment object
* @return {moment.Moment|null} - Moment object representing given string
*/
export declare function toMoment(format: string, str: unknown): moment.Moment | null;
/**
* Given two dates, derive a string representation
*
* @param {moment.Moment} after - start date
* @param {moment.Moment} before - end date
* @return {string} - text value for the supplied date range
*/
export declare function getRangeLabel(after: moment.Moment, before: moment.Moment): string;
/**
* Gets the current time in the store time zone if set.
*
* @return {string} - Datetime string.
*/
export declare function getStoreTimeZoneMoment(): moment.Moment;
/**
* Get a DateValue object for a period prior to the current period.
*
* @param {moment.DurationInputArg2} period - the chosen period
* @param {string} compare - `previous_period` or `previous_year`
* @return {DateValue} - DateValue data about the selected period
*/
export declare function getLastPeriod(period: moment.DurationInputArg2, compare: string): {
primaryStart: moment.Moment;
primaryEnd: moment.Moment;
secondaryStart: moment.Moment;
secondaryEnd: moment.Moment;
};
/**
* Get a DateValue object for a curent period. The period begins on the first day of the period,
* and ends on the current day.
*
* @param {moment.DurationInputArg2} period - the chosen period
* @param {string} compare - `previous_period` or `previous_year`
* @return {DateValue} - DateValue data about the selected period
*/
export declare function getCurrentPeriod(period: moment.DurationInputArg2, compare: string): {
primaryStart: moment.Moment;
primaryEnd: moment.Moment;
secondaryStart: moment.Moment;
secondaryEnd: moment.Moment;
};
/**
* Add default date-related parameters to a query object
*
* @param {Object} query - query object
* @param {string} query.period - period value, ie `last_week`
* @param {string} query.compare - compare value, ie `previous_year`
* @param {string} query.after - date in iso date format, ie `2018-07-03`
* @param {string} query.before - date in iso date format, ie `2018-07-03`
* @param {string} defaultDateRange - the store's default date range
* @return {DateParams} - date parameters derived from query parameters with added defaults
*/
export declare const getDateParamsFromQuery: (query: Query, defaultDateRange?: string) => DateParams;
/**
* Get Date Value Objects for a primary and secondary date range
*
* @param {Object} query - query object
* @param {string} query.period - period value, ie `last_week`
* @param {string} query.compare - compare value, ie `previous_year`
* @param {string} query.after - date in iso date format, ie `2018-07-03`
* @param {string} query.before - date in iso date format, ie `2018-07-03`
* @param {string} defaultDateRange - the store's default date range
* @return {{primary: DataPickerOptions, secondary: DataPickerOptions}} - Primary and secondary DataPickerOptions objects
*/
export declare const getCurrentDates: (query: Query, defaultDateRange?: string) => {
primary: DataPickerOptions;
secondary: DataPickerOptions;
};
/**
* Calculates the date difference between two dates. Used in calculating a matching date for previous period.
*
* @param {string} date - Date to compare
* @param {string} date2 - Secondary date to compare
* @return {number} - Difference in days.
*/
export declare const getDateDifferenceInDays: (date: moment.MomentInput, date2: moment.MomentInput) => number;
/**
* Get the previous date for either the previous period of year.
*
* @param {string} date - Base date
* @param {string} date1 - primary start
* @param {string} date2 - secondary start
* @param {string} compare - `previous_period` or `previous_year`
* @param {moment.unitOfTime.Diff} interval - interval
* @return {Object} - Calculated date
*/
export declare const getPreviousDate: (date: string, date1: string, date2: string, compare: string | undefined, interval: moment.unitOfTime.Diff | moment.DurationInputArg2) => moment.Moment;
/**
* Returns the allowed selectable intervals for a specific query.
*
* @param {Query} query Current query
* @param {string} defaultDateRange - the store's default date range
* @return {Array} Array containing allowed intervals.
*/
export declare function getAllowedIntervalsForQuery(query: Query, defaultDateRange?: string): string[];
/**
* Returns the current interval to use.
*
* @param {Query} query Current query
* @param {string} defaultDateRange - the store's default date range
* @return {string} Current interval.
*/
export declare function getIntervalForQuery(query: Query, defaultDateRange?: string): string;
/**
* Returns the current chart type to use.
*
* @param {Query} query Current query
* @param {string} query.chartType
* @return {string} Current chart type.
*/
export declare function getChartTypeForQuery({ chartType }: Query): string;
export declare const dayTicksThreshold = 63;
export declare const weekTicksThreshold = 9;
export declare const defaultTableDateFormat = "m/d/Y";
/**
* Returns d3 date formats for the current interval.
* See https://github.com/d3/d3-time-format for chart formats.
*
* @param {string} interval Interval to get date formats for.
* @param {number} [ticks] Number of ticks the axis will have.
* @return {string} Current interval.
*/
export declare function getDateFormatsForIntervalD3(interval: string, ticks?: number): {
screenReaderFormat: string;
tooltipLabelFormat: string;
xFormat: string;
x2Format: string;
tableFormat: string;
};
/**
* Returns php date formats for the current interval.
* See see https://www.php.net/manual/en/datetime.format.php.
*
* @param {string} interval Interval to get date formats for.
* @param {number} [ticks] Number of ticks the axis will have.
* @return {string} Current interval.
*/
export declare function getDateFormatsForIntervalPhp(interval: string, ticks?: number): {
screenReaderFormat: string;
tooltipLabelFormat: string;
xFormat: string;
x2Format: string;
tableFormat: string;
};
/**
* Returns date formats for the current interval.
*
* @param {string} interval Interval to get date formats for.
* @param {number} [ticks] Number of ticks the axis will have.
* @param {Object} [option] Options
* @param {string} [option.type] Date format type, d3 or php, defaults to d3.
* @return {string} Current interval.
*/
export declare function getDateFormatsForInterval(interval: string, ticks?: number, option?: {
type: string;
}): {
screenReaderFormat: string;
tooltipLabelFormat: string;
xFormat: string;
x2Format: string;
tableFormat: string;
};
/**
* Gutenberg's moment instance is loaded with i18n values, which are
* PHP date formats, ie 'LLL: "F j, Y g:i a"'. Override those with translations
* of moment style js formats.
*
* @param {Object} config Locale config object, from store settings.
* @param {string} config.userLocale
* @param {Array} config.weekdaysShort
*/
export declare function loadLocaleData({ userLocale, weekdaysShort, }: {
userLocale: string;
weekdaysShort?: moment.LocaleSpecification['weekdaysMin'];
}): void;
export declare const dateValidationMessages: {
invalid: string;
future: string;
startAfterEnd: string;
endBeforeStart: string;
};
/**
* @typedef {Object} validatedDate
* @property {Object|null} date - A resulting Moment date object or null, if invalid
* @property {string} error - An optional error message if date is invalid
*/
/**
* Validate text input supplied for a date range.
*
* @param {string} type - Designate beginning or end of range, eg `before` or `after`.
* @param {string} value - User input value
* @param {Object|null} [before] - If already designated, the before date parameter
* @param {Object|null} [after] - If already designated, the after date parameter
* @param {string} format - The expected date format in a user's locale
* @return {Object} validatedDate - validated date object
*/
export declare function validateDateInputForRange(type: string, value: string, before: moment.MomentInput | null, after: moment.MomentInput | null, format: string): {
date: null;
error: string;
} | {
date: moment.Moment;
error?: undefined;
};
/**
* Checks whether the year is a leap year.
*
* @param year Year to check
* @return {boolean} True if leap year
*/
export declare function isLeapYear(year: number): boolean;
/**
* Checks whether a date range contains leap year.
*
* @param {string} startDate Start date
* @param {string} endDate End date
* @return {boolean} True if date range contains a leap year
*/
export declare function containsLeapYear(startDate: string, endDate: string): boolean;
export {};
//# sourceMappingURL=index.d.ts.map