mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
* refact(NA): apply root_input_dir=src to each already created pkg * refact(NA): update package generator * fix(NA): correctly use rootDir * fix(NA): use root input dir on latest introduced pkgs for jsts_transpiler macro * chore(NA): merge with main * chore(NA): first attempt to complete removal of src folder on a small group of pkgs * Revert "chore(NA): first attempt to complete removal of src folder on a small group of pkgs" This reverts commitb6f34b7530
. * chore(NA): remove src folder requirement from xpack pkgs * chore(NA): remove src folder from analytics pkgs * chore(NA): remove src folder from home pkgs * chore(NA): remove src folder from shared_ux pkgs * fix(NA): remove missing src folder inputs on sharedux pkg * chore(NA): remove src folder from kbn-a* pkgs * chore(NA): remove src folder from kbn-b* pkgs * chore(NA): remove src folder from kbn-c* pkgs * chore(NA): correct exclude pattern for each changed pkg * chore(NA): remove src folder from kbn-y* pkgs * chore(NA): remove src folder from kbn-e* pkgs * chore(NA): remove src folder from kbn-f* and kbn-g* pkgs * chore(NA): remove src folder from kbn-f* and kbn-g* pkgs * chore(NA): remove src folder from kbn-h** pkgs * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * Revert "chore(NA): remove src folder from kbn-h** pkgs" This reverts commitdcdf72bcc1
. * fix(NA): grammar location * test(NA): fix tests for kbn/config-schema * test(NA): fix tests for kbn/config-schema * chore(NA): multiple errors fixed * chore(NA): remove kuery grammar fix * fix(NA): @kbn/ace imports * fix(NA): grammar location * fix(NA): add missing files to tsconfigs * [CI] Auto-commit changed files from 'node scripts/build_plugin_list_docs' * chore(NA): complete tsconfigs * Revert "chore(NA): complete tsconfigs" This reverts commitf48c616864
. * chore(NA): remove src folder from kbn-core* pkgs * chore(NA): remove src folder from kbn-u* pkgs * chore(NA): remove src folder from kbn-ui-shared-deps* pkgs * chore(NA): fix problems on core pkgs * chore(NA): fix problems on core pkgs * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' * fix(NA): shared_built_assets correct location' * test(NA): update @kbn/optimizer integration snapshots * chore(NA): remove src folder from kbn-t* p1 pkgs * chore(NA): remove src folder from kbn-t* p2 pkgs * chore(NA): update rootDir on utility types pkg * chore(NA): include missing files on @kbn/test * chore(NA): include missing files on @kbn/test * fix(NA): new jest preset paths on @kbn/test * chore(NA): fix eslint * fix(NA): new jest preset paths on @kbn/test * chore(NA): remove exclusion for mocks folde on @kbn/test * fix(NA): several imports from target_node * fix(NA): @kbn/test mocha and jest types clash * chore(NA): remove src folder from kbn-storybook pkg * chore(NA): remove src folder from kbn-stdio-dev-helpers pkg * chore(NA): remove src folder from kbn-std pkg * chore(NA): remove src folder from kbn-sort-pkg-json pkg * chore(NA): remove src folder from kbn-some-dev-log and kbn-shared-ux-utility pkgs * chore(NA): remove src folder from kbn-ux-storybook pkg * chore(NA): remove src folder from kbn-shared-ux-services pkg * chore(NA): remove src folder from kbn-shared-ux-components pkg * chore(NA): remove src folder from kbn-shared-svg pkg * chore(NA): remove src folder from kbn-server-http-tools pkg * chore(NA): remove src folder from kbn-securitysolution-* pkgs * chore(NA): remove src folder from kbn-r-* pkgs * chore(NA): remove src folder from kbn-p* pkgs * chore(NA): remove src folder from kbn-o* pkgs * chore(NA): remove src folder from kbn-m* pkgs * chore(NA): remove src folder from kbn-j,k,l* pkgs * chore(NA): remove src folder from kbn-j,k,l* pkgs * chore(NA): remove src folder from kbn-io-ts-utils* pkgs * chore(NA): remove src folder from kbn-* pkgs except a few * chore(NA): update @kbn/generate * fix(NA): wrong exclusion on kbn-storybook * chore(NA): remove src folder from kbn-monaco pkg * chore(NA): remove src folder from kbn-interpreter pkg * fix(NA): wrong exclusion on kbn-storybook * chore(NA): update every require for target_*/src * chore(NA): remover src folder from @kbn/handlebars * fix(NA): license for @kbn/handlebars * chore(NA): copy templates as part of the jsts_transpiler macro for @kbn/storybook * chore(NA): update handlebars * fix(NA): @kbn/plugin-generator import paths * fix(NA): bundle sizes * fix(NA): web bundle for @kbn/i18n-react * Revert "fix(NA): bundle sizes" This reverts commit8aefe84fbc
. * Revert "Revert "fix(NA): bundle sizes"" This reverts commite9d87d72a4
. * fix(NA): @kbn/docs-utils index.ts path expectation * chore(NA): merge and solve conflicts with main * fix(NA): relative import to index * chore(NA): merge and solve conflicts with main * [CI] Auto-commit changed files from 'node scripts/precommit_hook.js --ref HEAD~1..HEAD --fix' * chore(NA): apply eslint fix * [CI] Auto-commit changed files from 'node scripts/eslint --no-cache --fix' Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
177 lines
5.1 KiB
TypeScript
177 lines
5.1 KiB
TypeScript
/*
|
|
* Licensed to Elasticsearch B.V. under one or more contributor
|
|
* license agreements. See the NOTICE file distributed with
|
|
* this work for additional information regarding copyright
|
|
* ownership. Elasticsearch B.V. licenses this file to you under
|
|
* the Apache License, Version 2.0 (the "License"); you may
|
|
* not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
|
|
import moment from 'moment';
|
|
|
|
export type Unit = 'ms' | 's' | 'm' | 'h' | 'd' | 'w' | 'M' | 'y';
|
|
export type UnitsMap = {
|
|
[k in Unit]: {
|
|
weight: number;
|
|
type: 'calendar' | 'fixed' | 'mixed';
|
|
base: number;
|
|
};
|
|
};
|
|
|
|
export const unitsMap: UnitsMap = {
|
|
ms: { weight: 1, type: 'fixed', base: 1 },
|
|
s: { weight: 2, type: 'fixed', base: 1000 },
|
|
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', 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', base: NaN },
|
|
};
|
|
export const units: Unit[] = Object.keys(unitsMap).sort(
|
|
(a, b) => unitsMap[b as Unit].weight - unitsMap[a as Unit].weight
|
|
) as Unit[];
|
|
export const unitsDesc: Unit[] = [...units] as Unit[];
|
|
export const unitsAsc: Unit[] = [...units].reverse() as Unit[];
|
|
|
|
const isDate = (d: string) => Object.prototype.toString.call(d) === '[object Date]';
|
|
const isValidDate = (d: string) => isDate(d) && !isNaN(d.valueOf() as any);
|
|
|
|
/*
|
|
* This is a simplified version of elasticsearch's date parser.
|
|
* If you pass in a momentjs instance as the third parameter the calculation
|
|
* will be done using this (and its locale settings) instead of the one bundled
|
|
* with this library.
|
|
*/
|
|
export function parse(
|
|
input: string,
|
|
options: { roundUp?: boolean; momentInstance?: typeof moment; forceNow?: Date } = {}
|
|
) {
|
|
const text = input;
|
|
const { roundUp = false, momentInstance = moment, forceNow } = options;
|
|
|
|
if (!text) return undefined;
|
|
if (momentInstance.isMoment(text)) return text;
|
|
if (isDate(text)) return momentInstance(text);
|
|
if (forceNow !== undefined && !isValidDate(forceNow as any)) {
|
|
throw new Error('forceNow must be a valid Date');
|
|
}
|
|
|
|
let time;
|
|
let mathString = '';
|
|
let index;
|
|
let parseString;
|
|
|
|
if (text.substring(0, 3) === 'now') {
|
|
time = momentInstance(forceNow);
|
|
mathString = text.substring('now'.length);
|
|
} else {
|
|
index = text.indexOf('||');
|
|
if (index === -1) {
|
|
parseString = text;
|
|
mathString = ''; // nothing else
|
|
} else {
|
|
parseString = text.substring(0, index);
|
|
mathString = text.substring(index + 2);
|
|
}
|
|
// We're going to just require ISO8601 timestamps, k?
|
|
time = momentInstance(parseString);
|
|
}
|
|
|
|
if (!mathString.length) {
|
|
return time;
|
|
}
|
|
|
|
return parseDateMath(mathString, time, roundUp);
|
|
}
|
|
|
|
function parseDateMath(mathString: string, time: moment.Moment, roundUp: boolean) {
|
|
const dateTime = time;
|
|
const len = mathString.length;
|
|
let i = 0;
|
|
|
|
while (i < len) {
|
|
const c = mathString.charAt(i++);
|
|
let type;
|
|
let num;
|
|
let unit: Unit;
|
|
|
|
if (c === '/') {
|
|
type = 0;
|
|
} else if (c === '+') {
|
|
type = 1;
|
|
} else if (c === '-') {
|
|
type = 2;
|
|
} else {
|
|
return;
|
|
}
|
|
|
|
if (isNaN(mathString.charAt(i) as any)) {
|
|
num = 1;
|
|
} else if (mathString.length === 2) {
|
|
num = mathString.charAt(i);
|
|
} else {
|
|
const numFrom = i;
|
|
while (!isNaN(mathString.charAt(i) as any)) {
|
|
i++;
|
|
if (i >= len) return;
|
|
}
|
|
num = parseInt(mathString.substring(numFrom, i), 10);
|
|
}
|
|
|
|
if (type === 0) {
|
|
// rounding is only allowed on whole, single, units (eg M or 1M, not 0.5M or 2M)
|
|
if (num !== 1) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
unit = mathString.charAt(i++) as Unit;
|
|
|
|
// append additional characters in the unit
|
|
for (let j = i; j < len; j++) {
|
|
const unitChar = mathString.charAt(i);
|
|
if (/[a-z]/i.test(unitChar)) {
|
|
unit += unitChar;
|
|
i++;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (units.indexOf(unit) === -1) {
|
|
return;
|
|
} else {
|
|
if (type === 0) {
|
|
if (roundUp) dateTime.endOf(unit as any);
|
|
else dateTime.startOf(unit as any);
|
|
} else if (type === 1) {
|
|
dateTime.add(num as any, unit);
|
|
} else if (type === 2) {
|
|
dateTime.subtract(num as any, unit);
|
|
}
|
|
}
|
|
}
|
|
|
|
return dateTime;
|
|
}
|
|
|
|
// eslint-disable-next-line import/no-default-export
|
|
export default {
|
|
parse,
|
|
unitsMap: Object.freeze(unitsMap),
|
|
units: Object.freeze(units),
|
|
unitsAsc: Object.freeze(unitsAsc),
|
|
unitsDesc: Object.freeze(unitsDesc),
|
|
};
|