From dd4afee1b042bff5212ebe20c6fe1cb4ac409b42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Tue, 28 Feb 2017 00:54:18 +0800 Subject: [PATCH] new project --- History.md | 52 +++++++++++++ Readme.md | 198 ++++++++++++++++++++++++++++++++++++++++++++++++++ lib/array.js | 27 +++++++ lib/date.js | 106 +++++++++++++++++++++++++++ lib/global.js | 48 ++++++++++++ lib/index.js | 15 ++++ lib/object.js | 52 +++++++++++++ lib/string.js | 166 ++++++++++++++++++++++++++++++++++++++++++ package.json | 16 ++++ 9 files changed, 680 insertions(+) create mode 100644 History.md create mode 100644 Readme.md create mode 100644 lib/array.js create mode 100644 lib/date.js create mode 100644 lib/global.js create mode 100644 lib/index.js create mode 100644 lib/object.js create mode 100644 lib/string.js create mode 100644 package.json diff --git a/History.md b/History.md new file mode 100644 index 0000000..c6666d3 --- /dev/null +++ b/History.md @@ -0,0 +1,52 @@ +1.1.9 / 2017-02-10 +================== + * 优化字符串splice方法的实现; + * 修复empty方法对字符串'0'的判断; + + +1.1.8 / 2017-01-23 +================== + * 简化gmdate方法对传入时间字符串的处理; + + +1.1.6 / 2016-06-30 +================== + * 对merge方法设置为可写(妥协于superagent模块中的qs依赖冲突); + + +1.1.5 / 2016-06-30 +================== + * 优化判断 + + +1.1.4 / 2016-06-24 +================== + * 增加特殊字符的转义方法 + + +1.1.2 / 2016-04-19 +================== + * 修复改写定义方式之后,参数错误的bug + * 修复日历周数方法判断是否存在的bug + + + +1.1.0 / 2016-04-19 +================== + * 修复单个项目多处引用本模块导致报错的bug + * 优化内置方法定义方式,避免for in遍历时出现不必要的坑 + + + + +1.0.1 / 2016-04-11 +================== + * 补充文档 + * 修正pakage.json配置 + + + + +1.0.0 / 2016-04-10 +================== + * v1.0.0 正式发布 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..c4bbf8a --- /dev/null +++ b/Readme.md @@ -0,0 +1,198 @@ +![module info](https://nodei.co/npm/es.shim.png?downloads=true&downloadRank=true&stars=true) + +# es.shim +> `es.shim` is an extend module for letting you can use some future api in current Node.js version. +> Also some useful api for you. + + ++ Obejct + * empty() + + ++ Obejct.prototype + * merge() + + ++ Array.prototype + * includes() + + + ++ Date + * isDate() + + ++ Date.prototype + * getFullWeek() + * getWeek() + * format() + + ++ String.prototype + * splice() + * htmlspecialchars() + * tohtml() + * xss() + * escape() + * padStart() + * padEnd() + + + ++ global + * gmdate() + * empty() + + + +## Usage + +### 1. Object.prototype.merge() +```javascript +let obj1 = {a: 123, b: 456} +let obj2 = {a: 22, c: 44} +let obj3 = {c: 11, e: 55} + +o1.merge(o2) +// now obj1 is {a: 22, b: 456, c: 44} +// nothing to obj2 + +o1.merge(o2, o3) +// obj1 will be {a: 22, b: 456, c: 11, e: 55} +// nothing to obj2 & obj3 + +``` + +### 2. Object.empty() +```javascript +Object.empty({}) // true +Object.empty({a: 213}) // false +Object.empty([]) // true +Object.empty([null]) // false +Object.empty([undefined]) // false + +``` + + +### 3. Array.prototype.includes() +```javascript +let arr = [1, '3', 54, 32, 'foo'] + +arr.includes(1) // true +arr.includes(3) // false +arr.includes('bar') // false +arr.includes('54') // false + +``` + + +### 4. Date.isDate() +```javascript + +Date.isDate(new Date()) // true +Date.isDate({}) // false +Date.isDate(['bar']) // false +Date.isDate('foo') // false + +``` + + +### 5. Date.prototype.format(format) +> `format` can be these below: +> - Y (with century) eg. 1970,2017 +> - y (without century) eg. 70, 117 +> - m month, 01-12 +> - n month, 1-12 +> - d date, 01-31 +> - j date, 1-31 +> - H hours 00-23 +> - h hours 00-12 +> - G hours 0-23 +> - g hours 0-12 +> - i minutes, 00-59 +> - s seconds, 00-59 +> - W how many weeks from 01-01 this year +> - w how many weeks from 01 this month +> - D week name, like Mon, Tue, Wed, Thu, Fri, Sat, Sun + +```javascript + +new Date().format() // default 2017-02-08 12:11:23 +new Date().format('Y-m-d') // 2017-02-08 +new Date().format('Y/n/j') // 2017/2/8 +new Date().format('Y年n月j日 第W周') // 2017年2月10日 第6周 +new Date('Wed Feb 10 2016 23:34:04 GMT+0800 (CST)').format() // 2016-02-10 23:34:04 +new Date('2016-08-10T13:14:44.000Z').format() //2016-08-10 21:14:44 +new Date(1470834884000).format('') //2016-08-10 21:14:44 + +``` + + +### 6. String.prototype.splice(start, len[, fill]) +- start `` +- len `` +- fill `` + +```javascript +let str = 'Hello baby'; + +str.splice(0, 5) // return ' baby' +console.log(str) // nothing tostr, so it return 'Hello baby' + +str.splice(0, 5, 'Love') //return 'Love baby' + +str.splice(6, 0, 'world, ')// return 'Hello world, baby' + +str = str.splice(6) //return 'Holle ' + +``` + + +### 7. String.prototype.htmlspecialchars([sign]) +> Just like php's function - `htmlspecialchars` +- sign `` (ENT_QUOTES/ENT_NOQUOTES) + +```javascript +let str = `` + +str.htmlspecialchars() // <script>alert('hello world')</script> +str.htmlspecialchars('ENT_QUOTES') // <script>alert('hello world')</script> + +``` + + +### 8. String.prototype.tohtml() +```javascript +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/lib/array.js b/lib/array.js new file mode 100644 index 0000000..8af8de7 --- /dev/null +++ b/lib/array.js @@ -0,0 +1,27 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2017-02-27 21:56:03 + * + */ + +"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 + }) +} + diff --git a/lib/date.js b/lib/date.js new file mode 100644 index 0000000..965e690 --- /dev/null +++ b/lib/date.js @@ -0,0 +1,106 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2017-02-27 22:01:10 + * + */ + +"use strict"; + + +//获取当天是本月第几周 +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(), + 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 + }) +} + + + +//时间格式化 +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; + + 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){ + str = str.replace(new RegExp(i, 'g'), re[i]) + } + return str + }, + enumerable: false + }) +} + diff --git a/lib/global.js b/lib/global.js new file mode 100644 index 0000000..895c462 --- /dev/null +++ b/lib/global.js @@ -0,0 +1,48 @@ +/** + * + * @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 new file mode 100644 index 0000000..40c4c34 --- /dev/null +++ b/lib/index.js @@ -0,0 +1,15 @@ +/** + * + * @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 new file mode 100644 index 0000000..56ff0b6 --- /dev/null +++ b/lib/object.js @@ -0,0 +1,52 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2017-02-27 18:02:56 + * + */ + +"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) + + 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 + }) +} + diff --git a/lib/string.js b/lib/string.js new file mode 100644 index 0000000..0aef816 --- /dev/null +++ b/lib/string.js @@ -0,0 +1,166 @@ +/** + * + * @authors yutent (yutent@doui.cc) + * @date 2017-02-27 22:01:10 + * + */ + +"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; + + 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, '&').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 + }) +} + + +//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 + }) +} + + +//简单的过滤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, + 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 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 + }) +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..9097206 --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "es.shim", + "version": "0.0.1", + "description": "Some shim api that let you can use in all node.js environment", + "keyworks": [ + "es5", + "es6", + "polyfill", + "extend", + "shim" + ], + "main": "lib/index.js", + "repository": "https://github.com/yutent/es.shim.git", + "author": "yutent", + "license": "MIT" +}