diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..34c52c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,14 @@ + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride +._* +.idea +.vscode + +package-lock.json + +dist/ +node_modules/ diff --git a/.npmignore b/.npmignore new file mode 100644 index 0000000..a42d72f --- /dev/null +++ b/.npmignore @@ -0,0 +1,13 @@ + +.Spotlight-V100 +.Trashes +.DS_Store +.AppleDouble +.LSOverride +._* +.idea +.vscode + +src/ +node_modules/ +build.js diff --git a/Readme.md b/Readme.md index 437ecb6..1b46438 100644 --- a/Readme.md +++ b/Readme.md @@ -4,41 +4,43 @@ > `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下: -+ Obejct - * empty() - - -+ Array.prototype - * flat() - * flatMap() - * item() +``` +├── Obejct +│ └── empty(any) // 判断对象是否为空对象 +├── Obejct.prototype +│ └── toParams() // 把对象转为 key1=value1&key2=value2 格式 +│ +├── Array.prototype +│ ├── flat(depth) // 数组降维 +│ ├── flatMap(fn) // 等价于 map(fn) -> flat(1) +│ ├── at(index) // 读取指定位置的元素, 负值则从后往前读 +│ ├── findLast(fn) // 查找匹配的最后一项 +│ └── findLastIndex(fn) // 查找匹配的最后一项的索引值 +│ +├── Date +│ └── isDate(any) // 判断对象是否为 日期对象 +├── Date.prototype +│ ├── getWeek() // 获取当前是本月第几周 +│ ├── getFullWeek() // 获取当前是本年度第几周 +│ └── format(formatStr) // 把日期按指定格式转换 +│ +├── String.prototype +│ ├── splice(index, len, pad) // 类似数组的splice方法 +│ ├── htmlspecialchars() // 字符串HTML安全转义 +│ ├── tohtml() // htmlspecialchars的还原 +│ ├── xss() // 字符串安全转义 +│ ├── escape() // js特殊字符的转义 +│ ├── at() // 读取指定位置的字符, 负值则从后往前读 +│ └── toJson() // 将url参数转为对象 +│ +├── Number +│ ├── parse(str) // 将安全范围内的数字字符串转为数字类型 +│ └── fromString(str) // 将字符串转为数字类型 +│ +└── Promise + └── defer() // 创建一个延迟的Promise对象 +``` -+ Date - * isDate() - - -+ Date.prototype - * getFullWeek() - * getWeek() - * format() - - -+ String.prototype - * splice() - * htmlspecialchars() - * tohtml() - * xss() - * escape() - * item() - - -+ Number - * parse() - * fromString() - -+ Promise - * defer() - diff --git a/build.js b/build.js new file mode 100644 index 0000000..f62df39 --- /dev/null +++ b/build.js @@ -0,0 +1,25 @@ +/** + * {build} + * @author yutent + * @date 2021/08/09 11:59:41 + */ + +const Es = require('esbuild') + +Es.build({ + entryPoints: ['src/index.mjs'], + outfile: 'dist/index.mjs', + platform: 'node', + bundle: true, + minify: true, + format: 'esm' +}) + +Es.build({ + entryPoints: ['src/index.mjs'], + outfile: 'dist/index.js', + platform: 'node', + minify: true, + bundle: true, + target: 'node8' +}) diff --git a/index.js b/index.js deleted file mode 100644 index 162e0f0..0000000 --- a/index.js +++ /dev/null @@ -1,13 +0,0 @@ -/** - * - * @authors yutent (yutent@doui.cc) - * @date 2017-02-27 18:01:54 - * - */ - -require('./lib/object') -require('./lib/array.v6') -require('./lib/string') -require('./lib/number') -require('./lib/date') -require('./lib/promise') diff --git a/lib/array.js b/lib/array.js deleted file mode 100644 index fec45d9..0000000 --- a/lib/array.js +++ /dev/null @@ -1,55 +0,0 @@ -/** - * @author yutent - * @date 2020/09/16 11:54:31 - */ - -if (!Array.prototype.flat) { - Object.defineProperty(Array.prototype, 'flat', { - value: function(deep = 1) { - var arr = [] - if (deep < 0) { - deep = 0 - } - - deep-- - - for (let it of this) { - if (it === void 0) { - continue - } - if (Array.isArray(it) && deep >= 0) { - arr = arr.concat(it.flat(deep)) - } else { - arr.push(it) - } - } - - return arr - }, - enumerable: false, - writable: true - }) - - // 没有flat, 当然也不会flatMap - Object.defineProperty(Array.prototype, 'flatMap', { - value: function(fn) { - return this.map(fn).flat() - }, - enumerable: false, - writable: true - }) -} - -if (!Array.prototype.item) { - Object.defineProperty(Array.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/array.v6.js b/lib/array.v6.js deleted file mode 100644 index 638f69a..0000000 --- a/lib/array.v6.js +++ /dev/null @@ -1,71 +0,0 @@ -/** - * @author yutent - * @date 2020/09/16 11:54:31 - */ - -// 判断数组是否包含指定元素 -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.flat) { - Object.defineProperty(Array.prototype, 'flat', { - value: function(deep = 1) { - var arr = [] - if (deep < 0) { - deep = 0 - } - - deep-- - - for (let it of this) { - if (it === void 0) { - continue - } - if (Array.isArray(it) && deep >= 0) { - arr = arr.concat(it.flat(deep)) - } else { - arr.push(it) - } - } - - return arr - }, - enumerable: false, - writable: true - }) - - // 没有flat, 当然也不会flatMap - Object.defineProperty(Array.prototype, 'flatMap', { - value: function(fn) { - return this.map(fn).flat() - }, - enumerable: false, - writable: true - }) -} - -if (!Array.prototype.item) { - Object.defineProperty(Array.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/package.json b/package.json index 70e9417..be7fd07 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,16 @@ { "name": "es.shim", - "version": "2.0.2", + "version": "2.1.0", "description": "实现部分新API, 以及一些常用的扩展方法", - "keyworks": ["es5", "es6", "es7", "polyfill", "extend", "shim"], - "main": "index.js", + "keyworks": ["es5", "es6", "es7", "es2015", "lodash", "polyfill", "extend", "shim"], + "main": "dist/index.js", + "files": ["dist/*"], + "scripts": { + "start": "node ./build.js" + }, "exports": { - "require": "./index.js", - "import": "./index.mjs" + "require": "./dist/index.js", + "import": "./dist/index.mjs" }, "repository": "https://github.com/bytedo/es.shim.git", "author": "yutent", diff --git a/index.mjs b/src/index.mjs similarity index 100% rename from index.mjs rename to src/index.mjs diff --git a/src/lib/array.js b/src/lib/array.js new file mode 100644 index 0000000..6f9b1a0 --- /dev/null +++ b/src/lib/array.js @@ -0,0 +1,45 @@ +/** + * @author yutent + * @date 2020/09/16 11:54:31 + */ + +/** + * 重写原生的flat方法, 性能提升5~10倍(看数组的结构) + */ +Object.defineProperty(Array.prototype, 'flat', { + value: function(deep = 1, arr = []) { + for (let it of this) { + if (Array.isArray(it) && deep > 0) { + it.flat(deep - 1, arr) + } else { + arr.push(it) + } + } + + return arr + }, + enumerable: false, + writable: true +}) + +Object.defineProperty(Array.prototype, 'flatMap', { + value: function(fn) { + return this.map(fn).flat() + }, + enumerable: false, + writable: true +}) + +if (!Array.prototype.at) { + Object.defineProperty(Array.prototype, 'at', { + value: function(num) { + var n = +num + if (n < 0) { + n += this.length + } + return this[n] + }, + enumerable: false, + writable: true + }) +} diff --git a/lib/date.js b/src/lib/date.js similarity index 100% rename from lib/date.js rename to src/lib/date.js diff --git a/lib/number.js b/src/lib/number.js similarity index 100% rename from lib/number.js rename to src/lib/number.js diff --git a/lib/object.js b/src/lib/object.js similarity index 50% rename from lib/object.js rename to src/lib/object.js index fbffbe6..bbb75ee 100644 --- a/lib/object.js +++ b/src/lib/object.js @@ -21,3 +21,20 @@ if (!Object.empty) { enumerable: false }) } + +/** + * 将对象转为url参数字符串 + * 注意: 这里不会处理复杂类型, 直接按toString结果拼接 + */ +if (!Object.prototype.toParams) { + Object.defineProperty(Object.prototype, 'toParams', { + value: function() { + var params = '' + for (let k in this) { + params += `&${k}=${this[k]}` + } + return params.slice(1) + }, + enumerable: false + }) +} diff --git a/lib/promise.js b/src/lib/promise.js similarity index 100% rename from lib/promise.js rename to src/lib/promise.js diff --git a/lib/string.js b/src/lib/string.js similarity index 80% rename from lib/string.js rename to src/lib/string.js index b9fc591..32d0cd2 100644 --- a/lib/string.js +++ b/src/lib/string.js @@ -101,12 +101,12 @@ if (!String.prototype.escape) { }) } -if (!String.prototype.item) { - Object.defineProperty(String.prototype, 'item', { +if (!String.prototype.at) { + Object.defineProperty(String.prototype, 'at', { value: function(num) { var n = +num if (n < 0) { - n = this.length + n + n += this.length } return this[n] }, @@ -114,3 +114,25 @@ if (!String.prototype.item) { writable: true }) } + +/** + * 将url参数转为对象 + * 注意: 这里不处理同一个参数多次出现的情况, 会直接覆盖 + */ +if (!String.prototype.toJson) { + Object.defineProperty(String.prototype, 'toJson', { + value: function() { + var str = this.replace(/^\?/, '') + var params = decodeURIComponent(str) + .split('&') + .filter(_ => _) + .map(_ => _.split('=')) + var obj = {} + for (let it of params) { + obj[it[0]] = it[1] || '' + } + return obj + }, + enumerable: false + }) +}