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 🙏

© 2024 – Pkg Stats / Ryan Hefner

permissions.js

v1.0.1

Published

This module is a manager for permissions. With this module you can defined and host the permissions of user as a integer which makes it possible to limit the use of memory.

Downloads

11

Readme

Permissions

This module is a manager for permissions. With this module you can defined and host the permissions of user as a integer which makes it possible to limit the use of memory.

How it work ?

It work with a bitwise operations using the bigint library.

Getting started

Prerequisites

Node.js 12.0.0 or newer is required.

Instalation

With npm :

npm install permissions.js

With yarn :

yarn add permissions.js

Usage

Import the module from node_modules :

With CommonJS syntax :

const { Permissions } = require("permissions.js");

With module syntax :

import { Permissions } = from 'permissions.js';

Create a new Permissions with all available permissions as a parameter (an array).

| PARAMETER | TYPE | OPTIONAL | DEFAULT | DESCRIPTION | | ---------------- | ------------------- | -------- | ------- | ------------------------- | | permissions | Array<string> | ✖️ | none | All available permissions | | user permissions | int, bigint, string | ✓ | none | The user permissions |

Note : The order of the permissions is verry important, after choosing it you can add new permissions but at the end of array.

Example : new Permissions(['ADMINISTRATOR', 'VIEW_ALL_MEMBERS', 'UPDATE_MEMBERS', 'DELETE_MEMBERS', 'BAN_MEMBERS']).

Properties


Permissions.default

Return the default permissions as a integer.
Type : Number

Permissions.MAX

Return the max permission as a string.
Type : String

Permissions.permissionCalc

The permissions of user (has a string).
Type : String

Methods


Permissions#find(name)

Find the permission value bay name

Params :

  • name : The name of permission (string or Array<string>)

Return :

The permission (Array<Object> or empty array)

console.log(userPermissions.find(4));

Permissions#toArray()

Return an array of permissions

Return :

Array<permission>

console.log(userPermissions.toArray());

Permissions#toString()

Return the permissions names of user (separate by `, `)

Return :

String<permission>

console.log(userPermissions.toString());

Permissions#hasAnyPermissions()

Return true if the user has at least one permission.

Return :

Boolean

console.log(userPermissions.hasAnyPermissions());

Permissions#hasPermission(permission)

Return true if user have permission(s).

Params :

permission : The permission to check (Array, String, Number)

Return :

Object

console.log(userPermissions.hasPermission([3]));

Permissions#missing(permissions)

Return the missings permissions of user in the parameter.

Params :

permissions : The permissions to test (Array, String, Number)

Return :

Object

console.log(userPermissions(["3"]));

Permissions#equals(other)

Return true if user permissions are equal to parameter.

Params :

  • other : The compared (Array, String, Number)

Return :

Boolean

console.log(userPermissions.equals([100, 100]));

Permissions#addAllPermissions()

Add all permissions from user.

Return :

String (the new bits permissions)

console.log(userPermissions.addAllPermissions());

Permissions#addPermission(permission)

Add a permission to a user.

Params :

  • permission : The permission to add (string | Array<string> | number)

Return :

String (the new bits of permissions.)

console.log(userPermissions.addPermission("ADMINISTRATOR"));

Permissions#removeAllPermissions()

Remove all permissions of the user.

Return :

String (the new bits of permissions).

console.log(userPermissions.removeAllPermissions());

Permissions#removePermission(permission)

Remove a permission to a user.

Params :

  • permission : The permission to remove (string | Array<string>) | number

Return :

String (the new bits of permissions.)

console.log(userPermissions.removePermission("ADMINISTRATOR"));

Example

const { Permissions } = require("permissions");
const permissionsList = [
  "ADMINISTRATOR",
  "VIEW_ALL_MEMBERS",
  "UPDATE_MEMBERS",
  "DELETE_MEMBERS",
  "BAN_MEMBERS",
];
const userPermissions = new Permissions(permissionsList, 100);

console.log(`Default : ${userPermissions.default}`);
console.log(`Max : ${userPermissions.MAX}`);
console.log("User bits : " + userPermissions.permissionCalc);
console.log(userPermissions.find(4));
console.log(userPermissions.toArray());
console.log(userPermissions.toString());
console.log(userPermissions.hasAnyPermissions());
console.log(userPermissions.missing([3]));
console.log(userPermissions.equals([100, 100]));

console.log(userPermissions.addPermission("ADMINISTRATOR"));
console.log(userPermissions.removePermission("ADMINISTRATOR"));
console.log(userPermissions.addAllPermissions());
console.log(userPermissions.removeAllPermissions());