Skip to content

Commit

Permalink
[ui/public/utils] Move items into ui/vis
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Dec 10, 2019
1 parent b4a2751 commit 6e65cb9
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/legacy/ui/public/agg_types/buckets/geo_hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
*/

import { i18n } from '@kbn/i18n';
import { geohashColumns } from 'ui/vis/map/decode_geo_hash';
import chrome from '../../chrome';
import { BucketAggType, IBucketAggConfig } from './_bucket_agg_type';
import { AutoPrecisionParamEditor } from '../../vis/editors/default/controls/auto_precision';
import { UseGeocentroidParamEditor } from '../../vis/editors/default/controls/use_geocentroid';
import { IsFilteredByCollarParamEditor } from '../../vis/editors/default/controls/is_filtered_by_collar';
import { PrecisionParamEditor } from '../../vis/editors/default/controls/precision';
import { geohashColumns } from '../../utils/decode_geo_hash';
import { AggGroupNames } from '../../vis/editors/default/agg_groups';
import { KBN_FIELD_TYPES } from '../../../../../plugins/data/public';

Expand Down
28 changes: 0 additions & 28 deletions src/legacy/ui/public/utils/range.d.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import React, { useCallback } from 'react';

import { EuiFieldNumber, EuiFlexGroup, EuiFlexItem, EuiButtonIcon } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { Range } from '../../../../../../utils/range';
import { Range } from './range';

