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)