# pemi

> permission map

Latest version **1.0.6** (published 2023-01-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install pemi
pnpm add pemi
yarn add pemi
bun add pemi
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2023-01-03 |
| First published | 2022-11-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 9.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Dan |
| Maintainers | victan |
| Keywords | permission |

## Links

- npm: https://www.npmjs.com/package/pemi
- Repository: https://github.com/tanikou/pemi
- Issues: https://github.com/tanikou/pemi/issues
- npm.io page: https://npm.io/package/pemi

## Recent versions

- 1.0.6 (latest) — 2023-01-03
- 1.0.5 — 2023-01-03
- 1.0.4 — 2022-12-08
- 1.0.3 — 2022-11-21
- 1.0.2 — 2022-11-21
- 1.0.1 — 2022-11-21
- 1.0.0 — 2022-11-18

## README

`用户权限值映射到16进制，用于缩小数据传输与保存时使用更小的空间`

# 安装

```
npm i --save pemi
```


```
import { Permission, Module } from 'pemi'

const Sys = {
  View: { id: 'Sys.V', position: 1 }
}
const Log = {
  View: { id: 'Log.V', position: 1 },
  Create: { id: 'Log.C', position: 2 },
  Update: { id: 'Log.U', position: 3 },
  Delete: { id: 'Log.D', position: 4 }
}

const permission = new Permission(
  [new Module('Sys', Sys), new Module('Log', Log)]
)
```

权限位结构
```
interface Position {
  id?: string
  name?: string
  disabled?: boolean
  position: number
}
```

设置授权
```
permission.permit(['Log.V', 'Log.D'])
or
permission.permit({Sys: '0', Log: '9'})
```

判断是否有某一具体权限
```
permission.has(Log.View)
结果:
true
```
```
permission.has(Log.Create)
结果:
false
```

获取某一个权限块的权限具体结果
```
permission.get('Log')
结果:
{View: true, Create: false, Update: false, Delete: true}
```

将权限值转为16进制
```
permission.hexify()
结果:
{Sys: '0', Log: '9'}
```
或者获取所有有权限的id值
```
permission.arrayify()
结果:
['Log.V', 'Log.D']
```

后端可用16进制值进行保存或者去redis缓存，减少服务器内存消耗

- permission.has: (permit: string | {id: string}) 判断是否有指定某一项权限
- permission.any: (ary: string[]) 判断是否有指定任意一项权限
- permission.all: (ary: string[]) 判断是否有指定的全部一项权限

如果需要控制权限定义及权限是否正常匹配等错误信息，可以new Permission(modules, { logger: console })

---
_Source: https://npm.io/package/pemi · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
