unix-permissions
Version:
Swiss Army knife for Unix permissions
61 lines (29 loc) • 788 B
JavaScript
export const tokenize=(stat)=>{
if(typeof stat!=="string"){
return
}
const tokens=STAT_REGEXP.exec(stat);
if(tokens===null){
return
}
const[u,g,o]=tokens.slice(1).map(removeDashes).map(expandSpecial);
return{u,g,o}
};
const STAT_REGEXP=
/^\s*[-dlpscbD]?\s*([-rwxXsS]{3})\s*([-rwxXsS]{3})\s*([-rwxXtT]{3})\s*$/u;
const removeDashes=(part)=>part.replaceAll("-","");
const expandSpecial=(part)=>EXPAND_REGEXPS.reduce(specialReduce,part);
const EXPAND_REGEXPS=[
[/X/gu,"x"],
[/s/gu,"xs"],
[/S/gu,"s"],
[/t/gu,"xt"],
[/T/gu,"t"]];
export const contractSpecial=(part)=>
CONTRACT_STRINGS.reduce(specialReduce,part);
const CONTRACT_STRINGS=[
["-t","T"],
["xt","t"],
["-s","S"],
["xs","s"]];
const specialReduce=(part,[before,after])=>part.replaceAll(before,after);