UNPKG

@visulima/string

Version:

Functions for manipulating strings.

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