/** * Adds a flag to the current flags. * @param {number} flags - The current set of flags. * @param {number} flagToAdd - The flag to add. * @returns {number} - The updated flags with the new flag added. */ function addFlag(flags, flagToAdd) { return flags | flagToAdd; } /** * Removes a flag from the current flags. * @param {number} flags - The current set of flags. * @param {number} flagToRemove - The flag to remove. * @returns {number} - The updated flags with the flag removed. */ function removeFlag(flags, flagToRemove) { return flags & ~flagToRemove; } /** * Checks if a flag is set in the current flags. * @param {number} flags - The current set of flags. * @param {number} flagToCheck - The flag to check. * @returns {boolean} - True if the flag is set, otherwise false. */ function hasFlag(flags, flagToCheck) { return (flags & flagToCheck) !== 0; } /** * Toggles a flag in the current flags. * @param {number} flags - The current set of flags. * @param {number} flagToToggle - The flag to toggle. * @returns {number} - The updated flags with the flag toggled. */ function toggleFlag(flags, flagToToggle) { return flags ^ flagToToggle; } /** * Checks if all specified flags are set. * @param {number} flags - The current set of flags. * @param {number} flagsToCheck - The flags to check (can be multiple combined). * @returns {boolean} - True if all specified flags are set, otherwise false. */ function hasAllFlags(flags, flagsToCheck) { return (flags & flagsToCheck) === flagsToCheck; } /** * Checks if any of the specified flags are set. * @param {number} flags - The current set of flags. * @param {number} flagsToCheck - The flags to check (can be multiple combined). * @returns {boolean} - True if any of the specified flags are set, otherwise false. */ function hasAnyFlag(flags, flagsToCheck) { return (flags & flagsToCheck) !== 0; } /** * Generates a flag mapping from an array of names. * @param {string[]} flagNames - An array of names to define as flags. * @returns {Object} - An object where keys are the flag names and values are powers of 2. */ function defineFlags(flagNames) { return flagNames.reduce((flags, name, index) => { flags[name] = 1 << index; // Assign a power of 2 to each flag return flags; }, {}); } /** * Retrieves the names of the flags that are set. * @param {number} flags - The current set of flags. * @param {Object} flagDefinitions - An object where keys are flag names and values are powers of 2. * @returns {string[]} - An array of flag names that are set. */ function getSetFlags(flags, flagDefinitions) { return Object.keys(flagDefinitions).filter(flagName => (flags & flagDefinitions[flagName]) !== 0 ); } module.exports = { addFlag, removeFlag, hasFlag, toggleFlag, hasAllFlags, hasAnyFlag, defineFlags, getSetFlags }