Skip to content

Commit

Permalink
added: [n4s] add isNaN rule (#300)
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush authored Aug 8, 2020
1 parent f9f1d39 commit 062c40a
Show file tree
Hide file tree
Showing 15 changed files with 99 additions and 463 deletions.
20 changes: 7 additions & 13 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
node_modules
.vscode
.sass-cache
dev_docpress
_docpress
dev
version.json
version.txt
.version
documentation/MAIN.md
documentation/assets/**/*.css
.DS_Store
*.log
packages/vest/utilities/
packages/n4s/extended/
packages/n4s/dist

# build artifacts
packages/vest/dist
packages/vest/esm
packages/vest/*.js
packages/vest/*.d.ts
packages/vest/*.cjs
packages/vest/*.mjs
packages/vest/utilities/
packages/n4s/dist
packages/n4s/*.js
packages/n4s/extended/
packages/n4s/*.js
2 changes: 1 addition & 1 deletion packages/n4s/dist/ensure.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/n4s/dist/extended/enforce.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/n4s/dist/extended/ensure.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/n4s/dist/n4s.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions packages/n4s/docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ Enforce rules are functions that allow you to test your data against different c
- [isNotArray](#isnotarray)
- [isNumber](#isnumber)
- [isNotNumber](#isnotnumber)
- [isNaN](#isNaN)
- [isNotNaN](#isNotNaN)
- [isString](#isstring)
- [isNotString](#isnotstring)
- [isOdd](#isodd)
Expand Down Expand Up @@ -802,6 +804,7 @@ enforce('hello').isNotArray();
### Description

Checks if a value is of type `number`.
Equals to `typeof value === 'number'`

### Usage examples:

Expand All @@ -824,6 +827,8 @@ enforce('143').isNumber();
Checks if a value is of any type other than `number`.
Reverse implementation of `isNumber`.

**note** isNotNumber does not check for `NaN` value. For NaN values, use [isNaN](#isNaN).

### Usage examples:

```js
Expand All @@ -838,6 +843,48 @@ enforce(NaN).isNotNumber(); // throws (NaN is of type 'number!')
// throws
```

## isNaN

### Description

A wrapper around JavaScripts Number.isNaN() function. Checks if a value is NaN.

### Usage examples:

```js
enforce(NaN).isNaN();
enforce('A' / 'B').isNaN();
// passes
```

```js
enforce(null * null).isNaN(); // null*null = 0
enforce(200).isNaN();
enforce('1984').isNaN();
// throws
```

## isNotNaN

### Description

Reverse implementation of `isNaN`. Checks that a value is not NaN.

### Usage examples:

```js
enforce(null * null).isNaN(); // null*null = 0
enforce(200).isNaN();
enforce('1984').isNaN();
// passes
```

```js
enforce(NaN).isNaN();
enforce('A' / 'B').isNaN();
// throws
```

## isString

### Description
Expand Down
2 changes: 2 additions & 0 deletions packages/n4s/src/rules/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import inside from './inside';
import isArray from './isArray';
import isEmpty from './isEmpty';
import isEven from './isEven';
import isNaN from './isNaN';
import isNumber from './isNumber';
import isNumeric from './isNumeric';
import isOdd from './isOdd';
Expand All @@ -29,6 +30,7 @@ const rules = {
isArray,
isEmpty,
isEven,
isNaN,
isNumber,
isNumeric,
isOdd,
Expand Down
7 changes: 7 additions & 0 deletions packages/n4s/src/rules/isNaN/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function isNaN(value) {
return Number.isNaN(value);
}

isNaN.negativeForm = 'isNotNaN';

export default isNaN;
28 changes: 28 additions & 0 deletions packages/n4s/src/rules/isNaN/spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import _isNaN from '.';

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

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

it('Should expose negativeForm property', () => {
expect(_isNaN.negativeForm).toBe('isNotNaN');
});
});
2 changes: 2 additions & 0 deletions packages/n4s/src/rules/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe('Tests enforce rules API', () => {
const negativeRules = [
'isNotArray',
'isNotEmpty',
'isNotNaN',
'isNotNumber',
'isNotNumeric',
'isNotString',
Expand All @@ -35,6 +36,7 @@ const positiveRules = [
'isArray',
'isEmpty',
'isEven',
'isNaN',
'isNumber',
'isNumeric',
'isOdd',
Expand Down
3 changes: 0 additions & 3 deletions packages/vest/any.d.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/vest/classNames.d.ts

This file was deleted.

19 changes: 0 additions & 19 deletions packages/vest/enforceExtended.d.ts

This file was deleted.

2 changes: 2 additions & 0 deletions packages/vest/src/vest.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ interface IEnforceRules<T = {}> {
isEmpty: () => IEnforceRules<T> & EnforceExtendMap<T>;
isEven: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNumber: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNaN: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNotNaN: () => IEnforceRules<T> & EnforceExtendMap<T>;
isNumeric: () => IEnforceRules<T> & EnforceExtendMap<T>;
isOdd: () => IEnforceRules<T> & EnforceExtendMap<T>;
isTruthy: () => IEnforceRules<T> & EnforceExtendMap<T>;
Expand Down
Loading

0 comments on commit 062c40a

Please sign in to comment.