-
Notifications
You must be signed in to change notification settings - Fork 8.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolver Light Theme And Kibana Integration #67859
Changes from all commits
181e00b
0cb51e8
697fc2c
abb136d
dea3be5
ed6a424
4432069
57f456d
9cc6243
900d241
489c290
e269a1a
bdd87fd
c9be18b
91a3440
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { getFriendlyElapsedTime } from './date'; | ||
|
||
describe('date', () => { | ||
describe('getFriendlyElapsedTime', () => { | ||
const second = 1000; | ||
const minute = second * 60; | ||
const hour = minute * 60; | ||
const day = hour * 24; | ||
const week = day * 7; | ||
const month = day * 30; | ||
const year = day * 365; | ||
|
||
const initialTime = new Date('6/1/2020').getTime(); | ||
|
||
const oneSecond = new Date(initialTime + 1 * second).getTime(); | ||
const oneMinute = new Date(initialTime + 1 * minute).getTime(); | ||
const oneHour = new Date(initialTime + 1 * hour).getTime(); | ||
const oneDay = new Date(initialTime + 1 * day).getTime(); | ||
const oneWeek = new Date(initialTime + 1 * week).getTime(); | ||
const oneMonth = new Date(initialTime + 1 * month).getTime(); | ||
const oneYear = new Date(initialTime + 1 * year).getTime(); | ||
|
||
const almostAMinute = new Date(initialTime + 59.9 * second).getTime(); | ||
const almostAnHour = new Date(initialTime + 59.9 * minute).getTime(); | ||
const almostADay = new Date(initialTime + 23.9 * hour).getTime(); | ||
const almostAWeek = new Date(initialTime + 6.9 * day).getTime(); | ||
const almostAMonth = new Date(initialTime + 3.9 * week).getTime(); | ||
const almostAYear = new Date(initialTime + 11.9 * month).getTime(); | ||
const threeYears = new Date(initialTime + 3 * year).getTime(); | ||
|
||
it('should return the correct singular relative time', () => { | ||
expect(getFriendlyElapsedTime(initialTime, oneSecond)).toEqual({ | ||
duration: 1, | ||
durationType: 'second', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneMinute)).toEqual({ | ||
duration: 1, | ||
durationType: 'minute', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneHour)).toEqual({ | ||
duration: 1, | ||
durationType: 'hour', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneDay)).toEqual({ | ||
duration: 1, | ||
durationType: 'day', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneWeek)).toEqual({ | ||
duration: 1, | ||
durationType: 'week', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneMonth)).toEqual({ | ||
duration: 1, | ||
durationType: 'month', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, oneYear)).toEqual({ | ||
duration: 1, | ||
durationType: 'year', | ||
}); | ||
}); | ||
|
||
it('should return the correct pluralized relative time', () => { | ||
expect(getFriendlyElapsedTime(initialTime, almostAMinute)).toEqual({ | ||
duration: 59, | ||
durationType: 'seconds', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAnHour)).toEqual({ | ||
duration: 59, | ||
durationType: 'minutes', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostADay)).toEqual({ | ||
duration: 23, | ||
durationType: 'hours', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAWeek)).toEqual({ | ||
duration: 6, | ||
durationType: 'days', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAMonth)).toEqual({ | ||
duration: 3, | ||
durationType: 'weeks', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, almostAYear)).toEqual({ | ||
duration: 11, | ||
durationType: 'months', | ||
}); | ||
expect(getFriendlyElapsedTime(initialTime, threeYears)).toEqual({ | ||
duration: 3, | ||
durationType: 'years', | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -0,0 +1,82 @@ | ||||
/* | ||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||||
* or more contributor license agreements. Licensed under the Elastic License; | ||||
* you may not use this file except in compliance with the Elastic License. | ||||
*/ | ||||
|
||||
import { DurationDetails, DurationTypes } from '../types'; | ||||
|
||||
/* | ||||
* Given two unix timestamps, it will return an object containing the time difference and properly pluralized friendly version of the time difference. | ||||
* i.e. a time difference of 1000ms will yield => { duration: 1, durationType: 'second' } and 10000ms will yield => { duration: 10, durationType: 'seconds' } | ||||
* | ||||
*/ | ||||
export const getFriendlyElapsedTime = ( | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would you mind adding a comment like
and would you mind adding an explicit return type. Also, can you explain the reasoning for accepting either numbers or strings? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||||
from: number | string, | ||||
to: number | string | ||||
): DurationDetails | null => { | ||||
const startTime = typeof from === 'number' ? from : parseInt(from, 10); | ||||
const endTime = typeof to === 'number' ? to : parseInt(to, 10); | ||||
const elapsedTimeInMs = endTime - startTime; | ||||
|
||||
if (Number.isNaN(elapsedTimeInMs)) { | ||||
return null; | ||||
} | ||||
|
||||
const second = 1000; | ||||
const minute = second * 60; | ||||
const hour = minute * 60; | ||||
const day = hour * 24; | ||||
const week = day * 7; | ||||
const month = day * 30; | ||||
const year = day * 365; | ||||
|
||||
let duration: number; | ||||
let singularType: DurationTypes; | ||||
let pluralType: DurationTypes; | ||||
switch (true) { | ||||
case elapsedTimeInMs >= year: | ||||
duration = elapsedTimeInMs / year; | ||||
singularType = 'year'; | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔴 i18n here, right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. these values are passed as arguments into an ICU message via |
||||
pluralType = 'years'; | ||||
break; | ||||
case elapsedTimeInMs >= month: | ||||
duration = elapsedTimeInMs / month; | ||||
singularType = 'month'; | ||||
pluralType = 'months'; | ||||
break; | ||||
case elapsedTimeInMs >= week: | ||||
duration = elapsedTimeInMs / week; | ||||
singularType = 'week'; | ||||
pluralType = 'weeks'; | ||||
break; | ||||
case elapsedTimeInMs >= day: | ||||
duration = elapsedTimeInMs / day; | ||||
singularType = 'day'; | ||||
pluralType = 'days'; | ||||
break; | ||||
case elapsedTimeInMs >= hour: | ||||
duration = elapsedTimeInMs / hour; | ||||
singularType = 'hour'; | ||||
pluralType = 'hours'; | ||||
break; | ||||
case elapsedTimeInMs >= minute: | ||||
duration = elapsedTimeInMs / minute; | ||||
singularType = 'minute'; | ||||
pluralType = 'minutes'; | ||||
break; | ||||
case elapsedTimeInMs >= second: | ||||
duration = elapsedTimeInMs / second; | ||||
singularType = 'second'; | ||||
pluralType = 'seconds'; | ||||
break; | ||||
default: | ||||
duration = elapsedTimeInMs; | ||||
singularType = 'millisecond'; | ||||
pluralType = 'milliseconds'; | ||||
break; | ||||
} | ||||
|
||||
const durationType = duration > 1 ? pluralType : singularType; | ||||
return { duration: Math.floor(duration), durationType }; | ||||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔴 probably want the same i18n as you use in
date.ts
in case they test it in different languages?