Sindre Sorhus 7 years ago
committed by GitHub
parent
commit
6b179a037a
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      package.json
  2. 2
      source/index.ts
  3. 2
      source/lib/operators/not.ts
  4. 3
      source/lib/predicates/boolean.ts
  5. 3
      source/lib/predicates/number.ts
  6. 7
      source/lib/predicates/predicate.ts
  7. 3
      source/lib/predicates/string.ts
  8. 12
      source/ow.ts
  9. 5
      tslint.json

2
package.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"
}
}

2
source/index.ts

@ -1,3 +1,3 @@
import { ow } from './ow';
import {ow} from './ow';
export = ow;

2
source/lib/operators/not.ts

@ -1,4 +1,4 @@
import { Predicate, Validator, validatorSymbol } from '../predicates/predicate';
import {Predicate, Validator, validatorSymbol} from '../predicates/predicate';
/**
* Operator which inverts all the validations.

3
source/lib/predicates/boolean.ts

@ -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);
}

3
source/lib/predicates/number.ts

@ -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);
}

7
source/lib/predicates/predicate.ts

@ -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)}\``,

3
source/lib/predicates/string.ts

@ -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);
}

12
source/ow.ts

@ -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);

5
tslint.json

@ -1,3 +1,6 @@
{
"extends": "tslint-xo"
"extends": "tslint-xo",
"rules": {
"completed-docs": [true, "methods"]
}
}

Loading…
Cancel
Save