Добавлены функции проверки isEmpty, isNotEmpty, isObject, isEmptyObject, toNumber, isNullable, isNotNullable, покрыты тестами (#29)
This commit is contained in:
2
src/core/dts/comon.d.ts
vendored
2
src/core/dts/comon.d.ts
vendored
@ -1 +1,3 @@
|
||||
type Undefinable<T> = T | undefined;
|
||||
|
||||
type Nullable<T> = T | undefined | null;
|
||||
|
||||
54
src/core/referers/__test__/common.test.ts
Normal file
54
src/core/referers/__test__/common.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
@ -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 => (
|
||||
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);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
import {numberToString} from '_utils/common';
|
||||
import {numberToString} from '../common';
|
||||
|
||||
describe('test numberToString', () => {
|
||||
it('success convert', () => {
|
||||
25
src/core/utils/__test__/parsers.test.ts
Normal file
25
src/core/utils/__test__/parsers.test.ts
Normal 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();
|
||||
});
|
||||
});
|
||||
@ -1,4 +1,8 @@
|
||||
export const toNumber = (val: unknown): Undefinable<number> => {
|
||||
const prepareValue = Number(val);
|
||||
import {isNumber, isString} from 'lodash';
|
||||
|
||||
export const toNumber = (value: unknown): Undefinable<number> => {
|
||||
if (isNumber(value) || isString(value)) {
|
||||
const prepareValue = Number(value);
|
||||
return Number.isNaN(prepareValue) ? undefined : prepareValue;
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user