增加MIT开源协议;删除global的拓展方法;增加Promise.defer方法拓展

master
宇天 2018-05-25 00:38:52 +08:00
parent 09b11fd3e6
commit a863fc891b
12 changed files with 295 additions and 366 deletions

View File

@ -1,3 +1,9 @@
1.0.0 / 2018-05-25
==================
* delete global extends
* add Promise.defer extend
0.0.3 / 2017-03-17 0.0.3 / 2017-03-17
================== ==================
* Optimise String extend function. * Optimise String extend function.

21
LICENSE Normal file
View File

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2018
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@ -39,9 +39,6 @@
+ global
* gmdate()
* empty()
@ -167,32 +164,3 @@ let str = `<script>alert('hello world')</script>`
str.tohtml() // <script>alert('hello world')</script> str.tohtml() // <script>alert('hello world')</script>
``` ```
### 9.global.empty()
```javascript
empty('') //true
empty(null) //true
empty(undefined) //true
empty([]) //true
empty({}) //true
empty({a: 123}) //false
empty(0) //true
empty('0') //false
empty(1) //false
```
### 10.global.gmdate()
> Base on Date.prototype.format
```javascript
gmdate() // 2017-02-08 12:11:23
gmdate('Y-m-d') // 2017-02-08
gmdate('', 1470834884000) //2016-08-10 21:14:44
```

14
index.js Normal file
View File

@ -0,0 +1,14 @@
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2017-02-27 18:01:54
*
*/
'use strict'
require('./lib/object')
require('./lib/array')
require('./lib/string')
require('./lib/date')
require('./lib/promise')

View File

@ -1,27 +1,24 @@
/** /**
* *
* @authors yutent (yutent@doui.cc) * @authors yutent (yutent@doui.cc)
* @date 2017-02-27 21:56:03 * @date 2017-02-27 21:56:03
* *
*/ */
"use strict"; 'use strict'
// 判断数组是否包含指定元素 // 判断数组是否包含指定元素
if(!Array.prototype.includes){ if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, Object.defineProperty(Array.prototype, 'includes', {
'includes', value: function(val) {
{ for (let it of this) {
value: function(val){ if (it === val) {
for(let it of this){ return true
if(it === val) }
return true }
} return false
return false },
}, enumerable: false,
enumerable: false, writable: true
writable: true })
})
} }

View File

