Allow to configure roundUp in date helper of url drilldown (#137874)

This commit is contained in:
Anton Dosov 2022-08-03 15:33:05 +02:00 committed by GitHub
parent c5391dc253
commit 3d5632310b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 2 deletions

View file

@ -46,7 +46,8 @@ a|Format dates. Supports relative dates expressions (for example, "now-15d"). R
Example:
`{{date event.from “YYYY MM DD”}}` +
`{{date “now-15”}}`
`{{date “now-15d”}}` +
`{{date “now/d” roundUp=true}}`
|formatNumber
a|Format numbers. Numbers can be formatted to look like currency, percentages, times or numbers with decimal places, thousands, and abbreviations.

View file

@ -49,12 +49,13 @@ handlebars.registerHelper(
handlebars.registerHelper('date', (...args) => {
const values = args.slice(0, -1) as [string | Date, string | undefined];
const { hash } = args.slice(-1)[0] as Handlebars.HelperOptions;
// eslint-disable-next-line prefer-const
let [date, format] = values;
if (typeof date === 'undefined') throw new Error(`[date]: unknown variable`);
let momentDate: Moment | undefined;
if (typeof date === 'string') {
momentDate = dateMath.parse(date);
momentDate = dateMath.parse(date, { roundUp: hash.roundUp === true });
if (!momentDate || !momentDate.isValid()) {
const ts = Number(date);
if (!Number.isNaN(ts)) {

View file

@ -108,6 +108,13 @@ describe('date helper', () => {
);
});
test('can configure roundUp for dateMath', async () => {
const url = 'https://elastic.co/from={{date from}}&to={{date to roundUp=true}}';
expect(await compile(url, { from: 'now/d', to: 'now/d' })).toMatchInlineSnapshot(
`"https://elastic.co/from=2020-08-18T00:00:00.000Z&to=2020-08-18T23:59:59.999Z"`
);
});
test('can use format', async () => {
const url = 'https://elastic.co/{{date time "dddd, MMMM Do YYYY, h:mm:ss a"}}';
expect(await compile(url, { time: 'now' })).toMatchInlineSnapshot(