From 0cd0e90ebf2ce3cc440a89f2bd92cf31bf4c44f5 Mon Sep 17 00:00:00 2001 From: webkubor Date: Sun, 29 Sep 2024 15:18:40 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=20=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA?= =?UTF-8?q?=E5=8A=A0=E5=85=A5object|=20boolean=20=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 3 ++- src/EchoUtils.ts | 14 +++++++++++--- src/WURL.ts | 17 +++++++++++++++++ test/URL.test.ts | 18 +++++++++++++----- 4 files changed, 43 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 4cb0f80..8eba987 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "dev": "vitepress dev docs --host --port 5180", "docs:build": "vitepress build docs", "docs:preview": "vitepress preview docs --port 8080", - "test": "jest test/ObjectUtils.test.ts", + "test-object": "jest test/ObjectUtils.test.ts", + "test-url": "jest test/URL.test.ts", "login": "npm login", "publish": "npm publish", "build": "rollup -c rollup.config.mjs", diff --git a/src/EchoUtils.ts b/src/EchoUtils.ts index c7a88cb..c5aba61 100644 --- a/src/EchoUtils.ts +++ b/src/EchoUtils.ts @@ -61,20 +61,28 @@ const colorMap: { [key in ColorType]: string } = { creamWhite: '\x1b[38;5;255m', // 奶白色 ANSI 代码示例,可能需要根据实际情况调整 }; -function colorLog(color: ColorType,...texts: (string | number)[]): void { +function colorLog(color: ColorType,...texts: (string | number| object| boolean)[]): void { if (!colorMap[color]) { throw new Error(`Invalid color: ${color}`); } let output = ''; for (const text of texts) { - output += typeof text === 'number'? text.toString() : text; + if(ObjectUtils.judgeTypes(text) === 'number') { + output += text.toString(); + } else if (ObjectUtils.judgeTypes(text) ==='object') { + output += JSON.stringify(text, null, 2); + } else if (ObjectUtils.judgeTypes(text) ==='boolean') { + output += text.toString(); + } else { + output += text; + } output += ' '; } console.log(`${colorMap[color]}${output.trim()}${colorMap['reset']}`); } const EchoUtils = Object.fromEntries( - Object.keys(colorMap).map((color) => [color, (...texts: (string | number)[]) => colorLog(color as ColorType,...texts)]) + Object.keys(colorMap).map((color) => [color, (...texts: (string | number| object| boolean)[]) => colorLog(color as ColorType,...texts)]) ); export default EchoUtils; \ No newline at end of file diff --git a/src/WURL.ts b/src/WURL.ts index f5991f3..37ce755 100644 --- a/src/WURL.ts +++ b/src/WURL.ts @@ -39,6 +39,23 @@ export function createWURL(urlString: string) { return params; }, + /** + * 将指定的参数添加到 URL 的查询字符串中 + * @param url - 要添加参数的 URL + * @param params - 要添加的参数对象 + * @returns 添加了参数的 URL 字符串 + */ + addParamsToURL(params) { + let urlObj = url; + console.log(`output->`,JSON.stringify(urlObj)) + for (const key in params) { + if (params.hasOwnProperty(key)) { + urlObj.searchParams.append(key, params[key]); + } + } + return urlObj.toString(); + }, + /** * @method isHttps * @description 检查当前 URL 是否使用 HTTPS 协议 diff --git a/test/URL.test.ts b/test/URL.test.ts index 0eb3d65..781d121 100644 --- a/test/URL.test.ts +++ b/test/URL.test.ts @@ -1,14 +1,22 @@ import { createWURL } from '../src/WURL'; +import EchoUtils from '../src/EchoUtils'; describe('URL class', () => { it('should get main domain correctly', () => { const myUrl = createWURL('https://example.com/path?query=123'); - console.log(myUrl.getMainDomain()); // 'example.com' - console.log(myUrl.parseQueryParams()); // { query: '123' } - console.log(myUrl.isHttps()); // true - console.log(myUrl.getPathname()); // '/path' - console.log(myUrl.getPort()); // '80' + EchoUtils.green(myUrl.getMainDomain()); // 'example.com' + EchoUtils.green(myUrl.parseQueryParams()); // { query: '123' } + EchoUtils.green(myUrl.isHttps()); // true + EchoUtils.green(myUrl.getPathname()); // '/path' + EchoUtils.green(myUrl.getPort()); // '80' }); + it('test addParamsToURL', () => { + const url = 'https://example.com'; + const params = { name: 'John', age: '30' }; + const myUrl = createWURL(url); + const newUrl = myUrl.addParamsToURL(params); + EchoUtils.orange(`addParamsToURL`, newUrl) // function() {} function + }); }); \ No newline at end of file