UNPKG

string-left-right

Version:

Looks up the first non-whitespace character to the left/right of a given index

25 lines (23 loc) 5.91 kB
/** * @name string-left-right * @fileoverview Looks up the first non-whitespace character to the left/right of a given index * @version 6.2.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-left-right/} */ import{isInt as R,isPlainObject as h,isStr as p}from"codsen-utils";var O="6.2.0";var j=O,$="\xA0";function L(n){let e={value:n,hungry:!1,optional:!1};return(e.value.endsWith("?*")||e.value.endsWith("*?"))&&e.value.length>2?(e.value=e.value.slice(0,e.value.length-2),e.optional=!0,e.hungry=!0):e.value.endsWith("?")&&e.value.length>1?(e.value=e.value.slice(0,~-e.value.length),e.optional=!0):e.value.endsWith("*")&&e.value.length>1&&(e.value=e.value.slice(0,~-e.value.length),e.hungry=!0),e}function V({str:n,idx:e=0,stopAtNewlines:l=!1,stopAtRawNbsp:o=!1}){if(typeof n!="string"||!n.length||((!e||typeof e!="number")&&(e=0),!n[e+1]))return null;if(n[e+1]&&(n[e+1].trim()||l&&` \r`.includes(n[e+1])||o&&n[e+1]===$))return e+1;if(n[e+2]&&(n[e+2].trim()||l&&` \r`.includes(n[e+2])||o&&n[e+2]===$))return e+2;for(let t=e+1,m=n.length;t<m;t++)if(n[t].trim()||l&&` \r`.includes(n[t])||o&&n[t]===$)return t;return null}function y(n,e=0){return V({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!1})}function k(n,e){return V({str:n,idx:e,stopAtNewlines:!0,stopAtRawNbsp:!1})}function W(n,e){return V({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!0})}function N({str:n,idx:e,stopAtNewlines:l,stopAtRawNbsp:o}){if(typeof n!="string"||!n.length||((!e||typeof e!="number")&&(e=0),e<1))return null;if(n[~-e]&&(n[~-e].trim()||l&&` \r`.includes(n[~-e])||o&&n[~-e]===$))return~-e;if(n[e-2]&&(n[e-2].trim()||l&&` \r`.includes(n[e-2])||o&&n[e-2]===$))return e-2;for(let t=e;t--;)if(n[t]&&(n[t].trim()||l&&` \r`.includes(n[t])||o&&n[t]===$))return t;return null}function D(n,e=0){return N({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!1})}function _(n,e){return N({str:n,idx:e,stopAtNewlines:!0,stopAtRawNbsp:!1})}function M(n,e){return N({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!0})}function w(n,e,l,o,t){if(typeof e!="string"||!e.length||(typeof l!="number"&&(l=0),n==="right"&&!e[l+1]||n==="left"&&!e[~-l]))return null;let m=l,u=[],f,i,s,r=0;for(;r<t.length;){if(!p(t[r])||!t[r].length){r+=1;continue}let{value:a,optional:T,hungry:v}=L(t[r]),g=n==="right"?y(e,m):D(e,m),d=e[g];if(d!==void 0&&(o.i&&d.toLowerCase()===a.toLowerCase()||!o.i&&d===a)){let I=n==="right"?y(e,g):D(e,g),S=!1;if(v){let E=e[I];S=E!==void 0&&(o.i&&E.toLowerCase()===a.toLowerCase()||!o.i&&E===a)}S?s=!0:r+=1,typeof g=="number"&&n==="right"&&g>m+1?u.push([m+1,g]):n==="left"&&typeof g=="number"&&g<~-m&&u.unshift([g+1,m]),m=g,n==="right"?(f===void 0&&(f=g),i=g):(i===void 0&&(i=g),f=g)}else if(T)r+=1;else if(s)r+=1,s=void 0;else return null}return f===void 0||i===void 0?null:{gaps:u,leftmostChar:f,rightmostChar:i}}var b={i:!1};function A(n,e,...l){if(!l?.length)throw new Error("string-left-right/leftSeq(): [THROW_ID_01] only two input arguments were passed! Did you intend to use left() method instead?");let o;return h(l[0])?o={...b,...l.shift()}:o=b,w("left",n,e,o,Array.from(l).reverse())}function U(n,e,...l){if(!l?.length)throw new Error("string-left-right/rightSeq(): [THROW_ID_02] only two input arguments were passed! Did you intend to use right() method instead?");let o;return h(l[0])?o={...b,...l.shift()}:o=b,w("right",n,e,o,l)}function c(n,e,l,o,t=[]){if(typeof e!="string"||!e.length||((!l||typeof l!="number")&&(l=0),n==="right"&&!e[l+1]||n==="left"&&+l==0))return null;let m=null,u=null;do m=n==="right"?U(e,typeof u=="number"?u:l,...t):A(e,typeof u=="number"?u:l,...t),m!==null&&(u=n==="right"?m.rightmostChar:m.leftmostChar);while(m);if(u!=null&&n==="right"&&(u+=1),u===null)return null;if(n==="right"){if(e[u]?.trim())return u;let i=y(e,u);if(!o||o.mode===0){if(i===u+1)return u;if(e.slice(u,i||e.length).trim()||e.slice(u,i||e.length).includes(` `)||e.slice(u,i||e.length).includes("\r")){for(let s=u,r=e.length;s<r;s++)if(` \r`.includes(e[s]))return s}else return i?~-i:e.length}else{if(o.mode===1)return u;if(o.mode===2){let s=e.slice(u);if(s.trim()||s.includes(` `)||s.includes("\r")){for(let r=u,a=e.length;r<a;r++)if(e[r].trim()||` \r`.includes(e[r]))return r}return e.length}}return i||e.length}if(e[u]&&e[~-u]?.trim())return u;let f=D(e,u);if(!o||o.mode===0){if(f===u-2)return u;if(e.slice(0,u).trim()||e.slice(0,u).includes(` `)||e.slice(0,u).includes("\r")){for(let i=u;i--;)if(` \r`.includes(e[i])||e[i].trim())return i+1+(e[i].trim()?1:0)}return 0}if(o.mode===1)return u;if(o.mode===2){let i=e.slice(0,u);if(i.trim()||i.includes(` `)||i.includes("\r")){for(let s=u;s--;)if(e[s].trim()||` \r`.includes(e[s]))return s+1}return 0}return f!==null?f+1:0}function H(n,e,...l){if(!l.length||l.length===1&&h(l[0]))return null;let o={mode:0};if(h(l[0])){let t={...o,...l[0]};if(!t.mode)t.mode=0;else if(p(t.mode)&&t.mode.length===1&&"0123".includes(t.mode))t.mode=+t.mode;else if(!R(t.mode)||t.mode<0||t.mode>3)throw new Error(`string-left-right/chompLeft(): [THROW_ID_03] the opts.mode is wrong! It should be 0, 1, 2 or 3. It was given as ${t.mode} (type ${typeof t.mode})`);return c("left",n,e,t,l.slice(1))}return p(l[0])?c("left",n,e,o,l):c("left",n,e,o,l.slice(1))}function P(n,e,...l){if(!l.length||l.length===1&&h(l[0]))return null;let o={mode:0};if(h(l[0])){let t={...o,...l[0]};if(!t.mode)t.mode=0;else if(p(t.mode)&&/^[0-3]$/.test(t.mode))t.mode=Number.parseInt(t.mode,10);else if(!R(t.mode)||t.mode<0||t.mode>3)throw new Error(`string-left-right/chompRight(): [THROW_ID_04] the opts.mode is wrong! It should be 0, 1, 2 or 3. It was given as ${t.mode} (type ${typeof t.mode})`);return c("right",n,e,t,l.slice(1))}return p(l[0])?c("right",n,e,o,l):c("right",n,e,o,l.slice(1))}export{H as chompLeft,P as chompRight,D as left,A as leftSeq,_ as leftStopAtNewLines,M as leftStopAtRawNbsp,y as right,U as rightSeq,k as rightStopAtNewLines,W as rightStopAtRawNbsp,j as version};