From dab02dbb64c090309398c028420e935f68622e6a Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 22 Aug 2024 18:03:35 +0800 Subject: [PATCH] 2.4.0 --- History.md | 4 ++ Readme.md | 10 +++++ package.json | 2 +- src/index.mjs | 1 + src/lib/array.js | 55 ++++++++++------------- src/lib/date.js | 11 +++-- src/lib/map.js | 2 +- src/lib/number.js | 6 +-- src/lib/object.js | 8 ++-- src/lib/set.js | 108 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib/string.js | 27 ++++++------ 11 files changed, 172 insertions(+), 62 deletions(-) create mode 100644 src/lib/set.js diff --git a/History.md b/History.md index f82695c..8e02388 100644 --- a/History.md +++ b/History.md @@ -1,3 +1,7 @@ +2.4.0 / 2024-08-22 +================== + * 增加Set的一系列原型方法`union、equal、difference、symmetricDifference、intersection、isSubsetOf、isSupersetOf、isDisjointFrom` + 2.3.0 / 2024-08-07 ================== * 增加修复版toFixed diff --git a/Readme.md b/Readme.md index ecbe684..8081fd6 100644 --- a/Readme.md +++ b/Readme.md @@ -29,6 +29,16 @@ │ ├── .group(fn) // 数组分组 │ └── .groupToMap(fn) // 数组分组, 返回Map对象 │ +├── Set.prototype +│ ├── .union(other) // 合并2个集合 +│ ├── .equal(other) // 判断2个集合是否一致(仅元素相同, 无关顺序) +│ ├── .difference(fn) // 返回不存在于另一个集合的所有元素集合 +│ ├── .symmetricDifference(fn) // 返回当前集合与给定集合中, 不同时存在的所有元素集合 +│ ├── .intersection(fn) // 返回共有的元素集合 +│ ├── .isSubsetOf(fn) // 判断当前集合是否为给定集合的子集 +│ ├── .isSupersetOf(fn) // 判断当前集合是否为给定集合的子超集 +│ ├── .isDisjointFrom(fn) // 判断当前集合,是否与给定集合完全不重合 +│ ├── Date │ └── .isDate(any) // 判断对象是否为 日期对象 ├── Date.prototype diff --git a/package.json b/package.json index 144996c..a3b7a33 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "es.shim", - "version": "2.3.1", + "version": "2.4.0", "description": "实现部分新API, 以及一些常用的扩展方法", "keyworks": [ "es5", diff --git a/src/index.mjs b/src/index.mjs index c4dd4c6..7b0b6c2 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -4,6 +4,7 @@ * @date 2022/01/27 15:33:09 */ +import './lib/set.js' import './lib/array.js' import './lib/object.js' import './lib/map.js' diff --git a/src/lib/array.js b/src/lib/array.js index 69094d4..17f3531 100644 --- a/src/lib/array.js +++ b/src/lib/array.js @@ -7,7 +7,7 @@ * 重写原生的flat方法, 性能提升5~10倍(看数组的结构) */ Object.defineProperty(Array.prototype, 'flat', { - value: function (deep = 1, arr = []) { + value(deep = 1, arr = []) { for (let it of this) { if (Array.isArray(it) && deep > 0) { it.flat(deep - 1, arr) @@ -17,33 +17,30 @@ Object.defineProperty(Array.prototype, 'flat', { } return arr - }, - writable: true + } }) Object.defineProperty(Array.prototype, 'flatMap', { - value: function (fn) { + value(fn) { return this.map(fn).flat() - }, - writable: true + } }) if (!Array.prototype.at) { Object.defineProperty(Array.prototype, 'at', { - value: function (num) { + value(num) { let n = +num if (n < 0) { n += this.length } return this[n] - }, - writable: true + } }) } if (!Array.prototype.findLast) { Object.defineProperty(Array.prototype, 'findLast', { - value: function (fn) { + value(fn) { let num = this.length while (num > 0) { let item = this[--num] @@ -51,13 +48,12 @@ if (!Array.prototype.findLast) { return item } } - }, - writable: true + } }) // 没有findLast也不会有findLastIndex Object.defineProperty(Array.prototype, 'findLastIndex', { - value: function (fn) { + value(fn) { let num = this.length while (num > 0) { let item = this[--num] @@ -66,36 +62,32 @@ if (!Array.prototype.findLast) { } } return -1 - }, - writable: true + } }) } // 最新增加的几种方法 if (!Array.prototype.toSorted) { Object.defineProperty(Array.prototype, 'toSorted', { - value: function (fn) { + value(fn) { return [...this].sort(fn) - }, - writable: true + } }) Object.defineProperty(Array.prototype, 'toReversed', { - value: function () { + value() { return [...this].reverse() - }, - writable: true + } }) Object.defineProperty(Array.prototype, 'toSpliced', { - value: function (...args) { + value(...args) { return [...this].splice(...args) - }, - writable: true + } }) Object.defineProperty(Array.prototype, 'with', { - value: function (num, val) { + value(num, val) { let n = +num let arr = [...this] if (n < 0) { @@ -103,15 +95,14 @@ if (!Array.prototype.toSorted) { } arr[n] = val return arr - }, - writable: true + } }) } // 数组分组 if (!Array.prototype.group) { Object.defineProperty(Array.prototype, 'group', { - value: function (fn) { + value(fn) { let output = {} if (typeof fn === 'function') { for (let it of this) { @@ -126,12 +117,11 @@ if (!Array.prototype.group) { } else { throw Error('argument callback must be a function.') } - }, - writable: true + } }) Object.defineProperty(Array.prototype, 'groupToMap', { - value: function (fn) { + value(fn) { let output = new Map() if (typeof fn === 'function') { for (let it of this) { @@ -146,7 +136,6 @@ if (!Array.prototype.group) { } else { throw Error('argument callback must be a function.') } - }, - writable: true + } }) } diff --git a/src/lib/date.js b/src/lib/date.js index 572785a..c6b07d9 100644 --- a/src/lib/date.js +++ b/src/lib/date.js @@ -6,17 +6,16 @@ //获取当天是本月第几周 if (!Date.isDate) { Object.defineProperty(Date, 'isDate', { - value: function (obj) { + value(obj) { return obj && typeof obj === 'object' && obj.getTime ? true : false - }, - writable: true + } }) } if (!Date.prototype.getFullWeek) { //获取当天是本年度第几周 Object.defineProperty(Date.prototype, 'getFullWeek', { - value: function () { + value() { let thisYear = this.getFullYear() let that = new Date(thisYear, 0, 1) let firstDay = that.getDay() @@ -27,7 +26,7 @@ if (!Date.prototype.getFullWeek) { //获取当天是本月第几周 Object.defineProperty(Date.prototype, 'getWeek', { - value: function () { + value() { let today = this.getDate() let thisMonth = this.getMonth() let thisYear = this.getFullYear() @@ -40,7 +39,7 @@ if (!Date.prototype.getFullWeek) { //时间格式化 if (!Date.prototype.format) { Object.defineProperty(Date.prototype, 'format', { - value: function (str) { + value(str) { let dt = { fullyear: this.getFullYear(), year: this.getYear(), diff --git a/src/lib/map.js b/src/lib/map.js index bea7cf7..fd3eca7 100644 --- a/src/lib/map.js +++ b/src/lib/map.js @@ -6,7 +6,7 @@ if (!Map.groupBy) { Object.defineProperty(Map, 'groupBy', { - value: function (arr, fn) { + value(arr, fn) { return arr.groupToMap(fn) } }) diff --git a/src/lib/number.js b/src/lib/number.js index b4a4d73..505e0eb 100644 --- a/src/lib/number.js +++ b/src/lib/number.js @@ -8,7 +8,7 @@ const NATIVE_TO_FIXED = Number.prototype.toFixed Object.defineProperty(Number.prototype, 'toFixed', { - value: function (n) { + value(n) { return NATIVE_TO_FIXED.call(this + Number.EPSILON, n) } }) @@ -18,7 +18,7 @@ Object.defineProperty(Number.prototype, 'toFixed', { // 否则转为字符串类型 if (!Number.parse) { Object.defineProperty(Number, 'parse', { - value: function (val) { + value(val) { if (typeof val === 'number' || typeof val === 'string') { val += '' if (val.startsWith('0') && !val.startsWith('0.')) { @@ -47,7 +47,7 @@ if (!Number.parse) { // 将字符串转为数字类型 if (!Number.fromString) { Object.defineProperty(Number, 'fromString', { - value: function (val) { + value(val) { return +val || 0 } }) diff --git a/src/lib/object.js b/src/lib/object.js index e4430cf..0827b34 100644 --- a/src/lib/object.js +++ b/src/lib/object.js @@ -10,7 +10,7 @@ */ if (!Object.empty) { Object.defineProperty(Object, 'empty', { - value: function (obj) { + value(obj) { try { for (let i in obj) { return false @@ -23,7 +23,7 @@ if (!Object.empty) { if (!Object.groupBy) { Object.defineProperty(Object, 'groupBy', { - value: function (arr, fn) { + value(arr, fn) { return arr.group(fn) } }) @@ -31,7 +31,7 @@ if (!Object.groupBy) { if (!Object.hasOwn) { Object.defineProperty(Object, 'hasOwn', { - value: function (obj, key) { + value(obj, key) { if (obj === null) { return false } @@ -46,7 +46,7 @@ if (!Object.hasOwn) { */ if (!Object.prototype.toParams) { Object.defineProperty(Object.prototype, 'toParams', { - value: function () { + value() { let params = '' for (let k in this) { if (this[k] === void 0) { diff --git a/src/lib/set.js b/src/lib/set.js new file mode 100644 index 0000000..db4a8a2 --- /dev/null +++ b/src/lib/set.js @@ -0,0 +1,108 @@ +/** + * @author yutent + * @date 2020/09/16 11:54:31 + */ + +if (!Set.prototype.union) { + // 类似 Array的concat, 合并2个集合, 返回1个新集合, 原集合不发生变化 + Object.defineProperty(Set.prototype, 'union', { + value(other) { + let output = new Set([...this]) + if ( + other.size !== void 0 && + other.has !== void 0 && + other.keys !== void 0 + ) { + for (let it of other.keys()) { + output.add(it) + } + } + return output + } + }) + + // 同一批 API + // 返回不存在于另一个集合的所有元素集合 + Object.defineProperty(Set.prototype, 'difference', { + value(other) { + let output = new Set() + for (let it of this) { + if (!other.has(it)) { + output.add(it) + } + } + return output + } + }) + + // 返回当前集合与给定集合中, 不同时存在的所有元素集合 + Object.defineProperty(Set.prototype, 'symmetricDifference', { + value(other) { + let output = this.difference(other) + for (let it of other) { + if (!this.has(it)) { + output.add(it) + } + } + return output + } + }) + + // 返回共有的元素集合 + Object.defineProperty(Set.prototype, 'intersection', { + value(other) { + let output = new Set() + + for (let it of this) { + if (other.has(it)) { + output.add(it) + } + } + + return output + } + }) + + // 判断当前集合是否为给定集合的子集 + Object.defineProperty(Set.prototype, 'isSubsetOf', { + value(other) { + for (let it of this) { + if (!other.has(it)) { + return false + } + } + return true + } + }) + + // 判断当前集合是否为给定集合的子超集 + Object.defineProperty(Set.prototype, 'isSupersetOf', { + value(other) { + for (let it of other) { + if (!this.has(it)) { + return false + } + } + return true + } + }) + + // 判断当前集合,是否与给定集合完全不重合 + Object.defineProperty(Set.prototype, 'isDisjointFrom', { + value(other) { + for (let it of this) { + if (other.has(it)) { + return false + } + } + return true + } + }) +} + +// 判断2个集合是否一致(仅元素相同, 无关顺序) +Object.defineProperty(Set.prototype, 'equal', { + value(other) { + return this.size === other.size && this.isSubsetOf(other) + } +}) diff --git a/src/lib/string.js b/src/lib/string.js index a450cd7..025d0d0 100644 --- a/src/lib/string.js +++ b/src/lib/string.js @@ -34,7 +34,7 @@ function compare(v1, v2) { //类似于Array 的splice方法 if (!String.prototype.splice) { Object.defineProperty(String.prototype, 'splice', { - value: function (start, len, fill) { + value(start, len, fill) { let length = this.length let argLen = arguments.length @@ -70,7 +70,7 @@ if (!String.prototype.splice) { //同php的htmlspecialchars函数 if (!String.prototype.htmlspecialchars) { Object.defineProperty(String.prototype, 'htmlspecialchars', { - value: function (sign) { + value(sign) { let str = this.replace(/&(?!\w+;)/g, '&') .replace(//g, '>') @@ -89,7 +89,7 @@ if (!String.prototype.htmlspecialchars) { //htmlspecialchars的还原 if (!String.prototype.tohtml) { Object.defineProperty(String.prototype, 'tohtml', { - value: function () { + value() { return this.replace(/</gi, '<') .replace(/>/gi, '>') .replace(/"/gi, '"') @@ -102,7 +102,7 @@ if (!String.prototype.tohtml) { //简单的过滤xss if (!String.prototype.xss) { Object.defineProperty(String.prototype, 'xss', { - value: function () { + value() { let str = this.htmlspecialchars('ENT_QUOTES') str = str .replace( @@ -118,7 +118,7 @@ if (!String.prototype.xss) { // js特殊字符的转义 if (!String.prototype.escape) { Object.defineProperty(String.prototype, 'escape', { - value: function () { + value() { return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1') } }) @@ -126,14 +126,13 @@ if (!String.prototype.escape) { if (!String.prototype.at) { Object.defineProperty(String.prototype, 'at', { - value: function (num) { + value(num) { let n = +num if (n < 0) { n += this.length } return this[n] - }, - writable: true + } }) } @@ -143,7 +142,7 @@ if (!String.prototype.at) { */ if (!String.prototype.toJson) { Object.defineProperty(String.prototype, 'toJson', { - value: function () { + value() { let str = this.replace(/^\?/, '') let params = decodeURIComponent(str) .split('&') @@ -161,31 +160,31 @@ if (!String.prototype.toJson) { // 用于版本号的比较 if (!String.prototype.lt) { Object.defineProperty(String.prototype, 'lt', { - value: function (v) { + value(v) { return compare(this, v) === -1 } }) Object.defineProperty(String.prototype, 'lte', { - value: function (v) { + value(v) { return compare(this, v) < 1 } }) Object.defineProperty(String.prototype, 'gt', { - value: function (v) { + value(v) { return compare(this, v) === 1 } }) Object.defineProperty(String.prototype, 'gte', { - value: function (v) { + value(v) { return compare(this, v) > -1 } }) Object.defineProperty(String.prototype, 'eq', { - value: function (v) { + value(v) { return compare(this, v) === 0 } })