From 1d2f25ef043087bcae1af59e812e3b478b7bbf46 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Mon, 7 Feb 2022 15:51:16 +0100 Subject: [PATCH] added parent to ObjID --- src/types/ObjID.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/types/ObjID.ts b/src/types/ObjID.ts index f111005..fad7a8f 100644 --- a/src/types/ObjID.ts +++ b/src/types/ObjID.ts @@ -53,6 +53,30 @@ class ObjID { return new ObjID(this.bucket,parts.join("/")) } + /** + * Get the parent of the object. If Obj is a file then the containing directory + * if Obj is a directory it returns the parent + * @returns parent ObjID or null if already at root + */ + public parent(): ObjID | null { + if (this.key == "/") { + // Already at root. We dont have a parent + return null + } + + if (this.isDirectory()) { + const parts = this.key.split("/") + const parent = new ObjID(this.bucket, parts.slice(0,-2).join("/") + "/") + parent.normalize() + return parent + } else { + const parts = this.key.split("/") + const parent = new ObjID(this.bucket, parts.slice(0,-1).join("/") + "/") + parent.normalize() + return parent + } + } + public static fromString(from: string): ObjID{ const match = stringRegex.exec(from)