https://github.com/bytesleo/role-calc
library to work with roles, contains calculation of times for the session, validation of required roles
https://github.com/bytesleo/role-calc
has-role role role-calc time-session
Last synced: about 1 year ago
JSON representation
library to work with roles, contains calculation of times for the session, validation of required roles
- Host: GitHub
- URL: https://github.com/bytesleo/role-calc
- Owner: bytesleo
- License: mit
- Created: 2017-09-04T05:43:42.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-02-24T18:39:46.000Z (about 8 years ago)
- Last Synced: 2024-10-24T10:10:06.172Z (over 1 year ago)
- Topics: has-role, role, role-calc, time-session
- Language: JavaScript
- Size: 44.9 KB
- Stars: 4
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# role-calc
[](https://npmjs.org/package/role-calc) [](https://travis-ci.org/kevoj/role-calc) [](https://david-dm.org/kevoj/role-calc) [](https://david-dm.org/kevoj/role-calc?type=dev)
[](https://raw.githubusercontent.com/kevoj/role-calc/master/LICENSE)
> library to work with roles, contains calculation of times for the session, validation of required roles
This library is ideal for working with [redis-jwt](https://github.com/kevoj/redis-jwt)
## Installation
Npm
```bash
npm install role-calc --save
```
Yarn
```bash
yarn add role-calc
```
## Usage
### has
Evaluate if any of the required roles are met or all
```javascript
import {has} from 'role-calc';
// Roles required
let required = [
'Developer',
'Specialist',
'Technician',
'Director',
'Manager',
'Designer',
'Analyst',
'Administrator'
];
// Roles user
let candidates = [
'Developer',
'Administrator',
'Designer'
];
// if it contains one
has(required, candidates); // -> true
has(['Developer'], candidates); // -> true
has(required, ['Other', 'Fake']); // -> false
// must meet everyone '*'
has(required, candidates, '*'); // -> false
has(['Developer', 'Designer'], candidates, '*'); // -> true
has(['Director'], ['Director', 'Other'], '*'); // -> true
```
### time
Gets the times of a list from an array of roles
```javascript
import {time} from 'role-calc';
// Roles with time
let roles = [
{
role: 'Developer',
ttl: '15 minutes'
},
{
role: 'Specialist',
ttl: '2 hours'
},
{
role: 'Manager',
ttl: '3 days'
},
{
role: 'Administrator',
ttl: '1 years'
},
{
role: 'Director',
ttl: '90 minutes'
},
{
role: 'Designer',
ttl: '55 hours'
},
{
role: 'other'
}
];
// Roles user
let candidates = [
'Developer',
'Administrator',
'Designer'
];
time(roles, candidates);
/*
->
[
{ role: 'Developer', ttl: '15 minutes' },
{ role: 'Administrator', ttl: '1 years' },
{ role: 'Designer', ttl: '55 hours' }
]
*/
```
### calc
Calculate time of multiple roles assigned to a user
```javascript
import {calc} from 'role-calc';
// Roles with time
let roles = [
{
role: 'Developer',
ttl: '15 minutes'
},
{
role: 'Specialist',
ttl: '2 hours'
},
{
role: 'Manager',
ttl: '3 days'
},
{
role: 'Administrator',
ttl: '1 years'
},
{
role: 'Director',
ttl: '90 minutes'
},
{
role: 'Designer',
ttl: '55 hours'
},
{
role: 'other'
}
];
/*
time(roles, option, format)
- roles: array
- option: max, min, sum, average
- format(optional): milliseconds, seconds, minutes, hours, days, weeks, months, years
*/
calc(roles, 'sum'); // -> 533805 minutes
calc(roles, 'max', 'hours'); // -> 8766 hours
calc(roles, 'min', 'minutes'); // -> 15 minutes
calc(roles, 'average', 'days'); // -> 61.782986111111114 days
```
## Development
### Start
`npm start`
### Compile
`npm run compile`
### Test
`npm test`
## License
MIT © [Leonardo Rico](https://github.com/kevoj/role-calc/blob/master/LICENSE)