You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
const formatTime = date => {
|
|
|
|
const year = date.getFullYear()
|
|
|
|
const month = date.getMonth() + 1
|
|
|
|
const day = date.getDate()
|
|
|
|
const hour = date.getHours()
|
|
|
|
const minute = date.getMinutes()
|
|
|
|
const second = date.getSeconds()
|
|
|
|
|
|
|
|
return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}`
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatNumber = n => {
|
|
|
|
n = n.toString()
|
|
|
|
return n[1] ? n : `0${n}`
|
|
|
|
}
|
|
|
|
|
|
|
|
// 通过字面量方式实现的函数each
|
|
|
|
const each = function(object, callback){
|
|
|
|
var type = (function(){
|
|
|
|
switch (object.constructor){
|
|
|
|
case Object:
|
|
|
|
return 'Object';
|
|
|
|
break;
|
|
|
|
case Array:
|
|
|
|
return 'Array';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return 'null';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
// 为数组或类数组时, 返回: index, value
|
|
|
|
if(type === 'Array' || type === 'NodeList'){
|
|
|
|
// 由于存在类数组NodeList, 所以不能直接调用every方法
|
|
|
|
[].every.call(object, function(v, i){
|
|
|
|
return callback.call(v, i, v) === false ? false : true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// 为对象格式时,返回:key, value
|
|
|
|
else if(type === 'Object'){
|
|
|
|
for(var i in object){
|
|
|
|
if(callback.call(object[i], i, object[i]) === false){
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
formatTime,
|
|
|
|
each
|
|
|
|
}
|