Skip to content

Commit

Permalink
Use Optimized Number Check
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 27, 2024
1 parent 1ba927c commit fb3e28c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/compiler/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export namespace Policy {
: `(typeof ${value} === 'object' && ${value} !== null && !(${value} instanceof Date) && !(${value} instanceof Uint8Array))`
}
export function IsNumberLike(value: string): string {
return !TypeSystemPolicy.AllowNaN ? `(typeof ${value} === 'number' && Number.isFinite(${value}))` : `typeof ${value} === 'number'`
return TypeSystemPolicy.AllowNaN ? `typeof ${value} === 'number'` : `Number.isFinite(${value})`
}
export function IsVoidLike(value: string): string {
return TypeSystemPolicy.AllowNullVoid ? `(${value} === undefined || ${value} === null)` : `${value} === undefined`
Expand Down Expand Up @@ -288,7 +288,7 @@ export namespace TypeCompiler {
yield `(typeof ${value} === 'function')`
}
function* FromInteger(schema: TInteger, references: TSchema[], value: string): IterableIterator<string> {
yield `(typeof ${value} === 'number' && Number.isInteger(${value}))`
yield `Number.isInteger(${value})`
if (IsNumber(schema.exclusiveMaximum)) yield `${value} < ${schema.exclusiveMaximum}`
if (IsNumber(schema.exclusiveMinimum)) yield `${value} > ${schema.exclusiveMinimum}`
if (IsNumber(schema.maximum)) yield `${value} <= ${schema.maximum}`
Expand Down
3 changes: 1 addition & 2 deletions src/system/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ export namespace TypeSystemPolicy {
}
/** Asserts this value using the AllowNaN policy */
export function IsNumberLike(value: unknown): value is number {
const isNumber = IsNumber(value)
return AllowNaN ? isNumber : isNumber && Number.isFinite(value)
return AllowNaN ? IsNumber(value) : Number.isFinite(value)
}
/** Asserts this value using the AllowVoidNull policy */
export function IsVoidLike(value: unknown): value is void {
Expand Down
2 changes: 1 addition & 1 deletion src/value/guard/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ export function IsNumber(value: unknown): value is number {
}
/** Returns true if this value is an integer */
export function IsInteger(value: unknown): value is number {
return IsNumber(value) && Number.isInteger(value)
return Number.isInteger(value)
}
/** Returns true if this value is bigint */
export function IsBigInt(value: unknown): value is bigint {
Expand Down

0 comments on commit fb3e28c

Please sign in to comment.