Holds properties whose numeric values are non-overlapping binary values, suitable for bitmasking.
The given properties are set under the "bit" key in the object and hold the numeric value.
const space = new BitSpaces().create("up", "down", "left", "right");
// {
// bit: {
// up: 1, // at bit 0, i.e. 1 << 0
// down: 2, // at bit 1, i.e. 1 << 1
// left: 4, // at bit 2, i.e. 1 << 2
// right: 8, // at bit 3, i.e. 1 << 3
// },
// start: 0,
// end: 3,
// bitmask: 15, // 1 << 0 | 1 << 1 | 1 << 2 | 1 << 3
// has: (p) => p === "up" || p === "down" || p === "left" || p === "right",
// bitmaskFor: (pStart, pEnd) => ...
// nameOf: (v) => v === 1 ? "up" : v === 2 ? "down" : v === 4 ...
// }
space.bitmaskFor(); // => space.bitmask (15)
space.bitmaskFor("left"); // => space.bit.left | space.bit.right (12)
space.bitmaskFor(null, "down"); // => space.bit.up | space.bit.down (3)
A bitmask of all values in the space.
Takes the names of two properties within the space and returns a bitmask that covers all values between them including the starting and ending one.*
If pStart > pEnd, they are reversed.
BitSpaces.create
,
function.Optional
pStart: null | TThe name of the property that holds the start value. If null the bitmask will cover from the lowest property.
Optional
pEnd: null | TThe name of the property that holds the end cut-off value for the bitmask. The bitmask with not include pEnd's value. If null the bitmask will cover to the highest property, including.
Returns a non-0 bitmask containing all values in the space between the given ones. Returns 0 if one or both of the given properties do not exist in the space.
The ending bit of the space. It's always equal to start + (# of properties in space) - 1
Returns true if the given name is one of the properties in the space. It is case-sensitive.
Returns the name of the property with the given value, or null if the value does not correspond to one of the space properties.
The starting bit of the space. It's 0 for the first space created in a given set of BitSpaces.
BitSpace represents a single set of mutually exclusive (or orthogonal) properties.
Each property has a numeric value equal to 1 bit-shifted by a certain number of bits.
Created using BitSpaces.create