Finetuning

Signed-off-by: Lauri Nevala <lauri.nevala@gmail.com>
This commit is contained in:
Lauri Nevala committed 2020-12-18 13:04:21 +02:00
1 parent 3453c337de
commit ed2703c424
2 files changed
+6 -7

No files matched your search

@@ -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']");
+2 -3
View File
@@ -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("")}`;