/
githubmirror
/
client
Обзор
Документация
Войти
/
githubmirror
/
client
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v6.0.2
shared/util/map.tsx
35 строк
880 B
chrisnojima
Hookify + Type remote windows (#21882)
07 янв 2020, 23:21
Не верифицирован
07 янв 2020, 23:21
bc6fab5
Код
Авторство
О чём код?
type MapType<M> = M extends Map<infer K, infer V> ? [K, V] : never /** Get a value from a map, if missing, add the default value and return it */ export function mapGetEnsureValue<M extends Map<any, any>>( map: M, key: MapType<M>[0], def: MapType<M>[1] ): MapType<M>[1] { const existing = map.get(key) if (existing === undefined) { map.set(key, def) return def } else { return existing } } export function mapEqual<M extends Map<any, any>>(map1: M, map2: M): boolean { if (map1 === map2) { return true } const k1 = [...map1.keys()] const k2 = [...map2.keys()] if (k1.length !== k2.length) { return false } return !k1.some(key => map1.get(key) !== map2.get(key)) } export function mapFilterByKey<M extends Map<any, any>>(map: M, keys: Set<string>): M { return new Map([...map.entries()].filter(([k]) => keys.has(k))) as M }