From 7527818a418f7cf1c3923a80bfe6de3604a1fab2 Mon Sep 17 00:00:00 2001 From: liuyi Date: Thu, 19 Oct 2023 19:24:01 +0800 Subject: [PATCH] fix: #90 optimize getParameterByName's return --- src/utils.ts | 10 +++++++++- test/utils.test.ts | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/utils.ts b/src/utils.ts index e7159bb..9485918 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -100,7 +100,15 @@ const utils = { if (!results[2]) { return ''; } - return decodeURIComponent(results[2].replace(/\+/g, ' ')); + + const paramValue = decodeURIComponent(results[2].replace(/\+/g, ' ')); + if (paramValue === 'null') { + return null; + } + if (paramValue === 'undefined') { + return undefined; + } + return paramValue; }, /** * diff --git a/test/utils.test.ts b/test/utils.test.ts index ca1e935..798a63f 100644 --- a/test/utils.test.ts +++ b/test/utils.test.ts @@ -79,6 +79,18 @@ describe('utils:', () => { '张三' ); }); + test('getParameterByName()=>null', () => { + assert.strictEqual( + getParameterByName('name', 'http://gitlab.prod.dtstack.cn?name=null'), + null + ); + }); + test('getParameterByName()=>undefined', () => { + assert.strictEqual( + getParameterByName('name', 'http://gitlab.prod.dtstack.cn?name=undefined'), + undefined + ); + }); test('percent(1)=>100%', () => { assert.strictEqual(percent(1), '100%'); });