init router
This commit is contained in:
13
src/utils/parse.ts
Normal file
13
src/utils/parse.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
export const parseIfJson = (input: string): { [key: string]: any } | string => {
|
||||
try {
|
||||
// 尝试解析 JSON
|
||||
const parsed = JSON.parse(input);
|
||||
// 检查解析结果是否为对象(数组或普通对象)
|
||||
if (typeof parsed === 'object' && parsed !== null) {
|
||||
return parsed;
|
||||
}
|
||||
} catch (e) {
|
||||
// 如果解析失败,直接返回原始字符串
|
||||
}
|
||||
return input;
|
||||
};
|
||||
9
src/utils/pick.ts
Normal file
9
src/utils/pick.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
export function pick<T extends object, K extends keyof T>(obj: T, keys: K[]): Pick<T, K> {
|
||||
const result = {} as Pick<T, K>;
|
||||
keys.forEach((key) => {
|
||||
if (key in obj) {
|
||||
result[key] = obj[key];
|
||||
}
|
||||
});
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user