@ -1,106 +1,93 @@
/** /**
* *
* @authors yutent (yutent@doui.cc) * @authors yutent (yutent@doui.cc)
* @date 2017-02-27 22:01:10 * @date 2017-02-27 22:01:10
* *
*/ */
"use strict"; 'use strict'
//获取当天是本月第几周 //获取当天是本月第几周
if(!Date.isDate){ if (!Date.isDate) {
Object.defineProperty(Date, Object.defineProperty(Date, 'isDate', {
'isDate', value: function(obj) {
{ return typeof obj === 'object' && obj.getTime ? true : false
value: function(obj){ },
return (typeof obj === 'object') && obj.getTime ? true : false enumerable: 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
})
if(!Date.prototype.getFullWeek){ //获取当天是本月第几周
//获取当天是本年度第几周 Object.defineProperty(Date.prototype, 'getWeek', {
Object.defineProperty(Date.prototype, value: function() {
'getFullWeek', let today = this.getDate()
{ let thisMonth = this.getMonth()
value: function(){ let thisYear = this.getFullYear()
let thisYear = this.getFullYear(), let firstDay = new Date(thisYear, thisMonth, 1).getDay()
that = new Date(thisYear, 0, 1), return Math.ceil((today + firstDay) / 7)
firstDay = that.getDay(), },
numsOfToday = (this - that) / 86400000; enumerable: false
return Math.ceil((numsOfToday + firstDay) / 7) })
},
enumerable: false
})
//获取当天是本月第几周
Object.defineProperty(Date.prototype,
'getWeek',
{
value: function(){
let today = this.getDate(),
thisMonth = this.getMonth(),
thisYear = this.getFullYear(),
firstDay = new Date(thisYear, thisMonth, 1).getDay();
return Math.ceil((today + firstDay) / 7)
},
enumerable: false
})
} }
//时间格式化 //时间格式化
if(!Date.prototype.format){ if (!Date.prototype.format) {
Object.defineProperty(Date.prototype, Object.defineProperty(Date.prototype, 'format', {
'format', value: function(str) {
{ str = str || 'Y-m-d H:i:s'
value: function(str){ let week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun']
str = str || 'Y-m-d H:i:s' let dt = {
let week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'], fullyear: this.getFullYear(),
dt = { year: this.getYear(),
'fullyear': this.getFullYear(), fullweek: this.getFullWeek(),
'year': this.getYear(), week: this.getWeek(),
'fullweek': this.getFullWeek(), month: this.getMonth() + 1,
'week': this.getWeek(), date: this.getDate(),
'month': this.getMonth() + 1, day: week[this.getDay()],
'date': this.getDate(), hours: this.getHours(),
'day': week[this.getDay()], minutes: this.getMinutes(),
'hours': this.getHours(), seconds: this.getSeconds()
'minutes': this.getMinutes(), }
'seconds': this.getSeconds() let reg = null
},
re;
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours 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
}
for(let i in re){ reg = {
str = str.replace(new RegExp(i, 'g'), re[i]) Y: dt.fullyear,
} y: dt.year,
return str m: dt.month < 10 ? '0' + dt.month : dt.month,
}, n: dt.month,
enumerable: false 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 re) {
str = str.replace(new RegExp(i, 'g'), reg[i])
}
return str
},
enumerable: false
})
} }

View File

@ -1,48 +0,0 @@
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2017-02-27 21:58:03
*
*/
"use strict";
if(!global.gmdate){
global.gmdate = function (str, stamp){
let oDate;
if(!stamp || arguments.length < 2){
oDate = new Date()
}else if(!Date.isDate(stamp)){
if(!/[^\d]/.test(stamp)){
stamp = Number(stamp)
}
oDate = new Date(stamp);
if((oDate + '') === 'Invalid Date')
return 'Invalid Date'
}else{
oDate = stamp
}
return oDate.format(str)
}
}
if(!global.empty){
global.empty = function(o){
if(o === undefined || o === null || o === '' || !o || o === 0){
return true
}else if(typeof o === 'object'){
try{
return Object.empty(o)
}catch(e){}
return false
}
return false
}
}

View File

@ -1,15 +0,0 @@
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2017-02-27 18:01:54
*
*/
"use strict";
require('./object')
require('./array')
require('./string')
require('./date')
require('./global')

View File

@ -1,52 +1,45 @@
/** /**
* *
* @authors yutent (yutent@doui.cc) * @authors yutent (yutent@doui.cc)
* @date 2017-02-27 18:02:56 * @date 2017-02-27 18:02:56
* *
*/ */
"use strict"; 'use strict'
// 对象合并 // 对象合并
if(!Object.prototype.merge){ if (!Object.prototype.merge) {
Object.defineProperty(Object.prototype, Object.defineProperty(Object.prototype, 'merge', {
'merge', value: function() {
{ let args = Array.from(arguments)
value: function(){ if (args.length < 1 || typeof args[0] !== 'object') {
let args = Array.from(arguments) return this
if(args.length < 1 || typeof args[0] !== 'object') }
return this args.unshift(this)
args.unshift(this)
Object.assign.apply(null, args) Object.assign.apply(null, args)
return this return this
}, },
enumerable: false, enumerable: false,
writable: true writable: true
}) })
} }
/** /**
* [ 判断对象/数组是否为空] * [ 判断对象/数组是否为空]
* eg. * eg.
* Object.empty(obj/arr) * Object.empty(obj/arr)
*/ */
if(!Object.empty){ if (!Object.empty) {
Object.defineProperty(Object, Object.defineProperty(Object, 'empty', {
'empty', value: function(obj) {
{ try {
value: function(obj){ for (let i in obj) {
try{ return false
for(let i in obj){ }
return false } catch (e) {}
} return true
}catch(e){} },
return true enumerable: false
}, })
enumerable: false
})
} }

17
lib/promise.js Normal file
View File

@ -0,0 +1,17 @@
/**
*
* @authors yutent (yutent@doui.cc)
* @date 2018-05-25 00:29:03
* @version $Id$
*/
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,166 +1,155 @@
/** /**
* *
* @authors yutent (yutent@doui.cc) * @authors yutent (yutent@doui.cc)
* @date 2017-02-27 22:01:10 * @date 2017-02-27 22:01:10
* *
*/ */
"use strict"; 'use strict'
//类似于Array 的splice方法 //类似于Array 的splice方法
if(!String.prototype.splice){ if (!String.prototype.splice) {
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'splice', {
'splice', value: function(start, len, fill) {
{ let length = this.length
value: function(start, len, fill){ let argLen = arguments.length
let length = this.length,
argLen = arguments.length;
fill = fill === undefined ? '' : fill fill = fill === undefined ? '' : fill
if(argLen < 1){ if (argLen < 1) {
return this return this
} }
//处理负数 //处理负数
if(start < 0){ if (start < 0) {
if(Math.abs(start) >= length) if (Math.abs(start) >= length) {
start = 0 start = 0
else } else {
start = length + start start = length + start
} }
}
if(argLen === 1){ if (argLen === 1) {
return this.slice(0, start) return this.slice(0, start)
}else{ } else {
len -= 0; len -= 0
let strl = this.slice(0, start), let strl = this.slice(0, start)
strr = this.slice(start + len); let strr = this.slice(start + len)
return strl + fill + strr return strl + fill + strr
} }
}, },
enumerable: false enumerable: false
}) })
} }
//同php的htmlspecialchars函数 //同php的htmlspecialchars函数
if(!String.prototype.htmlspecialchars){ if (!String.prototype.htmlspecialchars) {
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'htmlspecialchars', {
'htmlspecialchars', value: function(sign) {
{ let str = this.replace(/&(?!\w+;)/g, '&amp;')
value: function(sign){ .replace(/</g, '&lt;')
let str = this.replace(/&(?!\w+;)/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') .replace(/>/g, '&gt;')
if(sign === 'ENT_QUOTES') if (sign === 'ENT_QUOTES') {
return str.replace(/"/g, '&quot;').replace(/'/g, '&#39;') return str.replace(/"/g, '&quot;').replace(/'/g, '&#39;')
else if(sign === 'ENT_NOQUOTES') } else if (sign === 'ENT_NOQUOTES') {
return str return str
else } else {
return str.replace(/"/g, '&quot;') return str.replace(/"/g, '&quot;')
}, }
enumerable: false },
}) enumerable: false
})
} }
//htmlspecialchars的还原 //htmlspecialchars的还原
if(!String.prototype.tohtml){ if (!String.prototype.tohtml) {
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'tohtml', {
'tohtml', value: function() {
{ return this.replace(/&lt;/gi, '<')
value: function(){ .replace(/&gt;/gi, '>')
return this.replace(/&lt;/ig, '<') .replace(/&quot;/gi, '"')
.replace(/&gt;/ig, '>') .replace(/&#39;/g, "'")
.replace(/&quot;/ig, '"') .replace(/&amp;/gi, '&')
.replace(/&#39;/g, '\'') },
.replace(/&amp;/ig, '&') enumerable: false
}, })
enumerable: false
})
} }
//简单的过滤xss //简单的过滤xss
if(!String.prototype.xss){ if (!String.prototype.xss) {
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'xss', {
'xss', value: function() {
{ let str = this.htmlspecialchars('ENT_QUOTES')
value: function(){ str = str
let str = this.htmlspecialchars('ENT_QUOTES') .replace(
str = str.replace(/(document\.cookie)|(document\.write)|(\.parentNode)|(window\.location)|(\.innerHTML)/g, '') /(document\.cookie)|(document\.write)|(\.parentNode)|(window\.location)|(\.innerHTML)/g,
.replace(/(%0[0-8bcef])|(%1[0-9a-f])/g, '') ''
return str )
}, .replace(/(%0[0-8bcef])|(%1[0-9a-f])/g, '')
enumerable: false return str
}) },
enumerable: false
})
} }
// js特殊字符的转义 // js特殊字符的转义
if(!String.prototype.escape){ if (!String.prototype.escape) {
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'escape', {
'escape', value: function() {
{ return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1')
value: function(){ },
return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1') enumerable: false
}, })
enumerable: false
})
} }
// padStart & padEnd // padStart & padEnd
if(!String.prototype.padStart){ if (!String.prototype.padStart) {
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'padStart', {
'padStart', value: function(len, fill) {
{ let alen = arguments.length
value: function(len, fill){ let length = this.length
let alen = arguments.length, let ilen = len - length
length = this.length,
ilen = len - length;
if(alen < 1 || ilen < 1) if (alen < 1 || ilen < 1) {
return this return this
}
if(alen < 2 || typeof fill !== 'string') if (alen < 2 || typeof fill !== 'string') {
fill = ' ' fill = ' '
}
while(fill.length < ilen){ while (fill.length < ilen) {
fill += fill fill += fill
} }
return fill.slice(0, ilen) + this return fill.slice(0, ilen) + this
}, },
enumerable: false enumerable: false
}) })
Object.defineProperty(String.prototype, Object.defineProperty(String.prototype, 'padEnd', {
'padEnd', value: function(len, fill) {
{ let alen = arguments.length,
value: function(len, fill){ length = this.length,
let alen = arguments.length, ilen = len - length
length = this.length,
ilen = len - length;
if(alen < 1 || ilen < 1) if (alen < 1 || ilen < 1) {
return this return this
}
if(alen < 2 || typeof fill !== 'string') if (alen < 2 || typeof fill !== 'string') {
fill = ' ' fill = ' '
}
while(fill.length < ilen){ while (fill.length < ilen) {
fill += fill fill += fill
} }
return this + fill.slice(0, ilen) return this + fill.slice(0, ilen)
}, },
enumerable: false enumerable: false
}) })
} }

View File

@ -1,6 +1,6 @@
{ {
"name": "es.shim", "name": "es.shim",
"version": "0.0.3", "version": "1.0.0",
"description": "Some shim api that let you can use in all node.js environment", "description": "Some shim api that let you can use in all node.js environment",
"keyworks": [ "keyworks": [
"es5", "es5",
@ -9,7 +9,7 @@
"extend", "extend",
"shim" "shim"
], ],
"main": "lib/index.js", "main": "index.js",
"repository": "https://github.com/yutent/es.shim.git", "repository": "https://github.com/yutent/es.shim.git",
"author": "yutent", "author": "yutent",
"license": "MIT" "license": "MIT"