Skip to content

Commit

Permalink
feat(n4s): add isBlank rule
Browse files Browse the repository at this point in the history
Co-authored-by: Evyatar <code@ealush.com>
  • Loading branch information
lpizzinidev and ealush authored Oct 9, 2021
1 parent 0bcf915 commit 5adc337
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
28 changes: 28 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)
- [isBoolean](#isboolean)
- [isNotBoolean](#isnotboolean)
- [isBlank](#isblank)
- [isNotBlank](#isnotblank)
- [isNumber](#isnumber)
- [isNotNumber](#isnotnumber)
- [isNaN](#isNaN)
Expand Down Expand Up @@ -856,6 +858,32 @@ enforce(false).isNotBoolean();
// throws
```

## isBlank

### Description

Determines wheter an enforced string contains only whitespaces

### Usage examples:

```js
enforce(' ').isBlank(); // passes
enforce('not blank').isBlank(); // throws
```

## isNotBlank

### Description

Determines wheter an enforced string contains at least a non-whitespace character

### Usage examples:

```js
enforce('not blank').isNotBlank(); // passes
enforce(' ').isNotBlank(); // throws
```

## isNumber

### Description
Expand Down
3 changes: 3 additions & 0 deletions packages/n4s/src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { greaterThanOrEquals } from 'greaterThanOrEquals';
import { inside, notInside } from 'inside';
import { isArray, isNotArray } from 'isArray';
import { isBetween, isNotBetween } from 'isBetween';
import { isBlank, isNotBlank } from 'isBlank';
import { isBoolean, isNotBoolean } from 'isBoolean';
import { isEmpty, isNotEmpty } from 'isEmpty';
import { isEven } from 'isEven';
Expand Down Expand Up @@ -42,13 +43,15 @@ export default function rules() {
isArray,
isBetween,
isBoolean,
isBlank,
isEmpty,
isEven,
isFalsy,
isNaN,
isNegative,
isNotArray,
isNotBetween,
isNotBlank,
isNotBoolean,
isNotEmpty,
isNotNaN,
Expand Down
21 changes: 21 additions & 0 deletions packages/n4s/src/rules/__tests__/isBlank.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { isBlank, isNotBlank } from 'isBlank';

describe('isBlank', () => {
it('Should return true for a string of whitespaces', () => {
expect(isBlank(' ')).toBe(true);
});

it('Should return false for a string with at least a non-whitespace', () => {
expect(isBlank('not blank')).toBe(false);
});
});

describe('isNotBlank', () => {
it('Should return false for a string of whitespaces', () => {
expect(isNotBlank(' ')).toBe(false);
});

it('Should return true for a string with at least a non-whitespace', () => {
expect(isNotBlank('not blank')).toBe(true);
});
});
7 changes: 7 additions & 0 deletions packages/n4s/src/rules/isBlank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import bindNot from 'bindNot';

export function isBlank(value) {
return typeof value === 'string' && value.trim() === '';
}

export const isNotBlank = bindNot(isBlank);

0 comments on commit 5adc337

Please sign in to comment.