export interface NumberRowProps {
autoFocus: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,30 @@
* under the License.
*/

import _ from 'lodash';
import expect from '@kbn/expect';
import { parseRange } from '../range';
import { forOwn } from 'lodash';
import { parseRange } from './range';

describe('Range parsing utility', function () {

it('throws an error for inputs that are not formatted properly', function () {
expect(function () {
describe('Range parsing utility', () => {
test('throws an error for inputs that are not formatted properly', () => {
expect(() => {
parseRange('');
}).to.throwException(TypeError);
}).toThrowError(TypeError);

expect(function () {
expect(function() {
parseRange('p10202');
}).to.throwException(TypeError);
}).toThrowError(TypeError);

expect(function () {
expect(function() {
parseRange('{0,100}');
}).to.throwException(TypeError);
}).toThrowError(TypeError);

expect(function () {
expect(function() {
parseRange('[0,100');
}).to.throwException(TypeError);
}).toThrowError(TypeError);

expect(function () {
expect(function() {
parseRange(')0,100(');
}).to.throwException(TypeError);
}).toThrowError(TypeError);
});

const tests = {
Expand All @@ -51,52 +49,52 @@ describe('Range parsing utility', function () {
min: 0,
max: 100,
minInclusive: true,
maxInclusive: true
maxInclusive: true,
},
within: [
[0, true],
[0.0000001, true],
[1, true],
[99.99999, true],
[100, true]
]
[100, true],
],
},
'(26.3 , 42]': {
props: {
min: 26.3,
max: 42,
minInclusive: false,
maxInclusive: true
maxInclusive: true,
},
within: [
[26.2999999, false],
[26.3000001, true],
[30, true],
[41, true],
[42, true]
]
[42, true],
],
},
'(-50,50)': {
props: {
min: -50,
max: 50,
minInclusive: false,
maxInclusive: false
maxInclusive: false,
},
within: [
[-50, false],
[-49.99999, true],
[0, true],
[49.99999, true],
[50, false]
]
[50, false],
],
},
'(Infinity, -Infinity)': {
props: {
min: -Infinity,
max: Infinity,
minInclusive: false,
maxInclusive: false
maxInclusive: false,
},
within: [
[0, true],
Expand All @@ -105,25 +103,24 @@ describe('Range parsing utility', function () {
[-10000000000, true],
[-Infinity, false],
[Infinity, false],
]
}
],
},
};

_.forOwn(tests, function (spec, str) {

describe(str, function () {
forOwn(tests, (spec, str: any) => {
// eslint-disable-next-line jest/valid-describe
describe(str, () => {
const range = parseRange(str);

it('creation', function () {
expect(range).to.eql(spec.props);
it('creation', () => {
expect(range).toEqual(spec.props);
});

spec.within.forEach(function (tup) {
it('#within(' + tup[0] + ')', function () {
expect(range.within(tup[0])).to.be(tup[1]);
spec.within.forEach((tup: any[]) => {
it('#within(' + tup[0] + ')', () => {
expect(range.within(tup[0])).toBe(tup[1]);
});
});
});

});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* under the License.
*/

import _ from 'lodash';

/**
* Regexp portion that matches our number
*
Expand All @@ -44,41 +42,39 @@ const _RE_NUMBER = '(\\-?(?:\\d+(?:\\.\\d+)?|Infinity))';
*
* @type {RegExp}
*/
const RANGE_RE = new RegExp('^\\s*([\\[|\\(])\\s*' + _RE_NUMBER + '\\s*,\\s*' + _RE_NUMBER + '\\s*([\\]|\\)])\\s*$');
const RANGE_RE = new RegExp(
'^\\s*([\\[|\\(])\\s*' + _RE_NUMBER + '\\s*,\\s*' + _RE_NUMBER + '\\s*([\\]|\\)])\\s*$'
);

export class Range {
constructor(
public minInclusive: boolean,
public min: number,
public max: number,
public maxInclusive: boolean
) {}

export function parseRange(input) {
within(n: number): boolean {
if ((this.min === n && !this.minInclusive) || this.min > n) return false;
if ((this.max === n && !this.maxInclusive) || this.max < n) return false;

return true;
}
}

export function parseRange(input: string): Range {
const match = String(input).match(RANGE_RE);
if (!match) {
throw new TypeError('expected input to be in interval notation e.g., (100, 200]');
}

return new Range(
match[1] === '[',
parseFloat(match[2]),
parseFloat(match[3]),
match[4] === ']'
);
}

function Range(/* minIncl, min, max, maxIncl */) {
const args = _.toArray(arguments);
if (args[1] > args[2]) args.reverse();
const args = [match[1] === '[', parseFloat(match[2]), parseFloat(match[3]), match[4] === ']'];

this.minInclusive = args[0];
this.min = args[1];
this.max = args[2];
this.maxInclusive = args[3];
}

Range.prototype.within = function (n) {
if (this.min === n && !this.minInclusive) return false;
if (this.min > n) return false;

if (this.max === n && !this.maxInclusive) return false;
if (this.max < n) return false;

return true;
};
if (args[1] > args[2]) {
args.reverse();
}

const [minInclusive, min, max, maxInclusive] = args;

return new Range(minInclusive as boolean, min as number, max as number, maxInclusive as boolean);
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
getNextModel,
getRange,
} from './utils';
import { Range } from '../../../../../../utils/range';
import { Range } from './range';
import { NumberRowModel } from './number_row';

describe('NumberList utils', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { last } from 'lodash';
import { i18n } from '@kbn/i18n';
import { htmlIdGenerator } from '@elastic/eui';

import { parseRange, Range } from '../../../../../../utils/range';
import { parseRange, Range } from './range';
import { NumberRowModel } from './number_row';

const EMPTY_STRING = '';
Expand Down
3 changes: 1 addition & 2 deletions src/legacy/ui/public/vis/map/convert_to_geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
* under the License.
*/

import { decodeGeoHash } from 'ui/utils/decode_geo_hash';
import { decodeGeoHash } from './decode_geo_hash';
import { gridDimensions } from './grid_dimensions';


export function convertToGeoJson(tabifiedResponse, { geohash, geocentroid, metric }) {

let features;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,18 @@
* under the License.
*/

import { geohashColumns, decodeGeoHash } from '../decode_geo_hash';
import { geohashColumns, decodeGeoHash } from './decode_geo_hash';

test('geohashColumns', function () {
test('geohashColumns', () => {
expect(geohashColumns(1)).toBe(8);
expect(geohashColumns(2)).toBe(8 * 4);
expect(geohashColumns(3)).toBe(8 * 4 * 8);
expect(geohashColumns(4)).toBe(8 * 4 * 8 * 4);
});

test('decodeGeoHash', function () {
test('decodeGeoHash', () => {
expect(decodeGeoHash('drm3btev3e86')).toEqual({
latitude: [
41.119999922811985,
41.12000009045005,
41.12000000663102,
],
longitude: [
-71.34000029414892,
-71.3399999588728,
-71.34000012651086,
],
latitude: [41.119999922811985, 41.12000009045005, 41.12000000663102],
longitude: [-71.34000029414892, -71.3399999588728, -71.34000012651086],
});
});

2 changes: 1 addition & 1 deletion src/legacy/ui/public/vis/map/kibana_map.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { createZoomWarningMsg } from './map_messages';
import L from 'leaflet';
import $ from 'jquery';
import _ from 'lodash';
import { zoomToPrecision } from '../../utils/zoom_to_precision';
import { zoomToPrecision } from './zoom_to_precision';
import { i18n } from '@kbn/i18n';
import { ORIGIN } from '../../../../core_plugins/tile_map/common/origin';

Expand Down
Loading

0 comments on commit 6e65cb9

Please sign in to comment.