master
宇天 2020-09-18 11:09:50 +08:00
parent 5e27ffba3a
commit 0f18f9f1df
15 changed files with 14 additions and 350 deletions

View File

@ -5,9 +5,10 @@
*
*/
require('./lib/cjs/object')
require('./lib/cjs/array')
require('./lib/cjs/string')
require('./lib/cjs/number')
require('./lib/cjs/date')
require('./lib/cjs/promise')
require('./lib/object')
require('./lib/array.v6')
require('./lib/array')
require('./lib/string')
require('./lib/number')
require('./lib/date')
require('./lib/promise')

View File

@ -5,9 +5,9 @@
*
*/
import './lib/esm/object'
import './lib/esm/array'
import './lib/esm/string'
import './lib/esm/number'
import './lib/esm/date'
import './lib/esm/promise'
import './lib/object.js'
import './lib/array.js'
import './lib/string.js'
import './lib/number.js'
import './lib/date.js'
import './lib/promise.js'

View File

@ -1,88 +0,0 @@
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 11:54:58
*/
//获取当天是本月第几周
if (!Date.isDate) {
Object.defineProperty(Date, 'isDate', {
value: function(obj) {
return obj && typeof obj === 'object' && obj.getTime ? true : false
},
enumerable: false
})
}
if (!Date.prototype.getFullWeek) {
//获取当天是本年度第几周
Object.defineProperty(Date.prototype, 'getFullWeek', {
value: function() {
let thisYear = this.getFullYear()
let that = new Date(thisYear, 0, 1)
let firstDay = that.getDay()
let numsOfToday = (this - that) / 24 / 360 / 1000
return Math.ceil((numsOfToday + firstDay) / 7)
},
enumerable: false
})
//获取当天是本月第几周
Object.defineProperty(Date.prototype, 'getWeek', {
value: function() {
let today = this.getDate()
let thisMonth = this.getMonth()
let thisYear = this.getFullYear()
let firstDay = new Date(thisYear, thisMonth, 1).getDay()
return Math.ceil((today + firstDay) / 7)
},
enumerable: false
})
}
//时间格式化
if (!Date.prototype.format) {
Object.defineProperty(Date.prototype, 'format', {
value: function(str) {
str = str || 'Y-m-d H:i:s'
let dt = {
fullyear: this.getFullYear(),
year: this.getYear(),
fullweek: this.getFullWeek(),
week: this.getWeek(),
month: this.getMonth() + 1,
date: this.getDate(),
day: this.getDay() + 1,
hours: this.getHours(),
minutes: this.getMinutes(),
seconds: this.getSeconds()
}
let reg = null
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
reg = {
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
}
for (let i in reg) {
str = str.replace(new RegExp(i, 'g'), reg[i])
}
return str
},
enumerable: false
})
}

View File

