UNPKG

molstar

Version:

A comprehensive macromolecular library.

46 lines (45 loc) 1.43 kB
"use strict"; /** * Copyright (c) 2018-2025 mol* contributors, licensed under MIT, See LICENSE file for more info. * * @author Alexander Rose <alexander.rose@weirdbyte.de> * @author David Sehnal <david.sehnal@gmail.com> */ Object.defineProperty(exports, "__esModule", { value: true }); exports.DefaultMap = DefaultMap; exports.arrayMapAdd = arrayMapAdd; /** A `Map` instance with a `getDefault` method added. */ function DefaultMap(valueCtor) { const map = new Map(); map.getDefault = (key) => { if (map.has(key)) return map.get(key); const value = valueCtor(); map.set(key, value); return value; }; return map; } // TODO currently not working, see https://github.com/Microsoft/TypeScript/issues/10853 // /** A `Map` with a `getDefault` method added. */ // export class DefaultMap<K, V> extends Map<K, V> { // constructor(private valueCtor: () => V, entries?: ReadonlyArray<[K, V]>) { // super(entries) // } // /** Return the value for `key` when available or the default value otherwise. */ // getDefault(key: K) { // if (this.has(key)) return this.get(key)! // const value = this.valueCtor() // this.set(key, value) // return value // } // } function arrayMapAdd(map, key, value) { const a = map.get(key); if (a) { a.push(value); } else { map.set(key, [value]); } }