From a863fc891bfa2592c26289185afafac410945acf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 25 May 2018 00:38:52 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0MIT=E5=BC=80=E6=BA=90?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE;=E5=88=A0=E9=99=A4global=E7=9A=84=E6=8B=93?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95;=E5=A2=9E=E5=8A=A0Promise.defer?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=8B=93=E5=B1=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- History.md | 6 ++ LICENSE | 21 +++++ Readme.md | 32 ------- index.js | 14 +++ lib/array.js | 33 +++---- lib/date.js | 165 ++++++++++++++++----------------- lib/global.js | 48 ---------- lib/index.js | 15 --- lib/object.js | 63 ++++++------- lib/promise.js | 17 ++++ lib/string.js | 243 +++++++++++++++++++++++-------------------------- package.json | 4 +- 12 files changed, 295 insertions(+), 366 deletions(-) create mode 100644 LICENSE create mode 100644 index.js delete mode 100644 lib/global.js delete mode 100644 lib/index.js create mode 100644 lib/promise.js diff --git a/History.md b/History.md index cd0b434..45ea766 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,9 @@ +1.0.0 / 2018-05-25 +================== + * delete global extends + * add Promise.defer extend + + 0.0.3 / 2017-03-17 ================== * Optimise String extend function. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ab60297 --- /dev/null +++ b/LICENSE @@ -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. diff --git a/Readme.md b/Readme.md index c4bbf8a..d850b1f 100644 --- a/Readme.md +++ b/Readme.md @@ -39,9 +39,6 @@ -+ global - * gmdate() - * empty() @@ -167,32 +164,3 @@ let str = `<script>alert('hello world')</script>` str.tohtml() // ``` - - - - -### 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 - -``` diff --git a/index.js b/index.js new file mode 100644 index 0000000..824980e --- /dev/null +++ b/index.js @@ -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') diff --git a/lib/array.js b/lib/array.js index 8af8de7..b118082 100644 --- a/lib/array.js +++ b/lib/array.js @@ -1,27 +1,24 @@ /** - * + * * @authors yutent (yutent@doui.cc) * @date 2017-02-27 21:56:03 * */ -"use strict"; - +'use strict' // 判断数组是否包含指定元素 -if(!Array.prototype.includes){ - Object.defineProperty(Array.prototype, - 'includes', - { - value: function(val){ - for(let it of this){ - if(it === val) - return true - } - return false - }, - enumerable: false, - writable: true - }) +if (!Array.prototype.includes) { + Object.defineProperty(Array.prototype, 'includes', { + value: function(val) { + for (let it of this) { + if (it === val) { + return true + } + } + return false + }, + enumerable: false, + writable: true + }) } - diff --git a/lib/date.js b/lib/date.js index 965e690..dfe72a0 100644 --- a/lib/date.js +++ b/lib/date.js @@ -1,106 +1,93 @@ /** - * + * * @authors yutent (yutent@doui.cc) * @date 2017-02-27 22:01:10 * */ -"use strict"; - +'use strict' //获取当天是本月第几周 -if(!Date.isDate){ - Object.defineProperty(Date, - 'isDate', - { - value: function(obj){ - return (typeof obj === 'object') && obj.getTime ? true : false - }, - 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.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, - 'getFullWeek', - { - value: function(){ - let thisYear = this.getFullYear(), - that = new Date(thisYear, 0, 1), - firstDay = that.getDay(), - numsOfToday = (this - that) / 86400000; - 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 - }) + //获取当天是本月第几周 + 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 week = ['Mon', 'Tues', 'Wed', 'Thur', 'Fri', 'Sat', 'Sun'], - 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() - }, - re; +if (!Date.prototype.format) { + Object.defineProperty(Date.prototype, 'format', { + value: function(str) { + str = str || 'Y-m-d H:i:s' + let week = ['Mon', 'Tues', 'Wed', 'Thur', '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 reg = null - 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 - } + dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours - for(let i in re){ - str = str.replace(new RegExp(i, 'g'), re[i]) - } - return str - }, - enumerable: false - }) + 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 re) { + str = str.replace(new RegExp(i, 'g'), reg[i]) + } + return str + }, + enumerable: false + }) } - diff --git a/lib/global.js b/lib/global.js deleted file mode 100644 index 895c462..0000000 --- a/lib/global.js +++ /dev/null @@ -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 - } -} \ No newline at end of file diff --git a/lib/index.js b/lib/index.js deleted file mode 100644 index 40c4c34..0000000 --- a/lib/index.js +++ /dev/null @@ -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') \ No newline at end of file diff --git a/lib/object.js b/lib/object.js index 56ff0b6..2600b38 100644 --- a/lib/object.js +++ b/lib/object.js @@ -1,52 +1,45 @@ /** - * + * * @authors yutent (yutent@doui.cc) * @date 2017-02-27 18:02:56 * */ -"use strict"; - +'use strict' // 对象合并 -if(!Object.prototype.merge){ - Object.defineProperty(Object.prototype, - 'merge', - { - value: function(){ - let args = Array.from(arguments) - if(args.length < 1 || typeof args[0] !== 'object') - return this - args.unshift(this) +if (!Object.prototype.merge) { + Object.defineProperty(Object.prototype, 'merge', { + value: function() { + let args = Array.from(arguments) + if (args.length < 1 || typeof args[0] !== 'object') { + return this + } + args.unshift(this) - Object.assign.apply(null, args) - return this - }, - enumerable: false, - writable: true - }) + Object.assign.apply(null, args) + return this + }, + enumerable: false, + writable: true + }) } - - /** * [ 判断对象/数组是否为空] * 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 - }) +if (!Object.empty) { + Object.defineProperty(Object, 'empty', { + value: function(obj) { + try { + for (let i in obj) { + return false + } + } catch (e) {} + return true + }, + enumerable: false + }) } - diff --git a/lib/promise.js b/lib/promise.js new file mode 100644 index 0000000..e96939e --- /dev/null +++ b/lib/promise.js @@ -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 + } +} diff --git a/lib/string.js b/lib/string.js index 6827117..987b561 100644 --- a/lib/string.js +++ b/lib/string.js @@ -1,166 +1,155 @@ /** - * + * * @authors yutent (yutent@doui.cc) * @date 2017-02-27 22:01:10 * */ -"use strict"; - +'use strict' //类似于Array 的splice方法 -if(!String.prototype.splice){ - Object.defineProperty(String.prototype, - 'splice', - { - value: function(start, len, fill){ - let length = this.length, - argLen = arguments.length; +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 + fill = fill === undefined ? '' : fill - if(argLen < 1){ - return this - } + if (argLen < 1) { + return this + } - //处理负数 - if(start < 0){ - if(Math.abs(start) >= length) - start = 0 - else - start = length + start - } + //处理负数 + 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; + if (argLen === 1) { + return this.slice(0, start) + } else { + len -= 0 - let strl = this.slice(0, start), - strr = this.slice(start + len); + let strl = this.slice(0, start) + let strr = this.slice(start + len) - return strl + fill + strr - } - }, - enumerable: false - }) + 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, '&').replace(//g, '>') +if (!String.prototype.htmlspecialchars) { + Object.defineProperty(String.prototype, 'htmlspecialchars', { + value: function(sign) { + let str = this.replace(/&(?!\w+;)/g, '&') + .replace(//g, '>') - if(sign === 'ENT_QUOTES') - return str.replace(/"/g, '"').replace(/'/g, ''') - else if(sign === 'ENT_NOQUOTES') - return str - else - return str.replace(/"/g, '"') - }, - enumerable: false - }) + if (sign === 'ENT_QUOTES') { + return str.replace(/"/g, '"').replace(/'/g, ''') + } else if (sign === 'ENT_NOQUOTES') { + return str + } else { + return str.replace(/"/g, '"') + } + }, + enumerable: false + }) } - //htmlspecialchars的还原 -if(!String.prototype.tohtml){ - Object.defineProperty(String.prototype, - 'tohtml', - { - value: function(){ - return this.replace(/</ig, '<') - .replace(/>/ig, '>') - .replace(/"/ig, '"') - .replace(/'/g, '\'') - .replace(/&/ig, '&') - }, - enumerable: false - }) +if (!String.prototype.tohtml) { + Object.defineProperty(String.prototype, 'tohtml', { + value: function() { + return this.replace(/</gi, '<') + .replace(/>/gi, '>') + .replace(/"/gi, '"') + .replace(/'/g, "'") + .replace(/&/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 - }) +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 - }) +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, - length = this.length, - ilen = len - length; +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 < 1 || ilen < 1) { + return this + } - if(alen < 2 || typeof fill !== 'string') - fill = ' ' + if (alen < 2 || typeof fill !== 'string') { + fill = ' ' + } - while(fill.length < ilen){ - fill += fill - } + while (fill.length < ilen) { + fill += fill + } - return fill.slice(0, ilen) + this - }, - enumerable: false - }) + 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; + 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 < 1 || ilen < 1) { + return this + } - if(alen < 2 || typeof fill !== 'string') - fill = ' ' + if (alen < 2 || typeof fill !== 'string') { + fill = ' ' + } - while(fill.length < ilen){ - fill += fill - } + while (fill.length < ilen) { + fill += fill + } - return this + fill.slice(0, ilen) - }, - enumerable: false - }) -} \ No newline at end of file + return this + fill.slice(0, ilen) + }, + enumerable: false + }) +} diff --git a/package.json b/package.json index 4f8f6ba..ae45384 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "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", "keyworks": [ "es5", @@ -9,7 +9,7 @@ "extend", "shim" ], - "main": "lib/index.js", + "main": "index.js", "repository": "https://github.com/yutent/es.shim.git", "author": "yutent", "license": "MIT"