From a38343966ae269a21ff98e8beca90dd3f7645814 Mon Sep 17 00:00:00 2001 From: Matthew Kime Date: Tue, 24 Sep 2024 12:13:26 -0500 Subject: [PATCH] [7.17] [data views] Remove regex for creating dot notation (#193795) (#193811) # Backport This will backport the following commits from `main` to `7.17`: - [[data views] Remove regex for creating dot notation (#193795)](https://github.com/elastic/kibana/pull/193795) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) --- .../data_views/common/fields/utils.test.ts | 6 +++--- src/plugins/data_views/common/fields/utils.ts | 17 +++++++++++++---- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/plugins/data_views/common/fields/utils.test.ts b/src/plugins/data_views/common/fields/utils.test.ts index 0f2ff280eb61b..e7d84f4dc6f0a 100644 --- a/src/plugins/data_views/common/fields/utils.test.ts +++ b/src/plugins/data_views/common/fields/utils.test.ts @@ -16,8 +16,8 @@ describe('shortenDottedString', () => { test('should ignore non-string values', () => { const obj = { key: 'val' }; - expect(shortenDottedString(true)).toBe(true); - expect(shortenDottedString(123)).toBe(123); - expect(shortenDottedString(obj)).toBe(obj); + expect(shortenDottedString(true as unknown as string)).toBe(true); + expect(shortenDottedString(123 as unknown as string)).toBe(123); + expect(shortenDottedString(obj as unknown as string)).toBe(obj); }); }); diff --git a/src/plugins/data_views/common/fields/utils.ts b/src/plugins/data_views/common/fields/utils.ts index adb5057798b1c..a75d0b9f6a0e7 100644 --- a/src/plugins/data_views/common/fields/utils.ts +++ b/src/plugins/data_views/common/fields/utils.ts @@ -25,16 +25,25 @@ export const isMultiField = isDataViewFieldSubtypeMulti; export const getFieldSubtypeMulti = getDataViewFieldSubtypeMulti; export const getFieldSubtypeNested = getDataViewFieldSubtypeNested; -const DOT_PREFIX_RE = /(.).+?\./g; - /** * Convert a dot.notated.string into a short * version (d.n.string) * * @return {any} */ -export function shortenDottedString(input: any) { - return typeof input !== 'string' ? input : input.replace(DOT_PREFIX_RE, '$1.'); + +export function shortenDottedString(input: string): string { + if (typeof input === 'string') { + const split = input.split('.'); + return split.reduce((acc, part, i) => { + if (i === split.length - 1) { + return acc + part; + } + return acc + part[0] + '.'; + }, ''); + } + + return input; } // Note - this code is duplicated from @kbn/es-query