parent
f041fcb775
commit
59aeaaee7d
@ -0,0 +1,39 @@
|
||||
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));
|
||||
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
import test from "node:test"
|
||||
import assert from "node:assert"
|
||||
import { gronReadStream } from "./ngronstream.mjs";
|
||||
|
||||
const asString = async (stream) => {
|
||||
const chunks = [];
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(Buffer.from(chunk))
|
||||
}
|
||||
return Buffer.concat(chunks).toString('utf8');
|
||||
}
|
||||
|
||||
test("number", async () => {
|
||||
const rs = gronReadStream(3);
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `json=3;\n`)
|
||||
});
|
||||
|
||||
test("string", async () => {
|
||||
const rs = gronReadStream("hello")
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `json="hello";\n`)
|
||||
});
|
||||
|
||||
test("null", async () => {
|
||||
const rs = gronReadStream(null)
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `json=null;\n`)
|
||||
});
|
||||
|
||||
test("empty obj", async () => {
|
||||
const rs = gronReadStream({})
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `json={};\n`)
|
||||
});
|
||||
|
||||
test("empty array", async () => {
|
||||
const rs = gronReadStream([])
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `json=[];\n`)
|
||||
});
|
||||
|
||||
test("nested obj", async () => {
|
||||
const rs = gronReadStream({ one: 1 })
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `
|
||||
json={};
|
||||
json.one=1;
|
||||
`.trimStart())
|
||||
});
|
||||
|
||||
test("nested array", async () => {
|
||||
const rs = gronReadStream(["hello", "world"])
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `
|
||||
json=[];
|
||||
json[0]="hello";
|
||||
json[1]="world";
|
||||
`.trimStart())
|
||||
});
|
||||
|
||||
test("complex", async () => {
|
||||
const rs = gronReadStream([
|
||||
{ name: "Alice", startYear: 2001 },
|
||||
{ name: "Ralph", startYear: 2010 },
|
||||
{ name: "Jesse", startYear: 2007, tired: true },
|
||||
]);
|
||||
const result = await asString(rs);
|
||||
|
||||
assert.strictEqual(result, `
|
||||
json=[];
|
||||
json[0]={};
|
||||
json[0].name="Alice";
|
||||
json[0].startYear=2001;
|
||||
json[1]={};
|
||||
json[1].name="Ralph";
|
||||
json[1].startYear=2010;
|
||||
json[2]={};
|
||||
json[2].name="Jesse";
|
||||
json[2].startYear=2007;
|
||||
json[2].tired=true;
|
||||
`.trimStart());
|
||||
|
||||
});
|
Loading…
Reference in new issue