You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
745 B
40 lines
745 B
import { Readable } from "node:stream";
|
|
|
|
const objType = (obj) => {
|
|
if (obj === null) {
|
|
return 'null'
|
|
}
|
|
if (Array.isArray(obj)) {
|
|
return 'array'
|
|
}
|
|
|
|
return typeof obj
|
|
}
|
|
|
|
function* gronGenerator(obj, root = `json`) {
|
|
switch (objType(obj)) {
|
|
case 'object':
|
|
yield `${root}={};\n`;
|
|
for (const key in obj) {
|
|
yield* gronGenerator(obj[key], `${root}.${key}`);
|
|
}
|
|
break;
|
|
case 'array':
|
|
yield `${root}=[];\n`;
|
|
for (let i = 0; i < obj.length; i++) {
|
|
yield* gronGenerator(obj[i], `${root}[${i}]`);
|
|
}
|
|
break;
|
|
|
|
default:
|
|
yield `${root}=${JSON.stringify(obj)};\n`;
|
|
|
|
}
|
|
}
|
|
|
|
export const gronReadStream = (obj) => {
|
|
|
|
return Readable.from(gronGenerator(obj));
|
|
|
|
}
|