Skip to content

Commit

Permalink
fix: binary data string key
Browse files Browse the repository at this point in the history
  • Loading branch information
tien committed Oct 22, 2024
1 parent fe28a8c commit fccd977
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 26 deletions.
5 changes: 5 additions & 0 deletions .changeset/plenty-rocks-smell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@reactive-dot/core": patch
---

Fixed string key generation of binary data.
6 changes: 4 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
"scripts": {
"dev": "tsc --build --watch",
"build": "rm -rf build && tsc --build",
"lint": "eslint src"
"lint": "eslint src",
"test": "vitest run"
},
"dependencies": {
"@reactive-dot/utils": "workspace:^"
Expand All @@ -42,7 +43,8 @@
"@tsconfig/strictest": "^2.0.5",
"eslint": "^9.13.0",
"polkadot-api": "^1.6.2",
"typescript": "^5.6.3"
"typescript": "^5.6.3",
"vitest": "^2.1.3"
},
"peerDependencies": {
"polkadot-api": "1.x"
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/utils/__snapshots__/stringify.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html

exports[`matches snapshot 1`] = `"{"array":[4,2,6,1],"bigInteger":"420","binary":"0x48656c6c6f2c20776f726c6421","boolean":true,"float":1.5,"integer":1,"null":null,"object":{"a":0,"b":1,"c":2},"string":"Hello, world!"}"`;
20 changes: 20 additions & 0 deletions packages/core/src/utils/stringify.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { stringify } from "./stringify.js";
import { Binary } from "polkadot-api";
import { expect, it } from "vitest";

it("matches snapshot", () => {
const string = stringify({
undefined: undefined,
null: null,
boolean: true,
integer: 1,
bigInteger: 420n,
float: 1.5,
string: "Hello, world!",
object: { b: 1, c: 2, a: 0 },
array: [4, 2, 6, 1],
binary: Binary.fromText("Hello, world!"),
});

expect(string).toMatchSnapshot();
});
52 changes: 29 additions & 23 deletions packages/core/src/utils/stringify.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
function hasObjectPrototype(o: unknown) {
return Object.prototype.toString.call(o) === "[object Object]";
import { Binary } from "polkadot-api";

export function stringify<T>(queryInstruction: T) {
return JSON.stringify(queryInstruction, (_, value) => {
if (typeof value === "bigint") {
return value.toString();
}

if (value instanceof Binary) {
return value.asHex();
}

if (isPlainObject(value)) {
return Object.keys(value)
.sort()
.reduce(
(result, key) => {
result[key] = value[key as keyof typeof value];
return result;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{} as any,
);
}

return value;
});
}

function isPlainObject(value: unknown): value is object {
Expand All @@ -24,25 +49,6 @@ function isPlainObject(value: unknown): value is object {
return true;
}

export function stringify<T>(queryInstruction: T) {
return JSON.stringify(queryInstruction, (_, value) => {
if (typeof value === "bigint") {
return value.toString();
}

if (isPlainObject(value)) {
return Object.keys(value)
.sort()
.reduce(
(result, key) => {
result[key] = value[key as keyof typeof value];
return result;
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{} as any,
);
}

return value;
});
function hasObjectPrototype(o: unknown) {
return Object.prototype.toString.call(o) === "[object Object]";
}
3 changes: 2 additions & 1 deletion packages/core/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
"sourceMap": true,
"outDir": "build"
},
"include": ["src"]
"include": ["src"],
"exclude": ["**/*.test.*"]
}
3 changes: 3 additions & 0 deletions packages/core/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from "vitest/config";

export default defineConfig({});
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4852,6 +4852,7 @@ __metadata:
eslint: "npm:^9.13.0"
polkadot-api: "npm:^1.6.2"
typescript: "npm:^5.6.3"
vitest: "npm:^2.1.3"
peerDependencies:
polkadot-api: 1.x
languageName: unknown
Expand Down

0 comments on commit fccd977

Please sign in to comment.