From 82b91ed4228f796a8f3fbdce433f1f945b80dabb Mon Sep 17 00:00:00 2001 From: Nikolay <46225163+vigdorov@users.noreply.github.com> Date: Mon, 28 Dec 2020 10:33:04 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20toRequestParamValue=20(#32)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../__test__/toRequestParamValue.test.ts | 26 +++++++++++++++++++ src/core/utils/toRequestParamValue.ts | 7 +++++ 2 files changed, 33 insertions(+) create mode 100644 src/core/utils/__test__/toRequestParamValue.test.ts create mode 100644 src/core/utils/toRequestParamValue.ts 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; +}