From 0f18f9f1df81a2c7d332b37097213b5e5e157a57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=87=E5=A4=A9?= Date: Fri, 18 Sep 2020 11:09:50 +0800 Subject: [PATCH] update --- index.js | 13 +-- index.mjs | 12 +-- lib/{esm/array.mjs => array.js} | 0 lib/{cjs/array.js => array.v6.js} | 0 lib/cjs/date.js | 88 ---------------- lib/cjs/string.js | 165 ------------------------------ lib/{esm/date.mjs => date.js} | 0 lib/esm/number.mjs | 46 --------- lib/esm/object.mjs | 23 ----- lib/esm/promise.mjs | 15 --- lib/{cjs => }/number.js | 0 lib/{cjs => }/object.js | 0 lib/{cjs => }/promise.js | 0 lib/{esm/string.mjs => string.js} | 0 package.json | 2 +- 15 files changed, 14 insertions(+), 350 deletions(-) rename lib/{esm/array.mjs => array.js} (100%) rename lib/{cjs/array.js => array.v6.js} (100%) delete mode 100644 lib/cjs/date.js delete mode 100644 lib/cjs/string.js rename lib/{esm/date.mjs => date.js} (100%) delete mode 100644 lib/esm/number.mjs delete mode 100644 lib/esm/object.mjs delete mode 100644 lib/esm/promise.mjs rename lib/{cjs => }/number.js (100%) rename lib/{cjs => }/object.js (100%) rename lib/{cjs => }/promise.js (100%) rename lib/{esm/string.mjs => string.js} (100%) diff --git a/index.js b/index.js index 798c619..9ef5fc0 100644 --- a/index.js +++ b/index.js @@ -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') diff --git a/index.mjs b/index.mjs index 17bbbc1..814ce0a 100644 --- a/index.mjs +++ b/index.mjs @@ -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' diff --git a/lib/esm/array.mjs b/lib/array.js similarity index 100% rename from lib/esm/array.mjs rename to lib/array.js diff --git a/lib/cjs/array.js b/lib/array.v6.js similarity index 100% rename from lib/cjs/array.js rename to lib/array.v6.js diff --git a/lib/cjs/date.js b/lib/cjs/date.js deleted file mode 100644 index 0545be1..0000000 --- a/lib/cjs/date.js +++ /dev/null @@ -1,88 +0,0 @@ -/** - * @author yutent - * @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 - }) -} diff --git a/lib/cjs/string.js b/lib/cjs/string.js deleted file mode 100644 index d3cdaa0..0000000 --- a/lib/cjs/string.js +++ /dev/null @@ -1,165 +0,0 @@ -/** - * @author yutent - * @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, '&') - .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(/</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 - }) -} - -// 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 - }) -} diff --git a/lib/esm/date.mjs b/lib/date.js similarity index 100% rename from lib/esm/date.mjs rename to lib/date.js diff --git a/lib/esm/number.mjs b/lib/esm/number.mjs deleted file mode 100644 index bbdf9cf..0000000 --- a/lib/esm/number.mjs +++ /dev/null @@ -1,46 +0,0 @@ -/** - * @author yutent - * @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 - }) -} diff --git a/lib/esm/object.mjs b/lib/esm/object.mjs deleted file mode 100644 index fbffbe6..0000000 --- a/lib/esm/object.mjs +++ /dev/null @@ -1,23 +0,0 @@ -/** - * @author yutent - * @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 - }) -} diff --git a/lib/esm/promise.mjs b/lib/esm/promise.mjs deleted file mode 100644 index abbd882..0000000 --- a/lib/esm/promise.mjs +++ /dev/null @@ -1,15 +0,0 @@ -/** - * @author yutent - * @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 - } -} diff --git a/lib/cjs/number.js b/lib/number.js similarity index 100% rename from lib/cjs/number.js rename to lib/number.js diff --git a/lib/cjs/object.js b/lib/object.js similarity index 100% rename from lib/cjs/object.js rename to lib/object.js diff --git a/lib/cjs/promise.js b/lib/promise.js similarity index 100% rename from lib/cjs/promise.js rename to lib/promise.js diff --git a/lib/esm/string.mjs b/lib/string.js similarity index 100% rename from lib/esm/string.mjs rename to lib/string.js diff --git a/package.json b/package.json index 7de86a3..9746c44 100644 --- a/package.json +++ b/package.json @@ -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",