Skip to content

Commit

Permalink
Revision 0.9.14
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 27, 2024
1 parent a0cc8ba commit dab3693
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 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.9.13",
"version": "0.9.14",
"description": "Code Generation Tools for TypeBox",
"main": "index.js",
"keywords": [
Expand Down
44 changes: 43 additions & 1 deletion src/model/model-to-typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,48 @@ import { Formatter } from '../common/formatter'
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 @@ -90,7 +132,7 @@ export namespace ModelToTypeScript {
(optional && readonly) ? `readonly ${key}?: ${Visit(property)}` :
readonly ? `readonly ${key}: ${Visit(property)}` :
optional ? `${key}?: ${Visit(property)}` :
`${key}: ${Visit(property)}`
`${PropertyKey.Encode(key)}: ${Visit(property)}`
)
}).join(',\n')
return `{\n${properties}\n}`
Expand Down

0 comments on commit dab3693

Please sign in to comment.