Skip to content

Commit

Permalink
Use @osd/std to prettify objects for display (#8232)
Browse files Browse the repository at this point in the history
* Use @osd/std to stringify JSON when formatting objects for display

Signed-off-by: Miki <[email protected]>

* Changeset file for PR #8232 created/updated

---------

Signed-off-by: Miki <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
(cherry picked from commit 6e3351c)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 842ec4b commit 512963e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/8232.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Use @osd/std to prettify objects for display ([#8232](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8232))
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ describe('asPrettyString', () => {
});

test('Converts objects values into presentable strings', () => {
expect(asPrettyString({ key: 'value' })).toBe('{\n "key": "value"\n}');
const longPositive = BigInt(Number.MAX_SAFE_INTEGER) * 2n;
const longNegative = BigInt(Number.MIN_SAFE_INTEGER) * 2n;
expect(asPrettyString({ key: 'value', longPositive, longNegative })).toBe(
`{\n "key": "value",\n "longPositive": ${longPositive.toString()},\n "longNegative": ${longNegative.toString()}\n}`
);
});

test('Converts other non-string values into strings', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
* under the License.
*/

import { stringify } from '@osd/std';

/**
* Convert a value to a presentable string
*/
Expand All @@ -37,7 +39,7 @@ export function asPrettyString(val: any): string {
case 'string':
return val;
case 'object':
return JSON.stringify(val, null, ' ');
return stringify(val, null, ' ');

Check warning on line 42 in src/plugins/data/common/field_formats/utils/as_pretty_string.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/field_formats/utils/as_pretty_string.ts#L42

Added line #L42 was not covered by tests
default:
return '' + val;
}
Expand Down

0 comments on commit 512963e

Please sign in to comment.