mirror of
https://github.com/robonen/tools.git
synced 2026-03-20 10:54:44 +00:00
refactor: change separate tools by category
This commit is contained in:
22
core/stdlib/src/bits/flags/index.ts
Normal file
22
core/stdlib/src/bits/flags/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* @name flagsGenerator
|
||||
* @category Bits
|
||||
* @description Create a function that generates unique flags
|
||||
*
|
||||
* @returns {Function} A function that generates unique flags
|
||||
* @throws {RangeError} If more than 31 flags are created
|
||||
*
|
||||
* @since 0.0.2
|
||||
*/
|
||||
export function flagsGenerator() {
|
||||
let lastFlag = 0;
|
||||
|
||||
return () => {
|
||||
// 31 flags is the maximum number of flags that can be created
|
||||
// (without zero) because of the 32-bit integer limit in bitwise operations
|
||||
if (lastFlag & 0x40000000)
|
||||
throw new RangeError('Cannot create more than 31 flags');
|
||||
|
||||
return (lastFlag = lastFlag === 0 ? 1 : lastFlag << 1);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user