Mod-Enum Conversion

The getModsEnum and getEnumMods utility functions allow you to convert an array of mods into its numerical representation and vice-versa respectively. These functions are expected to work only for mods within this enum.

Example

import { getModsEnum, getEnumMods } from 'osu-web.js';

let mods = getEnumMods(0); // Output: []
mods = getEnumMods(24); // Output: ['HD', 'HR']
mods = getEnumMods(88); // Output: ['HD', 'HR', 'DT']

let enumValue = getModsEnum([]); // Output: 0
enumValue = getModsEnum(['HR', 'SD']); // Output: 48
enumValue = getModsEnum(['HR', 'SD', 'FL']); // Output: 1072
enumValue = getModsEnum(['DC']); // Output: 0, because DC (Daycore) is not present in the enum mentioned above

Note: In some responses, DT might be paired with NC and PF might paired with PF. DTNC and SDPF aren’t possible mod combinations, but in the API, they are paired because NC derives from DT and PF derives from SD. So, if the API returns DTNC, it will be parsed as NC, and if it returns SDPF, it will parsed as PF.

let mods = getEnumMods(512); // Output: ['NC']
mods = getEnumMods(576); // DTNC is not a possible mod combo, so the output is: ['NC']
mods = getEnumMods(16384); // Output: ['PF']
mods = getEnumMods(16416); // SDPF is not a possible mod combo, so the output is: ['PF']

For the inverse operation, NC and PF will output their respective enum value (512 and 16384 respectively). If you have specific cases in which you want NC to output the enum value for DTNC, and PF to output the value for SDPF, then you can set derivativeModsWithOriginal to true.

let enumValue = getModsEnum(['NC']); // Output: 512
enumValue = getModsEnum(['PF']); // Output: 16384

enumValue = getModsEnum(['NC'], true); // Output: 576 (Same output for ['DT', 'NC'])
enumValue = getModsEnum(['PF'], true); // Output: 16416 (Same output for ['SD', 'PF'])

Notice that something is missing? Found a typo? Think something's incomplete? Or think that something can be explained better? Feel free to open a pull request or submit an issue on the library's Github repository .