diff --git a/src/core/dts/comon.d.ts b/src/core/dts/comon.d.ts index f933aa0..c832333 100644 --- a/src/core/dts/comon.d.ts +++ b/src/core/dts/comon.d.ts @@ -1 +1,3 @@ type Undefinable = T | undefined; + +type Nullable = T | undefined | null; diff --git a/src/core/referers/__test__/common.test.ts b/src/core/referers/__test__/common.test.ts new file mode 100644 index 0000000..9e41856 --- /dev/null +++ b/src/core/referers/__test__/common.test.ts @@ -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(); + }); +}); diff --git a/src/core/referers/common.ts b/src/core/referers/common.ts index e96ae14..296c4c5 100644 --- a/src/core/referers/common.ts +++ b/src/core/referers/common.ts @@ -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(value: Nullable): value is (null | undefined) { + return value === null || value === undefined; +} + +export function isNotNullable(value: Nullable): value is NonNullable { + return !isNullable(value); +} + +export function isObject(value: Nullable): value is T { + return ( + isNotNullable(value) + && !Array.isArray(value) + && !isNaN(value) + && typeof value === 'object' + ); +} + +export const isEmptyObject = (value: Nullable): boolean => ( + !Object.keys(value ?? {}).length +); + +export const isNotEmpty = (value?: Nullable): 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); diff --git a/src/core/__test__/utils.test.ts b/src/core/utils/__test__/common.test.ts similarity index 74% rename from src/core/__test__/utils.test.ts rename to src/core/utils/__test__/common.test.ts index 8bd96a1..bbf67d5 100644 --- a/src/core/__test__/utils.test.ts +++ b/src/core/utils/__test__/common.test.ts @@ -1,4 +1,4 @@ -import {numberToString} from '_utils/common'; +import {numberToString} from '../common'; describe('test numberToString', () => { it('success convert', () => { diff --git a/src/core/utils/__test__/parsers.test.ts b/src/core/utils/__test__/parsers.test.ts new file mode 100644 index 0000000..bdf1446 --- /dev/null +++ b/src/core/utils/__test__/parsers.test.ts @@ -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(); + }); +}); diff --git a/src/core/utils/parsers.ts b/src/core/utils/parsers.ts index 68cf6b5..df94b65 100644 --- a/src/core/utils/parsers.ts +++ b/src/core/utils/parsers.ts @@ -1,4 +1,8 @@ -export const toNumber = (val: unknown): Undefinable => { - const prepareValue = Number(val); - return Number.isNaN(prepareValue) ? undefined : prepareValue; +import {isNumber, isString} from 'lodash'; + +export const toNumber = (value: unknown): Undefinable => { + if (isNumber(value) || isString(value)) { + const prepareValue = Number(value); + return Number.isNaN(prepareValue) ? undefined : prepareValue; + } };