mirror of
https://github.com/Steffo99/sophon.git
synced 2024-12-23 07:14:21 +00:00
✨ Add PathSplitter
utility class
This commit is contained in:
parent
0d5ac18fcf
commit
2662d70517
2 changed files with 130 additions and 0 deletions
78
frontend/src/utils/PathSplitter.test.js
Normal file
78
frontend/src/utils/PathSplitter.test.js
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
import {splitPath} from "./PathSplitter"
|
||||||
|
|
||||||
|
|
||||||
|
test("splits empty path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/")
|
||||||
|
).toMatchObject(
|
||||||
|
{}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("splits instance path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/i/https:api:sophon:steffo:eu:")
|
||||||
|
).toMatchObject(
|
||||||
|
{
|
||||||
|
instance: "https:api:sophon:steffo:eu:"
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("splits username path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/i/https:api:sophon:steffo:eu:/u/steffo")
|
||||||
|
).toMatchObject(
|
||||||
|
{
|
||||||
|
instance: "https:api:sophon:steffo:eu:",
|
||||||
|
userName: "steffo",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("splits userid path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/i/https:api:sophon:steffo:eu:/u/1")
|
||||||
|
).toMatchObject(
|
||||||
|
{
|
||||||
|
instance: "https:api:sophon:steffo:eu:",
|
||||||
|
userId: "1",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("splits research group path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/i/https:api:sophon:steffo:eu:/g/testers")
|
||||||
|
).toMatchObject(
|
||||||
|
{
|
||||||
|
instance: "https:api:sophon:steffo:eu:",
|
||||||
|
researchGroup: "testers",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("splits research project path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/i/https:api:sophon:steffo:eu:/g/testers/p/test")
|
||||||
|
).toMatchObject(
|
||||||
|
{
|
||||||
|
instance: "https:api:sophon:steffo:eu:",
|
||||||
|
researchGroup: "testers",
|
||||||
|
researchProject: "test",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("splits research project path", () => {
|
||||||
|
expect(
|
||||||
|
splitPath("/i/https:api:sophon:steffo:eu:/g/testers/p/test/n/testerino")
|
||||||
|
).toMatchObject(
|
||||||
|
{
|
||||||
|
instance: "https:api:sophon:steffo:eu:",
|
||||||
|
researchGroup: "testers",
|
||||||
|
researchProject: "test",
|
||||||
|
notebook: "testerino",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
52
frontend/src/utils/PathSplitter.ts
Normal file
52
frontend/src/utils/PathSplitter.ts
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/**
|
||||||
|
* Possible contents of the path.
|
||||||
|
*/
|
||||||
|
export interface SplitPath {
|
||||||
|
/**
|
||||||
|
* The URL of the Sophon instance.
|
||||||
|
*/
|
||||||
|
instance?: string,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The numeric id of the user.
|
||||||
|
*/
|
||||||
|
userId?: string,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The username of the user.
|
||||||
|
* @warning Matches {@link userId} if it is defined.
|
||||||
|
*/
|
||||||
|
userName?: string,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The research group slug.
|
||||||
|
*/
|
||||||
|
researchGroup?: string,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The research project slug.
|
||||||
|
*/
|
||||||
|
researchProject?: string,
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The notebook slug.
|
||||||
|
*/
|
||||||
|
notebook?: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Split the URL path into various components.
|
||||||
|
* @param path - The path to split.
|
||||||
|
*/
|
||||||
|
export function splitPath(path: string): SplitPath {
|
||||||
|
let result: SplitPath = {}
|
||||||
|
|
||||||
|
result.instance = path.match(/[/]i[/]([^/]+)/) ?.[1]
|
||||||
|
result.userId = path.match(/[/]u[/]([0-9]+)/) ?.[1]
|
||||||
|
result.userName = path.match(/[/]u[/]([A-Za-z0-9_-]+)/)?.[1]
|
||||||
|
result.researchGroup = path.match(/[/]g[/]([A-Za-z0-9_-]+)/)?.[1]
|
||||||
|
result.researchProject = path.match(/[/]p[/]([A-Za-z0-9_-]+)/)?.[1]
|
||||||
|
result.notebook = path.match(/[/]n[/]([A-Za-z0-9_-]+)/)?.[1]
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
Loading…
Reference in a new issue