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

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

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# 以'#'开始的行,被视为注释.
node_modules
package-lock.json
.vscode
.svn
~*
.gitattributes
dump.rdb

16
.prettierrc.js Normal file
View File

@ -0,0 +1,16 @@
/*
VSCode Prettier 有效建议一直要有本配置文件否则不同版本的 Prettier 的默认配置会不同例如 TrailingComma
VSCode Prettier Standard 无效似乎是集成了不能修改的配置
*/
module.exports = {
printWidth: 160, // default 80
tabWidth: 2, // default 2
useTabs: false,
semi: false, // default true
singleQuote: true, // default false
trailingComma: 'es5', // none (default in v 1.*), es5 (default in v2.0.0), all
bracketSpacing: true, // default true
jsxBracketSameLine: false, // default false
arrowParens: 'always', // avoid (default in v1.9.0), always (default since v2.0.0)
quoteProps: 'as-needed', // as-needed (default), consistent, preserve
}

View File

@ -1,2 +1,2 @@
# sol.util
# sol.tool

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

13
package.json Normal file
View File

@ -0,0 +1,13 @@
{
"name": "sol.tool",
"version": "0.1.0",
"private": true,
"dependencies": {
},
"devDependencies": {
},
"scripts": {
"setup": "npm install"
},
"author": ""
}