Sindre Sorhus
7 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with
20 additions and
19 deletions
-
package.json
-
source/index.ts
-
source/lib/operators/not.ts
-
source/lib/predicates/boolean.ts
-
source/lib/predicates/number.ts
-
source/lib/predicates/predicate.ts
-
source/lib/predicates/string.ts
-
source/ow.ts
-
tslint.json
|
|
@ -55,7 +55,7 @@ |
|
|
|
"del-cli": "^1.1.0", |
|
|
|
"nyc": "^11.2.1", |
|
|
|
"tslint": "^5.8.0", |
|
|
|
"tslint-xo": "^0.1.0", |
|
|
|
"tslint-xo": "^0.2.0", |
|
|
|
"typescript": "^2.6.1" |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
@ -1,3 +1,3 @@ |
|
|
|
import { ow } from './ow'; |
|
|
|
import {ow} from './ow'; |
|
|
|
|
|
|
|
export = ow; |
|
|
|
|
|
@ -1,4 +1,4 @@ |
|
|
|
import { Predicate, Validator, validatorSymbol } from '../predicates/predicate'; |
|
|
|
import {Predicate, Validator, validatorSymbol} from '../predicates/predicate'; |
|
|
|
|
|
|
|
/** |
|
|
|
* Operator which inverts all the validations. |
|
|
|
|
|
@ -1,7 +1,6 @@ |
|
|
|
import { Predicate, Context } from './predicate'; |
|
|
|
import {Predicate, Context} from './predicate'; |
|
|
|
|
|
|
|
export class BooleanPredicate extends Predicate<boolean> { |
|
|
|
|
|
|
|
constructor(context?: Context) { |
|
|
|
super('boolean', context); |
|
|
|
} |
|
|
|
|
|
@ -1,8 +1,7 @@ |
|
|
|
import * as is from '@sindresorhus/is'; |
|
|
|
import { Predicate, Context } from './predicate'; |
|
|
|
import {Predicate, Context} from './predicate'; |
|
|
|
|
|
|
|
export class NumberPredicate extends Predicate<number> { |
|
|
|
|
|
|
|
constructor(context?: Context) { |
|
|
|
super('number', context); |
|
|
|
} |
|
|
|
|
|
@ -1,5 +1,5 @@ |
|
|
|
import * as is from '@sindresorhus/is'; |
|
|
|
import { not } from '../operators/not'; |
|
|
|
import {not} from '../operators/not'; |
|
|
|
|
|
|
|
export interface Validator<T> { |
|
|
|
message(value: T): string; |
|
|
@ -13,10 +13,11 @@ export interface Context { |
|
|
|
export const validatorSymbol = Symbol('validators'); |
|
|
|
|
|
|
|
export abstract class Predicate<T = any> { |
|
|
|
|
|
|
|
constructor( |
|
|
|
type: string, |
|
|
|
private context: Context = { validators: [] } |
|
|
|
private context: Context = { |
|
|
|
validators: [] |
|
|
|
} |
|
|
|
) { |
|
|
|
this.addValidator({ |
|
|
|
message: value => `Expected argument to be of type \`${type}\` but received type \`${is(value)}\``, |
|
|
|
|
|
@ -1,8 +1,7 @@ |
|
|
|
import * as valiDate from 'vali-date'; |
|
|
|
import { Predicate, Context } from './predicate'; |
|
|
|
import {Predicate, Context} from './predicate'; |
|
|
|
|
|
|
|
export class StringPredicate extends Predicate<string> { |
|
|
|
|
|
|
|
constructor(context?: Context) { |
|
|
|
super('string', context); |
|
|
|
} |
|
|
|
|
|
@ -1,8 +1,8 @@ |
|
|
|
import { ArgumentError } from './lib/argument-error'; |
|
|
|
import { Predicate, validatorSymbol } from './lib/predicates/predicate'; |
|
|
|
import { StringPredicate } from './lib/predicates/string'; |
|
|
|
import { NumberPredicate } from './lib/predicates/number'; |
|
|
|
import { BooleanPredicate } from './lib/predicates/boolean'; |
|
|
|
import {ArgumentError} from './lib/argument-error'; |
|
|
|
import {Predicate, validatorSymbol} from './lib/predicates/predicate'; |
|
|
|
import {StringPredicate} from './lib/predicates/string'; |
|
|
|
import {NumberPredicate} from './lib/predicates/number'; |
|
|
|
import {BooleanPredicate} from './lib/predicates/boolean'; |
|
|
|
|
|
|
|
export interface Ow { |
|
|
|
(value: any, predicate: Predicate): void; |
|
|
@ -21,7 +21,7 @@ export interface Ow { |
|
|
|
} |
|
|
|
|
|
|
|
const main = (value: any, predicate: Predicate) => { |
|
|
|
for (const { validator, message } of predicate[validatorSymbol]) { |
|
|
|
for (const {validator, message} of predicate[validatorSymbol]) { |
|
|
|
if (!validator(value)) { |
|
|
|
// TODO: Modify the stack output to show the original `ow()` call instead of this `throw` statement
|
|
|
|
throw new ArgumentError(message(value), main); |
|
|
|
|
|
@ -1,3 +1,6 @@ |
|
|
|
{ |
|
|
|
"extends": "tslint-xo" |
|
|
|
"extends": "tslint-xo", |
|
|
|
"rules": { |
|
|
|
"completed-docs": [true, "methods"] |
|
|
|
} |
|
|
|
} |
|
|
|