Skip to content

Commit

Permalink
feat 日志输出加入object| boolean 类型
Browse files Browse the repository at this point in the history
  • Loading branch information
webkubor committed Sep 29, 2024
1 parent 6ccd068 commit 0cd0e90
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
14 changes: 11 additions & 3 deletions src/EchoUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
17 changes: 17 additions & 0 deletions src/WURL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 协议
Expand Down
18 changes: 13 additions & 5 deletions test/URL.test.ts
Original file line number Diff line number Diff line change
@@ -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
});

});

0 comments on commit 0cd0e90

Please sign in to comment.