UNPKG

commitlint-plugin-spend

Version:

A commitlint plugin that requires the GitLab /spend or /spend_time directive in commit messages

3 lines (2 loc) 1.71 kB
var q=["/spend","/spend_time"],S=new RegExp(`^(${q.join("|")})$`);var x=["y","mo","w","d","h","m"];var G={y:99,mo:11,w:4,d:6,h:23,m:59},j=new RegExp(`^(\\d{1,2})(${x.join("|")})$`);function z(E){return j.test(E)}var A=(E,D)=>{if(D==="never")return[!0,void 0];let{body:R}=E;if(!R)return[!1,"Commit message must contain a body (with a spend directive)"];let _=R.split(` `).filter((o)=>q.some((c)=>o.startsWith(c)));if(_.length!==1||!_[0])return[!1,`Commit message body must contain a single spend directive, i.e., one of the following: ${q.join(", ")}. Found ${_.length} spend directives.`];let $=_[0].split(" ");if($.length<2)return[!1,`Spend directive must contain at least one time value. A time value must follow the RegEx pattern: ${j.source}`];let F=$.slice(1),k=[];for(let o of F){if(!z(o))return[!1,`The time value "${o}" is not a valid time value. A time value must follow the RegEx pattern: ${j.source}`];k.push(o)}let p=[];for(let o of k){let c=/^(\d+)/.exec(o)?.[1];if(!c)return[!1,`The time value "${o}" does not contain a numeric part. A time value must follow the RegEx pattern: ${j.source}`];let f=Math.abs(parseInt(c,10)),h=o.replace(c,"");if(f>G[h])return[!1,`The time value "${o}" exceeds the maximum value for "${h}" (max value: ${G[h]})`];p.push(h)}if(new Set(p).size!==p.length)return[!1,`Duplicate time units found in the spend directive: ${[...p].join(", ")}`];let b=p.map((o)=>x.indexOf(o));if(!b.every((o,c)=>{if(c===0)return!0;let f=b[c-1];return f&&o>=f}))return[!1,`Time values are not in the correct order. Time values must be ordered from largest to smallest unit: ${x.join(", ")}. Found: ${p.join(", ")}`];return[!0,void 0]};var H={rules:{spend:A}},M=H;export{M as default};