Skip to content

Commit

Permalink
feat: maintain property order (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-ebey authored Aug 12, 2024
1 parent 6d55e96 commit f0f1537
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function stringify(this: ThisEncode, input: unknown, index: number) {

const partsForObj = (obj: any) =>
Object.keys(obj)
.map((k) => `"${flatten.call(this, k)}":${flatten.call(this, obj[k])}`)
.map((k) => `"_${flatten.call(this, k)}":${flatten.call(this, obj[k])}`)
.join(",");

switch (typeof input) {
Expand Down
24 changes: 24 additions & 0 deletions src/turbo-stream.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,30 @@ test("should encode and decode large payload", async () => {
expect(output).toEqual(input);
});

test("should encode and decode object maintaining property order for re-used keys", async () => {
const input = [
{ a: "a value 1", b: "b value" },
{ c: "c value", a: "a value 2" },
];
const output = await quickDecode(encode(input));
expect(JSON.stringify(output)).toEqual(JSON.stringify(input));
});

test("should encode and decode null prototype object maintaining property order for re-used keys", async () => {
const input = Object.create(null);
const test1 = Object.create(null);
test1.a = "a value 1";
test1.b = "b value";
input.test1 = test1;
const test2 = Object.create(null);
test2.c = "c value";
test2.a = "a value 2";
input.test2 = test2;

const output = await quickDecode(encode(input));
expect(JSON.stringify(output)).toEqual(JSON.stringify(input));
});

test("should encode and decode object and dedupe object key, value, and promise value", async () => {
const input = { foo: "bar", bar: "bar", baz: Promise.resolve("bar") };
const output = await quickDecode(encode(input));
Expand Down
8 changes: 4 additions & 4 deletions src/unflatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ function hydrate(this: ThisDecode, index: number): any {
case TYPE_NULL_OBJECT:
const obj = Object.create(null);
hydrated[index] = obj;
for (const key in b) {
for (const key of Object.keys(b).reverse()) {
const r: any[] = [];
stack.push([
b[key],
Expand All @@ -161,7 +161,7 @@ function hydrate(this: ThisDecode, index: number): any {
},
]);
stack.push([
Number(key),
Number(key.slice(1)),
(k) => {
r[0] = k;
},
Expand Down Expand Up @@ -244,7 +244,7 @@ function hydrate(this: ThisDecode, index: number): any {
const object: Record<string, unknown> = {};
hydrated[index] = object;

for (const key in value) {
for (const key of Object.keys(value).reverse()) {
const r: any[] = [];
stack.push([
(value as Record<string, number>)[key],
Expand All @@ -253,7 +253,7 @@ function hydrate(this: ThisDecode, index: number): any {
},
]);
stack.push([
Number(key),
Number(key.slice(1)),
(k) => {
r[0] = k;
},
Expand Down

0 comments on commit f0f1537

Please sign in to comment.