Skip to content

Commit

Permalink
Revert Mutability
Browse files Browse the repository at this point in the history
  • Loading branch information
sinclairzx81 committed Apr 25, 2024
1 parent e8ae691 commit bf1e98d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 35 deletions.
2 changes: 1 addition & 1 deletion readme.md
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`. Convert is a mutable operation. To avoid mutation, Clone the value first.
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`.
```typescript
const T = Type.Object({ x: Type.Number() })
Expand Down
19 changes: 9 additions & 10 deletions src/value/convert/convert.ts
Expand Up @@ -164,10 +164,7 @@ function Default(value: any) {
// ------------------------------------------------------------------
function FromArray(schema: TArray, references: TSchema[], value: any): any {
const elements = IsArray(value) ? value : [value]
for(let i = 0; i < elements.length; i++) {
elements[i] = Visit(schema.items, references, elements[i])
}
return elements
return elements.map((element) => Visit(schema.items, references, element))
}
function FromBigInt(schema: TBigInt, references: TSchema[], value: any): unknown {
return TryConvertBigInt(value)
Expand Down Expand Up @@ -197,11 +194,13 @@ function FromNumber(schema: TNumber, references: TSchema[], value: any): unknown
function FromObject(schema: TObject, references: TSchema[], value: any): unknown {
const isConvertable = IsObject(value)
if(!isConvertable) return value
const result: Record<PropertyKey, unknown> = {}
for(const key of Object.keys(value)) {
if(!HasPropertyKey(schema.properties, key)) continue
value[key] = Visit(schema.properties[key], references, value[key])
result[key] = HasPropertyKey(schema.properties, key)
? Visit(schema.properties[key], references, value[key])
: value[key]
}
return value
return result
}
function FromRecord(schema: TRecord, references: TSchema[], value: any): unknown {
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0]
Expand Down Expand Up @@ -292,11 +291,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. Convert is a mutable operation. To avoid mutation, Clone the value first. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
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. Convert is a mutable operation. To avoid mutation, Clone the value first. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(schema: TSchema, value: unknown): unknown
/** 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. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
// prettier-ignore
export function Convert(...args: any[]) {
return args.length === 3
Expand Down
6 changes: 3 additions & 3 deletions src/value/value/value.ts
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)
}
/** `[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. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown
/** `[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. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(schema: TSchema, value: unknown): unknown
/** `[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. */
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
export function Convert(...args: any[]) {
return ConvertValue.apply(ConvertValue, args as any)
}
Expand Down
9 changes: 0 additions & 9 deletions test/runtime/value/convert/array.ts
Expand Up @@ -53,13 +53,4 @@ 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])
})
})
12 changes: 0 additions & 12 deletions test/runtime/value/convert/object.ts
Expand Up @@ -21,16 +21,4 @@ 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 V = { x: '42', y: 'true' }
Value.Convert(T, V)
Assert.IsEqual(V, { x: 42, y: true })
})
})

0 comments on commit bf1e98d

Please sign in to comment.