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

[7.17] [data views] Remove regex for creating dot notation (#193795) #193811

Merged
merged 3 commits into from
Sep 24, 2024
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
6 changes: 3 additions & 3 deletions src/plugins/data_views/common/fields/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
17 changes: 13 additions & 4 deletions src/plugins/data_views/common/fields/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down