From bef0f797abf3bc31cce36a04bf8ded7e2ee239be Mon Sep 17 00:00:00 2001 From: Domas Date: Mon, 21 Jun 2021 18:41:11 +0300 Subject: [PATCH] add duration utils docs (#35995) --- .../grafana-data/src/datetime/durationutil.ts | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/grafana-data/src/datetime/durationutil.ts b/packages/grafana-data/src/datetime/durationutil.ts index d1a02639a8a..542e7ea27d2 100644 --- a/packages/grafana-data/src/datetime/durationutil.ts +++ b/packages/grafana-data/src/datetime/durationutil.ts @@ -32,6 +32,13 @@ export function intervalToAbbreviatedDurationString(interval: Interval, includeS }, ''); } +/** + * parseDuration parses duration string into datefns Duration object + * + * @param duration - string to convert. For example '2m', '5h 20s' + * + * @public + */ export function parseDuration(duration: string): Duration { return duration.split(' ').reduce((acc, value) => { const match = value.match(/(\d+)(.+)/); @@ -44,15 +51,37 @@ export function parseDuration(duration: string): Duration { }, {}); } +/** + * addDurationToDate adds given duration to given date and returns a new Date object + * + * @param date - date to add to. Can be either Date object or a number (milliseconds since epoch) + * @param duration - duration to add. For example '2m', '5h 20s' + * + * @public + */ export function addDurationToDate(date: Date | number, duration: Duration): Date { return add(date, duration); } +/** + * durationToMilliseconds convert a duration object to milliseconds + * + * @param duration - datefns Duration object + * + * @public + */ export function durationToMilliseconds(duration: Duration): number { const now = new Date(); return addDurationToDate(now, duration).getTime() - now.getTime(); } -export function isValidDate(dateString: string) { +/** + * isValidDate returns true if given string can be parsed into valid Date object, false otherwise + * + * @param dateString - string representation of a date + * + * @public + */ +export function isValidDate(dateString: string): boolean { return !isNaN(Date.parse(dateString)); }