Revision 0.31.15 (#594)

* Support Unicode Hash

* Version
This commit is contained in:
sinclairzx81
2023-09-14 16:15:24 +09:00
committed by GitHub
parent 6b27faba28
commit f2ccea9623
5 changed files with 28 additions and 8 deletions
+2 -2
View File
@@ -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",
+1 -1
View File
@@ -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",
+2 -4
View File
@@ -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
+12 -1
View File
@@ -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>) {
@@ -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) {
+11
View File
@@ -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)
})
})