文件路径
npm init
一个package.json文件
package.json:
{
"name": "sybase-util",
"version": "1.0.5",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
工具类Index文件
export function isEmpty(v) {
switch (typeof v) {
case 'undefined':
return true;
case 'string':
if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0) return true;
break;
case 'boolean':
if (!v) return true;
break;
case 'number':
if (0 === v || isNaN(v)) return true;
break;
case 'object':
if (null === v || v.length === 0) return true;
for (var i in v) {
return false;
}
return true;
}
return false;
}
引用:
首先安装该工具类:npm install sybase-util
然后在页面中使用该方法:
const sybase = require(“sybase-util”);
sybase.isEmpty(null)
也可以挂载到window对象上去,如下:
import Storage from 'web-storage-cache'
(function () {
var syUtiles = syUtiles || {};
const localStorage = new Storage();
syUtiles.commonUtils = {
/**
* @Desc: 判断值为空 undefined、 null、 ''、 NaN、false、0、[]、{} 、空白字符串 ,null的字符串,这些都返回true
* @Date: 2020-09-07 18:40:29
* @param {type}
* @return {type}
*/
isEmpty: function (v) {
switch (typeof v) {
case 'undefined':
return true;
case 'string':
if (v.replace(/(^[ \t\n\r]*)|([ \t\n\r]*$)/g, '').length == 0||v.indexOf('null')!=-1) return true;
break;
case 'boolean':
if (!v) return true;
break;
case 'number':
if (0 === v || isNaN(v)) return true;
break;
case 'object':
if (null === v || v.length === 0) return true;
for (var i in v) {
return false;
}
return true;
}
return false;
},
/**
* @Desc: 两个浮点数求和
* @Date: 2020-09-07 18:41:34
* @param {type}
* @return {type}
*/
accAdd: function (num1, num2) {
var r1, r2, m;
try {
r1 = num1.toString().split('.')[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = num2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
// return (num1*m+num2*m)/m;
return Math.round(num1 * m + num2 * m) / m;
},
/**
* @Desc: 两个浮点数相减
* @Date: 2020-09-07 18:41:42
* @param {type}
* @return {type}
*/
accSub: function (num1, num2) {
var r1, r2, m;
try {
r1 = num1.toString().split('.')[1].length;
} catch (e) {
r1 = 0;
}
try {
r2 = num2.toString().split(".")[1].length;
} catch (e) {
r2 = 0;
}
m = Math.pow(10, Math.max(r1, r2));
n = (r1 >= r2) ? r1 : r2;
return (Math.round(num1 * m - num2 * m) / m).toFixed(n);
},
/**
* @Desc: 两个浮点数相除
* @Date: 2020-09-07 18:41:42
* @param {type}
* @return {type}
*/
accDiv: function (num1, num2) {
var t1, t2, r1, r2;
try {
t1 = num1.toString().split('.')[1].length;
} catch (e) {
t1 = 0;
}
try {
t2 = num2.toString().split(".")[1].length;
} catch (e) {
t2 = 0;
}
r1 = Number(num1.toString().replace(".", ""));
r2 = Number(num2.toString().replace(".", ""));
return (r1 / r2) * Math.pow(10, t2 - t1);
},
/**
* @Desc: 两个浮点数相乘
* @Date: 2020-09-07 18:41:42
* @param {type}
* @return {type}
*/
accMul: function () {
var m = 0, s1 = num1.toString(), s2 = num2.toString();
try { m += s1.split(".")[1].length } catch (e) { };
try { m += s2.split(".")[1].length } catch (e) { };
return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}
},
syUtiles.localStorage = {
//localStorage的封装
setLocalStorage: function (key, value) {
return localStorage.set(key, value);
},
getLocalStorage: function (key) {
return localStorage.get(key);
},
removeLocalStorage: function (key) {
return localStorage.delete(key);
},
//全部清空
clearLocalStorage: function () {
return localStorage.clear();
}
}
//暴露给window
window['syUtiles'] = syUtiles;
})();
这样的话就可以直接syUtiles.commonUtils.isEmpty('null')
这样使用了。