Skip to content

Commit

Permalink
Revision 0.10.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 27, 2024
1 parent 7ac1a14 commit 655325f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 53 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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-codegen",
"version": "0.10.0",
"version": "0.10.1",
"description": "Code Generation Tools for TypeBox",
"main": "index.js",
"keywords": [
Expand Down
12 changes: 6 additions & 6 deletions src/common/encoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ namespace Character {
return code === 95
}
export function IsAlpha(code: number) {
return (code >= 64 && code <= 90) || (code >= 97 && code <= 122)
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
}
export function IsNumeric(code: number) {
return code >= 48 && code <= 57
Expand Down Expand Up @@ -73,7 +73,7 @@ export namespace PropertyEncoder {
if (value.length === 0) return false
return Character.IsNumeric(value.charCodeAt(0))
}
function IsSafeProperty(value: string) {
function IsAccessor(value: string) {
if (IsFirstCharacterNumeric(value)) return false
for (let i = 0; i < value.length; i++) {
const code = value.charCodeAt(i)
Expand All @@ -82,10 +82,10 @@ export namespace PropertyEncoder {
}
return true
}
function Quoted(value: string) {
return `'${value}'`
function EscapeHyphen(key: string) {
return key.replace(/'/g, "\\'")
}
export function Encode(value: string) {
return IsSafeProperty(value) ? value : Quoted(value)
export function Encode(key: string) {
return IsAccessor(key) ? `${key}` : `'${EscapeHyphen(key)}'`
}
}
46 changes: 2 additions & 44 deletions src/model/model-to-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,52 +25,10 @@ THE SOFTWARE.
---------------------------------------------------------------------------*/

import { TypeBoxModel } from './model'
import { Formatter } from '../common/formatter'
import { Formatter, PropertyEncoder } from '../common/index'
import { TypeCompiler } from '@sinclair/typebox/compiler'
import * as Types from '@sinclair/typebox'

// ------------------------------------------------------------------
// Character
// ------------------------------------------------------------------
namespace Character {
export function DollarSign(code: number) {
return code === 36
}
export function IsUnderscore(code: number) {
return code === 95
}
export function IsAlpha(code: number) {
return (code >= 65 && code <= 90) || (code >= 97 && code <= 122)
}
export function IsNumeric(code: number) {
return code >= 48 && code <= 57
}
}
// ------------------------------------------------------------------
// PropertyKey
// ------------------------------------------------------------------
namespace PropertyKey {
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(key: string) {
return IsAccessor(key) ? `${key}` : `'${EscapeHyphen(key)}'`
}
}

export namespace ModelToTypeScript {
function Any(schema: Types.TAny) {
return 'any'
Expand Down Expand Up @@ -132,7 +90,7 @@ export namespace ModelToTypeScript {
(optional && readonly) ? `readonly ${key}?: ${Visit(property)}` :
readonly ? `readonly ${key}: ${Visit(property)}` :
optional ? `${key}?: ${Visit(property)}` :
`${PropertyKey.Encode(key)}: ${Visit(property)}`
`${PropertyEncoder.Encode(key)}: ${Visit(property)}`
)
}).join(',\n')
return `{\n${properties}\n}`
Expand Down

0 comments on commit 655325f

Please sign in to comment.