Skip to content

Commit

Permalink
Improve performance further and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding committed Oct 21, 2024
1 parent 5a68cf2 commit 9b35af5
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('index', () => {
"KnownCaipNamespace",
"PendingJsonRpcResponseStruct",
"StrictHexStruct",
"UnsafeFastJsonStruct",
"UnsafeJsonStruct",
"VersionRangeStruct",
"VersionStruct",
Expand Down
8 changes: 2 additions & 6 deletions src/json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ describe('json', () => {
const [error] = validate(undefined, JsonStruct);
assert(error !== undefined);
expect(error.message).toBe(
'Expected the value to satisfy a union of `literal | boolean | finite number | string | array | record`, but received: undefined',
'Expected a value of type `JSON`, but received: `undefined`',
);
});
});
Expand Down Expand Up @@ -432,11 +432,7 @@ describe('json', () => {
},
},
};
const now = performance.now();
expect(() =>
assertIsJsonRpcRequest(request),
).not.toThrow();
console.log('Asserting took', performance.now() - now);
expect(() => assertIsJsonRpcRequest(request)).not.toThrow();
});
});

Expand Down
24 changes: 19 additions & 5 deletions src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,36 @@ function validateJson(json: unknown): boolean {
}

if (typeof json === 'object') {
let every = true;
if (Array.isArray(json)) {
return json.every((value) => validateJson(value));
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < json.length; i++) {
if (!validateJson(json[i])) {
every = false;
break;
}
}
return every;
}

const entries = Object.entries(json);
return entries.every(
([key, value]) => typeof key === 'string' && validateJson(value),
);
// eslint-disable-next-line @typescript-eslint/prefer-for-of
for (let i = 0; i < entries.length; i++) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (typeof entries[i]![0] !== 'string' || !validateJson(entries[i]![1])) {
every = false;
break;
}
}
return every;
}

return false;
}

/**
* A struct to check if the given value is a valid JSON-serializable value.
*
*
* A faster alternative to {@link UnsafeJsonStruct}.
*
* Note that this struct is unsafe. For safe validation, use {@link JsonStruct}.
Expand Down
1 change: 1 addition & 0 deletions src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('node', () => {
"KnownCaipNamespace",
"PendingJsonRpcResponseStruct",
"StrictHexStruct",
"UnsafeFastJsonStruct",
"UnsafeJsonStruct",
"VersionRangeStruct",
"VersionStruct",
Expand Down

0 comments on commit 9b35af5

Please sign in to comment.