mirror of https://github.com/lukechilds/ow.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
25 lines
709 B
25 lines
709 B
7 years ago
|
import * as is from '@sindresorhus/is';
|
||
|
import { ArgumentError } from './lib/argument-error';
|
||
|
import { Predicate } from './lib/predicates/predicate';
|
||
|
import { StringPredicate } from './lib/predicates/string';
|
||
|
|
||
|
export interface Ow {
|
||
|
(value: any, predicate: Predicate): void;
|
||
|
/**
|
||
|
* Test the value to be a string.
|
||
|
*/
|
||
|
string?: StringPredicate;
|
||
|
}
|
||
|
|
||
|
export const ow: Ow = (value: any, predicate: Predicate) => {
|
||
|
for (const validator of predicate.context.validators) {
|
||
|
const result = validator(value);
|
||
|
if (result) {
|
||
|
// TODO: Modify the stack output to show the original `ow()` call instead of this `throw` statement
|
||
|
throw new ArgumentError(result, ow);
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
ow.string = new StringPredicate();
|