Skip to content

Commit

Permalink
Fix bug in function print
Browse files Browse the repository at this point in the history
  • Loading branch information
stuymedova authored and Sofia Tuimedova committed Nov 27, 2023
1 parent 03d65bd commit 9ff5732
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Struct, Infer, Result, Context, Describe } from './struct'
import { Failure } from './error'
import { Context, Describe, Infer, Result, Struct } from './struct'

/**
* Check if a value is an iterator.
Expand Down Expand Up @@ -39,7 +39,11 @@ export function print(value: any): string {
return value.toString()
}

return typeof value === 'string' ? JSON.stringify(value) : `${value}`
if (value instanceof Object) {
return JSON.stringify(value)
}

return `${value}`
}

/**
Expand Down Expand Up @@ -74,8 +78,7 @@ export function toFailure<T, S>(
const { type } = struct
const {
refinement,
message = `Expected a value of type \`${type}\`${
refinement ? ` with refinement \`${refinement}\`` : ''
message = `Expected a value of type \`${type}\`${refinement ? ` with refinement \`${refinement}\`` : ''

Check failure on line 81 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `⏎······`

Check failure on line 81 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `⏎······`

Check failure on line 81 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `⏎······`
}, but received: \`${print(value)}\``,
} = result

Expand Down Expand Up @@ -248,8 +251,8 @@ export type IsExactMatch<T, U> = (<G>() => G extends T ? 1 : 2) extends <

export type IsRecord<T> = T extends object
? string extends keyof T
? T
: never
? T

Check failure on line 254 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 254 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 254 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: never

Check failure on line 255 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 255 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 255 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: never
/**
* Check if a type is a tuple.
Expand Down Expand Up @@ -340,43 +343,43 @@ export type If<B extends Boolean, Then, Else> = B extends true ? Then : Else

export type StructSchema<T> = [T] extends [string | undefined | null]
? [T] extends [IsMatch<T, string | undefined | null>]
? null
: [T] extends [IsUnion<T>]
? EnumSchema<T>
: T
? null

Check failure on line 346 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 346 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 346 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: [T] extends [IsUnion<T>]

Check failure on line 347 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 347 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 347 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
? EnumSchema<T>

Check failure on line 348 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 348 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 348 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: T

Check failure on line 349 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 349 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 349 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: [T] extends [number | undefined | null]
? [T] extends [IsMatch<T, number | undefined | null>]
? null
: [T] extends [IsUnion<T>]
? EnumSchema<T>
: T
? null

Check failure on line 352 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 352 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 352 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: [T] extends [IsUnion<T>]

Check failure on line 353 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Replace `··` with `····`

Check failure on line 353 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Replace `··` with `····`

Check failure on line 353 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Replace `··` with `····`
? EnumSchema<T>

Check failure on line 354 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (14.x)

Insert `··`

Check failure on line 354 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

Insert `··`

Check failure on line 354 in src/utils.ts

View workflow job for this annotation

GitHub Actions / ci (18.x)

Insert `··`
: T
: [T] extends [boolean]
? [T] extends [IsExactMatch<T, boolean>]
? null
: T
? null
: T
: T extends
| bigint
| symbol
| undefined
| null
| Function
| Date
| Error
| RegExp
| Map<any, any>
| WeakMap<any, any>
| Set<any>
| WeakSet<any>
| Promise<any>
| bigint
| symbol
| undefined
| null
| Function
| Date
| Error
| RegExp
| Map<any, any>
| WeakMap<any, any>
| Set<any>
| WeakSet<any>
| Promise<any>
? null
: T extends Array<infer E>
? T extends IsTuple<T>
? null
: Struct<E>
? null
: Struct<E>
: T extends object
? T extends IsRecord<T>
? null
: { [K in keyof T]: Describe<T[K]> }
? null
: { [K in keyof T]: Describe<T[K]> }
: null

/**
Expand All @@ -403,8 +406,8 @@ export type InferStructTuple<
Length extends number = Tuple['length']
> = Length extends Length
? number extends Length
? Tuple
: _InferTuple<Tuple, Length, []>
? Tuple
: _InferTuple<Tuple, Length, []>
: never
type _InferTuple<
Tuple extends AnyStruct[],
Expand Down

0 comments on commit 9ff5732

Please sign in to comment.