diff --git a/package-lock.json b/package-lock.json index 51286a3..fb6e034 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sinclair/typebox", - "version": "0.31.14", + "version": "0.31.15", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@sinclair/typebox", - "version": "0.31.14", + "version": "0.31.15", "license": "MIT", "devDependencies": { "@sinclair/hammer": "^0.17.1", diff --git a/package.json b/package.json index 13a376c..4ecd123 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/readme.md b/readme.md index cd4af3a..3133e0c 100644 --- a/readme.md +++ b/readme.md @@ -58,11 +58,9 @@ type T = Static // 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 diff --git a/src/value/hash.ts b/src/value/hash.ts index fdeec07..cd8935e 100644 --- a/src/value/hash.ts +++ b/src/value/hash.ts @@ -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 { + 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) { @@ -105,7 +114,9 @@ function ObjectType(value: Record) { 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) { diff --git a/test/runtime/value/hash/hash.ts b/test/runtime/value/hash/hash.ts index b93b676..8d06d50 100644 --- a/test/runtime/value/hash/hash.ts +++ b/test/runtime/value/hash/hash.ts @@ -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) + }) })