把几个基础小功能迁移到独立的 sol.tool 库中

This commit is contained in:
luk.lu
2021-06-07 12:24:46 +08:00
parent e35794ced1
commit dc4cf12c08
5 changed files with 63 additions and 1 deletions

25
index.js Normal file
View File

@@ -0,0 +1,25 @@
/* 基础小工具,可通用于服务端和用户端
*/
module.exports = {
sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
parseJsonPossible(value) {
try {
return JSON.parse(value)
} catch (e) {
return value
}
},
sortAndFilterJson({ fields, entity, exclude = [] } = {}) {
const newEntity = {}
for (let key of Object.keys(fields).sort()) {
if (typeof (entity[key] !== 'undefined') && !Number.isNaN(entity[key]) && entity[key] !== Infinity) {
newEntity[key] = entity[key]
}
}
for (let exkey of exclude) {
delete newEntity[exkey]
}
return JSON.stringify(newEntity)
},
}