From b39bb080482664e1ed9aee65ee901b2c5eb247d6 Mon Sep 17 00:00:00 2001 From: zhl Date: Mon, 11 Jan 2021 10:10:22 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0Map=E7=9A=84=E4=B8=80?= =?UTF-8?q?=E4=B8=AA=E5=A2=9E=E5=8A=A0=E5=80=BC=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/Extend.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/common/Extend.ts b/src/common/Extend.ts index b385726..585c47e 100644 --- a/src/common/Extend.ts +++ b/src/common/Extend.ts @@ -861,3 +861,25 @@ Object.defineProperties(Array.prototype, { } }); + +interface Map { + /** + * 只针对V为number的Map, 有值的话, 加上V, 没值则直接set + * V为其他类型时, 直接set + * @param key + * @param value + */ + inc?(key: K, value: V): this; +} + +Object.defineProperties(Map.prototype, { + inc: { + value: function (key: K, value: V) { + if (typeof value == 'number') { + this.set(key, (this.get(key) || 0) + value ); + } else { + this.set(key, value); + } + } + } +});