UNPKG

@visulima/string

Version:

Functions for manipulating strings.

2 lines (1 loc) 5.22 kB
var R=Object.defineProperty;var k=(t,l)=>R(t,"name",{value:l,configurable:!0});import{a as v,b as L,c as O}from"./packem_shared/constants-CDcaCZKC.mjs";import{getStringWidth as I}from"./get-string-width.mjs";import N from"./packem_shared/LRUCache-Bjo6gsbC.mjs";var j=Object.defineProperty,b=k((t,l)=>j(t,"name",{value:l,configurable:!0}),"y");const _=new N(100),F=new Intl.Segmenter("en",{granularity:"grapheme"}),M=new Set(["0","22","23","24","27","28","29","39","49"]),P=new Set(["1","3","4","7","8","9"]),y=new Map([["22","1"],["23","3"],["24","4"],["27","7"],["28","8"],["29","9"]]),S=b(t=>t>="30"&&t<="37"||t>="90"&&t<="97"||t.startsWith("38;"),"isForegroundColor"),w=b(t=>t>="40"&&t<="47"||t>="100"&&t<="107"||t.startsWith("48;"),"isBackgroundColor"),V=[{close:"\x1B[22m",open:"\x1B[1m"},{close:"\x1B[23m",open:"\x1B[3m"},{close:"\x1B[24m",open:"\x1B[4m"},{close:"\x1B[27m",open:"\x1B[7m"},{close:"\x1B[28m",open:"\x1B[8m"},{close:"\x1B[29m",open:"\x1B[9m"}],A=b((t,l)=>{let c=-1;for(const d of l){const h=t.indexOf(d);h!==-1&&(c===-1||h<c)&&(c=h)}return c},"findFirstPositionOfAny"),$=b((t,l)=>{if(!t.includes("\x1B"))return[{after:"",before:"",content:t,visibleLength:I(t,l.width)}];const c=_.get(t);if(c)return c;const d=[],h=[];let g=0,m;for(O.lastIndex=0;(m=O.exec(t))!==null;){const f=m[0];d.push(t.slice(g,m.index)),h.push(f),g=m.index+f.length}g<t.length&&d.push(t.slice(g));const a=[];for(const f of h){if(f==="\x1B[0m"||f==="\x1B]8;;\x07"){a.push(f);continue}if(f.startsWith("\x1B[")&&f.endsWith("m")){const x=f.slice(2,-1);if(M.has(x)){a.push(f);continue}}}const s=new Set,B=[];for(const[f,x]of d.entries()){if(f>0&&h[f-1]){const n=h[f-1];if(n==="\x1B[0m")s.clear();else if(n==="\x1B]8;;\x07"){for(const i of s)if(i.startsWith("\x1B]8;;")&&!i.endsWith("\x1B]8;;\x07")){s.delete(i);break}}else if(n.startsWith("\x1B[")&&n.endsWith("m")){const i=n.slice(2,-1);if(i==="39"){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const o=e.slice(2,-1);if(S(o)){s.delete(e);break}}}else if(i==="49"){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const o=e.slice(2,-1);if(w(o)){s.delete(e);break}}}else if(y.has(i)){const e=y.get(i);for(const o of s)if(o===`\x1B[${e}m`){s.delete(o);break}}else{if(S(i)){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const o=e.slice(2,-1);if(S(o)){s.delete(e);break}}}else if(w(i)){for(const e of s)if(e.startsWith("\x1B[")&&e.endsWith("m")){const o=e.slice(2,-1);if(w(o)){s.delete(e);break}}}else if(P.has(i)){const e=`\x1B[${i}m`;s.has(e)&&s.delete(e)}s.add(n)}}else if(n.startsWith("\x1B]8;;")&&!n.endsWith("\x1B]8;;\x07")){for(const i of s)if(i.startsWith("\x1B]8;;")&&!i.endsWith("\x1B]8;;\x07")){s.delete(i);break}s.add(n)}}if(x==="")continue;let u="";if(s.size>0)if(a.includes("\x1B[0m"))u="\x1B[0m";else{const n=[];let i=!1;for(const r of s)if(r.startsWith("\x1B]8;;")&&!r.endsWith("\x1B]8;;\x07")){i=!0;break}i&&a.includes("\x1B]8;;\x07")&&n.push("\x1B]8;;\x07");let e=!1,o=!1;for(const r of s)if(r.startsWith("\x1B[")&&r.endsWith("m")){const W=r.slice(2,-1);S(W)?e=!0:w(W)&&(o=!0)}if(e&&o){const r=A(t,["\x1B[3","\x1B[9","\x1B[38;"]),W=A(t,["\x1B[4","\x1B[10","\x1B[48;"]);r>=0&&W>=0?r>W?n.push("\x1B[39m","\x1B[49m"):n.push("\x1B[49m","\x1B[39m"):n.push("\x1B[39m","\x1B[49m")}else e&&n.push("\x1B[39m"),o&&n.push("\x1B[49m");e&&o&&t.includes("\x1B[39m")&&t.includes("\x1B[49m")&&(t.indexOf("\x1B[39m")<t.indexOf("\x1B[49m")?(n.length-=e&&o?2:e||o?1:0,n.push("\x1B[39m","\x1B[49m")):t.indexOf("\x1B[49m")<t.indexOf("\x1B[39m")&&(n.length-=e&&o?2:e||o?1:0,n.push("\x1B[49m","\x1B[39m")));const E=V.filter(r=>s.has(r.open)).map(r=>({position:t.indexOf(r.open),style:r})).filter(r=>r.position>=0).sort((r,W)=>W.position-r.position);for(const r of E)a.includes(r.style.close)&&n.push(r.style.close);u=n.join("")}const p=[...s].join("");B.push({after:u,before:p,content:x,visibleLength:I(x,{fullWidth:1,wideWidth:1})})}return _.set(t,B),B},"processIntoStyledSegments"),C=b((t,l,c,d)=>{const h=[...d.segmenter.segment(t)],g=[];let m=0,a=!1;for(const s of h){const B=I(s.segment,d.width),f=m,x=m+B;if(!a)if(f<l){m=x;continue}else a=!0;if(f>=c||x>c)break;g.push(s.segment),m=x}return g.join("")},"fastSlice"),H=b((t,l=0,c=Number.MAX_SAFE_INTEGER,d={})=>{const h={segmenter:F,...d};if(l>=c||t==="")return"";const g=I(t,h.width);if(l===0&&c>=g)return t;if(l<0||c<0)throw new RangeError("Negative indices aren't supported");if(!t.includes("\x1B"))return/^[\u0000-\u007F]*$/.test(t)?t.slice(l,c):C(t,l,c,h);if(v.lastIndex=0,L.lastIndex=0,!v.test(t)&&!L.test(t))return t;const m=$(t,h);if(m.length===0)return"";let a=0;const s=[];for(const[x,u]of m.entries()){const p=a,n=a+u.visibleLength;if(n>l&&p<c){const i=Math.max(0,l-p),e=Math.min(u.visibleLength,c-p);s.push({end:e,index:x,segment:u,start:i})}a=n}if(s.length===0)return"";const B=[],f=s[0].segment;B.push(f.before);for(let x=0;x<s.length;x++){const{end:u,segment:p,start:n}=s[x];if(n===0&&u===p.visibleLength)B.push(p.content);else{const i=[...h.segmenter.segment(p.content)];B.push(i.slice(n,u).map(e=>e.segment).join(""))}if(x<s.length-1){const i=s[x+1].segment,e=p.after,o=i.before;e!==o&&(e||o)&&B.push(e,o)}}return B.push(s.at(-1).segment.after),B.join("")},"slice");export{H as slice};