[@kbn/datemath] improve types (#24671)

* [kbn-datemath][parseEsInterval] improve types slightly

* [kbn-datemath][vis/leastCommonInterval] make types more precise

* [ui/leastCommonInterval] fix bug in finding same types

* add back valid test
This commit is contained in:
Spencer 2018-10-29 17:25:08 -07:00 committed by GitHub
parent fd4e63c5a9
commit 39be1afe1b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 58 additions and 26 deletions

View file

@ -17,13 +17,29 @@
* under the License.
*/
declare module '@kbn/datemath' {
const dateMath: {
parse: any;
unitsMap: any;
units: string[];
unitsAsc: string[];
unitsDesc: string[];
import moment from 'moment';
export type Unit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y';
declare const datemath: {
unitsMap: {
[k in Unit]: {
weight: number;
type: 'calendar' | 'fixed' | 'mixed';
base: number;
}
};
export default dateMath;
}
units: Unit[];
unitsAsc: Unit[];
unitsDesc: Unit[];
parse(
input: string,
options?: {
roundUp?: boolean;
forceNow?: boolean;
momentInstance?: typeof moment;
}
): moment.Moment | undefined;
};
export default datemath;

View file

@ -25,10 +25,10 @@ const unitsMap = {
m: { weight: 3, type: 'mixed', base: 1000 * 60 },
h: { weight: 4, type: 'mixed', base: 1000 * 60 * 60 },
d: { weight: 5, type: 'mixed', base: 1000 * 60 * 60 * 24 },
w: { weight: 6, type: 'calendar' },
M: { weight: 7, type: 'calendar' },
w: { weight: 6, type: 'calendar', base: NaN },
M: { weight: 7, type: 'calendar', base: NaN },
// q: { weight: 8, type: 'calendar' }, // TODO: moment duration does not support quarter
y: { weight: 9, type: 'calendar' },
y: { weight: 9, type: 'calendar', base: NaN },
};
const units = Object.keys(unitsMap).sort((a, b) => unitsMap[b].weight - unitsMap[a].weight);
const unitsDesc = [...units];