feat: 新增Hooks包,支持创建排序函数、解析JSON字符串、深度解析JSON、JSON转字符串
All checks were successful
/ release (push) Successful in 31s

This commit is contained in:
zhaoyingbo 2024-08-20 01:46:14 +00:00
parent 8dc6677fc6
commit 510dc2c623
6 changed files with 140 additions and 2 deletions

22
package-lock.json generated
View File

@ -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",

View File

@ -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"
}
}
}

View File

@ -0,0 +1,8 @@
src
node_modules
.git
.vscode
.devcontainer
.npmrc
tsconfig.json
tsconfig.tsbuildinfo

View 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": "*"
}
}

View 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)
}
}

View File

@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"rootDir": "./src"
},
"include": ["./src"]
}