Browse Source

Invert only the following predicate with the 'not' operator (#71)

master
Luke Travis 7 years ago
committed by Sindre Sorhus
parent
commit
c1de125aa0
  1. 2
      readme.md
  2. 6
      source/lib/operators/not.ts
  3. 5
      source/test/test.ts

2
readme.md

@ -132,7 +132,7 @@ The following predicates are available on every type.
#### not
Inverts the following predicates.
Inverts the following predicate.
```ts
ow(1, ow.number.not.infinite);

6
source/lib/operators/not.ts

@ -1,12 +1,14 @@
import {Predicate, validatorSymbol} from '../predicates/predicate';
/**
* Operator which inverts all the validations.
* Operator which inverts the following validation.
*
* @hidden
* @param predictate Predicate to wrap inside the operator.
*/
export const not = <T, P extends Predicate<T>>(predicate: P) => {
const originalAddValidator = predicate.addValidator;
predicate.addValidator = validator => {
const fn = validator.validator;
const message = validator.message;
@ -16,6 +18,8 @@ export const not = <T, P extends Predicate<T>>(predicate: P) => {
predicate[validatorSymbol].push(validator);
predicate.addValidator = originalAddValidator;
return predicate;
};

5
source/test/test.ts

@ -2,12 +2,13 @@ import test from 'ava';
import m from '..';
test('not', t => {
t.notThrows(() => m('foo!', m.string.not.alphanumeric));
t.notThrows(() => m(1, m.number.not.infinite));
t.notThrows(() => m(1, m.number.not.infinite.greaterThan(5)));
t.notThrows(() => m(1, m.number.not.infinite.not.greaterThan(5)));
t.throws(() => m(6, m.number.not.infinite.not.greaterThan(5)));
t.notThrows(() => m('foo!', m.string.not.alphabetical));
t.notThrows(() => m('foo!', m.string.not.alphanumeric));
t.notThrows(() => m('foo!', m.string.label('foo').not.alphanumeric));
t.notThrows(() => m('foo!', m.string.not.label('foo').alphanumeric));
t.notThrows(() => m('foo!', m.string.not.alphanumeric.label('foo')));
t.notThrows(() => m('FOO!', m.string.not.lowercase));
t.notThrows(() => m('foo!', m.string.not.uppercase));

Loading…
Cancel
Save