Created
July 19, 2021 08:34
-
-
Save omochi/c078d195f921089e2af7e93437a65d94 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BracketQueryEncoder { | |
private result: [string, string][] = []; | |
encode(json: any): [string, string][] { | |
this.result = []; | |
this.encodeStep([], json); | |
return this.result; | |
} | |
encodeStep(path: string[], json: any) { | |
if (typeof json == "object") { | |
for (const key of Object.keys(json)) { | |
this.encodeStep(path.concat(key), json[key]); | |
} | |
} else if (typeof json == "string") { | |
this.result.push([this.pathString(path), json]); | |
} else { | |
// ... | |
} | |
} | |
pathString(path: string[]): string { | |
var str = ""; | |
path.forEach((elem, i) => { | |
if (i == 0) { | |
str = elem; | |
} else { | |
str += "[" + elem + "]"; | |
} | |
}); | |
return str; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment