From ed2703c424c1d98f1d7986d4a25fa641fbd1af18 Mon Sep 17 00:00:00 2001 From: Lauri Nevala Date: Fri, 18 Dec 2020 13:04:21 +0200 Subject: [PATCH] Finetuning Signed-off-by: Lauri Nevala --- src/renderer/utils/__tests__/jsonPath.test.tsx | 8 ++++---- src/renderer/utils/jsonPath.ts | 5 ++--- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/renderer/utils/__tests__/jsonPath.test.tsx b/src/renderer/utils/__tests__/jsonPath.test.tsx index b069857893..7821afa6f0 100644 --- a/src/renderer/utils/__tests__/jsonPath.test.tsx +++ b/src/renderer/utils/__tests__/jsonPath.test.tsx @@ -1,7 +1,7 @@ import { parseJsonPath } from "../jsonPath"; describe("parseJsonPath", () => { - test("should convert \. to use indexed notation", () => { + test("should convert \\. to use indexed notation", () => { const res = parseJsonPath(".metadata.labels.kubesphere\\.io/alias-name"); expect(res).toBe(".metadata.labels['kubesphere.io/alias-name']"); @@ -13,20 +13,20 @@ describe("parseJsonPath", () => { expect(res).toBe(".metadata.labels['alias-name']"); }); - test("should handle scenario when both \ and indexed notation is present", () => { + test("should handle scenario when both \\. and indexed notation are present", () => { const rest = parseJsonPath(".metadata.labels\\.serving['some.other.item']"); expect(rest).toBe(".metadata['labels.serving']['some.other.item']"); }); - test("should not touch given jsonPath if no invalid characters", () => { + test("should not touch given jsonPath if no invalid characters present", () => { const res = parseJsonPath(".status.conditions[?(@.type=='Ready')].status"); expect(res).toBe(".status.conditions[?(@.type=='Ready')].status"); }); - test("strips '\' away from the result", () => { + test("strips '\\' away from the result", () => { const res = parseJsonPath(".metadata.labels['serving\\.knative\\.dev/configuration']"); expect(res).toBe(".metadata.labels['serving.knative.dev/configuration']"); diff --git a/src/renderer/utils/jsonPath.ts b/src/renderer/utils/jsonPath.ts index f9db610be6..ea31ffa80e 100644 --- a/src/renderer/utils/jsonPath.ts +++ b/src/renderer/utils/jsonPath.ts @@ -1,11 +1,10 @@ +// Helper to convert strings used for jsonPath where \. or - is present to use indexed notation, +// for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata.labels['kubesphere\.io/alias-name'] export function parseJsonPath(jsonPath: string) { let pathExpression = jsonPath; if (jsonPath.match(/[\\-]/g)) { // search for '\' and '-' - // convert cases where \. or - is present to use indexed notation, - // for example: .metadata.labels.kubesphere\.io/alias-name -> .metadata.labels.['kubesphere\.io/alias-name'] - const [first, ...rest] = jsonPath.split(/(?<=\w)\./); // split jsonPath by '.' (\. cases are ignored) pathExpression = `${convertToIndexNotation(first, true)}${rest.map(value => convertToIndexNotation(value)).join("")}`;