From 6016c7e6d94cff037dc5c689c188ca8c781fe263 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Sat, 20 Jan 2024 11:13:34 -0800 Subject: [PATCH] getparentid --- d2js/js.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/d2js/js.go b/d2js/js.go index 7c6a0c880..a642ed625 100644 --- a/d2js/js.go +++ b/d2js/js.go @@ -20,6 +20,7 @@ import ( ) func main() { + js.Global().Set("d2GetParentID", js.FuncOf(jsGetParentID)) js.Global().Set("d2GetObjOrder", js.FuncOf(jsGetObjOrder)) js.Global().Set("d2GetRefRanges", js.FuncOf(jsGetRefRanges)) js.Global().Set("d2Compile", js.FuncOf(jsCompile)) @@ -64,6 +65,26 @@ func jsGetObjOrder(this js.Value, args []js.Value) interface{} { return string(str) } +func jsGetParentID(this js.Value, args []js.Value) interface{} { + id := args[0].String() + + mk, _ := d2parser.ParseMapKey(id) + + if len(mk.Edges) > 0 { + return "" + } + + if mk.Key != nil { + if len(mk.Key.Path) == 1 { + return "root" + } + mk.Key.Path = mk.Key.Path[:len(mk.Key.Path)-1] + return strings.Join(mk.Key.IDA(), ".") + } + + return "" +} + type jsRefRanges struct { Ranges []d2ast.Range `json:"ranges"` ParseError string `json:"parseError"`