2.2.1 • Published 4 years ago
mode-mask v2.2.1
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
}
}