Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: maintain property order #45

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading