Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: sinclairzx81/typebox
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 0.26.3
Choose a base ref
...
head repository: sinclairzx81/typebox
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 0.26.4
Choose a head ref
  • 1 commit
  • 3 files changed
  • 1 contributor

Commits on Mar 26, 2023

  1. Verified

    This commit was signed with the committer’s verified signature.
    dmathieu Damien Mathieu
    Copy the full SHA
    89564f1 View commit details
Showing with 34 additions and 30 deletions.
  1. +1 −1 package.json
  2. +29 −29 src/compiler/compiler.ts
  3. +4 −0 test/runtime/compiler/object.ts
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.26.3",
"version": "0.26.4",
"description": "JSONSchema Type Builder with Static Type Resolution for TypeScript",
"keywords": [
"typescript",
58 changes: 29 additions & 29 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
@@ -60,14 +60,38 @@ namespace Character {
export function DollarSign(code: number) {
return code === 36
}
export function Underscore(code: number) {
export function IsUnderscore(code: number) {
return code === 95
}
export function Numeric(code: number) {
export function IsAlpha(code: number) {
return (code >= 64 && code <= 90) || (code >= 97 && code <= 122)
}
export function IsNumeric(code: number) {
return code >= 48 && code <= 57
}
export function Alpha(code: number) {
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
}
// -------------------------------------------------------------------
// MemberExpression
// -------------------------------------------------------------------
namespace MemberExpression {
function IsFirstCharacterNumeric(value: string) {
if (value.length === 0) return false
return Character.IsNumeric(value.charCodeAt(0))
}
function IsAccessor(value: string) {
if (IsFirstCharacterNumeric(value)) return false
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i)
const check = Character.IsAlpha(code) || Character.IsNumeric(code) || Character.DollarSign(code) || Character.IsUnderscore(code)
if (!check) return false
}
return true
}
function EscapeHyphen(key: string) {
return key.replace(/'/g, "\\'")
}
export function Encode(object: string, key: string) {
return IsAccessor(key) ? `${object}.${key}` : `${object}['${EscapeHyphen(key)}']`
}
}
// -------------------------------------------------------------------
@@ -78,7 +102,7 @@ namespace Identifier {
const buffer: string[] = []
for (let i = 0; i < $id.length; i++) {
const code = $id.charCodeAt(i)
if (Character.Numeric(code) || Character.Alpha(code)) {
if (Character.IsNumeric(code) || Character.IsAlpha(code)) {
buffer.push($id.charAt(i))
} else {
buffer.push(`_${code}_`)
@@ -88,30 +112,6 @@ namespace Identifier {
}
}
// -------------------------------------------------------------------
// MemberExpression
// -------------------------------------------------------------------
export namespace MemberExpression {
function Check(propertyName: string) {
if (propertyName.length === 0) return false
{
const code = propertyName.charCodeAt(0)
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code))) {
return false
}
}
for (let i = 1; i < propertyName.length; i++) {
const code = propertyName.charCodeAt(i)
if (!(Character.DollarSign(code) || Character.Underscore(code) || Character.Alpha(code) || Character.Numeric(code))) {
return false
}
}
return true
}
export function Encode(object: string, key: string) {
return !Check(key) ? `${object}['${key}']` : `${object}.${key}`
}
}
// -------------------------------------------------------------------
// TypeCompiler
// -------------------------------------------------------------------
export class TypeCompilerUnknownTypeError extends Error {
4 changes: 4 additions & 0 deletions test/runtime/compiler/object.ts
Original file line number Diff line number Diff line change
@@ -132,12 +132,16 @@ describe('type/compiler/Object', () => {
'0-leading': Type.Literal(2),
'$-leading': Type.Literal(3),
'!@#$%^&*(': Type.Literal(4),
'node-mirror:release': Type.Literal(5), // issue: 353
"a'a": Type.Literal(6),
})
Ok(T, {
'with-hyphen': 1,
'0-leading': 2,
'$-leading': 3,
'!@#$%^&*(': 4,
'node-mirror:release': 5,
"a'a": 6,
})
})
it('Should validate schema additional properties of string', () => {