Skip to content

Commit

Permalink
Update Intersect Transform Logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Nov 20, 2023
1 parent ea217cc commit 6e79451
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 213 deletions.
70 changes: 35 additions & 35 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
import { TypeSystem } from '@sinclair/typebox/system'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import { Value, ValuePointer } from '@sinclair/typebox/value'
import { Type, TypeGuard, Kind, Static, TSchema } from '@sinclair/typebox'

// -----------------------------------------------------------
// Create: Type
// -----------------------------------------------------------

const T = Type.Object({
x: Type.Number(),
y: Type.Number(),
z: Type.Number(),
import { Type, StaticDecode, StaticEncode, Static } from '@sinclair/typebox'

const TBItem = Type.Transform(
Type.Object({
isHybrid: Type.Boolean(),
}),
)
.Decode((value) => ({ isHybrid: value.isHybrid ? 1 : 0 }))
.Encode((value) => ({ isHybrid: value.isHybrid === 1 ? true : false }))

let decoded = Value.Decode(TBItem, { isHybrid: true })
let encoded = Value.Encode(TBItem, { isHybrid: 1 })
console.log('decoded', decoded)
console.log('encoded', encoded)

const TBIntersect = Type.Intersect([
Type.Object({
model: Type.String(),
}),
Type.Object({
features: Type.Array(TBItem),
}),
])

const aencoded = Value.Encode(TBIntersect, {
model: 'Prius',
features: [
{
isHybrid: 1,
},
{
isHybrid: 1,
},
],
})

type T = Static<typeof T>

console.log(T)

// -----------------------------------------------------------
// Create: Value
// -----------------------------------------------------------

const V = Value.Create(T)

console.log(V)

// -----------------------------------------------------------
// Compile: Type
// -----------------------------------------------------------

const C = TypeCompiler.Compile(T)

console.log(C.Code())

// -----------------------------------------------------------
// Check: Value
// -----------------------------------------------------------

console.log(C.Check(V))
console.log(aencoded)
10 changes: 5 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1719,11 +1719,11 @@ The following table lists esbuild compiled and minified sizes for each TypeBox m
┌──────────────────────┬────────────┬────────────┬─────────────┐
│ (index) │ CompiledMinifiedCompression
├──────────────────────┼────────────┼────────────┼─────────────┤
typebox/compiler'148.9 kb'' 65.8 kb''2.26 x'
typebox/errors'111.5 kb'' 49.1 kb''2.27 x'
typebox/system' 82.6 kb'' 36.8 kb''2.24 x'
typebox/value'190.5 kb'' 82.4 kb''2.31 x'
typebox' 72.4 kb'' 31.6 kb''2.29 x'
typebox/compiler'163.6 kb'' 71.6 kb''2.28 x'
typebox/errors'113.3 kb'' 50.1 kb''2.26 x'
typebox/system' 83.9 kb'' 37.5 kb''2.24 x'
typebox/value'191.1 kb'' 82.3 kb''2.32 x'
typebox' 73.8 kb'' 32.3 kb''2.29 x'
└──────────────────────┴────────────┴────────────┴─────────────┘
```
Expand Down
Loading

0 comments on commit 6e79451

Please sign in to comment.