kt-valid-tool 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +31 -0
- package/package.json +13 -0
- package/test.js +9 -0
package/index.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
|
|
2
|
+
// 阈值配置
|
|
3
|
+
const thresholds = {
|
|
4
|
+
pres: { min: 900, max: 1100 }, // 气压
|
|
5
|
+
temp: { min: -40, max: 120 }, // 温度
|
|
6
|
+
wind_s: { min: 0, max: 50 }, // 风速
|
|
7
|
+
wind_d: { min: 0, max: 360 }, // 风向
|
|
8
|
+
wind_l: { min: 0, max: 10 }, // 风级
|
|
9
|
+
prep: { min: 0, max: 100 }, // 降雨量
|
|
10
|
+
rh: { min: 0, max: 100 } // 湿度
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const ktValid = (value, type) => {
|
|
14
|
+
// 处理空值
|
|
15
|
+
if (value === undefined || value === null || value === '') return '--';
|
|
16
|
+
// 检查阈值
|
|
17
|
+
if (type && thresholds[type]) {
|
|
18
|
+
const { min, max } = thresholds[type];
|
|
19
|
+
let val = "";
|
|
20
|
+
if(type == 'rh'){
|
|
21
|
+
val = Number(value.replace('%', ''));
|
|
22
|
+
}else{
|
|
23
|
+
val = Number(value);
|
|
24
|
+
if(isNaN(val)) return '--'
|
|
25
|
+
}
|
|
26
|
+
if (val < min || val > max) return '--';
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export default ktValid;
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "kt-valid-tool",
|
|
3
|
+
"type": "module",
|
|
4
|
+
"version": "1.0.0",
|
|
5
|
+
"description": "工具函数库,校验是否为有效数据",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
|
9
|
+
},
|
|
10
|
+
"keywords": [],
|
|
11
|
+
"author": "zhaap",
|
|
12
|
+
"license": "MIT"
|
|
13
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import ktValid from './index.js';
|
|
2
|
+
|
|
3
|
+
console.log('湿度:',ktValid('10%', 'rh'));
|
|
4
|
+
console.log('降雨量:',ktValid('80', 'prep'));
|
|
5
|
+
console.log('温度:',ktValid('20', 'temp'));
|
|
6
|
+
console.log('风向:',ktValid('100', 'wind_d'));
|
|
7
|
+
console.log('风速:',ktValid('30', 'wind_s'));
|
|
8
|
+
console.log('风级:',ktValid('8', 'wind_l'));
|
|
9
|
+
|