A Node.js package for managing permissions using bitwise operations.
bitmask-permissions
is a lightweight library for managing permissions using bitmask values. It allows you to easily add, remove, and validate permissions with high performance and minimal memory usage.
Install the package using npm:
npm install bitmask-permissions
Or using yarn:
yarn add bitmask-permissions
Here's a basic example of how to use the bitmask-permissions
library:
import { BitmaskPermission } from 'bitmask-permissions';
import { permissions } from './constants/permissions';
const bitmask = new BitmaskPermission(permissions);
const initialPermissions = 0;
const updatedPermissions = bitmask.assignPermission(initialPermissions, 'GET');
const hasGetPermission = bitmask.validatePermission(updatedPermissions, 'GET');
console.log(hasGetPermission); // true
Creates a new instance of BitmaskPermission
.
-
permissionsList
: An array of permissions.
Returns the list of permissions.
Validates if the permissions value contains the specified permission.
-
permissions
: The permissions as a bitmask value. -
permission
: The name of the permission.
Assigns a permission to the permissions value.
-
permissions
: The permissions as a bitmask value. -
permission
: The name of the permission.
Removes a permission from the permissions value.
-
permissions
: The permissions as a bitmask value. -
permission
: The name of the permission.
Gets an array of permission names from the permissions value.
-
permissions
: The permissions as a bitmask value.
import { BitmaskPermission } from 'bitmask-permissions';
import { permissions } from './constants/permissions';
const bitmask = new BitmaskPermission(permissions);
let permissionsValue = 0;
permissionsValue = bitmask.assignPermission(permissionsValue, 'GET');
permissionsValue = bitmask.assignPermission(permissionsValue, 'POST');
const hasGetPermission = bitmask.validatePermission(permissionsValue, 'GET');
console.log(hasGetPermission); // true
const hasPutPermission = bitmask.validatePermission(permissionsValue, 'PUT');
console.log(hasPutPermission); // false
import { BitmaskPermission } from 'bitmask-permissions';
import { permissions } from './constants/permissions';
const bitmask = new BitmaskPermission(permissions);
let permissionsValue = 1 | 2 | 4; // Assume 1 = GET, 2 = POST, 4 = PUT
permissionsValue = bitmask.removePermission(permissionsValue, 'GET');
const hasGetPermission = bitmask.validatePermission(permissionsValue, 'GET');
console.log(hasGetPermission); // false
Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.
This project is licensed under the MIT License. See the LICENSE file for details.
Author: Felix Manuel Martinez Sosa
GitHub: Fmanuel809