-
Notifications
You must be signed in to change notification settings - Fork 163
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df2cdfc
commit b2c5420
Showing
3 changed files
with
93 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,39 @@ | ||
import { TypeSystem } from '@sinclair/typebox/system' | ||
import { TypeCompiler } from '@sinclair/typebox/compiler' | ||
import { Value, ValueGuard } from '@sinclair/typebox/value' | ||
import { Type, TypeGuard, Kind, Static, TSchema, KeyResolver } from '@sinclair/typebox' | ||
import { Type, TypeGuard, Kind, Static, TSchema, KeyResolver, TObject } from '@sinclair/typebox' | ||
import { Run } from './benchmark' | ||
|
||
// Todo: Investigate Union Default (initialize interior types) | ||
// Todo: Implement Value.Clean() Tests | ||
|
||
const T = Type.Intersect([Type.Number(), Type.Number()]) | ||
const R = Value.Clean(T, { u: null, x: 1 }) | ||
console.log(R) | ||
// const T = Type.Intersect([ | ||
// Type.Object({ x: Type.Number() }), | ||
// Type.Object({ y: Type.Number() }) | ||
// ], { | ||
// unevaluatedProperties: Type.Union([ | ||
// Type.Number(), | ||
// Type.Null() | ||
// ]) | ||
// }) | ||
|
||
// const T = Type.Object( | ||
// { | ||
// number: Type.Number({ default: 1 }), | ||
// negNumber: Type.Number({ default: 1 }), | ||
// maxNumber: Type.Number({ default: 3 }), | ||
// string: Type.String({ default: 5 }), | ||
// longString: Type.String(), | ||
// boolean: Type.Boolean(), | ||
// deeplyNested: Type.Object({ | ||
// foo: Type.String({ default: 2 }), | ||
// num: Type.Number(), | ||
// bool: Type.Boolean(), | ||
// }), | ||
// }, | ||
// { | ||
// additionalProperties: Type.Any(), | ||
// }, | ||
// ) | ||
function ParseEnv<T extends TObject>(schema: T, value: unknown = process.env): Static<T> { | ||
const converted = Value.Convert(schema, value) | ||
const defaulted = Value.Default(schema, converted) | ||
const checked = Value.Check(schema, defaulted) | ||
return checked | ||
? (Value.Clean(schema, defaulted) as Static<T>) | ||
: (() => { | ||
const first = Value.Errors(schema, defaulted).First()! | ||
throw new Error(`${first.message} ${first.path}. Value is ${first.value}`) | ||
})() | ||
} | ||
console.log(process.env) | ||
|
||
// const A = Value.Default(T, {}) | ||
// console.log(A) | ||
const R = Run( | ||
() => { | ||
const E = ParseEnv( | ||
Type.Object({ | ||
PROCESSOR_ARCHITECTURE: Type.String(), | ||
PROCESSOR_IDENTIFIER: Type.String(), | ||
PROCESSOR_LEVEL: Type.Number(), | ||
PROCESSOR_REVISION: Type.Number(), | ||
}), | ||
) | ||
}, | ||
{ | ||
iterations: 2000, | ||
}, | ||
) | ||
|
||
// console.log(Value.Transmute({}, {})) | ||
console.log(R) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Type, Static, TSchema } from '@sinclair/typebox' | ||
import { Value } from '@sinclair/typebox/value' | ||
|
||
/** User defined parse function */ | ||
export function Parse<T extends TSchema>(schema: T, value: unknown): Static<T> { | ||
const converted = Value.Convert(schema, value) | ||
const defaulted = Value.Default(schema, converted) | ||
const checked = Value.Check(schema, defaulted) | ||
return checked ? Value.Clean(schema, defaulted) as Static<T> : (() => { | ||
const first = Value.Errors(schema, defaulted).First()! | ||
throw new Error(`${first.message} ${first.path}. Value is ${first.value}`) | ||
})() | ||
} | ||
|
||
// Parse some environment variables | ||
const environment = Parse(Type.Object({ | ||
PROCESSOR_ARCHITECTURE: Type.String(), | ||
PROCESSOR_IDENTIFIER: Type.String(), | ||
PROCESSOR_LEVEL: Type.Number(), | ||
PROCESSOR_REVISION: Type.Number(), | ||
}), process.env) | ||
|
||
console.log(environment) |