An open API service indexing awesome lists of open source software.

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.

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
}
}
```