Добавлены функции проверки isEmpty, isNotEmpty, isObject, isEmptyObject, toNumber, isNullable, isNotNullable, покрыты тестами (#29)

This commit is contained in:
Nikolay
2020-12-28 00:26:37 +03:00
committed by GitHub
parent e8635972c6
commit d536fe3e21
6 changed files with 138 additions and 5 deletions

View File

@ -1 +1,3 @@
type Undefinable<T> = T | undefined; type Undefinable<T> = T | undefined;
type Nullable<T> = T | undefined | null;

View File

@ -0,0 +1,54 @@
import {noop} from 'lodash';
import {isEmptyObject, isNotEmpty, isObject} from '../common';
describe('isObject', () => {
it('Должен вернуть true', () => {
expect(isObject({})).toBeTruthy();
expect(isObject({go: 8})).toBeTruthy();
expect(isObject(Object.create(null))).toBeTruthy();
expect(isObject(Object.create({}))).toBeTruthy();
});
it('Должен вернуть false', () => {
expect(isObject(null)).toBeFalsy();
expect(isObject([])).toBeFalsy();
expect(isObject(NaN)).toBeFalsy();
expect(isObject(noop)).toBeFalsy();
expect(isObject(0)).toBeFalsy();
expect(isObject('')).toBeFalsy();
});
});
describe('isEmptyObject', () => {
it('Должен вернуть true', () => {
expect(isEmptyObject({})).toBeTruthy();
expect(isEmptyObject(Object.create(null))).toBeTruthy();
});
it('Должен вернуть false', () => {
expect(isEmptyObject({g: 'g'})).toBeFalsy();
expect(isEmptyObject({g: undefined})).toBeFalsy();
});
});
describe('isNotEmpty', () => {
it('Должен вернуть true', () => {
expect(isNotEmpty(['3'])).toBeTruthy();
expect(isNotEmpty({f: 'f'})).toBeTruthy();
expect(isNotEmpty({f: undefined})).toBeTruthy();
expect(isNotEmpty(0)).toBeTruthy();
expect(isNotEmpty(12)).toBeTruthy();
expect(isNotEmpty('fd')).toBeTruthy();
expect(isNotEmpty('0')).toBeTruthy();
});
it('Должен вернуть false', () => {
expect(isNotEmpty([])).toBeFalsy();
expect(isNotEmpty({})).toBeFalsy();
expect(isNotEmpty('')).toBeFalsy();
expect(isNotEmpty(' ')).toBeFalsy();
expect(isNotEmpty()).toBeFalsy();
expect(isNotEmpty(null)).toBeFalsy();
expect(isNotEmpty(NaN)).toBeFalsy();
});
});

View File

@ -1,5 +1,53 @@
import {PageType} from '../enums/common'; import {isNaN, isNumber, isString} from 'lodash';
import {PageType} from '_enums/common';
export const isPageType = (value?: string): value is PageType => ( export const isPageType = (value?: string): value is PageType => (
Object.values(PageType).some(pageType => pageType === value) Object.values(PageType).some(pageType => pageType === value)
); );
export function isNullable<T>(value: Nullable<T>): value is (null | undefined) {
return value === null || value === undefined;
}
export function isNotNullable<T>(value: Nullable<T>): value is NonNullable<T> {
return !isNullable(value);
}
export function isObject<T>(value: Nullable<T>): value is T {
return (
isNotNullable(value)
&& !Array.isArray(value)
&& !isNaN(value)
&& typeof value === 'object'
);
}
export const isEmptyObject = <T>(value: Nullable<T>): boolean => (
!Object.keys(value ?? {}).length
);
export const isNotEmpty = <T>(value?: Nullable<T>): value is T => {
if (isString(value)) {
return !!value?.trim();
}
if (Array.isArray(value)) {
return !!value.length;
}
if (isNaN(value)) {
return false;
}
if (isNumber(value)) {
return true;
}
if (isObject(value)) {
return !isEmptyObject(value);
}
return isNotNullable(value);
};
export const isEmpty = (value: unknown) => !isNotEmpty(value);

View File

@ -1,4 +1,4 @@
import {numberToString} from '_utils/common'; import {numberToString} from '../common';
describe('test numberToString', () => { describe('test numberToString', () => {
it('success convert', () => { it('success convert', () => {

View File

@ -0,0 +1,25 @@
import {toNumber} from '../parsers';
describe('toNumber', () => {
it('Возвращает число', () => {
expect(toNumber(0)).toBe(0);
expect(toNumber(' 0 ')).toBe(0);
expect(toNumber('0')).toBe(0);
expect(toNumber('56')).toBe(56);
expect(toNumber(' 56 ')).toBe(56);
expect(toNumber(' 5.6 ')).toBe(5.6);
expect(toNumber(' .9 ')).toBe(0.9);
expect(toNumber(1.4)).toBe(1.4);
expect(toNumber(.4)).toBe(0.4);
});
it('Возвращает undefined', () => {
expect(toNumber(' g')).toBeUndefined();
expect(toNumber(null)).toBeUndefined();
expect(toNumber(undefined)).toBeUndefined();
expect(toNumber({})).toBeUndefined();
expect(toNumber([])).toBeUndefined();
expect(toNumber(' 4 5 ')).toBeUndefined();
expect(toNumber(' 4 .5 ')).toBeUndefined();
});
});

View File

@ -1,4 +1,8 @@
export const toNumber = (val: unknown): Undefinable<number> => { import {isNumber, isString} from 'lodash';
const prepareValue = Number(val);
export const toNumber = (value: unknown): Undefinable<number> => {
if (isNumber(value) || isString(value)) {
const prepareValue = Number(value);
return Number.isNaN(prepareValue) ? undefined : prepareValue; return Number.isNaN(prepareValue) ? undefined : prepareValue;
}
}; };