1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Introduce getWorkloadKindFromUrl() method

Signed-off-by: Alex Andreev <alex.andreev.email@gmail.com>
This commit is contained in:
Alex Andreev 2023-04-03 12:55:27 +03:00
parent b5a085b55c
commit e2ced3bbb2
2 changed files with 64 additions and 0 deletions

View File

@ -0,0 +1,50 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getWorkloadKindFromUrl } from "./get-workload-kind-from-url";
describe("getWorkloadKindFromUrl", () => {
it('returns "endpoints" for "/api/v1/namespaces/default/endpoints/kubernetes"', () => {
const str = "/api/v1/namespaces/default/endpoints/kubernetes";
const lastSegment = getWorkloadKindFromUrl(str);
expect(lastSegment).toEqual("endpoints");
});
it('returns "namespaces" for "/api/v1/namespaces/acme-org"', () => {
const str = "/api/v1/namespaces/acme-org";
const lastSegment = getWorkloadKindFromUrl(str);
expect(lastSegment).toEqual("namespaces");
});
it('returns "bar" for "/foo/bar/"', () => {
const str = "/foo/bar/";
const lastSegment = getWorkloadKindFromUrl(str);
expect(lastSegment).toEqual("bar");
});
it('returns null for ""', () => {
const str = "";
const lastSegment = getWorkloadKindFromUrl(str);
expect(lastSegment).toBeNull();
});
it('returns null for "/"', () => {
const str = "/";
const lastSegment = getWorkloadKindFromUrl(str);
expect(lastSegment).toBeNull();
});
it('returns null for "invalidurl"', () => {
const str = "invalidurl";
const lastSegment = getWorkloadKindFromUrl(str);
expect(lastSegment).toBeNull();
});
});

View File

@ -0,0 +1,14 @@
/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
export function getWorkloadKindFromUrl(url: string) {
return getLastSegment(url);
}
function getLastSegment(url: string) {
const regex = /\/([^/]*)\/[^/]*$/;
const result = regex.exec(url);
return result ? result[1] : null;
}