added Map.groupBy

This commit is contained in:
wea_ondara
2023-09-26 16:42:02 +02:00
parent 089ac33d93
commit 641a4425aa
4 changed files with 20 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ import '@fortawesome/fontawesome-free/css/all.css';
import {createI18n} from '@/locale/locale'; import {createI18n} from '@/locale/locale';
import 'vue3-toastify/dist/index.css'; import 'vue3-toastify/dist/index.css';
import './polyfill'
const app = createApp(App); const app = createApp(App);

5
src/polyfill.ts Normal file
View File

@@ -0,0 +1,5 @@
/// reference <../types/polyfill.d.ts>
import groupBy from '@/util';
Map.groupBy = groupBy;

11
src/util.ts Normal file
View File

@@ -0,0 +1,11 @@
export default function groupBy<K, V>(arr: V[], fn: (v: V) => K): Map<K, V[]> {
const map = new Map<K, V[]>();
arr.forEach(e => {
const key = fn(e);
if (!map.has(key)) {
map.set(key, [] as V[]);
}
map.get(key)!.push(e);
});
return map;
}

3
types/polyfill.d.ts vendored Normal file
View File

@@ -0,0 +1,3 @@
declare interface MapConstructor {
groupBy<K, V>(arr: V[], fn: (v: V) => K): Map<K, V[]>;
}