From 641a4425aaa788a28bf9a22a29e6e7375f553824 Mon Sep 17 00:00:00 2001 From: wea_ondara Date: Tue, 26 Sep 2023 16:42:02 +0200 Subject: [PATCH] added Map.groupBy --- src/main.ts | 1 + src/polyfill.ts | 5 +++++ src/util.ts | 11 +++++++++++ types/polyfill.d.ts | 3 +++ 4 files changed, 20 insertions(+) create mode 100644 src/polyfill.ts create mode 100644 src/util.ts create mode 100644 types/polyfill.d.ts diff --git a/src/main.ts b/src/main.ts index 155e1f5..87c0382 100644 --- a/src/main.ts +++ b/src/main.ts @@ -15,6 +15,7 @@ import '@fortawesome/fontawesome-free/css/all.css'; import {createI18n} from '@/locale/locale'; import 'vue3-toastify/dist/index.css'; +import './polyfill' const app = createApp(App); diff --git a/src/polyfill.ts b/src/polyfill.ts new file mode 100644 index 0000000..0b9a076 --- /dev/null +++ b/src/polyfill.ts @@ -0,0 +1,5 @@ +/// reference <../types/polyfill.d.ts> + +import groupBy from '@/util'; + +Map.groupBy = groupBy; diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..f5ab148 --- /dev/null +++ b/src/util.ts @@ -0,0 +1,11 @@ +export default function groupBy(arr: V[], fn: (v: V) => K): Map { + const map = new Map(); + arr.forEach(e => { + const key = fn(e); + if (!map.has(key)) { + map.set(key, [] as V[]); + } + map.get(key)!.push(e); + }); + return map; +} \ No newline at end of file diff --git a/types/polyfill.d.ts b/types/polyfill.d.ts new file mode 100644 index 0000000..7a5eb7b --- /dev/null +++ b/types/polyfill.d.ts @@ -0,0 +1,3 @@ +declare interface MapConstructor { + groupBy(arr: V[], fn: (v: V) => K): Map; +} \ No newline at end of file