diff --git a/src/core/utils/__test__/toRequestParamValue.test.ts b/src/core/utils/__test__/toRequestParamValue.test.ts new file mode 100644 index 0000000..47d2626 --- /dev/null +++ b/src/core/utils/__test__/toRequestParamValue.test.ts @@ -0,0 +1,26 @@ +import {toRequestParamValue} from '../toRequestParamValue'; + +describe('toRequestParamValue', () => { + it('Возвращает простые значения', () => { + expect(toRequestParamValue('trt')).toBe('trt'); + expect(toRequestParamValue(0)).toBe(0); + expect(toRequestParamValue(false)).toBe(false); + }); + + it('Простые пустые значения возвращают undefined', () => { + expect(toRequestParamValue('')).toBeUndefined(); + expect(toRequestParamValue(null)).toBeUndefined(); + expect(toRequestParamValue(undefined)).toBeUndefined(); + }); + + it('Возвращает заполненные объекты', () => { + expect(toRequestParamValue({foo: undefined})).toMatchObject({foo: undefined}); + expect(toRequestParamValue({foo: 'bar'})).toMatchObject({foo: 'bar'}); + expect(toRequestParamValue(['rtt'])).toEqual(['rtt']); + }); + + it('Пустые объекты возвращают undefined', () => { + expect(toRequestParamValue({})).toBeUndefined(); + expect(toRequestParamValue([])).toBeUndefined(); + }); +}); diff --git a/src/core/utils/toRequestParamValue.ts b/src/core/utils/toRequestParamValue.ts new file mode 100644 index 0000000..f140aad --- /dev/null +++ b/src/core/utils/toRequestParamValue.ts @@ -0,0 +1,7 @@ +import {isNotEmpty} from '../referers/common'; + +export function toRequestParamValue(val: T): T; +export function toRequestParamValue(val?: T): Undefinable; +export function toRequestParamValue(val?: T) { + return isNotEmpty(val) ? val : undefined; +}