feat: 新增Hooks包,支持创建排序函数、解析JSON字符串、深度解析JSON、JSON转字符串
All checks were successful
/ release (push) Successful in 31s
All checks were successful
/ release (push) Successful in 31s
This commit is contained in:
parent
8dc6677fc6
commit
510dc2c623
22
package-lock.json
generated
22
package-lock.json
generated
@ -12,6 +12,7 @@
|
||||
"packages/*"
|
||||
],
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21",
|
||||
"winston": "^3.14.2",
|
||||
"winston-daily-rotate-file": "^5.0.0"
|
||||
},
|
||||
@ -20,6 +21,7 @@
|
||||
"@commitlint/config-conventional": "^19.2.2",
|
||||
"@eslint/js": "^9.9.0",
|
||||
"@lerna/conventional-commits": "^6.4.1",
|
||||
"@types/lodash": "^4.17.7",
|
||||
"@types/node": "^22.4.0",
|
||||
"eslint": "^9.9.0",
|
||||
"eslint-plugin-simple-import-sort": "^12.1.1",
|
||||
@ -388,6 +390,10 @@
|
||||
"kuler": "^2.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@egg/hooks": {
|
||||
"resolved": "packages/hooks",
|
||||
"link": true
|
||||
},
|
||||
"node_modules/@egg/logger": {
|
||||
"resolved": "packages/logger",
|
||||
"link": true
|
||||
@ -2449,6 +2455,13 @@
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"node_modules/@types/lodash": {
|
||||
"version": "4.17.7",
|
||||
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.7.tgz",
|
||||
"integrity": "sha512-8wTvZawATi/lsmNu10/j2hk1KEP0IvjubqPE3cu1Xz7xfXXt5oCq3SNUz4fMIP4XGF9Ky+Ue2tBA3hcS7LSBlA==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@types/minimatch": {
|
||||
"version": "3.0.5",
|
||||
"resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-3.0.5.tgz",
|
||||
@ -7788,7 +7801,6 @@
|
||||
"version": "4.17.21",
|
||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
|
||||
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==",
|
||||
"dev": true,
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/lodash.camelcase": {
|
||||
@ -12232,6 +12244,14 @@
|
||||
"url": "https://github.com/sponsors/sindresorhus"
|
||||
}
|
||||
},
|
||||
"packages/hooks": {
|
||||
"name": "@egg/hooks",
|
||||
"version": "1.0.0",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"lodash": "*"
|
||||
}
|
||||
},
|
||||
"packages/logger": {
|
||||
"name": "@egg/logger",
|
||||
"version": "1.2.1",
|
||||
|
@ -26,6 +26,7 @@
|
||||
"@commitlint/config-conventional": "^19.2.2",
|
||||
"@eslint/js": "^9.9.0",
|
||||
"@lerna/conventional-commits": "^6.4.1",
|
||||
"@types/lodash": "^4.17.7",
|
||||
"@types/node": "^22.4.0",
|
||||
"eslint": "^9.9.0",
|
||||
"eslint-plugin-simple-import-sort": "^12.1.1",
|
||||
@ -38,7 +39,8 @@
|
||||
"typescript-eslint": "^8.1.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"lodash": "^4.17.21",
|
||||
"winston": "^3.14.2",
|
||||
"winston-daily-rotate-file": "^5.0.0"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
8
packages/hooks/.npmignore
Normal file
8
packages/hooks/.npmignore
Normal file
@ -0,0 +1,8 @@
|
||||
src
|
||||
node_modules
|
||||
.git
|
||||
.vscode
|
||||
.devcontainer
|
||||
.npmrc
|
||||
tsconfig.json
|
||||
tsconfig.tsbuildinfo
|
24
packages/hooks/package.json
Normal file
24
packages/hooks/package.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "@egg/hooks",
|
||||
"version": "1.0.0",
|
||||
"description": "Hooks for Egg projects",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"prepublishOnly": "npm run build"
|
||||
},
|
||||
"keywords": [
|
||||
"egg",
|
||||
"tools",
|
||||
"hooks",
|
||||
"json",
|
||||
"sort"
|
||||
],
|
||||
"author": "RainSun <zhaoyingbo@live.cn>",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"lodash": "*"
|
||||
}
|
||||
}
|
76
packages/hooks/src/index.ts
Normal file
76
packages/hooks/src/index.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import _ from "lodash"
|
||||
|
||||
/**
|
||||
* 创建一个排序函数,用于根据指定的键和排序顺序对数组进行排序。
|
||||
* @param {boolean | "desc" | "asc"} isAscending 排序顺序,可以是布尔值或字符串 'asc' 或 'desc'。true 或 'asc' 表示升序,false 或 'desc' 表示降序。
|
||||
* @param {string | number} key 用于排序的键,可以是字符串或数字。
|
||||
* @returns {Function} 一个用于排序的比较函数。
|
||||
*/
|
||||
export const createSortFunction = (
|
||||
isAscending: boolean | "desc" | "asc",
|
||||
key: string | number
|
||||
) => {
|
||||
if (
|
||||
(typeof isAscending === "string" && isAscending === "asc") ||
|
||||
(typeof isAscending === "boolean" && isAscending)
|
||||
) {
|
||||
return (d1: any, d2: any) => (d1?.[key] ?? d1) - (d2?.[key] ?? d2)
|
||||
}
|
||||
return (d1: any, d2: any) => (d2?.[key] ?? d2) - (d1?.[key] ?? d1)
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析 JSON 字符串并返回解析后的对象。
|
||||
* @param {any} jsonString 要解析的 JSON 字符串。
|
||||
* @param {any} [defaultValue] 解析失败时返回的默认值。默认为输入字符串。
|
||||
* @returns {any} 解析后的 JSON 对象,如果解析失败则返回默认值。
|
||||
*/
|
||||
export const parseJsonString = (jsonString: any, defaultValue?: any) => {
|
||||
const failValue = defaultValue || jsonString
|
||||
if (typeof jsonString !== "string") return failValue
|
||||
try {
|
||||
return JSON.parse(jsonString)
|
||||
} catch {
|
||||
return failValue
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 递归解析 JSON 对象并返回一个包含所有解析值的新对象。
|
||||
* @param {any} jsonObject 要解析的 JSON 对象。
|
||||
* @param {any} [defaultValue] 解析失败时返回的默认值。默认为输入对象。
|
||||
* @returns {any} 一个包含所有解析值的新对象。
|
||||
*/
|
||||
export const deepParseJson = (jsonObject: any, defaultValue?: any) => {
|
||||
const finalObject: any = _.isArray(jsonObject) ? [] : {}
|
||||
const parse = (obj: any, keyList: (string | number)[]) => {
|
||||
if (typeof obj !== "object" || obj === null) return
|
||||
const handler = (val: any, key: string | number) => {
|
||||
const currentKeyList = [...keyList, key]
|
||||
const currentValue = parseJsonString(val)
|
||||
_.set(finalObject, currentKeyList, currentValue)
|
||||
parse(currentValue, currentKeyList)
|
||||
}
|
||||
if (_.isArray(obj)) {
|
||||
obj.forEach(handler)
|
||||
} else {
|
||||
_.forOwn(obj, handler)
|
||||
}
|
||||
}
|
||||
parse(parseJsonString(jsonObject, defaultValue), [])
|
||||
return finalObject
|
||||
}
|
||||
|
||||
/**
|
||||
* 安全地将对象转换为 JSON 字符串。
|
||||
* 如果转换失败,则返回对象的字符串表示。
|
||||
* @param {any} object - 要转换的对象。
|
||||
* @returns {string} - JSON 字符串或对象的字符串表示。
|
||||
*/
|
||||
export const stringifyJson = (object: any) => {
|
||||
try {
|
||||
return JSON.stringify(object)
|
||||
} catch {
|
||||
return String(object)
|
||||
}
|
||||
}
|
8
packages/hooks/tsconfig.json
Normal file
8
packages/hooks/tsconfig.json
Normal file
@ -0,0 +1,8 @@
|
||||
{
|
||||
"extends": "../../tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src"
|
||||
},
|
||||
"include": ["./src"]
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user