Comparing version 0.0.3 to 0.0.4
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw" | ||
import { Type } from "./Type" | ||
@@ -12,2 +13,5 @@ | ||
} | ||
flaw(value: any): true | Flaw { | ||
return this.is(value) || { type: this.name } | ||
} | ||
} | ||
@@ -14,0 +18,0 @@ export type Array<T> = ArrayClass<T> |
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw"; | ||
import { Type } from "./Type"; | ||
@@ -7,2 +8,3 @@ declare class ArrayClass<T> extends Type<T[]> { | ||
is(value: any | T[]): value is T[]; | ||
flaw(value: any): true | Flaw; | ||
} | ||
@@ -9,0 +11,0 @@ export declare type Array<T> = ArrayClass<T>; |
@@ -11,2 +11,5 @@ import { Type } from "./Type"; | ||
} | ||
flaw(value) { | ||
return this.is(value) || { type: this.name }; | ||
} | ||
} | ||
@@ -13,0 +16,0 @@ export function array(item) { |
@@ -0,9 +1,16 @@ | ||
import { Flaw } from "./Flaw"; | ||
import { Type } from "./Type"; | ||
declare class NumberClass extends Type<number> { | ||
private readonly criteria?; | ||
readonly condition?: string | undefined; | ||
readonly name = "number"; | ||
constructor(); | ||
constructor(criteria?: ((value: number) => boolean) | undefined, condition?: string | undefined); | ||
is(value: any | number): value is number; | ||
flaw(value: any): true | Flaw; | ||
} | ||
export declare type Number = NumberClass; | ||
export declare const number: NumberClass; | ||
export declare namespace Number { | ||
type Criteria = "positive" | "negative" | "integer"; | ||
} | ||
export declare function number(criteria?: number | Number.Criteria | Number.Criteria[] | number[] | ((value: number) => boolean)): NumberClass; | ||
export {}; |
import { Type } from "./Type"; | ||
class NumberClass extends Type { | ||
constructor() { | ||
constructor(criteria, condition) { | ||
super(); | ||
this.criteria = criteria; | ||
this.condition = condition; | ||
this.name = "number"; | ||
} | ||
is(value) { | ||
return typeof value == "number"; | ||
return typeof value == "number" && (!this.criteria || this.criteria(value)); | ||
} | ||
flaw(value) { | ||
return this.is(value) || { type: this.name }; | ||
} | ||
} | ||
export const number = new NumberClass(); | ||
export function number(criteria) { | ||
function fromCriteria(criteria) { | ||
return typeof criteria == "number" | ||
? value => value == criteria | ||
: criteria == "positive" | ||
? value => value > 0 | ||
: criteria == "negative" | ||
? value => value < 0 | ||
: criteria == "integer" | ||
? Number.isInteger | ||
: ((c) => Array.isArray(c) && c.every(c => typeof c == "number"))(criteria) | ||
? value => criteria.map(fromCriteria).some(c => c(value)) | ||
: ((c) => Array.isArray(c) && c.every(c => typeof c == "string"))(criteria) | ||
? value => criteria.map(fromCriteria).every(c => c(value)) | ||
: criteria; | ||
} | ||
return new NumberClass(criteria == undefined ? undefined : fromCriteria(criteria)); | ||
} | ||
//# sourceMappingURL=Number.js.map |
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw"; | ||
import { Type } from "./Type"; | ||
@@ -7,2 +8,3 @@ declare class StringClass extends Type<string> { | ||
is(value: any | string): value is string; | ||
flaw(value: any): true | Flaw; | ||
} | ||
@@ -9,0 +11,0 @@ export declare type String = StringClass; |
@@ -11,2 +11,5 @@ import { Type } from "./Type"; | ||
} | ||
flaw(value) { | ||
return this.is(value) || { type: this.name }; | ||
} | ||
} | ||
@@ -13,0 +16,0 @@ export function string(...strings) { |
import { Flaw } from "./Flaw"; | ||
export declare abstract class Type<T> { | ||
abstract readonly name: string; | ||
readonly condition?: ""; | ||
readonly condition?: string; | ||
abstract is(value: any | T): value is T; | ||
flaw(value: any): true | Flaw; | ||
abstract flaw(value: any): true | Flaw; | ||
} |
export class Type { | ||
flaw(value) { | ||
return this.is(value) || { type: this.name }; | ||
constructor() { | ||
this.condition = ""; | ||
} | ||
} | ||
//# sourceMappingURL=Type.js.map |
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw"; | ||
import { Type } from "./Type"; | ||
@@ -7,2 +8,3 @@ declare class UnionClass<T> extends Type<T> { | ||
is(value: any | T): value is T; | ||
flaw(value: any): true | Flaw; | ||
} | ||
@@ -9,0 +11,0 @@ export declare type Union<T> = UnionClass<T>; |
@@ -11,2 +11,8 @@ import { Type } from "./Type"; | ||
} | ||
flaw(value) { | ||
return (this.is(value) || { | ||
type: this.name, | ||
flaws: this.types.map(type => type.flaw(value)).filter(flaw => flaw != true), | ||
}); | ||
} | ||
} | ||
@@ -13,0 +19,0 @@ export function union(...types) { |
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw" | ||
import { Type } from "./Type" | ||
@@ -5,10 +6,37 @@ | ||
readonly name = "number" | ||
constructor() { | ||
constructor(private readonly criteria?: (value: number) => boolean, readonly condition?: string) { | ||
super() | ||
} | ||
is(value: any | number): value is number { | ||
return typeof value == "number" | ||
return typeof value == "number" && (!this.criteria || this.criteria(value)) | ||
} | ||
flaw(value: any): true | Flaw { | ||
return this.is(value) || { type: this.name } | ||
} | ||
} | ||
export type Number = NumberClass | ||
export const number = new NumberClass() | ||
export namespace Number { | ||
export type Criteria = "positive" | "negative" | "integer" | ||
} | ||
export function number( | ||
criteria?: number | Number.Criteria | Number.Criteria[] | number[] | ((value: number) => boolean) | ||
) { | ||
function fromCriteria( | ||
criteria: number | Number.Criteria | Number.Criteria[] | number[] | ((value: number) => boolean) | ||
): (value: number) => boolean { | ||
return typeof criteria == "number" | ||
? value => value == criteria | ||
: criteria == "positive" | ||
? value => value > 0 | ||
: criteria == "negative" | ||
? value => value < 0 | ||
: criteria == "integer" | ||
? Number.isInteger | ||
: ((c: any): c is number[] => Array.isArray(c) && c.every(c => typeof c == "number"))(criteria) | ||
? value => criteria.map(fromCriteria).some(c => c(value)) | ||
: ((c: any): c is Number.Criteria[] => Array.isArray(c) && c.every(c => typeof c == "string"))(criteria) | ||
? value => criteria.map(fromCriteria).every(c => c(value)) | ||
: criteria | ||
} | ||
return new NumberClass(criteria == undefined ? undefined : fromCriteria(criteria)) | ||
} |
{ | ||
"name": "isly", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "Library for type checking.", | ||
@@ -5,0 +5,0 @@ "author": "Utily Contributors", |
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw" | ||
import { Type } from "./Type" | ||
@@ -11,2 +12,5 @@ | ||
} | ||
flaw(value: any): true | Flaw { | ||
return this.is(value) || { type: this.name } | ||
} | ||
} | ||
@@ -13,0 +17,0 @@ |
@@ -5,7 +5,5 @@ import { Flaw } from "./Flaw" | ||
abstract readonly name: string | ||
readonly condition?: "" | ||
readonly condition?: string = "" | ||
abstract is(value: any | T): value is T | ||
flaw(value: any): true | Flaw { | ||
return this.is(value) || { type: this.name } | ||
} | ||
abstract flaw(value: any): true | Flaw | ||
} |
@@ -0,1 +1,2 @@ | ||
import { Flaw } from "./Flaw" | ||
import { Type } from "./Type" | ||
@@ -12,2 +13,10 @@ | ||
} | ||
flaw(value: any): true | Flaw { | ||
return ( | ||
this.is(value) || { | ||
type: this.name, | ||
flaws: this.types.map(type => type.flaw(value)).filter(flaw => flaw != true) as Flaw[], | ||
} | ||
) | ||
} | ||
} | ||
@@ -14,0 +23,0 @@ export type Union<T> = UnionClass<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
34347
590