Skip to content

Commit

Permalink
rule(n4s): add isNullish rule, improve isBlank
Browse files Browse the repository at this point in the history
  • Loading branch information
ealush committed Dec 8, 2021
1 parent c4a4140 commit 7daf6d2
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/n4s/src/plugins/schema/optional.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isNullish from 'isNullish';
import { isNullish } from 'isNullish';

import type { TLazy } from 'genEnforceLazy';
import type { TRuleDetailedResult } from 'ruleReturn';
Expand Down
16 changes: 16 additions & 0 deletions packages/n4s/src/rules/__tests__/isBlank.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,14 @@ describe('isBlank', () => {
it('Should return false for a string with at least a non-whitespace', () => {
expect(isBlank('not blank')).toBe(false);
});

it('Should return true for undefined', () => {
expect(isBlank(undefined)).toBeTruthy();
});

it('Should return true for null', () => {
expect(isBlank(null)).toBeTruthy();
});
});

describe('isNotBlank', () => {
Expand All @@ -18,4 +26,12 @@ describe('isNotBlank', () => {
it('Should return true for a string with at least a non-whitespace', () => {
expect(isNotBlank('not blank')).toBe(true);
});

it('Should return false for undefined', () => {
expect(isNotBlank(undefined)).toBeFalsy();
});

it('Should return false for null', () => {
expect(isNotBlank(null)).toBeFalsy();
});
});
55 changes: 55 additions & 0 deletions packages/n4s/src/rules/__tests__/isNullish.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { enforce } from 'n4s';

describe('enforce.isNullish', () => {
it('Should return true for `null` value', () => {
expect(enforce.isNullish().test(null)).toBe(true);
});

it('Should return true for `undefined` value', () => {
expect(enforce.isNullish().test(undefined)).toBe(true);
});

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

describe('enforce.isNotNullish', () => {
it('Should return false for `null` value', () => {
expect(enforce.isNotNullish().test(null)).toBe(false);
});

it('Should return false for `undefined` value', () => {
expect(enforce.isNotNullish().test(undefined)).toBe(false);
});

it.each([
NaN,
false,
true,
Object,
Array(0),
'',
' ',
0,
1,
'0',
'1',
Function.prototype,
])('Should return true for %s value', v => {
expect(enforce.isNotNullish().test(v)).toBe(true);
});
});
3 changes: 2 additions & 1 deletion packages/n4s/src/rules/isBlank.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import bindNot from 'bindNot';
import { isNullish } from 'isNullish';
import { isStringValue } from 'isStringValue';

export function isBlank(value: unknown): boolean {
return isStringValue(value) && !value.trim();
return isNullish(value) || (isStringValue(value) && !value.trim());
}

export const isNotBlank = bindNot(isBlank);
4 changes: 4 additions & 0 deletions packages/n4s/src/runtime/rules.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { isNullish, isNotNullish } from 'isNullish';

import { endsWith, doesNotEndWith } from 'endsWith';
import { equals, notEquals } from 'equals';
import { greaterThan } from 'greaterThan';
Expand Down Expand Up @@ -59,11 +61,13 @@ export default function rules() {
isNotEmpty,
isNotNaN,
isNotNull,
isNotNullish,
isNotNumber,
isNotNumeric,
isNotString,
isNotUndefined,
isNull,
isNullish,
isNumber,
isNumeric,
isOdd,
Expand Down
6 changes: 5 additions & 1 deletion packages/shared/src/isNullish.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { isNull } from 'isNull';
import { isUndefined } from 'isUndefined';

export default function isNullish(value: any): value is null | undefined {
import bindNot from 'bindNot';

export function isNullish(value: any): value is null | undefined {
return isNull(value) || isUndefined(value);
}

export const isNotNullish = bindNot(isNullish);
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/key.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import asArray from 'asArray';
import isNullish from 'isNullish';
import { isNullish } from 'isNullish';
import * as nestedArray from 'nestedArray';
import { throwErrorDeferred } from 'throwError';

Expand Down
2 changes: 1 addition & 1 deletion packages/vest/src/core/test/lib/useTestAtCursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import defaultTo from 'defaultTo';
import { isEmpty, isNotEmpty } from 'isEmpty';
import isNullish from 'isNullish';
import { isNullish } from 'isNullish';
import * as nestedArray from 'nestedArray';
import type { NestedArray } from 'nestedArray';
import { throwErrorDeferred } from 'throwError';
Expand Down

0 comments on commit 7daf6d2

Please sign in to comment.