diff --git a/src/util/slicerURL/mainSlicerUrl.ts b/src/util/slicerURL/mainSlicerUrl.ts new file mode 100644 index 0000000..d3b029f --- /dev/null +++ b/src/util/slicerURL/mainSlicerUrl.ts @@ -0,0 +1,13 @@ +import toolsSlicerUrl from "./toolsSlicerUrl.ts"; + +export default (at: number) => + at < 4 + ? (url: string) => url.slice(url.indexOf("/", url.indexOf("/") + 2)) + : toolsSlicerUrl.atDomain( + Array(at - 4) + .fill(null) + .reduceRight( + (acc) => toolsSlicerUrl.subFolder(acc), + toolsSlicerUrl.lastFolder, + ), + ); diff --git a/src/util/slicerURL/toolsSlicerUrl.ts b/src/util/slicerURL/toolsSlicerUrl.ts new file mode 100644 index 0000000..1256550 --- /dev/null +++ b/src/util/slicerURL/toolsSlicerUrl.ts @@ -0,0 +1,15 @@ +export default { + atDomain: + (f: { (currentPosition: number): (url: string) => number }) => + (url: string) => + ( + (currentPosition) => url.slice(f(currentPosition + 1)(url)) + )( + url.indexOf("/", url.indexOf("/") + 2), + ), + subFolder: + (f: { (currentPosition: number): (url: string) => number }) => + (position: number) => + (url: string) => f(url.indexOf("/", position) + 1)(url), + lastFolder: (position: number) => (url: string) => url.indexOf("/", position), +}; diff --git a/test/util/slicerURL.test.ts b/test/util/slicerURL.test.ts new file mode 100644 index 0000000..4274cf0 --- /dev/null +++ b/test/util/slicerURL.test.ts @@ -0,0 +1,27 @@ +import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; +import mainSlicerUrl from "../../src/util/slicerURL/mainSlicerUrl.ts"; + +const baseURL = "https://example.com/1/2/3/4/5"; + +Deno.test("SlierURL", () => { + assertEquals( + mainSlicerUrl(2)(baseURL), + "/1/2/3/4/5", + ); + assertEquals( + mainSlicerUrl(3)(baseURL), + "/1/2/3/4/5", + ); + assertEquals( + mainSlicerUrl(4)(baseURL), + "/2/3/4/5", + ); + assertEquals( + mainSlicerUrl(5)(baseURL), + "/3/4/5", + ); + assertEquals( + mainSlicerUrl(6)(baseURL), + "/4/5", + ); +});