This repository has been archived on 2023-08-29. You can view files and clone it, but cannot push or open issues/pull-requests.
yutent
/
anot.js
Archived
1
0
Fork 0
anot.js/src/01-profill.js

139 lines
3.4 KiB
JavaScript
Raw Normal View History

2018-08-04 16:26:50 +08:00
/*-----------------部分ES6的JS实现 start---------------*/
// ===============================
// ========== Promise ============
// ===============================
2019-01-28 15:22:05 +08:00
if (!Promise.defer) {
Promise.defer = function() {
let obj = {}
obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve
obj.reject = reject
2018-08-04 16:26:50 +08:00
})
return obj
}
}
//类似于Array 的splice方法
if (!String.prototype.splice) {
Object.defineProperty(String.prototype, 'splice', {
value: function(start, len, fill) {
2019-01-28 15:22:05 +08:00
let length = this.length
let argLen = arguments.length
2018-08-04 16:26:50 +08:00
fill = fill === undefined ? '' : fill
if (argLen < 1) {
return this
}
//处理负数
if (start < 0) {
2019-01-28 15:22:05 +08:00
if (Math.abs(start) >= length) {
start = 0
} else {
start = length + start
}
2018-08-04 16:26:50 +08:00
}
if (argLen === 1) {
return this.slice(0, start)
} else {
len -= 0
2019-01-28 15:22:05 +08:00
let strl = this.slice(0, start)
let strr = this.slice(start + len)
2018-08-04 16:26:50 +08:00
return strl + fill + strr
}
},
enumerable: false
})
}
if (!Date.prototype.getFullWeek) {
//获取当天是本年度第几周
Object.defineProperty(Date.prototype, 'getFullWeek', {
value: function() {
2019-01-28 15:22:05 +08:00
let thisYear = this.getFullYear()
let that = new Date(thisYear, 0, 1)
let firstDay = that.getDay() || 1
let numsOfToday = (this - that) / 86400000
2018-08-04 16:26:50 +08:00
return Math.ceil((numsOfToday + firstDay) / 7)
},
enumerable: false
})
//获取当天是本月第几周
Object.defineProperty(Date.prototype, 'getWeek', {
value: function() {
2019-01-28 15:22:05 +08:00
let today = this.getDate()
let thisMonth = this.getMonth()
let thisYear = this.getFullYear()
let firstDay = new Date(thisYear, thisMonth, 1).getDay()
2018-08-04 16:26:50 +08:00
return Math.ceil((today + firstDay) / 7)
},
enumerable: false
})
}
if (!Date.isDate) {
Object.defineProperty(Date, 'isDate', {
value: function(obj) {
return typeof obj === 'object' && obj.getTime ? true : false
},
enumerable: false
})
}
//时间格式化
if (!Date.prototype.format) {
Object.defineProperty(Date.prototype, 'format', {
value: function(str) {
str = str || 'Y-m-d H:i:s'
2019-01-28 15:22:05 +08:00
let week = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
let dt = {
fullyear: this.getFullYear(),
year: this.getYear(),
fullweek: this.getFullWeek(),
week: this.getWeek(),
month: this.getMonth() + 1,
date: this.getDate(),
day: week[this.getDay()],
hours: this.getHours(),
minutes: this.getMinutes(),
seconds: this.getSeconds()
}
let re
2018-08-04 16:26:50 +08:00
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
re = {
Y: dt.fullyear,
y: dt.year,
m: dt.month < 10 ? '0' + dt.month : dt.month,
n: dt.month,
d: dt.date < 10 ? '0' + dt.date : dt.date,
j: dt.date,
H: dt.hours < 10 ? '0' + dt.hours : dt.hours,
h: dt.g < 10 ? '0' + dt.g : dt.g,
G: dt.hours,
g: dt.g,
i: dt.minutes < 10 ? '0' + dt.minutes : dt.minutes,
s: dt.seconds < 10 ? '0' + dt.seconds : dt.seconds,
W: dt.fullweek,
w: dt.week,
D: dt.day
}
2019-01-28 15:22:05 +08:00
for (let i in re) {
2018-08-04 16:26:50 +08:00
str = str.replace(new RegExp(i, 'g'), re[i])
}
return str
},
enumerable: false
})
}
/*-----------------部分ES6的JS实现 ending---------------*/