added Map.groupBy
This commit is contained in:
@@ -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
5
src/polyfill.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
/// reference <../types/polyfill.d.ts>
|
||||||
|
|
||||||
|
import groupBy from '@/util';
|
||||||
|
|
||||||
|
Map.groupBy = groupBy;
|
||||||
11
src/util.ts
Normal file
11
src/util.ts
Normal 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
3
types/polyfill.d.ts
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
declare interface MapConstructor {
|
||||||
|
groupBy<K, V>(arr: V[], fn: (v: V) => K): Map<K, V[]>;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user