Skip to content

Commit

Permalink
Revision 0.31.15 (#594)
Browse files Browse the repository at this point in the history
* Support Unicode Hash

* Version
  • Loading branch information
sinclairzx81 authored Sep 14, 2023
1 parent 6b27fab commit f2ccea9
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sinclair/typebox",
"version": "0.31.14",
"version": "0.31.15",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
Expand Down
6 changes: 2 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ type T = Static<typeof T> // type T = {
## Overview
TypeBox is a runtime type builder that creates Json Schema objects that infer as TypeScript types. The schemas produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type that can be statically checked by TypeScript or runtime asserted using standard Json Schema validation.
TypeBox is a runtime type builder that creates in-memory JSON Schema objects that can be statically inferred as TypeScript types. The schemas produced by this library are designed to match the static type assertion rules of the TypeScript compiler. TypeBox enables one to create a unified type that can be statically checked by TypeScript and runtime asserted using standard JSON Schema validation.
TypeBox uses industry standard schematics for runtime type representation; enabling types to be reflected, serialized and published directly. Its type system is fully extensible and able to support type representation for multiple schema specifications. It also provides a high performance validation compiler, various tools for working with dynamic data and offers detailed structured error reporting.
TypeBox can be used as a simple tool to build up complex schemas or integrated into applications and frameworks to enable high performance runtime type checking for data received over the wire.
This library is designed to enable JSON schema to compose with the same flexibility as TypeScript's type system. It can be used as a simple tool to build up complex schemas or integrated into REST or RPC services to help validate data received over the wire.
License MIT
Expand Down
13 changes: 12 additions & 1 deletion src/value/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ const F64 = new Float64Array(1)
const F64In = new DataView(F64.buffer)
const F64Out = new Uint8Array(F64.buffer)
// --------------------------------------------------------------------------
// NumberToBytes
// --------------------------------------------------------------------------
function* NumberToBytes(value: number): IterableIterator<number> {
const byteCount = value === 0 ? 1 : Math.ceil(Math.floor(Math.log2(value) + 1) / 8)
for (let i = 0; i < byteCount; i++) {
yield (value >> (8 * (byteCount - 1 - i))) & 0xff
}
}
// --------------------------------------------------------------------------
// Hashing Functions
// --------------------------------------------------------------------------
function ArrayType(value: Array<unknown>) {
Expand Down Expand Up @@ -105,7 +114,9 @@ function ObjectType(value: Record<string, unknown>) {
function StringType(value: string) {
FNV1A64(ByteMarker.String)
for (let i = 0; i < value.length; i++) {
FNV1A64(value.charCodeAt(i))
for (const byte of NumberToBytes(value.charCodeAt(i))) {
FNV1A64(byte)
}
}
}
function SymbolType(value: symbol) {
Expand Down
11 changes: 11 additions & 0 deletions test/runtime/value/hash/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,15 @@ describe('value/hash/Hash', () => {
const B = ValueHash.Hash(BigInt(1))
Assert.IsEqual(A, B)
})
// ----------------------------------------------------------------
// Unicode
// ----------------------------------------------------------------
it('Should hash unicode 1 (retain single byte hash)', () => {
const hash = ValueHash.Hash('a')
Assert.IsEqual(hash, 586962220959696054n)
})
it('Should hash unicode 2', () => {
const hash = ValueHash.Hash('안녕 세계')
Assert.IsEqual(hash, 11219208047802711777n)
})
})

0 comments on commit f2ccea9

Please sign in to comment.