@ -1,165 +0,0 @@
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 12:09:15
*/
//类似于Array 的splice方法
if (!String.prototype.splice) {
Object.defineProperty(String.prototype, 'splice', {
value: function(start, len, fill) {
let length = this.length
let argLen = arguments.length
fill = fill === undefined ? '' : fill
if (argLen < 1) {
return this
}
//处理负数
if (start < 0) {
if (Math.abs(start) >= length) {
start = 0
} else {
start = length + start
}
}
if (argLen === 1) {
return this.slice(0, start)
} else {
len -= 0
let strl = this.slice(0, start)
let strr = this.slice(start + len)
return strl + fill + strr
}
},
enumerable: false
})
}
//同php的htmlspecialchars函数
if (!String.prototype.htmlspecialchars) {
Object.defineProperty(String.prototype, 'htmlspecialchars', {
value: function(sign) {
let str = this.replace(/&(?!\w+;)/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
if (sign === 'ENT_QUOTES') {
return str.replace(/"/g, '&quot;').replace(/'/g, '&#39;')
} else if (sign === 'ENT_NOQUOTES') {
return str
} else {
return str.replace(/"/g, '&quot;')
}
},
enumerable: false
})
}
//htmlspecialchars的还原
if (!String.prototype.tohtml) {
Object.defineProperty(String.prototype, 'tohtml', {
value: function() {
return this.replace(/&lt;/gi, '<')
.replace(/&gt;/gi, '>')
.replace(/&quot;/gi, '"')
.replace(/&#39;/g, "'")
.replace(/&amp;/gi, '&')
},
enumerable: false
})
}
//简单的过滤xss
if (!String.prototype.xss) {
Object.defineProperty(String.prototype, 'xss', {
value: function() {
let str = this.htmlspecialchars('ENT_QUOTES')
str = str
.replace(
/(document\.cookie)|(document\.write)|(\.parentNode)|(window\.location)|(\.innerHTML)/g,
''
)
.replace(/(%0[0-8bcef])|(%1[0-9a-f])/g, '')
return str
},
enumerable: false
})
}
// js特殊字符的转义
if (!String.prototype.escape) {
Object.defineProperty(String.prototype, 'escape', {
value: function() {
return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1')
},
enumerable: false
})
}
// padStart & padEnd
if (!String.prototype.padStart) {
Object.defineProperty(String.prototype, 'padStart', {
value: function(len, fill) {
let alen = arguments.length
let length = this.length
let ilen = len - length
if (alen < 1 || ilen < 1) {
return this
}
if (alen < 2 || typeof fill !== 'string') {
fill = ' '
}
while (fill.length < ilen) {
fill += fill
}
return fill.slice(0, ilen) + this
},
enumerable: false
})
Object.defineProperty(String.prototype, 'padEnd', {
value: function(len, fill) {
let alen = arguments.length,
length = this.length,
ilen = len - length
if (alen < 1 || ilen < 1) {
return this
}
if (alen < 2 || typeof fill !== 'string') {
fill = ' '
}
while (fill.length < ilen) {
fill += fill
}
return this + fill.slice(0, ilen)
},
enumerable: false
})
}
if (!String.prototype.item) {
Object.defineProperty(String.prototype, 'item', {
value: function(num) {
var n = +num
if (n < 0) {
n = this.length + n
}
return this[n]
},
enumerable: false,
writable: true
})
}

View File

@ -1,46 +0,0 @@
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 11:58:40
*/
// 简单的数字处理
// 将安全范围内的数字字符串转为数字类型
// 否则转为字符串类型
if (!Number.parse) {
Object.defineProperty(Number, 'parse', {
value: function(val) {
if (typeof val === 'number' || typeof val === 'string') {
val += ''
if (val.startsWith('0') && !val.startsWith('0.')) {
if (val === '0') {
return 0
} else {
return val
}
} else {
if (isFinite(val)) {
if (
val >= Number.MIN_SAFE_INTEGER &&
val <= Number.MAX_SAFE_INTEGER
) {
val = +val
}
}
return val
}
}
return val
},
enumerable: false
})
}
// 将字符串转为数字类型
if (!Number.fromString) {
Object.defineProperty(Number, 'fromString', {
value: function(val) {
return +val || 0
},
enumerable: false
})
}

View File

@ -1,23 +0,0 @@
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 12:07:09
*/
/**
* [ 判断对象/数组是否为空]
* eg.
* Object.empty(obj/arr)
*/
if (!Object.empty) {
Object.defineProperty(Object, 'empty', {
value: function(obj) {
try {
for (let i in obj) {
return false
}
} catch (e) {}
return true
},
enumerable: false
})
}

View File

@ -1,15 +0,0 @@
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 12:09:06
*/
if (!Promise.defer) {
Promise.defer = function() {
let obj = {}
obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve
obj.reject = reject
})
return obj
}
}

View File

@ -1,6 +1,6 @@
{
"name": "es.shim",
"version": "2.0.0",
"version": "2.0.1",
"description": "实现部分新API, 以及一些常用的扩展方法",
"keyworks": ["es5", "es6", "es7", "polyfill", "extend", "shim"],
"main": "index.js",