Created
January 18, 2020 22:56
-
-
Save komakino/a065692f15170b01cbf8caa5fd09d341 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function subsetSum(arr, val, i = 0) { | |
let r = [] | |
while (i < arr.length) { | |
let v = arr[i] | |
if (v == val) | |
r.push(v) | |
if (v < val) | |
subsetSum(arr, val - v, i + 1).forEach((item) => { | |
r.push(v + ',' + item) | |
}) | |
i++ | |
} | |
return r | |
} | |
| |
export const ROLE_ALPHA = 1 | |
export const ROLE_BETA = 2 | |
export const ROLE_GAMMA = 4 | |
export const ROLE_DELTA = 8 | |
export const ROLE_EPSILON = 16 | |
| |
let roleNames= {} | |
| |
function fetchRoleName(roleId) { | |
return roleNames[roleId] | |
} | |
| |
roleNames[ROLE_ALPHA] = 'Alpha' | |
roleNames[ROLE_BETA] = 'Bravo' | |
roleNames[ROLE_GAMMA] = 'Charlie' | |
roleNames[ROLE_DELTA] = 'Delta' | |
roleNames[ROLE_EPSILON] = 'Echo' | |
export default function (roles) { | |
let roleName = '' | |
let roles_arr = [ | |
ROLE_ALPHA, | |
ROLE_BETA, | |
ROLE_GAMMA, | |
ROLE_DELTA, | |
ROLE_EPSILON | |
] | |
let roleIds = subsetSum(roles_arr, roles) | |
if (typeof roleIds[0].length !== 'undefined' && roleIds[0].length > 0) { | |
let roleIdsArr = roleIds[0].split(',') | |
roleIdsArr.forEach((item) => { | |
roleName = roleName + fetchRoleName(item) + ', ' | |
}) | |
} else { | |
roleName = fetchRoleName(roleIds[0]) | |
} | |
return roleName | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment