Skip to content

Instantly share code, notes, and snippets.

@omochi
Created July 19, 2021 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save omochi/c078d195f921089e2af7e93437a65d94 to your computer and use it in GitHub Desktop.
Save omochi/c078d195f921089e2af7e93437a65d94 to your computer and use it in GitHub Desktop.
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