Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move as_pretty_string.js #45356

Merged
merged 5 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}