UNPKG

mock-violentmonkey

Version:

Mock violentmonkey's globals for testing userscripts

37 lines 1.41 kB
/* Adds the boilerplate for getting a value, and setting a default value otherwise */ class BetterMap extends Map { /** * @param getDefaultValue Return the value associated with the key. * If that value does not exist it sets the result of `getDefaultValue()` and returns that. */ get = (key, getDefaultValue) => { if (!this.has(key) && getDefaultValue !== undefined) { const value = getDefaultValue(); this.set(key, value); return value; } // The value is only undefined if getDefaultValue is undefined as well // If that is the case, the return-value is `V | undefined` anyway return super.get(key); }; } class BetterWeakMap extends WeakMap { /** * @param getDefaultValue Return the value associated with the key. * If that value does not exist it sets the result of `getDefaultValue()` and returns that. */ get = (key, getDefaultValue) => { if (!this.has(key) && getDefaultValue !== undefined) { const value = getDefaultValue(); this.set(key, value); return value; } // The value is only undefined if getDefaultValue is undefined as well // If that is the case, the return-value is `V | undefined` anyway return super.get(key); }; } export { BetterMap, BetterWeakMap }; //# sourceMappingURL=map.js.map