UNPKG

unix-permissions

Version:
116 lines (78 loc) 1.86 kB
import isPlainObj from"is-plain-obj"; import{SHORT_CATEGORIES}from"../../constants.js"; import{mapValues}from"../../utils.js"; import{ PARSE_SPECIAL, SHORT_PERMISSIONS, SPECIAL_CATEGORY}from "./constants.js"; export const parse=(object)=>{ if(!isPlainObj(object)){ return } const objectA=parseAll(object); const nodes=Object.entries(objectA).flatMap(parsePermissions); return validateNodes({nodes}) }; const parseAll=({all,...object})=>{ if(all===undefined){ return object } const objectA=mapValues(SHORT_CATEGORIES,(value,category)=>({ ...all, ...object[category] })); return{...object,...objectA} }; const parsePermissions=([category,permissions])=>{ if(!isPlainObj(permissions)){ return } const categoryA=getCategory({category}); if(categoryA===undefined){ return } const nodes=Object.entries(permissions). filter(hasDefinedValue). map(([permission,add])=> parsePermission({category:categoryA,permission,add}) ); return validateNodes({nodes}) }; const getCategory=({category})=>{ if(category===SPECIAL_CATEGORY){ return category } return SHORT_CATEGORIES[category] }; const hasDefinedValue=([,value])=>value!==undefined; const parsePermission=({category,permission,add})=>{ if(typeof add!=="boolean"){ return } if(category===SPECIAL_CATEGORY){ return parseSpecialPerm({permission,add}) } return parseNormalPerm({category,permission,add}) }; const parseSpecialPerm=({permission,add})=>{ const specialNode=PARSE_SPECIAL[permission]; if(specialNode===undefined){ return } return{...specialNode,add} }; const parseNormalPerm=({category,permission,add})=>{ const permissionA=SHORT_PERMISSIONS[permission]; if(permissionA===undefined){ return } return{category,permission:permissionA,add} }; const validateNodes=({nodes})=>{ if(nodes.some(isInvalidNode)){ return } return nodes }; const isInvalidNode=(node)=>node===undefined;