|
| 1 | +/** |
| 2 | + * Data structure that makes it easy to interact with a bitfield. |
| 3 | + */ |
| 4 | +class BitField { |
| 5 | + /** |
| 6 | + * @param {BitFieldResolvable} [bits=0] Bits(s) to read from |
| 7 | + */ |
| 8 | + constructor(bits) { |
| 9 | + /** |
| 10 | + * Bitfield of the packed bits |
| 11 | + * @type {number} |
| 12 | + */ |
| 13 | + this.bitfield = this.constructor.resolve(bits); |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * Checks whether the bitfield has a bit, or any of multiple bits. |
| 18 | + * @param {BitFieldResolvable} bit Bit(s) to check for |
| 19 | + * @returns {boolean} |
| 20 | + */ |
| 21 | + any(bit) { |
| 22 | + return (this.bitfield & this.constructor.resolve(bit)) !== 0; |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Checks if this bitfield equals another |
| 27 | + * @param {BitFieldResolvable} bit Bit(s) to check for |
| 28 | + * @returns {boolean} |
| 29 | + */ |
| 30 | + equals(bit) { |
| 31 | + return this.bitfield === this.constructor.resolve(bit); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Checks whether the bitfield has a bit, or multiple bits. |
| 36 | + * @param {BitFieldResolvable} bit Bit(s) to check for |
| 37 | + * @returns {boolean} |
| 38 | + */ |
| 39 | + has(bit) { |
| 40 | + if (Array.isArray(bit)) return bit.every(p => this.has(p)); |
| 41 | + bit = this.constructor.resolve(bit); |
| 42 | + return (this.bitfield & bit) === bit; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Gets all given bits that are missing from the bitfield. |
| 47 | + * @param {BitFieldResolvable} bits Bits(s) to check for |
| 48 | + * @param {...*} hasParams Additional parameters for the has method, if any |
| 49 | + * @returns {string[]} |
| 50 | + */ |
| 51 | + missing(bits, ...hasParams) { |
| 52 | + if (!Array.isArray(bits)) bits = new this.constructor(bits).toArray(false); |
| 53 | + return bits.filter(p => !this.has(p, ...hasParams)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Freezes these bits, making them immutable. |
| 58 | + * @returns {Readonly<BitField>} These bits |
| 59 | + */ |
| 60 | + freeze() { |
| 61 | + return Object.freeze(this); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Adds bits to these ones. |
| 66 | + * @param {...BitFieldResolvable} [bits] Bits to add |
| 67 | + * @returns {BitField} These bits or new BitField if the instance is frozen. |
| 68 | + */ |
| 69 | + add(...bits) { |
| 70 | + let total = 0; |
| 71 | + for (const bit of bits) { |
| 72 | + total |= this.constructor.resolve(bit); |
| 73 | + } |
| 74 | + if (Object.isFrozen(this)) return new this.constructor(this.bitfield | total); |
| 75 | + this.bitfield |= total; |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Removes bits from these. |
| 81 | + * @param {...BitFieldResolvable} [bits] Bits to remove |
| 82 | + * @returns {BitField} These bits or new BitField if the instance is frozen. |
| 83 | + */ |
| 84 | + remove(...bits) { |
| 85 | + let total = 0; |
| 86 | + for (const bit of bits) { |
| 87 | + total |= this.constructor.resolve(bit); |
| 88 | + } |
| 89 | + if (Object.isFrozen(this)) return new this.constructor(this.bitfield & ~total); |
| 90 | + this.bitfield &= ~total; |
| 91 | + return this; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Gets an object mapping field names to a {@link boolean} indicating whether the |
| 96 | + * bit is available. |
| 97 | + * @param {...*} hasParams Additional parameters for the has method, if any |
| 98 | + * @returns {Object} |
| 99 | + */ |
| 100 | + serialize(...hasParams) { |
| 101 | + const serialized = {}; |
| 102 | + for (const flag of Object.keys(this.constructor.FLAGS)) { |
| 103 | + serialized[flag] = this.has(this.constructor.FLAGS[flag], ...hasParams); |
| 104 | + } |
| 105 | + return serialized; |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Gets an {@link Array} of bitfield names based on the bits available. |
| 110 | + * @param {...*} hasParams Additional parameters for the has method, if any |
| 111 | + * @returns {string[]} |
| 112 | + */ |
| 113 | + toArray(...hasParams) { |
| 114 | + return Object.keys(this.constructor.FLAGS).filter(bit => this.has(bit, ...hasParams)); |
| 115 | + } |
| 116 | + |
| 117 | + toJSON() { |
| 118 | + return this.bitfield; |
| 119 | + } |
| 120 | + |
| 121 | + valueOf() { |
| 122 | + return this.bitfield; |
| 123 | + } |
| 124 | + |
| 125 | + *[Symbol.iterator]() { |
| 126 | + yield* this.toArray(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Data that can be resolved to give a bitfield. This can be: |
| 131 | + * * A string (see {@link BitField.FLAGS}) |
| 132 | + * * A bit number |
| 133 | + * * An instance of BitField |
| 134 | + * * An Array of BitFieldResolvable |
| 135 | + * @typedef {string|number|BitField|BitFieldResolvable[]} BitFieldResolvable |
| 136 | + */ |
| 137 | + |
| 138 | + /** |
| 139 | + * Resolves bitfields to their numeric form. |
| 140 | + * @param {BitFieldResolvable} [bit=0] - bit(s) to resolve |
| 141 | + * @returns {number} |
| 142 | + */ |
| 143 | + static resolve(bit = 0) { |
| 144 | + if (typeof bit === 'number' && bit >= 0) return bit; |
| 145 | + if (bit instanceof BitField) return bit.bitfield; |
| 146 | + if (Array.isArray(bit)) return bit.map(p => this.resolve(p)).reduce((prev, p) => prev | p, 0); |
| 147 | + if (typeof bit === 'string' && typeof this.FLAGS[bit] !== 'undefined') return this.FLAGS[bit]; |
| 148 | + throw new RangeError('Invalid bitfield flag or number.'); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +/** |
| 153 | + * Numeric bitfield flags. |
| 154 | + * <info>Defined in extension classes</info> |
| 155 | + * @type {Object} |
| 156 | + * @abstract |
| 157 | + */ |
| 158 | +BitField.FLAGS = {}; |
| 159 | + |
| 160 | +module.exports = BitField; |
0 commit comments