https://github.com/lxghtless/mode-mask
Given an array of strings or a {} of {string: number} build a mask data structure to manage things like user permissions in apps. Servers that are enabled with certain modes or features might also find this useful.
https://github.com/lxghtless/mode-mask
features mode-mask modes nodejs permissions typescript
Last synced: about 1 month ago
JSON representation
Given an array of strings or a {} of {string: number} build a mask data structure to manage things like user permissions in apps. Servers that are enabled with certain modes or features might also find this useful.
- Host: GitHub
- URL: https://github.com/lxghtless/mode-mask
- Owner: lxghtless
- License: mit
- Created: 2019-04-14T19:36:44.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-05T15:58:57.000Z (over 3 years ago)
- Last Synced: 2025-10-03T06:32:09.276Z (9 months ago)
- Topics: features, mode-mask, modes, nodejs, permissions, typescript
- Language: TypeScript
- Homepage: https://github.com/lxghtless/mode-mask
- Size: 592 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 8
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
mode mask
Given an array of strings or a {} of {string: number} build a mask data structure to manage things like user permissions in apps. Servers that are enabled with certain modes or features might also find this useful.
Install
npm i mode-mask
yarn add mode-mask
### Basic Usage with MaskFactory
```ts
import {buildMaskFactory} from 'mode-mask'
const modes = {
READ: 1,
WRITE: 2,
DELETE: 4,
AUTO_CREATE: 8
}
// build mask with factory from array of strings
const mask = buildMaskFactory({
values: Object.keys(modes)
})()
// get the MaskDatum of a given permissions sum
const rda = mask.indexOf(modes.READ + modes.DELETE + modes.AUTO_CREATE)
// with an explicit number
const rda = mask.indexOf(13)
// from string values
const rda = mask.fromValues(['READ', 'DELETE', 'AUTO_CREATE'])
```
### Basic Usage with MaskProvider
```ts
import {MaskProvider} from 'mode-mask'
const modes = {
READ: 1,
WRITE: 2,
DELETE: 4,
AUTO_CREATE: 8
}
// build mask with provider with a map of
const mask = MaskProvider.resolveMask(modes)
// get the MaskDatum of a given permissions sum
const rda = mask.indexOf(modes.READ + modes.DELETE + modes.AUTO_CREATE)
// with an explicit number
const rda = mask.indexOf(13)
// from string values
const rda = mask.fromValues(['READ', 'DELETE', 'AUTO_CREATE'])
```
### MaskProvider from values with derived mask
```ts
import {MaskProvider} from 'mode-mask'
const expectedModes = {
READ: 1,
WRITE: 2,
DELETE: 4,
AUTO_CREATE: 8
}
const maskProvider = MaskProvider.fromModesOrValues(Object.keys(expectedModes))
const {mask, modes, values} = maskProvider
// get the MaskDatum of a given permissions sum
const rda = mask.indexOf(modes.READ + modes.DELETE + modes.AUTO_CREATE)
// with an explicit number
const rda = mask.indexOf(13)
// from string values
const rda = mask.fromValues(['READ', 'DELETE', 'AUTO_CREATE'])
// modes are derived from values
expect(modes).to.deep.equal(expectedModes)
```
### MaskProvider from values with derived mask
```ts
import {MaskProvider} from 'mode-mask'
const expectedModes = {
READ: 1,
WRITE: 2,
DELETE: 4,
AUTO_CREATE: 8
}
const maskProvider = MaskProvider.fromModesOrValues(expectedModes)
const {mask, modes, values} = maskProvider
// get the MaskDatum of a given permissions sum
const rda = mask.indexOf(modes.READ + modes.DELETE + modes.AUTO_CREATE)
// with an explicit number
const rda = mask.indexOf(13)
// from string values
const rda = mask.fromValues(['READ', 'DELETE', 'AUTO_CREATE'])
// values are derived from modes
expect(values).to.deep.equal(Object.keys(expectedModes))
```
### rda output from usage examples
```json
{
"sum": 13,
"values": ["READ", "DELETE", "AUTO_CREATE"],
"nums": [1, 4, 8],
"map": {
"READ": 1,
"DELETE": 4,
"AUTO_CREATE": 8
}
}
```