Comparing version 0.1.16 to 0.1.17
import { Type } from "./Type" | ||
class IslyAny<T = any> extends Type<T> { | ||
is = (value => value != undefined) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => value != undefined | ||
} | ||
@@ -6,0 +6,0 @@ |
import { Type } from "./Type" | ||
class IslyBoolean<B extends boolean = boolean> extends Type<B> { | ||
constructor(protected readonly booleanValue?: B) { | ||
class IslyBoolean<T extends boolean = boolean> extends Type<T> { | ||
constructor(protected readonly booleanValue?: T) { | ||
super(booleanValue == undefined ? "boolean" : booleanValue ? "true" : "false") | ||
} | ||
is = (value => | ||
typeof value == "boolean" && (this.booleanValue == undefined || value == this.booleanValue)) as Type.IsFunction<B> | ||
is = (value: T | any): value is T => | ||
typeof value == "boolean" && (this.booleanValue == undefined || value == this.booleanValue) | ||
} | ||
@@ -10,0 +10,0 @@ |
@@ -8,3 +8,3 @@ "use strict"; | ||
super(...arguments); | ||
this.is = (value => value != undefined); | ||
this.is = (value) => value != undefined; | ||
} | ||
@@ -11,0 +11,0 @@ } |
@@ -9,3 +9,3 @@ "use strict"; | ||
this.booleanValue = booleanValue; | ||
this.is = (value => typeof value == "boolean" && (this.booleanValue == undefined || value == this.booleanValue)); | ||
this.is = (value) => typeof value == "boolean" && (this.booleanValue == undefined || value == this.booleanValue); | ||
} | ||
@@ -12,0 +12,0 @@ } |
@@ -12,4 +12,4 @@ import { Type } from "./Type"; | ||
const type: Type<Flaw>; | ||
const is: Type.IsFunction<Flaw>; | ||
const flaw: Type.FlawFunction; | ||
const is: (value: any) => value is Flaw; | ||
const flaw: (value: any) => Flaw; | ||
} |
@@ -7,3 +7,3 @@ import { Type } from "./Type"; | ||
*/ | ||
export declare function fromIs<T>(name: string, is: Type.IsFunction<T>): Type<T>; | ||
export declare function fromIs<T>(name: string, is: (value: T | any) => value is T): Type<T>; | ||
export declare function fromIs<T>(name: string, is: (value: any) => boolean): Type<T>; |
@@ -9,3 +9,3 @@ "use strict"; | ||
this.isFunction = isFunction; | ||
this.is = (value => this.isFunction(value)); | ||
this.is = (value) => this.isFunction(value); | ||
} | ||
@@ -12,0 +12,0 @@ } |
@@ -9,3 +9,3 @@ "use strict"; | ||
super("function"); | ||
this.is = (value => !!(value && typeof value == "function")); | ||
this.is = (value) => !!(value && typeof value == "function"); | ||
} | ||
@@ -12,0 +12,0 @@ } |
@@ -8,3 +8,3 @@ "use strict"; | ||
super(() => types.map(type => type.name).join(" & ")); | ||
this.is = (value => this.types.every(type => type.is(value))); | ||
this.is = (value) => this.types.every(type => type.is(value)); | ||
this.types = types; | ||
@@ -11,0 +11,0 @@ } |
@@ -9,4 +9,3 @@ "use strict"; | ||
this.factory = factory; | ||
this.is = (value => { var _a; return ((_a = this.backend) !== null && _a !== void 0 ? _a : (this.backend = this.factory())).is(value); }); | ||
this.get = value => this.backend.get(value); | ||
this.is = (value) => { var _a; return ((_a = this.backend) !== null && _a !== void 0 ? _a : (this.backend = this.factory())).is(value); }; | ||
} | ||
@@ -17,2 +16,5 @@ createFlaw(value) { | ||
} | ||
get(value) { | ||
return this.backend.get(value); | ||
} | ||
} | ||
@@ -19,0 +21,0 @@ /** |
@@ -9,4 +9,4 @@ "use strict"; | ||
this.backend = backend; | ||
this.is = (value => this.backend.is(value)); | ||
this.get = value => this.backend.get(value); | ||
this.is = (value) => this.backend.is(value); | ||
this.get = (value) => this.backend.get(value); | ||
} | ||
@@ -13,0 +13,0 @@ createFlaw(value) { |
@@ -47,5 +47,5 @@ "use strict"; | ||
this.isFunction = isFunction; | ||
this.is = (value => typeof value == "number" && | ||
this.is = (value) => typeof value == "number" && | ||
!Number.isNaN(value - value) && // NaN-NaN==NaN && Infinity-Infinity==NaN && (-Infinity)-(-Infinity)==NaN | ||
(!this.isFunction || this.isFunction(value))); | ||
(!this.isFunction || this.isFunction(value)); | ||
} | ||
@@ -52,0 +52,0 @@ } |
@@ -13,7 +13,7 @@ "use strict"; | ||
this.properties = properties; | ||
this.is = (value => !!(value && | ||
this.is = (value) => !!(value && | ||
(this.baseType == undefined || this.baseType.is(value)) && | ||
typeof value == "object" && | ||
!globalThis.Array.isArray(value) && | ||
globalThis.Object.entries(this.properties).every(([property, type]) => type.is(value[property])))); | ||
globalThis.Object.entries(this.properties).every(([property, type]) => type.is(value[property]))); | ||
} | ||
@@ -20,0 +20,0 @@ extend(properties, name) { |
@@ -10,6 +10,6 @@ "use strict"; | ||
this.valueType = valueType; | ||
this.is = (value => !!(value && | ||
this.is = (value) => !!(value && | ||
typeof value == "object" && | ||
!globalThis.Array.isArray(value) && | ||
globalThis.Object.entries(value).every(([key, value]) => this.keyType.is(this.keyType.name == "number" && `${+key}` == key ? +key : key) && this.valueType.is(value)))); | ||
globalThis.Object.entries(value).every(([key, value]) => this.keyType.is(this.keyType.name == "number" && `${+key}` == key ? +key : key) && this.valueType.is(value))); | ||
} | ||
@@ -16,0 +16,0 @@ createFlaw(value) { |
@@ -16,3 +16,3 @@ "use strict"; | ||
this.stringCondition = stringCondition; | ||
this.is = (value => { | ||
this.is = (value) => { | ||
const conditionObject = this.getConditionObject(); | ||
@@ -25,3 +25,3 @@ return (typeof value == "string" && | ||
: conditionObject)); | ||
}); | ||
}; | ||
} | ||
@@ -28,0 +28,0 @@ getConditionObject() { |
@@ -8,5 +8,5 @@ "use strict"; | ||
super(() => "[" + items.map(e => e.name).join(", ") + "]"); | ||
this.is = (value => globalThis.Array.isArray(value) && | ||
this.is = (value) => globalThis.Array.isArray(value) && | ||
value.length == this.items.length && | ||
this.items.every((item, index) => item.is(value[index]))); | ||
this.items.every((item, index) => item.is(value[index])); | ||
this.items = items; | ||
@@ -13,0 +13,0 @@ } |
@@ -22,3 +22,3 @@ import { Flaw } from "./Flaw"; | ||
*/ | ||
abstract is: Type.IsFunction<T>; | ||
abstract is(value: any | T): value is T; | ||
/** | ||
@@ -30,3 +30,3 @@ * Return the value if the value is valid for the type, otherwise undefined. | ||
*/ | ||
get: Type.GetFunction<T>; | ||
get(value: any): T | undefined; | ||
protected getValue(value: T): T; | ||
@@ -40,3 +40,3 @@ /** | ||
*/ | ||
flaw: Type.FlawFunction; | ||
flaw: (value: T | any) => Flaw; | ||
/** | ||
@@ -53,7 +53,4 @@ * Override this to create custom Flaws. | ||
export declare namespace Type { | ||
type IsFunction<T> = (value: any | T) => value is T; | ||
type FlawFunction = (value: any) => Flaw; | ||
type GetFunction<T> = (value: any) => T | undefined; | ||
/** Utility-type to get Value-Type from Type<Value>. */ | ||
type Value<T extends Type<unknown>> = T["is"] extends (x: any) => x is infer U ? U : never; | ||
type Value<T extends Type<unknown>> = T extends Type<infer U> ? U : never; | ||
} | ||
@@ -81,3 +78,3 @@ export declare namespace array { | ||
protected itemName(index: number): string; | ||
is: Type.IsFunction<T>; | ||
is: (value: T | any) => value is T; | ||
createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition">; | ||
@@ -84,0 +81,0 @@ protected getValue(value: T): T; |
@@ -24,9 +24,2 @@ "use strict"; | ||
/** | ||
* 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) ? this.getValue(value) : undefined); | ||
/** | ||
* Return a flaw object, describing the flaws of the value compared to expected type. | ||
@@ -38,3 +31,3 @@ * | ||
*/ | ||
this.flaw = value => { | ||
this.flaw = (value) => { | ||
return this.is(value) | ||
@@ -44,2 +37,11 @@ ? Object.assign(Object.assign({ type: this.name }, (this.condition ? { condition: this.condition } : undefined)), { isFlaw: false, message: "This type is correct." }) : Object.assign(Object.assign(Object.assign({}, (this.condition ? { condition: this.condition } : undefined)), this.createFlaw(value)), { type: this.name }); | ||
} | ||
/** | ||
* 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 | ||
*/ | ||
get(value) { | ||
return this.is(value) ? this.getValue(value) : undefined; | ||
} | ||
getValue(value) { | ||
@@ -69,3 +71,3 @@ return value; | ||
this.backend = backend; | ||
this.is = (value => value == undefined || this.backend.is(value)); | ||
this.is = (value) => value == undefined || this.backend.is(value); | ||
} | ||
@@ -80,3 +82,3 @@ createFlaw(value) { | ||
this.backend = backend; | ||
this.is = (value => value == undefined || this.backend.is(value)); | ||
this.is = (value) => value == undefined || this.backend.is(value); | ||
} | ||
@@ -110,5 +112,5 @@ createFlaw(value) { | ||
this.options = options; | ||
this.is = (value => globalThis.Array.isArray(value) && | ||
this.is = (value) => globalThis.Array.isArray(value) && | ||
this.options.every(option => criteriaFunctions[option.criteria].is(value, option.value)) && | ||
value.every(item => this.itemType.is(item))); | ||
value.every(item => this.itemType.is(item)); | ||
} | ||
@@ -115,0 +117,0 @@ baseName() { |
@@ -8,3 +8,3 @@ "use strict"; | ||
super(...arguments); | ||
this.is = (value => value === undefined); | ||
this.is = (value) => value === undefined; | ||
} | ||
@@ -11,0 +11,0 @@ } |
@@ -8,3 +8,3 @@ "use strict"; | ||
super(() => types.map(type => type.name).join(" | ")); | ||
this.is = (value => this.types.some(type => type.is(value))); | ||
this.is = (value) => this.types.some(type => type.is(value)); | ||
this.types = types; | ||
@@ -11,0 +11,0 @@ } |
@@ -8,3 +8,3 @@ "use strict"; | ||
super(...arguments); | ||
this.is = (value => value != undefined); | ||
this.is = (value) => value != undefined; | ||
} | ||
@@ -11,0 +11,0 @@ } |
@@ -5,3 +5,3 @@ import { Type } from "./Type"; | ||
super(...arguments); | ||
this.is = (value => value != undefined); | ||
this.is = (value) => value != undefined; | ||
} | ||
@@ -8,0 +8,0 @@ } |
@@ -6,3 +6,3 @@ import { Type } from "./Type"; | ||
this.booleanValue = booleanValue; | ||
this.is = (value => typeof value == "boolean" && (this.booleanValue == undefined || value == this.booleanValue)); | ||
this.is = (value) => typeof value == "boolean" && (this.booleanValue == undefined || value == this.booleanValue); | ||
} | ||
@@ -9,0 +9,0 @@ } |
@@ -12,4 +12,4 @@ import { Type } from "./Type"; | ||
const type: Type<Flaw>; | ||
const is: Type.IsFunction<Flaw>; | ||
const flaw: Type.FlawFunction; | ||
const is: (value: any) => value is Flaw; | ||
const flaw: (value: any) => Flaw; | ||
} |
@@ -7,3 +7,3 @@ import { Type } from "./Type"; | ||
*/ | ||
export declare function fromIs<T>(name: string, is: Type.IsFunction<T>): Type<T>; | ||
export declare function fromIs<T>(name: string, is: (value: T | any) => value is T): Type<T>; | ||
export declare function fromIs<T>(name: string, is: (value: any) => boolean): Type<T>; |
@@ -6,3 +6,3 @@ import { Type } from "./Type"; | ||
this.isFunction = isFunction; | ||
this.is = (value => this.isFunction(value)); | ||
this.is = (value) => this.isFunction(value); | ||
} | ||
@@ -9,0 +9,0 @@ } |
@@ -6,3 +6,3 @@ import { Type } from "./Type"; | ||
super("function"); | ||
this.is = (value => !!(value && typeof value == "function")); | ||
this.is = (value) => !!(value && typeof value == "function"); | ||
} | ||
@@ -9,0 +9,0 @@ } |
@@ -5,3 +5,3 @@ import { Type } from "./Type"; | ||
super(() => types.map(type => type.name).join(" & ")); | ||
this.is = (value => this.types.every(type => type.is(value))); | ||
this.is = (value) => this.types.every(type => type.is(value)); | ||
this.types = types; | ||
@@ -8,0 +8,0 @@ } |
@@ -6,4 +6,3 @@ import { Type } from "./Type"; | ||
this.factory = factory; | ||
this.is = (value => (this.backend ??= this.factory()).is(value)); | ||
this.get = value => this.backend.get(value); | ||
this.is = (value) => (this.backend ??= this.factory()).is(value); | ||
} | ||
@@ -13,2 +12,5 @@ createFlaw(value) { | ||
} | ||
get(value) { | ||
return this.backend.get(value); | ||
} | ||
} | ||
@@ -15,0 +17,0 @@ /** |
@@ -6,4 +6,4 @@ import { Type } from "./Type"; | ||
this.backend = backend; | ||
this.is = (value => this.backend.is(value)); | ||
this.get = value => this.backend.get(value); | ||
this.is = (value) => this.backend.is(value); | ||
this.get = (value) => this.backend.get(value); | ||
} | ||
@@ -10,0 +10,0 @@ createFlaw(value) { |
@@ -44,5 +44,5 @@ import { Type } from "./Type"; | ||
this.isFunction = isFunction; | ||
this.is = (value => typeof value == "number" && | ||
this.is = (value) => typeof value == "number" && | ||
!Number.isNaN(value - value) && // NaN-NaN==NaN && Infinity-Infinity==NaN && (-Infinity)-(-Infinity)==NaN | ||
(!this.isFunction || this.isFunction(value))); | ||
(!this.isFunction || this.isFunction(value)); | ||
} | ||
@@ -49,0 +49,0 @@ } |
@@ -11,7 +11,7 @@ import { Type } from "./Type"; | ||
this.properties = properties; | ||
this.is = (value => !!(value && | ||
this.is = (value) => !!(value && | ||
(this.baseType == undefined || this.baseType.is(value)) && | ||
typeof value == "object" && | ||
!globalThis.Array.isArray(value) && | ||
globalThis.Object.entries(this.properties).every(([property, type]) => type.is(value[property])))); | ||
globalThis.Object.entries(this.properties).every(([property, type]) => type.is(value[property]))); | ||
} | ||
@@ -18,0 +18,0 @@ extend(properties, name) { |
@@ -7,6 +7,6 @@ import { Type } from "./Type"; | ||
this.valueType = valueType; | ||
this.is = (value => !!(value && | ||
this.is = (value) => !!(value && | ||
typeof value == "object" && | ||
!globalThis.Array.isArray(value) && | ||
globalThis.Object.entries(value).every(([key, value]) => this.keyType.is(this.keyType.name == "number" && `${+key}` == key ? +key : key) && this.valueType.is(value)))); | ||
globalThis.Object.entries(value).every(([key, value]) => this.keyType.is(this.keyType.name == "number" && `${+key}` == key ? +key : key) && this.valueType.is(value))); | ||
} | ||
@@ -13,0 +13,0 @@ createFlaw(value) { |
@@ -13,3 +13,3 @@ import { Type } from "./Type"; | ||
this.stringCondition = stringCondition; | ||
this.is = (value => { | ||
this.is = (value) => { | ||
const conditionObject = this.getConditionObject(); | ||
@@ -22,3 +22,3 @@ return (typeof value == "string" && | ||
: conditionObject)); | ||
}); | ||
}; | ||
} | ||
@@ -25,0 +25,0 @@ getConditionObject() { |
@@ -5,5 +5,5 @@ import { Type } from "./Type"; | ||
super(() => "[" + items.map(e => e.name).join(", ") + "]"); | ||
this.is = (value => globalThis.Array.isArray(value) && | ||
this.is = (value) => globalThis.Array.isArray(value) && | ||
value.length == this.items.length && | ||
this.items.every((item, index) => item.is(value[index]))); | ||
this.items.every((item, index) => item.is(value[index])); | ||
this.items = items; | ||
@@ -10,0 +10,0 @@ } |
@@ -22,3 +22,3 @@ import { Flaw } from "./Flaw"; | ||
*/ | ||
abstract is: Type.IsFunction<T>; | ||
abstract is(value: any | T): value is T; | ||
/** | ||
@@ -30,3 +30,3 @@ * Return the value if the value is valid for the type, otherwise undefined. | ||
*/ | ||
get: Type.GetFunction<T>; | ||
get(value: any): T | undefined; | ||
protected getValue(value: T): T; | ||
@@ -40,3 +40,3 @@ /** | ||
*/ | ||
flaw: Type.FlawFunction; | ||
flaw: (value: T | any) => Flaw; | ||
/** | ||
@@ -53,7 +53,4 @@ * Override this to create custom Flaws. | ||
export declare namespace Type { | ||
type IsFunction<T> = (value: any | T) => value is T; | ||
type FlawFunction = (value: any) => Flaw; | ||
type GetFunction<T> = (value: any) => T | undefined; | ||
/** Utility-type to get Value-Type from Type<Value>. */ | ||
type Value<T extends Type<unknown>> = T["is"] extends (x: any) => x is infer U ? U : never; | ||
type Value<T extends Type<unknown>> = T extends Type<infer U> ? U : never; | ||
} | ||
@@ -81,3 +78,3 @@ export declare namespace array { | ||
protected itemName(index: number): string; | ||
is: Type.IsFunction<T>; | ||
is: (value: T | any) => value is T; | ||
createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition">; | ||
@@ -84,0 +81,0 @@ protected getValue(value: T): T; |
@@ -21,9 +21,2 @@ 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) ? this.getValue(value) : undefined); | ||
/** | ||
* Return a flaw object, describing the flaws of the value compared to expected type. | ||
@@ -35,3 +28,3 @@ * | ||
*/ | ||
this.flaw = value => { | ||
this.flaw = (value) => { | ||
return this.is(value) | ||
@@ -51,2 +44,11 @@ ? { | ||
} | ||
/** | ||
* 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 | ||
*/ | ||
get(value) { | ||
return this.is(value) ? this.getValue(value) : undefined; | ||
} | ||
getValue(value) { | ||
@@ -75,3 +77,3 @@ return value; | ||
this.backend = backend; | ||
this.is = (value => value == undefined || this.backend.is(value)); | ||
this.is = (value) => value == undefined || this.backend.is(value); | ||
} | ||
@@ -86,3 +88,3 @@ createFlaw(value) { | ||
this.backend = backend; | ||
this.is = (value => value == undefined || this.backend.is(value)); | ||
this.is = (value) => value == undefined || this.backend.is(value); | ||
} | ||
@@ -115,5 +117,5 @@ createFlaw(value) { | ||
this.options = options; | ||
this.is = (value => globalThis.Array.isArray(value) && | ||
this.is = (value) => globalThis.Array.isArray(value) && | ||
this.options.every(option => criteriaFunctions[option.criteria].is(value, option.value)) && | ||
value.every(item => this.itemType.is(item))); | ||
value.every(item => this.itemType.is(item)); | ||
} | ||
@@ -120,0 +122,0 @@ baseName() { |
@@ -5,3 +5,3 @@ import { Type } from "./Type"; | ||
super(...arguments); | ||
this.is = (value => value === undefined); | ||
this.is = (value) => value === undefined; | ||
} | ||
@@ -8,0 +8,0 @@ } |
@@ -5,3 +5,3 @@ import { Type } from "./Type"; | ||
super(() => types.map(type => type.name).join(" | ")); | ||
this.is = (value => this.types.some(type => type.is(value))); | ||
this.is = (value) => this.types.some(type => type.is(value)); | ||
this.types = types; | ||
@@ -8,0 +8,0 @@ } |
@@ -5,3 +5,3 @@ import { Type } from "./Type"; | ||
super(...arguments); | ||
this.is = (value => value != undefined); | ||
this.is = (value) => value != undefined; | ||
} | ||
@@ -8,0 +8,0 @@ } |
@@ -7,3 +7,3 @@ import { Type } from "./Type" | ||
} | ||
is = (value => this.isFunction(value)) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => this.isFunction(value) | ||
} | ||
@@ -16,6 +16,6 @@ | ||
*/ | ||
export function fromIs<T>(name: string, is: Type.IsFunction<T>): Type<T> | ||
export function fromIs<T>(name: string, is: (value: T | any) => value is T): Type<T> | ||
export function fromIs<T>(name: string, is: (value: any) => boolean): Type<T> | ||
export function fromIs<T>(name: string, is: (value: any) => boolean): Type<T> { | ||
export function fromIs<T>(name: string, is: ((value: T | any) => value is T) | ((value: any) => boolean)): Type<T> { | ||
return new IslyFromIs(name, is) | ||
} |
@@ -8,3 +8,3 @@ import { Type } from "./Type" | ||
} | ||
is = (value => !!(value && typeof value == "function")) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => !!(value && typeof value == "function") | ||
} | ||
@@ -11,0 +11,0 @@ |
@@ -10,3 +10,3 @@ import { Flaw } from "./Flaw" | ||
} | ||
is = (value => this.types.every(type => type.is(value))) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => this.types.every(type => type.is(value)) | ||
createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -13,0 +13,0 @@ return { |
@@ -9,7 +9,9 @@ import { Flaw } from "./Flaw" | ||
} | ||
is = (value => (this.backend ??= this.factory()).is(value)) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => (this.backend ??= this.factory()).is(value) | ||
createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
return this.createFlawFromType((this.backend ??= this.factory()), value) | ||
} | ||
get: Type.GetFunction<T> = value => this.backend.get(value) | ||
public get(value: any): T | undefined { | ||
return this.backend.get(value) | ||
} | ||
} | ||
@@ -16,0 +18,0 @@ |
@@ -8,7 +8,7 @@ import { Flaw } from "./Flaw" | ||
} | ||
is = (value => this.backend.is(value)) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => this.backend.is(value) | ||
createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
return this.createFlawFromType(this.backend, value) | ||
} | ||
get: Type.GetFunction<T> = value => this.backend.get(value) | ||
get = (value: any): T | undefined => this.backend.get(value) | ||
} | ||
@@ -15,0 +15,0 @@ |
@@ -50,10 +50,10 @@ import { Type } from "./Type" | ||
class IslyNumber<N extends number = number> extends Type<N> { | ||
class IslyNumber<T extends number = number> extends Type<T> { | ||
constructor(protected readonly isFunction?: (value: number) => boolean, condition?: string) { | ||
super("number", condition) | ||
} | ||
is = (value => | ||
is = (value: T | any): value is T => | ||
typeof value == "number" && | ||
!Number.isNaN(value - value) && // NaN-NaN==NaN && Infinity-Infinity==NaN && (-Infinity)-(-Infinity)==NaN | ||
(!this.isFunction || this.isFunction(value))) as Type.IsFunction<N> | ||
(!this.isFunction || this.isFunction(value)) | ||
} | ||
@@ -60,0 +60,0 @@ /** |
@@ -73,3 +73,3 @@ import type { Flaw } from "./Flaw" | ||
} | ||
is = (value => | ||
is = (value: T | any): value is T => | ||
!!( | ||
@@ -81,3 +81,3 @@ value && | ||
globalThis.Object.entries<Type<any>>(this.properties).every(([property, type]) => type.is(value[property])) | ||
)) as Type.IsFunction<T> | ||
) | ||
protected createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -84,0 +84,0 @@ return { |
{ | ||
"name": "isly", | ||
"version": "0.1.16", | ||
"version": "0.1.17", | ||
"description": "Library for type checking.", | ||
@@ -5,0 +5,0 @@ "author": "Utily Contributors", |
@@ -8,3 +8,3 @@ import { Flaw } from "./Flaw" | ||
} | ||
is = (value => | ||
is = (value: Record<K, V> | any): value is Record<K, V> => | ||
!!( | ||
@@ -18,3 +18,3 @@ value && | ||
) | ||
)) as Type.IsFunction<Record<K, V>> | ||
) | ||
protected createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -21,0 +21,0 @@ return { |
@@ -15,5 +15,4 @@ import { Type } from "./Type" | ||
} | ||
is = (value => { | ||
is = (value: T | any): value is T => { | ||
const conditionObject = this.getConditionObject() | ||
return ( | ||
@@ -27,3 +26,3 @@ typeof value == "string" && | ||
) | ||
}) as Type.IsFunction<T> | ||
} | ||
protected getConditionObject(): NonNullable<IslyString<T>["conditionObject"]> { | ||
@@ -30,0 +29,0 @@ return (this.conditionObject ??= Array.isArray(this.stringCondition) |
@@ -10,6 +10,6 @@ import { Flaw } from "./Flaw" | ||
} | ||
is = (value => | ||
is = (value: T | any): value is T => | ||
globalThis.Array.isArray(value) && | ||
value.length == this.items.length && | ||
this.items.every((item, index) => item.is(value[index]))) as Type.IsFunction<T> | ||
this.items.every((item, index) => item.is(value[index])) | ||
protected createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -16,0 +16,0 @@ return { |
21
Type.ts
@@ -34,3 +34,3 @@ import { Flaw } from "./Flaw" | ||
*/ | ||
abstract is: Type.IsFunction<T> | ||
abstract is(value: any | T): value is T | ||
/** | ||
@@ -42,3 +42,5 @@ * Return the value if the value is valid for the type, otherwise undefined. | ||
*/ | ||
public get: Type.GetFunction<T> = value => (this.is(value) ? this.getValue(value) : undefined) | ||
public get(value: any): T | undefined { | ||
return this.is(value) ? this.getValue(value) : undefined | ||
} | ||
protected getValue(value: T) { | ||
@@ -54,3 +56,3 @@ return value | ||
*/ | ||
public flaw: Type.FlawFunction = value => { | ||
public flaw = (value: T | any): Flaw => { | ||
return this.is(value) | ||
@@ -86,7 +88,4 @@ ? { | ||
export namespace Type { | ||
export type IsFunction<T> = (value: any | T) => value is T | ||
export type FlawFunction = (value: any) => Flaw | ||
export type GetFunction<T> = (value: any) => T | undefined | ||
/** Utility-type to get Value-Type from Type<Value>. */ | ||
export type Value<T extends Type<unknown>> = T["is"] extends (x: any) => x is infer U ? U : never | ||
export type Value<T extends Type<unknown>> = T extends Type<infer U> ? U : never | ||
} | ||
@@ -98,3 +97,3 @@ | ||
} | ||
is = (value => value == undefined || this.backend.is(value)) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => value == undefined || this.backend.is(value) | ||
protected createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -109,3 +108,3 @@ return this.createFlawFromType(this.backend, value) | ||
} | ||
is = (value => value == undefined || this.backend.is(value)) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => value == undefined || this.backend.is(value) | ||
protected createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -169,6 +168,6 @@ return this.createFlawFromType(this.backend, value) | ||
} | ||
is = (value => | ||
is = (value: T | any): value is T => | ||
globalThis.Array.isArray(value) && | ||
this.options.every(option => criteriaFunctions[option.criteria].is(value, option.value)) && | ||
value.every(item => this.itemType.is(item))) as Type.IsFunction<T> | ||
value.every(item => this.itemType.is(item)) | ||
createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { | ||
@@ -175,0 +174,0 @@ const flaws = |
import { Type } from "./Type" | ||
class IslyUndefined<T = undefined> extends Type<T> { | ||
is = (value => value === undefined) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => value === undefined | ||
} | ||
@@ -6,0 +6,0 @@ |
@@ -10,3 +10,3 @@ import type { Flaw } from "./Flaw" | ||
} | ||
is = (value => this.types.some(type => type.is(value))) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => this.types.some(type => type.is(value)) | ||
@@ -13,0 +13,0 @@ protected createFlaw(value: any): Omit<Flaw, "isFlaw" | "type" | "condition"> { |
import { Type } from "./Type" | ||
class IslyUnknown<T = unknown> extends Type<T> { | ||
is = (value => value != undefined) as Type.IsFunction<T> | ||
is = (value: T | any): value is T => value != undefined | ||
} | ||
@@ -6,0 +6,0 @@ export function unknown<T = unknown>(name?: string): 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
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
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
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
2421
159710