getparentid

This commit is contained in:
Alexander Wang 2024-01-20 11:13:34 -08:00
parent 9b2f68c89f
commit 6016c7e6d9
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927

View file

@ -20,6 +20,7 @@ import (
) )
func main() { func main() {
js.Global().Set("d2GetParentID", js.FuncOf(jsGetParentID))
js.Global().Set("d2GetObjOrder", js.FuncOf(jsGetObjOrder)) js.Global().Set("d2GetObjOrder", js.FuncOf(jsGetObjOrder))
js.Global().Set("d2GetRefRanges", js.FuncOf(jsGetRefRanges)) js.Global().Set("d2GetRefRanges", js.FuncOf(jsGetRefRanges))
js.Global().Set("d2Compile", js.FuncOf(jsCompile)) js.Global().Set("d2Compile", js.FuncOf(jsCompile))
@ -64,6 +65,26 @@ func jsGetObjOrder(this js.Value, args []js.Value) interface{} {
return string(str) 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 { type jsRefRanges struct {
Ranges []d2ast.Range `json:"ranges"` Ranges []d2ast.Range `json:"ranges"`
ParseError string `json:"parseError"` ParseError string `json:"parseError"`