Skip to content

Commit

Permalink
[ui/public/utils] Copy rarely used items to where they are consumed
Browse files Browse the repository at this point in the history
Closes: #52841
  • Loading branch information
alexwizp committed Dec 27, 2019
1 parent 3ed5264 commit 96d7ab2
Show file tree
Hide file tree
Showing 16 changed files with 337 additions and 156 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,20 @@
* under the License.
*/

// TODO: This file is copied from src/legacy/utils/case_conversion.ts
// because TS-imports from utils in ui are currently not possible.
// When the build process is updated, this file can be removed
import { keysToCamelCaseShallow } from './case_conversion';

import _ from 'lodash';
describe('keysToCamelCaseShallow', () => {
it("should convert all of an object's keys to camel case", () => {
const data = {
camelCase: 'camelCase',
'kebab-case': 'kebabCase',
snake_case: 'snakeCase',
};

export function keysToSnakeCaseShallow(object: Record<string, any>) {
return _.mapKeys(object, (value, key) => {
return _.snakeCase(key);
});
}
const result = keysToCamelCaseShallow(data);

export function keysToCamelCaseShallow(object: Record<string, any>) {
return _.mapKeys(object, (value, key) => {
return _.camelCase(key);
expect(result.camelCase).toBe('camelCase');
expect(result.kebabCase).toBe('kebabCase');
expect(result.snakeCase).toBe('snakeCase');
});
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* under the License.
*/

import moment from 'moment';
import { mapKeys, camelCase } from 'lodash';

export function parseInterval(interval: string): moment.Duration | null;
export function keysToCamelCaseShallow(object: Record<string, any>) {
return mapKeys(object, (value, key) => camelCase(key));
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
*/

import { kfetch } from 'ui/kfetch';
import { keysToCamelCaseShallow } from 'ui/utils/case_conversion';
import { keysToCamelCaseShallow } from './case_conversion';

export async function findObjects(findOptions) {
const response = await kfetch({
method: 'GET',
pathname: '/api/kibana/management/saved_objects/_find',
query: findOptions,
});

return keysToCamelCaseShallow(response);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
* under the License.
*/

import { parseInterval } from 'ui/utils/parse_interval';
import { GTE_INTERVAL_RE } from '../../common/interval_regexp';
import { i18n } from '@kbn/i18n';
import { parseInterval } from '../../../../../plugins/data/public';

export function validateInterval(bounds, panel, maxBuckets) {
const { interval } = panel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,40 +17,20 @@
* under the License.
*/

import { keysToCamelCaseShallow, keysToSnakeCaseShallow } from './case_conversion';
import { keysToSnakeCaseShallow } from './case_conversion';

describe('keysToSnakeCaseShallow', () => {
it("should convert all of an object's keys to snake case", () => {
const result = keysToSnakeCaseShallow({
const data = {
camelCase: 'camel_case',
'kebab-case': 'kebab_case',
snake_case: 'snake_case',
});
};

expect(result).toMatchInlineSnapshot(`
Object {
"camel_case": "camel_case",
"kebab_case": "kebab_case",
"snake_case": "snake_case",
}
`);
});
});

describe('keysToCamelCaseShallow', () => {
it("should convert all of an object's keys to camel case", () => {
const result = keysToCamelCaseShallow({
camelCase: 'camelCase',
'kebab-case': 'kebabCase',
snake_case: 'snakeCase',
});
const result = keysToSnakeCaseShallow(data);

expect(result).toMatchInlineSnapshot(`
Object {
"camelCase": "camelCase",
"kebabCase": "kebabCase",
"snakeCase": "snakeCase",
}
`);
expect(result.camel_case).toBe('camel_case');
expect(result.kebab_case).toBe('kebab_case');
expect(result.snake_case).toBe('snake_case');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,8 @@
* under the License.
*/

import _ from 'lodash';
import { mapKeys, snakeCase } from 'lodash';

export function keysToSnakeCaseShallow(object: Record<string, any>) {
return _.mapKeys(object, (value, key) => {
return _.snakeCase(key);
});
}

export function keysToCamelCaseShallow(object: Record<string, any>) {
return _.mapKeys(object, (value, key) => {
return _.camelCase(key);
});
return mapKeys(object, (value, key) => snakeCase(key));
}
2 changes: 1 addition & 1 deletion src/legacy/server/status/lib/metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import os from 'os';
import v8 from 'v8';
import { get, isObject, merge } from 'lodash';
import { keysToSnakeCaseShallow } from '../../../utils/case_conversion';
import { keysToSnakeCaseShallow } from './case_conversion';
import { getAllStats as cGroupStats } from './cgroup';
import { getOSInfo } from './get_os_info';

Expand Down
3 changes: 1 addition & 2 deletions src/legacy/ui/public/time_buckets/time_buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@
import _ from 'lodash';
import moment from 'moment';
import { npStart } from 'ui/new_platform';
import { parseInterval } from '../utils/parse_interval';
import { calcAutoIntervalLessThan, calcAutoIntervalNear } from './calc_auto_interval';
import {
convertDurationToNormalizedEsInterval,
convertIntervalToEsInterval,
} from './calc_es_interval';
import { FIELD_FORMAT_IDS } from '../../../../plugins/data/public';
import { FIELD_FORMAT_IDS, parseInterval } from '../../../../plugins/data/public';

const getConfig = (...args) => npStart.core.uiSettings.get(...args);

Expand Down
93 changes: 0 additions & 93 deletions src/legacy/ui/public/utils/__tests__/parse_interval.js

This file was deleted.

119 changes: 119 additions & 0 deletions src/legacy/ui/public/utils/parse_interval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* 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 { Duration, unitOfTime } from 'moment';
import { parseInterval } from '../../../../plugins/data/public';

const validateDuration = (duration: Duration | null, unit: unitOfTime.Base, value: number) => {
expect(duration).toBeDefined();

if (duration) {
expect(duration.as(unit)).toBe(value);
}
};

describe('parseInterval', () => {
describe('integer', () => {
test('should correctly parse 1d interval', () => {
validateDuration(parseInterval('1d'), 'd', 1);
});

test('should correctly parse 2y interval', () => {
validateDuration(parseInterval('2y'), 'y', 2);
});

test('should correctly parse 5M interval', () => {
validateDuration(parseInterval('5M'), 'M', 5);
});

test('should correctly parse 5m interval', () => {
validateDuration(parseInterval('5m'), 'm', 5);
});

test('should correctly parse 250ms interval', () => {
validateDuration(parseInterval('250ms'), 'ms', 250);
});

test('should correctly parse 100s interval', () => {
validateDuration(parseInterval('100s'), 's', 100);
});

test('should correctly parse 23d interval', () => {
validateDuration(parseInterval('23d'), 'd', 23);
});

test('should correctly parse 52w interval', () => {
validateDuration(parseInterval('52w'), 'w', 52);
});
});

describe('fractional interval', () => {
test('should correctly parse fractional 2.35y interval', () => {
validateDuration(parseInterval('2.35y'), 'y', 2.35);
});

test('should correctly parse fractional 1.5w interval', () => {
validateDuration(parseInterval('1.5w'), 'w', 1.5);
});
});

describe('less than 1', () => {
test('should correctly bubble up 0.5h interval which are less than 1', () => {
validateDuration(parseInterval('0.5h'), 'm', 30);
});

test('should correctly bubble up 0.5d interval which are less than 1', () => {
validateDuration(parseInterval('0.5d'), 'h', 12);
});
});

describe('unit in an interval only', () => {
test('should correctly parse ms interval', () => {
validateDuration(parseInterval('ms'), 'ms', 1);
});

test('should correctly parse d interval', () => {
validateDuration(parseInterval('d'), 'd', 1);
});

test('should correctly parse m interval', () => {
validateDuration(parseInterval('m'), 'm', 1);
});

test('should correctly parse y interval', () => {
validateDuration(parseInterval('y'), 'y', 1);
});

test('should correctly parse M interval', () => {
validateDuration(parseInterval('M'), 'M', 1);
});
});

test('should return null for an invalid interval', () => {
let duration = parseInterval('');
expect(duration).toBeNull();

// @ts-ignore
duration = parseInterval(null);
expect(duration).toBeNull();

duration = parseInterval('234asdf');
expect(duration).toBeNull();
});
});
Loading

0 comments on commit 96d7ab2

Please sign in to comment.