npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

simple-perms

v0.1.1

Published

Bitwise permissions made easy.

Readme

Simple Permissions

simple-permissions is a lightweight TypeScript library for managing permissions using bitwise operations. It allows you to easily convert permission strings into bitwise numbers and vice versa, making it perfect for access control, feature flags, and role-based systems.

Features

  • Convert permissions to bitwise numbers: Combine permission strings into a single number.

  • Convert bitwise numbers to permissions: Extract permission strings from a bitwise number.

  • Validation: Throws an error for invalid permissions.

  • Lightweight: No dependencies, just pure TypeScript.

Installation

To use the Permissions class, simply copy the code into your project or install it via npm (if published as a package).

npm install simple-perms

Usage

Importing the Class

import Permissions from 'simple-perms';

Creating an Instance

To create an instance of the Permissions class, pass an array of permission strings to the constructor. These strings represent the available permissions.

const permissions = new Permissions([
    "read",
    "write",
    "delete",
    "admin"
]);

Methods

to(perms: string[]): number

Converts an array of permission strings into a bitwise number.

  • Parameters:
    • perms: An array of permission strings (e.g., ["read", "write"]).
  • Returns: A bitwise number representing the combined permissions.
  • Throws: An error if any permission in perms is not in the original permissions array.
const bitwisePerms = permissions.to(["read", "write"]);
console.log(bitwisePerms); // Output: 3

from(bitwisePerms: number): string[]

Converts a bitwise number back into an array of permission strings.

  • Parameters:
    • bitwisePerms: A bitwise number representing the combined permissions.
  • Returns: An array of permission strings.
const permsArray = permissions.from(3);
console.log(permsArray); // Output: ["read", "write"]

Example

Here’s a complete example demonstrating how to use the Permissions class:

import Permissions from 'simple-perms';

// Initialize with available permissions
const permissions = new Permissions([
    "read",
    "write",
    "delete",
    "admin"
]);

// Convert permissions to a bitwise number
const bitwisePerms = permissions.to(["read", "write"]);
console.log(bitwisePerms); // Output: 3

// Convert the bitwise number back to permissions
const permsArray = permissions.from(3);
console.log(permsArray); // Output: ["read", "write"]

// Handling invalid permissions
try {
    const invalidPerms = permissions.to(["read", "invalid"]);
} catch (error) {
    console.error(error.message); // Output: Invalid permission: invalid
}

How It Works

  1. Bitwise Mapping:

    • Each permission string is assigned a unique bitwise value using 1 << index. For example:
      • "read"1 (1 << 0)
      • "write"2 (1 << 1)
      • "delete"4 (1 << 2)
      • "admin"8 (1 << 3)
  2. Combining Permissions:

    • The to method uses the bitwise OR (|) operator to combine permissions into a single number.
      • ["read", "write"]1 | 23
  3. Extracting Permissions:

    • The from method uses the bitwise AND (&) operator to check which permissions are set in the bitwise number.
      • 3 & 11 (true for "read")
      • 3 & 22 (true for "write")
      • 3 & 40 (false for "delete")
      • 3 & 80 (false for "admin")

Error Handling

  • If you pass an invalid permission to the to method, it will throw an error:
    try {
        const invalidPerms = permissions.to(["read", "invalid"]);
    } catch (error) {
        console.error(error.message); // Output: Invalid permission: invalid
    }

Use Cases

  • Access Control: Manage user permissions in an application.
  • Feature Flags: Enable or disable features based on permissions.
  • Role-Based Systems: Assign roles with specific permissions.

License

This project is licensed under the MIT License. See the LICENSE file for details.


Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.


Support

If you have any questions or need help, feel free to open an issue on GitHub.