Comparing version 0.1.2 to 0.1.3
@@ -64,2 +64,5 @@ import { Flaw } from "./Flaw" | ||
} | ||
protected getValue(value: T) { | ||
return value.map(item => this.itemType.get(item)) as T | ||
} | ||
} | ||
@@ -66,0 +69,0 @@ |
@@ -42,2 +42,5 @@ import { Type } from "./Type"; | ||
} | ||
getValue(value) { | ||
return value.map(item => this.itemType.get(item)); | ||
} | ||
} | ||
@@ -44,0 +47,0 @@ export function array(itemType, ...options) { |
@@ -7,2 +7,3 @@ import { Type } from "./Type"; | ||
this.is = (value => (this.backend ??= this.factory()).is(value)); | ||
this.get = value => this.backend.get(value); | ||
} | ||
@@ -9,0 +10,0 @@ createFlaw(value) { |
@@ -7,2 +7,3 @@ import { Type } from "./Type"; | ||
this.is = (value => this.backend.is(value)); | ||
this.get = value => this.backend.get(value); | ||
} | ||
@@ -9,0 +10,0 @@ createFlaw(value) { |
@@ -34,2 +34,13 @@ import { Type } from "./Type"; | ||
} | ||
/** | ||
* get-function on a object returns a object with only specified properties | ||
*/ | ||
getValue(value) { | ||
const result = this.baseType ? this.baseType.getValue(value) : {}; | ||
if (result) | ||
for (const [key, type] of globalThis.Object.entries(this.properties)) | ||
if (key in value) | ||
result[key] = type.get(value[key]); | ||
return result; | ||
} | ||
} | ||
@@ -36,0 +47,0 @@ export function object(properties, name) { |
@@ -25,2 +25,5 @@ import { Type } from "./Type"; | ||
} | ||
getValue(value) { | ||
return Object.fromEntries(Object.entries(value).map(([key, value]) => [key, this.valueType.get(value)])); | ||
} | ||
} | ||
@@ -27,0 +30,0 @@ export function record(keyType, valueType) { |
@@ -21,2 +21,5 @@ import { Type } from "./Type"; | ||
} | ||
getValue(value) { | ||
return this.items.map((item, index) => item.get(value[index])); | ||
} | ||
} | ||
@@ -23,0 +26,0 @@ export function tuple(...items) { |
@@ -23,2 +23,3 @@ import { Flaw } from "./Flaw"; | ||
* Return the value if the value is valid for the type, otherwise undefined. | ||
* For objects, unknown properties are filtered. | ||
* | ||
@@ -28,2 +29,3 @@ * Eg: isly.number().value(NaN) returns undefined | ||
get: Type.GetFunction<T>; | ||
protected getValue(value: T): T; | ||
/** | ||
@@ -30,0 +32,0 @@ * Return a flaw object, describing the flaws of the value compared to expected type. |
@@ -16,6 +16,7 @@ export class Type { | ||
* Return the value if the value is valid for the type, otherwise undefined. | ||
* For objects, unknown properties are filtered. | ||
* | ||
* Eg: isly.number().value(NaN) returns undefined | ||
*/ | ||
this.get = value => (this.is(value) ? value : undefined); | ||
this.get = value => (this.is(value) ? this.getValue(value) : undefined); | ||
/** | ||
@@ -43,2 +44,5 @@ * Return a flaw object, describing the flaws of the value compared to expected type. | ||
} | ||
getValue(value) { | ||
return value; | ||
} | ||
/** | ||
@@ -45,0 +49,0 @@ * Override this to create custom Flaws. |
import { Type } from "./Type"; | ||
/** | ||
* For unions with more than 6 types, provide the union type as the generic T. | ||
* | ||
* union.get(value) depends on the Order of types. | ||
* First matching type will be used. | ||
*/ | ||
@@ -5,0 +8,0 @@ export declare function union<T extends A | B, A, B>(...types: [Type<A>, Type<B>]): Type<T>; |
@@ -13,2 +13,5 @@ import { Type } from "./Type"; | ||
} | ||
getValue(value) { | ||
return this.types.find(type => type.is(value))?.get(value); | ||
} | ||
} | ||
@@ -15,0 +18,0 @@ export function union(...types) { |
@@ -16,2 +16,3 @@ import { Flaw } from "./Flaw" | ||
} | ||
//TODO: handle getValue(value) | ||
} | ||
@@ -18,0 +19,0 @@ |
@@ -13,2 +13,3 @@ import { Flaw } from "./Flaw" | ||
} | ||
get: Type.GetFunction<T> = value => this.backend.get(value) | ||
} | ||
@@ -15,0 +16,0 @@ |
@@ -12,2 +12,3 @@ import { Flaw } from "./Flaw" | ||
} | ||
get: Type.GetFunction<T> = value => this.backend.get(value) | ||
} | ||
@@ -14,0 +15,0 @@ |
@@ -23,3 +23,6 @@ import type { Flaw } from "./Flaw" | ||
class IslyObject<T extends B, B, TB extends Type<B> | undefined> extends Type<T> implements object.ExtendableType<T> { | ||
class IslyObject<T extends B, B extends object, TB extends IslyObject<B, any, any> | undefined> | ||
extends Type<T> | ||
implements object.ExtendableType<T> | ||
{ | ||
constructor( | ||
@@ -52,3 +55,3 @@ protected readonly baseType: TB, | ||
): object.ExtendableType<T2> { | ||
return new IslyObject<T2, T, Type<T>>(this, properties, name) | ||
return new IslyObject<T2, T, IslyObject<T, any, any>>(this, properties, name) | ||
} | ||
@@ -76,2 +79,14 @@ is = (value => | ||
} | ||
/** | ||
* get-function on a object returns a object with only specified properties | ||
*/ | ||
protected getValue(value: T) { | ||
const result: Record<any, any> = this.baseType ? this.baseType.getValue(value) : {} | ||
if (result) | ||
for (const [key, type] of globalThis.Object.entries(this.properties) as [keyof T, Type<any>][]) | ||
if (key in value) | ||
result[key] = type.get(value[key]) | ||
return result as T | ||
} | ||
} | ||
@@ -78,0 +93,0 @@ |
{ | ||
"name": "isly", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "Library for type checking.", | ||
@@ -5,0 +5,0 @@ "author": "Utily Contributors", |
@@ -96,2 +96,4 @@ # isly | ||
For object, a filtered object is returned, with only known properties. | ||
```typescript | ||
@@ -98,0 +100,0 @@ const myNumber = 234 / 0 // Infinity |
@@ -32,2 +32,8 @@ import { Flaw } from "./Flaw" | ||
} | ||
protected getValue(value: Record<K, V>): Record<K, V> { | ||
return Object.fromEntries(Object.entries(value).map(([key, value]) => [key, this.valueType.get(value)])) as Record< | ||
K, | ||
V | ||
> | ||
} | ||
} | ||
@@ -34,0 +40,0 @@ |
@@ -25,2 +25,5 @@ import { Flaw } from "./Flaw" | ||
} | ||
protected getValue(value: T): T { | ||
return this.items.map((item, index) => item.get(value[index])) as T | ||
} | ||
} | ||
@@ -27,0 +30,0 @@ |
@@ -31,6 +31,10 @@ import { Flaw } from "./Flaw" | ||
* Return the value if the value is valid for the type, otherwise undefined. | ||
* For objects, unknown properties are filtered. | ||
* | ||
* Eg: isly.number().value(NaN) returns undefined | ||
*/ | ||
public get: Type.GetFunction<T> = value => (this.is(value) ? value : undefined) | ||
public get: Type.GetFunction<T> = value => (this.is(value) ? this.getValue(value) : undefined) | ||
protected getValue(value: T) { | ||
return value | ||
} | ||
/** | ||
@@ -37,0 +41,0 @@ * Return a flaw object, describing the flaws of the value compared to expected type. |
@@ -17,2 +17,5 @@ import type { Flaw } from "./Flaw" | ||
} | ||
protected getValue(value: T): T { | ||
return this.types.find(type => type.is(value))?.get(value) as T | ||
} | ||
} | ||
@@ -22,2 +25,5 @@ | ||
* For unions with more than 6 types, provide the union type as the generic T. | ||
* | ||
* union.get(value) depends on the Order of types. | ||
* First matching type will be used. | ||
*/ | ||
@@ -24,0 +30,0 @@ export function union<T extends A | B, A, B>(...types: [Type<A>, Type<B>]): Type<T> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
82170
1344
147