1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 12:42:44 +01:00

🐛 fix Permission BitField

This commit is contained in:
Flam3rboy 2021-02-22 08:20:42 +01:00
parent 0ab57bb6cd
commit c058020a7c
3 changed files with 31 additions and 20 deletions

24
dist/util/BitField.js vendored
View File

@ -7,19 +7,19 @@ exports.BitField = void 0;
class BitField {
constructor(bits = 0) {
this.bitfield = BigInt(0);
this.bitfield = BitField.resolve(bits);
this.bitfield = BitField.resolve.call(this, bits);
}
/**
* Checks whether the bitfield has a bit, or any of multiple bits.
*/
any(bit) {
return (this.bitfield & BitField.resolve(bit)) !== 0n;
return (this.bitfield & BitField.resolve.call(this, bit)) !== 0n;
}
/**
* Checks if this bitfield equals another
*/
equals(bit) {
return this.bitfield === BitField.resolve(bit);
return this.bitfield === BitField.resolve.call(this, bit);
}
/**
* Checks whether the bitfield has a bit, or multiple bits.
@ -27,7 +27,7 @@ class BitField {
has(bit) {
if (Array.isArray(bit))
return bit.every((p) => this.has(p));
const BIT = BitField.resolve(bit);
const BIT = BitField.resolve.call(this, bit);
return (this.bitfield & BIT) === BIT;
}
/**
@ -52,7 +52,7 @@ class BitField {
add(...bits) {
let total = 0n;
for (const bit of bits) {
total |= BitField.resolve(bit);
total |= BitField.resolve.call(this, bit);
}
if (Object.isFrozen(this))
return new BitField(this.bitfield | total);
@ -66,7 +66,7 @@ class BitField {
remove(...bits) {
let total = 0n;
for (const bit of bits) {
total |= BitField.resolve(bit);
total |= BitField.resolve.call(this, bit);
}
if (Object.isFrozen(this))
return new BitField(this.bitfield & ~total);
@ -112,16 +112,22 @@ class BitField {
* @returns {number}
*/
static resolve(bit = 0n) {
// @ts-ignore
const FLAGS = this.FLAGS || this.constructor?.FLAGS;
if ((typeof bit === "number" || typeof bit === "bigint") && bit >= 0n)
return BigInt(bit);
if (bit instanceof BitField)
return bit.bitfield;
if (Array.isArray(bit))
return bit.map((p) => this.resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n);
if (typeof bit === "string" && typeof this.FLAGS[bit] !== "undefined")
if (Array.isArray(bit)) {
// @ts-ignore
const resolve = this.constructor?.resolve || this.resolve;
return bit.map((p) => resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n);
}
if (typeof bit === "string" && typeof FLAGS[bit] !== "undefined")
return this.FLAGS[bit];
throw new RangeError("BITFIELD_INVALID: " + bit);
}
}
exports.BitField = BitField;
BitField.FLAGS = {};
//# sourceMappingURL=BitField.js.map

View File

@ -1 +1 @@
{"version":3,"file":"BitField.js","sourceRoot":"","sources":["../../src/util/BitField.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;;;AAOb;;GAEG;AACH,MAAa,QAAQ;IAKpB,YAAY,OAA2B,CAAC;QAJjC,aAAQ,GAAW,MAAM,CAAC,CAAC,CAAC,CAAC;QAKnC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAuB;QAC1B,OAAO,CAAC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;IACvD,CAAC;IAED;;OAEG;IACH,MAAM,CAAC,GAAuB;QAC7B,OAAO,IAAI,CAAC,QAAQ,KAAK,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAChD,CAAC;IAED;;OAEG;IACH,GAAG,CAAC,GAAuB;QAC1B,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC;YAAE,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QAC7D,MAAM,GAAG,GAAG,QAAQ,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAClC,OAAO,CAAC,IAAI,CAAC,QAAQ,GAAG,GAAG,CAAC,KAAK,GAAG,CAAC;IACtC,CAAC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AAE,OAAO,IAAI,IAAI,CAAC,OAAO,CAAC;YAC1D,OAAO,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,EAAE,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;SACpF;QACD,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,OAAO,KAAK,CAAC,GAAG,CAAC,KAAK,WAAW;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;QACzF,MAAM,IAAI,UAAU,CAAC,oBAAoB,GAAG,GAAG,CAAC,CAAC;IAClD,CAAC;;AAnIF,4BAoIC;AAjIc,cAAK,GAA2B,EAAE,CAAC"}

View File

@ -11,24 +11,24 @@ export type BitFieldResolvable = number | BigInt | BitField | string | BitFieldR
export class BitField {
public bitfield: bigint = BigInt(0);
public static FLAGS: Record<string, bigint>;
public static FLAGS: Record<string, bigint> = {};
constructor(bits: BitFieldResolvable = 0) {
this.bitfield = BitField.resolve(bits);
this.bitfield = BitField.resolve.call(this, bits);
}
/**
* Checks whether the bitfield has a bit, or any of multiple bits.
*/
any(bit: BitFieldResolvable): boolean {
return (this.bitfield & BitField.resolve(bit)) !== 0n;
return (this.bitfield & BitField.resolve.call(this, bit)) !== 0n;
}
/**
* Checks if this bitfield equals another
*/
equals(bit: BitFieldResolvable): boolean {
return this.bitfield === BitField.resolve(bit);
return this.bitfield === BitField.resolve.call(this, bit);
}
/**
@ -36,7 +36,7 @@ export class BitField {
*/
has(bit: BitFieldResolvable): boolean {
if (Array.isArray(bit)) return bit.every((p) => this.has(p));
const BIT = BitField.resolve(bit);
const BIT = BitField.resolve.call(this, bit);
return (this.bitfield & BIT) === BIT;
}
@ -63,7 +63,7 @@ export class BitField {
add(...bits: BitFieldResolvable[]): BitField {
let total = 0n;
for (const bit of bits) {
total |= BitField.resolve(bit);
total |= BitField.resolve.call(this, bit);
}
if (Object.isFrozen(this)) return new BitField(this.bitfield | total);
this.bitfield |= total;
@ -77,7 +77,7 @@ export class BitField {
remove(...bits: BitFieldResolvable[]) {
let total = 0n;
for (const bit of bits) {
total |= BitField.resolve(bit);
total |= BitField.resolve.call(this, bit);
}
if (Object.isFrozen(this)) return new BitField(this.bitfield & ~total);
this.bitfield &= ~total;
@ -128,11 +128,16 @@ export class BitField {
* @returns {number}
*/
static resolve(bit: BitFieldResolvable = 0n): bigint {
// @ts-ignore
const FLAGS = this.FLAGS || this.constructor?.FLAGS;
if ((typeof bit === "number" || typeof bit === "bigint") && bit >= 0n) return BigInt(bit);
if (bit instanceof BitField) return bit.bitfield;
if (Array.isArray(bit))
return bit.map((p) => this.resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n);
if (typeof bit === "string" && typeof this.FLAGS[bit] !== "undefined") return this.FLAGS[bit];
if (Array.isArray(bit)) {
// @ts-ignore
const resolve = this.constructor?.resolve || this.resolve;
return bit.map((p) => resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n);
}
if (typeof bit === "string" && typeof FLAGS[bit] !== "undefined") return this.FLAGS[bit];
throw new RangeError("BITFIELD_INVALID: " + bit);
}
}