Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
2 lines (2 loc) · 2.41 KB
/
Copy pathindex.js
File metadata and controls
2 lines (2 loc) · 2.41 KB
1
2
"use strict";const z=t=>{if(!t)return[];if(typeof t!="string"||t.match(/^\s+$/))return[];const o=t.split(`
`);if(o.length===0)return[];const c=[];let i=null,d=null,g=0,f=0,l=null;const F=e=>{l&&(d?.changes.push({type:"normal",normal:!0,ln1:g++,ln2:f++,content:e}),l.oldLines--,l.newLines--)},h=e=>{const[r,n]=q(e)??[],s={chunks:[],deletions:0,additions:0,from:r,to:n};return i=s,c.push(s),s},a=()=>!i||i.chunks.length?h():i,H=(e,r)=>{const n=a();n.new=!0,n.newMode=r[1],n.from="/dev/null"},$=(e,r)=>{const n=a();n.deleted=!0,n.oldMode=r[1],n.to="/dev/null"},y=(e,r)=>{a().oldMode=r[1]},N=(e,r)=>{a().newMode=r[1]},k=(e,r)=>{const n=a();n.index=e.split(" ").slice(1),r[1]&&(n.oldMode=n.newMode=r[1].trim())},R=e=>{a().from=b(e)},S=e=>{a().to=b(e)},u=e=>+(e||1),M=(e,r)=>{const n=i??h(e),[s,m,p,x]=r.slice(1);g=+s,f=+p,d={content:e,changes:[],oldStart:+s,oldLines:u(m),newStart:+p,newLines:u(x)},l={oldLines:u(m),newLines:u(x)},n.chunks.push(d)},E=e=>{!d||!i||!l||(d.changes.push({type:"del",del:!0,ln:g++,content:e}),i.deletions++,l.oldLines--)},A=e=>{!d||!i||!l||(d.changes.push({type:"add",add:!0,ln:f++,content:e}),i.additions++,l.newLines--)},w=e=>{if(!d)return;const[r]=d.changes.slice(-1),{type:n,ln1:s,ln2:m,ln:p}=r;d.changes.push({type:n,[n]:!0,ln1:s,ln2:m,ln:p,content:e})},T=[[/^diff\s/,h],[/^new file mode (\d+)$/,H],[/^deleted file mode (\d+)$/,$],[/^old mode (\d+)$/,y],[/^new mode (\d+)$/,N],[/^index\s[\da-zA-Z]+\.\.[\da-zA-Z]+(\s(\d+))?$/,k],[/^---\s/,R],[/^\+\+\+\s/,S],[/^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s@@/,M],[/^\\ No newline at end of file$/,w]],v=[[/^\\ No newline at end of file$/,w],[/^-/,E],[/^\+/,A],[/^\s*/,F]],_=e=>{for(const[r,n]of v){const s=e.match(r);if(s){n(e,s);break}}l?.oldLines===0&&l.newLines===0&&(l=null)},D=e=>{for(const[r,n]of T){const s=e.match(r);if(s){n(e,s);break}}},O=e=>{l?_(e):D(e)};for(const e of o)O(e);return c},Z=/(a|i|w|c|o|1|2)\/.*(?=["']? ["']?(b|i|w|c|o|1|2)\/)|(b|i|w|c|o|1|2)\/.*$/g,L=/^(a|b|i|w|c|o|1|2)\//,q=t=>t?.match(Z)?.map(c=>c.replace(L,"").replace(/("|')$/,"")),j=/^\\?['"]|\\?['"]$/g,b=t=>{let o=B(t,"-+").trim();return o=I(o),o.replace(j,"").replace(L,"")},B=(t,o)=>{const c=J(o);return C(t).replace(new RegExp(`^${c}+`),"")},G=/\t.*|\d{4}-\d\d-\d\d\s\d\d:\d\d:\d\d(.\d+)?\s(\+|-)\d\d\d\d/,I=t=>{const o=G.exec(t);return o&&(t=t.substring(0,o.index).trim()),t},J=t=>t instanceof RegExp?t.source:`[${C(t).replace(/([.*+?^=!:${}()|[\]/\\])/g,"\\$1")}]`,C=t=>`${t??""}`;module.exports=z;