shu-c-view
Version:
rollup 打包vue@2.7组件库框架
70 lines (68 loc) • 1.71 kB
JavaScript
/**
* @desc 工具类
*/
// 判断参数是否为空
const isNotEmpty = str => {
if (str !== '' && str !== null && typeof str !== 'undefined') {
return true;
}
// console.warn('argument format is wrong');
return false;
};
// 开发环境可以输出日志信息
const devConsole = function(message) {
if (
process.env.NODE_ENV === 'development' ||
process.env.NODE_ENV === 'dev'
) {
console.warn(message);
}
};
// 来源对象覆盖目标源对象
const apply = (scope, config) => {
for (const i in config) {
scope[i] = config[i];
}
return scope;
};
// 来源对象覆盖目标源没有的属性
const applyIf = (scope, config) => {
for (const i in config) {
// if (!scope[i])
if (!Object.prototype.hasOwnProperty.call(scope, i)) {
scope[i] = config[i];
}
}
return scope;
};
// 根据传入的日期格式进行转换
const dateformat = (date, fmt) => {
const o = {
'M+': date.getMonth() + 1, // 月份
'd+': date.getDate(), // 日
'h+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
S: date.getMilliseconds() // 毫秒
};
if (!isNotEmpty(fmt)) {
fmt = 'yyyy-MM-dd hh:mm:ss';
}
if (/(y+)/.test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
`${date.getFullYear()}`.substr(4 - RegExp.$1.length)
);
}
for (const k in o) {
if (new RegExp(`(${k})`).test(fmt)) {
fmt = fmt.replace(
RegExp.$1,
RegExp.$1.length === 1 ? o[k] : `00${o[k]}`.substr(`${o[k]}`.length)
);
}
}
return fmt;
};
export { devConsole, apply, applyIf, dateformat, isNotEmpty };