Skip to content

Commit

Permalink
added: [N4S] isNull rule. (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
omriLugasi committed Sep 22, 2020
1 parent a13e860 commit a424282
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
41 changes: 41 additions & 0 deletions packages/n4s/docs/rules.md
Expand Up @@ -32,6 +32,8 @@ Enforce rules are functions that allow you to test your data against different c
- [isNotNumber](#isnotnumber)
- [isNaN](#isNaN)
- [isNotNaN](#isNotNaN)
- [isNull](#isnull)
- [isNotNull](#isnotnull)
- [isString](#isstring)
- [isNotString](#isnotstring)
- [isUndefined](#isundefined)
Expand Down Expand Up @@ -886,6 +888,45 @@ enforce('A' / 'B').isNaN();
// throws
```


## isNull

### Description

Enforces that a specified value is `null`.

### Usage examples:

```js
enforce(null).isNull();
// passes
```

```js
enforce(undefined).isNull();
enforce(true).isNull();
// throws
```

## isNotNull

### Description

Reverse implementation of `isNull`. Checks that a value is not null.

### Usage examples:

```js
enforce("hello").isNull();
enforce(200).isNull();
// passes
```

```js
enforce(null).isNull();
// throws
```

## isString

### Description
Expand Down
2 changes: 2 additions & 0 deletions packages/n4s/src/rules/index.js
Expand Up @@ -7,6 +7,7 @@ import isArray from './isArray';
import isEmpty from './isEmpty';
import isEven from './isEven';
import isNaN from './isNaN';
import isNull from './isNull';
import isNumber from './isNumber';
import isNumeric from './isNumeric';
import isOdd from './isOdd';
Expand All @@ -32,6 +33,7 @@ const rules = {
isEmpty,
isEven,
isNaN,
isNull,
isNumber,
isNumeric,
isOdd,
Expand Down
7 changes: 7 additions & 0 deletions packages/n4s/src/rules/isNull/index.js
@@ -0,0 +1,7 @@
function isNull(value) {
return value === null;
}

isNull.negativeForm = 'isNotNull';

export default isNull;
29 changes: 29 additions & 0 deletions packages/n4s/src/rules/isNull/spec.js
@@ -0,0 +1,29 @@
import isNull from '.';

describe('Tests isNull rule', () => {
it('Should return true for `null` value', () => {
expect(isNull(null)).toBe(true);
});

it.each([
undefined,
NaN,
false,
true,
Object,
Array(0),
'',
' ',
0,
1,
'0',
'1',
() => {},
])('Should return false for %s value', v => {
expect(isNull(v)).toBe(false);
});

it('Should expose negativeForm property', () => {
expect(isNull.negativeForm).toBe('isNotNull');
});
});
2 changes: 2 additions & 0 deletions packages/n4s/src/rules/spec.js
Expand Up @@ -25,6 +25,7 @@ const negativeRules = [
'notInside',
'notMatches',
'numberNotEquals',
'isNotNull',
];

const positiveRules = [
Expand Down Expand Up @@ -56,6 +57,7 @@ const positiveRules = [
'numberEquals',
'shorterThan',
'shorterThanOrEquals',
'isNull',
];

const allRules = [...new Set([...positiveRules, ...negativeRules])];

0 comments on commit a424282

Please sign in to comment.