Skip to content

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.

License

Notifications You must be signed in to change notification settings

lxghtless/mode-mask

Repository files navigation

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

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

import {MaskProvider} from 'mode-mask'

const modes = {
    READ: 1,
    WRITE: 2,
    DELETE: 4,
    AUTO_CREATE: 8
}

// build mask with provider with a map of <string, number>
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

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

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

{
    "sum": 13,
    "values": ["READ", "DELETE", "AUTO_CREATE"],
    "nums": [1, 4, 8],
    "map": {
        "READ": 1,
        "DELETE": 4,
        "AUTO_CREATE": 8
    }
}

About

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.

Topics

Resources

License

Stars

Watchers

Forks