From cff56e1e72949dc789e3ca9c354f9034bd5688f0 Mon Sep 17 00:00:00 2001 From: Miki Date: Thu, 19 Sep 2024 16:24:31 -0700 Subject: [PATCH] Use @osd/std to prettify objects for display (#8232) * Use @osd/std to stringify JSON when formatting objects for display Signed-off-by: Miki * Changeset file for PR #8232 created/updated --------- Signed-off-by: Miki Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com> --- changelogs/fragments/8232.yml | 2 ++ .../common/field_formats/utils/as_pretty_string.test.ts | 6 +++++- .../data/common/field_formats/utils/as_pretty_string.ts | 4 +++- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changelogs/fragments/8232.yml diff --git a/changelogs/fragments/8232.yml b/changelogs/fragments/8232.yml new file mode 100644 index 000000000000..3c4766037b6d --- /dev/null +++ b/changelogs/fragments/8232.yml @@ -0,0 +1,2 @@ +fix: +- Use @osd/std to prettify objects for display ([#8232](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8232)) \ No newline at end of file diff --git a/src/plugins/data/common/field_formats/utils/as_pretty_string.test.ts b/src/plugins/data/common/field_formats/utils/as_pretty_string.test.ts index dba46aa51315..d0461193178b 100644 --- a/src/plugins/data/common/field_formats/utils/as_pretty_string.test.ts +++ b/src/plugins/data/common/field_formats/utils/as_pretty_string.test.ts @@ -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', () => { diff --git a/src/plugins/data/common/field_formats/utils/as_pretty_string.ts b/src/plugins/data/common/field_formats/utils/as_pretty_string.ts index 9696130fda2f..dd6a2e5e5e75 100644 --- a/src/plugins/data/common/field_formats/utils/as_pretty_string.ts +++ b/src/plugins/data/common/field_formats/utils/as_pretty_string.ts @@ -28,6 +28,8 @@ * under the License. */ +import { stringify } from '@osd/std'; + /** * Convert a value to a presentable string */ @@ -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, ' '); default: return '' + val; }