Skip to content

Commit

Permalink
Use Array Mutation on Convert
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 25, 2024
1 parent 98102db commit e8ae691
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ const R = Value.Check(T, { x: 1 }) // const R = true
### Convert
Use the Convert function to convert a value into its target type if a reasonable conversion is possible. This function may return an invalid value and should be checked before use. Its return type is `unknown`.
Use the Convert function to convert a value into its target type if a reasonable conversion is possible. This function may return an invalid value and should be checked before use. Its return type is `unknown`. Convert is a mutable operation. To avoid mutation, Clone the value first.
```typescript
const T = Type.Object({ x: Type.Number() })
Expand Down
11 changes: 7 additions & 4 deletions src/value/convert/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ function Default(value: any) {
// ------------------------------------------------------------------
function FromArray(schema: TArray, references: TSchema[], value: any): any {
const elements = IsArray(value) ? value : [value]
return elements.map((element) => Visit(schema.items, references, element))
for(let i = 0; i < elements.length; i++) {
elements[i] = Visit(schema.items, references, elements[i])
}
return elements
}
function FromBigInt(schema: TBigInt, references: TSchema[], value: any): unknown {
return TryConvertBigInt(value)
Expand Down Expand Up @@ -289,11 +292,11 @@ function Visit(schema: TSchema, references: TSchema[], value: any): unknown {
// ------------------------------------------------------------------
// Convert
// ------------------------------------------------------------------
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. Convert is a mutable operation. To avoid mutation, Clone the value first. */
export function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. Convert is a mutable operation. To avoid mutation, Clone the value first. */
export function Convert(schema: TSchema, value: unknown): unknown
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. Convert is a mutable operation. To avoid mutation, Clone the value first. */
// prettier-ignore
export function Convert(...args: any[]) {
return args.length === 3
Expand Down
6 changes: 3 additions & 3 deletions src/value/value/value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ export function Clean(schema: TSchema, value: unknown): unknown
export function Clean(...args: any[]) {
return CleanValue.apply(CleanValue, args as any)
}
/** Converts any type mismatched values to their target type if a reasonable conversion is possible */
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. Convert is a mutable operation. To avoid mutation, Clone the value first. */
export function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown
/** Converts any type mismatched values to their target type if a reasonable conversion is possibl. */
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. Convert is a mutable operation. To avoid mutation, Clone the value first. */
export function Convert(schema: TSchema, value: unknown): unknown
/** Converts any type mismatched values to their target type if a reasonable conversion is possible */
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. Convert is a mutable operation. To avoid mutation, Clone the value first. */
export function Convert(...args: any[]) {
return ConvertValue.apply(ConvertValue, args as any)
}
Expand Down
9 changes: 9 additions & 0 deletions test/runtime/value/convert/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ describe('value/convert/Array', () => {
const R = Value.Convert(T, { x: '1', y: true, z: null })
Assert.IsEqual(R, [{ x: 1, y: 1, z: null }])
})
// ----------------------------------------------------------------
// Array Mutation
// ----------------------------------------------------------------
it('Should mutate array on conversion', () => {
const T = Type.Array(Type.Number())
const V = ['1', '2', '3']
Value.Convert(T, V)
Assert.IsEqual(V, [1, 2, 3])
})
})
9 changes: 6 additions & 3 deletions test/runtime/value/convert/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@ describe('value/convert/Object', () => {
const R = Value.Convert(T, { x: '42', y: 'true', z: 'hello' })
Assert.IsEqual(R, { x: 42, y: true, z: 'hello' })
})
// ----------------------------------------------------------------
// Object Mutation
// ----------------------------------------------------------------
it('Should mutate object on conversion', () => {
const T = Type.Object({
x: Type.Number(),
y: Type.Boolean()
})
const value = { x: '42', y: 'true', z: 'hello' }
Value.Convert(T, value)
Assert.IsEqual(value, { x: 42, y: true, z: 'hello' })
const V = { x: '42', y: 'true' }
Value.Convert(T, V)
Assert.IsEqual(V, { x: 42, y: true })
})
})

0 comments on commit e8ae691

Please sign in to comment.