Skip to content

Commit

Permalink
Move as_pretty_string.js (#45356) (#45624)
Browse files Browse the repository at this point in the history
* Convert asPrettyString to TS

* Migrate asPrettyString() to plugins/data
  • Loading branch information
Artyom Gospodarsky authored Sep 13, 2019
1 parent 9b7dc32 commit 62bd1ef
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { asPrettyString } from '../../utils/as_pretty_string';
import { asPrettyString } from '../../../../../../plugins/data/common/utils/as_pretty_string';

export function createBoolFormat(FieldFormat) {
return class BoolFormat extends FieldFormat {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import _ from 'lodash';
import { asPrettyString } from '../../utils/as_pretty_string';
import { asPrettyString } from '../../../../../../plugins/data/common/utils/as_pretty_string';
import { DEFAULT_COLOR } from './color_default';

const convertTemplate = _.template('<span style="<%- style %>"><%- val %></span>');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { asPrettyString } from '../../utils/as_pretty_string';
import { asPrettyString } from '../../../../../../plugins/data/common/utils/as_pretty_string';
import { shortenDottedString } from '../../utils/shorten_dotted_string';

const TRANSFORM_OPTIONS = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
*/
import { escape, isFunction } from 'lodash';
import { FieldFormatConvert, IFieldFormat, HtmlConventTypeConvert } from '../types';

// @ts-ignore
import { asPrettyString } from '../../../core_plugins/kibana/common/utils/as_pretty_string';
import { getHighlightHtml } from '../../../../plugins/data/common/highlight/highlight_html';
import { asPrettyString } from '../../../../plugins/data/common/utils/as_pretty_string';

const CONTEXT_TYPE = 'html';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@

import { isFunction } from 'lodash';
import { IFieldFormat, FieldFormatConvert, TextContextTypeConvert } from '../types';

// @ts-ignore
import { asPrettyString } from '../../../core_plugins/kibana/common/utils/as_pretty_string';
import { asPrettyString } from '../../../../plugins/data/common/utils/as_pretty_string';

const CONTEXT_TYPE = 'text';

Expand Down
4 changes: 1 addition & 3 deletions src/legacy/ui/field_formats/field_format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@
import { constant, trimRight, trimLeft, get } from 'lodash';
import { FieldFormat } from './field_format';
import { FieldFormatConvert } from './types';

// @ts-ignore
import { asPrettyString } from '../../core_plugins/kibana/common/utils/as_pretty_string';
import { asPrettyString } from '../../../plugins/data/common/utils/as_pretty_string';

const getTestFormat = (
_convert: FieldFormatConvert = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,25 @@
* under the License.
*/

import expect from '@kbn/expect';
import { asPrettyString } from '../as_pretty_string';
import { asPrettyString } from './as_pretty_string';

describe('asPrettyString', () => {

it('Converts null and undefined values into a string signifying no value', () => {
expect(asPrettyString(null)).to.equal(' - ');
expect(asPrettyString(undefined)).to.equal(' - ');
test('Converts null and undefined values into a string signifying no value', () => {
expect(asPrettyString(null)).toBe(' - ');
expect(asPrettyString(undefined)).toBe(' - ');
});

it('Does not mutate string values', () => {
test('Does not mutate string values', () => {
const s = 'I am a string!@';
expect(asPrettyString(s)).to.equal(s);
expect(asPrettyString(s)).toBe(s);
});

it('Converts objects values into presentable strings', () => {
expect(asPrettyString({ key: 'value' })).to.equal('{\n "key": "value"\n}');
test('Converts objects values into presentable strings', () => {
expect(asPrettyString({ key: 'value' })).toBe('{\n "key": "value"\n}');
});

it('Converts other non-string values into strings', () => {
expect(asPrettyString(true)).to.equal('true');
expect(asPrettyString(123)).to.equal('123');
test('Converts other non-string values into strings', () => {
expect(asPrettyString(true)).toBe('true');
expect(asPrettyString(123)).toBe('123');
});

});
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@

/**
* Convert a value to a presentable string
* @param {any} val - the value to transform
* @return {string}
*/
export function asPrettyString(val) {
export function asPrettyString(val: any): string {
if (val === null || val === undefined) return ' - ';
switch (typeof val) {
case 'string': return val;
case 'object': return JSON.stringify(val, null, ' ');
default: return '' + val;
case 'string':
return val;
case 'object':
return JSON.stringify(val, null, ' ');
default:
return '' + val;
}
}

0 comments on commit 62bd1ef

Please sign in to comment.