From be25f2dd4b442216edf50c7ffb90c7f3be20b977 Mon Sep 17 00:00:00 2001 From: yutent Date: Thu, 28 Mar 2024 18:50:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Obejct.groupBy=E5=92=8CMap.gr?= =?UTF-8?q?oupBy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Readme.md | 5 +++++ package.json | 2 +- src/index.mjs | 3 ++- src/lib/map.js | 13 +++++++++++++ src/lib/object.js | 19 +++++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 src/lib/map.js diff --git a/Readme.md b/Readme.md index 966e421..c2897b4 100644 --- a/Readme.md +++ b/Readme.md @@ -8,9 +8,14 @@ ```js ├── Obejct │ └── .empty(any) // 判断对象是否为空对象 +│ └── .groupBy(arr, fn) // 数组分组, 返回分组后的对象 +│ └── .hasOwn(any, key) // 安全版的 Object.prototype.hasOwnProperty() ├── Obejct.prototype │ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式 │ +├── Map +│ └── .groupBy(any) // 数组分组, 返回分组后的对象 +│ ├── Array.prototype │ ├── .flat(depth) // 数组降维 │ ├── .flatMap(fn) // 等价于 map(fn) -> flat(1) diff --git a/package.json b/package.json index b789345..2c24fcb 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "es.shim", - "version": "2.2.1", + "version": "2.2.2", "description": "实现部分新API, 以及一些常用的扩展方法", "keyworks": [ "es5", diff --git a/src/index.mjs b/src/index.mjs index 6d7c911..c4dd4c6 100644 --- a/src/index.mjs +++ b/src/index.mjs @@ -4,8 +4,9 @@ * @date 2022/01/27 15:33:09 */ -import './lib/object.js' import './lib/array.js' +import './lib/object.js' +import './lib/map.js' import './lib/string.js' import './lib/number.js' import './lib/date.js' diff --git a/src/lib/map.js b/src/lib/map.js new file mode 100644 index 0000000..bea7cf7 --- /dev/null +++ b/src/lib/map.js @@ -0,0 +1,13 @@ +/** + * {} + * @author yutent + * @date 2024/03/28 18:48:08 + */ + +if (!Map.groupBy) { + Object.defineProperty(Map, 'groupBy', { + value: function (arr, fn) { + return arr.groupToMap(fn) + } + }) +} diff --git a/src/lib/object.js b/src/lib/object.js index 293935c..e4430cf 100644 --- a/src/lib/object.js +++ b/src/lib/object.js @@ -21,6 +21,25 @@ if (!Object.empty) { }) } +if (!Object.groupBy) { + Object.defineProperty(Object, 'groupBy', { + value: function (arr, fn) { + return arr.group(fn) + } + }) +} + +if (!Object.hasOwn) { + Object.defineProperty(Object, 'hasOwn', { + value: function (obj, key) { + if (obj === null) { + return false + } + return obj.hasOwnProperty(key) + } + }) +} + /** * 将对象转为url参数字符串 * 注意: 这里不会处理复杂类型, 直接按toString结果拼接