added parent to ObjID

This commit is contained in:
Djeeberjr 2022-02-07 15:51:16 +01:00
parent 9462a74437
commit 1d2f25ef04

View File

@ -53,6 +53,30 @@ class ObjID {
return new ObjID(this.bucket,parts.join("/")) 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{ public static fromString(from: string): ObjID{
const match = stringRegex.exec(from) const match = stringRegex.exec(from)