UNPKG

isaacscript-common

Version:

Helper functions and features for IsaacScript mods.

120 lines (119 loc) 3.73 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.copyMap = copyMap; exports.defaultMapGetHash = defaultMapGetHash; exports.defaultMapSetHash = defaultMapSetHash; exports.getReversedMap = getReversedMap; exports.mapSetHash = mapSetHash; exports.objectToMap = objectToMap; exports.objectToReadonlyMap = objectToReadonlyMap; exports.sumMap = sumMap; const array_1 = require("./array"); /** Helper function to copy a map. (You can also use a Map constructor to accomplish this task.) */ // eslint-disable-next-line complete/no-mutable-return function copyMap(oldMap) { const newMap = new Map(); for (const [key, value] of oldMap) { newMap.set(key, value); } return newMap; } /** * Helper function to get the value from a `DefaultMap` that corresponds to an entity, assuming that * the map uses `PtrHash` as an index. */ function defaultMapGetHash(map, entity, ...extraArgs) { const ptrHash = GetPtrHash(entity); return map.getAndSetDefault(ptrHash, ...extraArgs); } /** * Helper function to set a value for a `DefaultMap` that corresponds to an entity, assuming that * the map uses `PtrHash` as an index. * * Since `Map` and `DefaultMap` set values in the same way, this function is simply an alias for the * `mapSetHash` helper function. */ function defaultMapSetHash( // eslint-disable-next-line complete/prefer-readonly-parameter-types map, entity, value) { mapSetHash(map, entity, value); } /** * Helper function to get a copy of a map with the keys and the values reversed. * * For example: * * ```ts * new Map<string, number>([ * ["foo", 1], * ["bar", 2], * ]); * ``` * * Would be reversed to: * * ```ts * new Map<number, string>([ * [1, "foo"], * [2, "bar"], * ]); * ``` */ function getReversedMap(map) { const reverseMap = new Map(); for (const [key, value] of map) { reverseMap.set(value, key); } return reverseMap; } /** * Helper function to set a value for a `DefaultMap` that corresponds to an entity, assuming that * the map uses `PtrHash` as an index. */ function mapSetHash( // eslint-disable-next-line complete/prefer-readonly-parameter-types map, entity, value) { const hash = GetPtrHash(entity); map.set(hash, value); } /** * Helper function to convert an object to a map. * * This is useful when you need to construct a type safe object with the `satisfies` operator, but * then later on you need to query it in a way where you expect the return value to be T or * undefined. In this situation, by converting the object to a map, you can avoid unsafe type * assertions. * * Note that the map values will be inserted in a random order, due to how `pairs` works under the * hood. * * Also see the `objectToReadonlyMap` function. */ function objectToMap(object) { const map = new Map(); for (const [key, value] of Object.entries(object)) { map.set(key, value); } return map; } /** * Helper function to convert an object to a read-only map. * * This is useful when you need to construct a type safe object with the `satisfies` operator, but * then later on you need to query it in a way where you expect the return value to be T or * undefined. In this situation, by converting the object to a map, you can avoid unsafe type * assertions. * * Note that the map values will be inserted in a random order, due to how `pairs` works under the * hood. * * Also see the `objectToMap` function. */ function objectToReadonlyMap(object) { return objectToMap(object); } /** Helper function to sum every value in a map together. */ function sumMap(map) { const values = [...map.values()]; return (0, array_1.sumArray)(values); }