function encode(obj) {
const $my = Symbol('my');
const $open = Symbol("open");
const $close = Symbol("close");
const $is = Symbol("is");
const $are = Symbol("are");
const $done = Symbol("done");
const gen = helper(obj, 0);
let _peek = null;
try {
for (;;) {
}
} catch (e) {
if (e !== $done) {
throw e;
}
}
function peek() {
if (_peek === null) {
_peek = pop();
}
return _peek;
}
function pop() {
if (_peek !== null) {
const temp = _peek;
_peek = null;
return temp;
}
const { value, done } = gen.next();
if (done) {
throw $done;
}
return value;
}
function* helper(obj, depth) {
for (const [key, value] of Object.entries(obj)) {
if (depth === 0) yield "My";
yield key;
if (typeof value === 'object') {
yield $is;
yield $open;
yield* helper(value, depth+1);
} else if (Array.isArray(value)) {
yield $are;
yield $open;
for (const item of value) {
if (typeof item === 'object') {
yield* helper(item, depth+1);
}
}
yield $close;
} else {
yield $is;
yield value;
}
}
}
}