UNPKG

string-left-right

Version:

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

19 lines (17 loc) 7.6 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/} */ "use strict";var stringLeftRight=(()=>{var y=Object.defineProperty;var F=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols;var I=Object.prototype.hasOwnProperty,J=Object.prototype.propertyIsEnumerable;var T=(t,e,n)=>e in t?y(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n,g=(t,e)=>{for(var n in e||(e={}))I.call(e,n)&&T(t,n,e[n]);if(C)for(var n of C(e))J.call(e,n)&&T(t,n,e[n]);return t};var x=(t,e)=>{for(var n in e)y(t,n,{get:e[n],enumerable:!0})},B=(t,e,n,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of M(e))!I.call(t,l)&&l!==n&&y(t,l,{get:()=>e[l],enumerable:!(r=F(e,l))||r.enumerable});return t};var q=t=>B(y({},"__esModule",{value:!0}),t);var X={};x(X,{chompLeft:()=>Z,chompRight:()=>Y,left:()=>v,leftSeq:()=>j,leftStopAtNewLines:()=>G,leftStopAtRawNbsp:()=>K,right:()=>E,rightSeq:()=>U,rightStopAtNewLines:()=>Q,rightStopAtRawNbsp:()=>z,version:()=>_});var ne=new Set([..."\u060B$\u20BC\u17DB\xA5\u20A1\u20B1\xA3\u20AC\xA2\u20B9\uFDFC\u20AA\u20A9\u20AD\u20A8\u20AE\u20A6\u20BD\u20AB\u0E3F\u20A9\u20BA\u20B4","$U","$b","B/.","BZ$","Br","Bs","C$","CHF","Ft","Gs","J$","KM","K\u010D","L","MT","NT$","P","Q","R","R$","RD$","RM","Rp","S","S/.","TT$","Z$","kn","kr","lei","z\u0142","\u0192","\u0414\u0438\u043D.","\u0434\u0435\u043D","\u043B\u0432","\u062F.\u0625","Lek"]);function h(t){if(t==null||typeof t!="object")return!1;let e=Object.getPrototypeOf(t);return e!==null&&e!==Object.prototype&&Object.getPrototypeOf(e)!==null?!1:!(Symbol.iterator in t)&&!(Symbol.toStringTag in t)}function d(t){return typeof t=="string"}function A(t){return Number.isSafeInteger(t)&&t>=0}var k="6.2.0";var _=k,b="\xA0";function H(t){let e={value:t,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 R({str:t,idx:e=0,stopAtNewlines:n=!1,stopAtRawNbsp:r=!1}){if(typeof t!="string"||!t.length||((!e||typeof e!="number")&&(e=0),!t[e+1]))return null;if(t[e+1]&&(t[e+1].trim()||n&&"\n\r".includes(t[e+1])||r&&t[e+1]===b))return e+1;if(t[e+2]&&(t[e+2].trim()||n&&"\n\r".includes(t[e+2])||r&&t[e+2]===b))return e+2;for(let l=e+1,s=t.length;l<s;l++)if(t[l].trim()||n&&"\n\r".includes(t[l])||r&&t[l]===b)return l;return null}function E(t,e=0){return R({str:t,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!1})}function Q(t,e){return R({str:t,idx:e,stopAtNewlines:!0,stopAtRawNbsp:!1})}function z(t,e){return R({str:t,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!0})}function V({str:t,idx:e,stopAtNewlines:n,stopAtRawNbsp:r}){if(typeof t!="string"||!t.length||((!e||typeof e!="number")&&(e=0),e<1))return null;if(t[~-e]&&(t[~-e].trim()||n&&"\n\r".includes(t[~-e])||r&&t[~-e]===b))return~-e;if(t[e-2]&&(t[e-2].trim()||n&&"\n\r".includes(t[e-2])||r&&t[e-2]===b))return e-2;for(let l=e;l--;)if(t[l]&&(t[l].trim()||n&&"\n\r".includes(t[l])||r&&t[l]===b))return l;return null}function v(t,e=0){return V({str:t,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!1})}function G(t,e){return V({str:t,idx:e,stopAtNewlines:!0,stopAtRawNbsp:!1})}function K(t,e){return V({str:t,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!0})}function L(t,e,n,r,l){if(typeof e!="string"||!e.length||(typeof n!="number"&&(n=0),t==="right"&&!e[n+1]||t==="left"&&!e[~-n]))return null;let s=n,i=[],f,p,m,o=0;for(;o<l.length;){if(!d(l[o])||!l[o].length){o+=1;continue}let{value:u,optional:c,hungry:S}=H(l[o]),a=t==="right"?E(e,s):v(e,s),D=e[a];if(D!==void 0&&(r.i&&D.toLowerCase()===u.toLowerCase()||!r.i&&D===u)){let W=t==="right"?E(e,a):v(e,a),N=!1;if(S){let O=e[W];N=O!==void 0&&(r.i&&O.toLowerCase()===u.toLowerCase()||!r.i&&O===u)}N?m=!0:o+=1,typeof a=="number"&&t==="right"&&a>s+1?i.push([s+1,a]):t==="left"&&typeof a=="number"&&a<~-s&&i.unshift([a+1,s]),s=a,t==="right"?(f===void 0&&(f=a),p=a):(p===void 0&&(p=a),f=a)}else if(c)o+=1;else if(m)o+=1,m=void 0;else return null}return f===void 0||p===void 0?null:{gaps:i,leftmostChar:f,rightmostChar:p}}var w={i:!1};function j(t,e,...n){if(!(n!=null&&n.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 r;return h(n[0])?r=g(g({},w),n.shift()):r=w,L("left",t,e,r,Array.from(n).reverse())}function U(t,e,...n){if(!(n!=null&&n.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 r;return h(n[0])?r=g(g({},w),n.shift()):r=w,L("right",t,e,r,n)}function $(t,e,n,r,l=[]){var p,m;if(typeof e!="string"||!e.length||((!n||typeof n!="number")&&(n=0),t==="right"&&!e[n+1]||t==="left"&&+n==0))return null;let s=null,i=null;do s=t==="right"?U(e,typeof i=="number"?i:n,...l):j(e,typeof i=="number"?i:n,...l),s!==null&&(i=t==="right"?s.rightmostChar:s.leftmostChar);while(s);if(i!=null&&t==="right"&&(i+=1),i===null)return null;if(t==="right"){if((p=e[i])!=null&&p.trim())return i;let o=E(e,i);if(!r||r.mode===0){if(o===i+1)return i;if(e.slice(i,o||e.length).trim()||e.slice(i,o||e.length).includes("\n")||e.slice(i,o||e.length).includes("\r")){for(let u=i,c=e.length;u<c;u++)if("\n\r".includes(e[u]))return u}else return o?~-o:e.length}else{if(r.mode===1)return i;if(r.mode===2){let u=e.slice(i);if(u.trim()||u.includes("\n")||u.includes("\r")){for(let c=i,S=e.length;c<S;c++)if(e[c].trim()||"\n\r".includes(e[c]))return c}return e.length}}return o||e.length}if(e[i]&&((m=e[~-i])!=null&&m.trim()))return i;let f=v(e,i);if(!r||r.mode===0){if(f===i-2)return i;if(e.slice(0,i).trim()||e.slice(0,i).includes("\n")||e.slice(0,i).includes("\r")){for(let o=i;o--;)if("\n\r".includes(e[o])||e[o].trim())return o+1+(e[o].trim()?1:0)}return 0}if(r.mode===1)return i;if(r.mode===2){let o=e.slice(0,i);if(o.trim()||o.includes("\n")||o.includes("\r")){for(let u=i;u--;)if(e[u].trim()||"\n\r".includes(e[u]))return u+1}return 0}return f!==null?f+1:0}function Z(t,e,...n){if(!n.length||n.length===1&&h(n[0]))return null;let r={mode:0};if(h(n[0])){let l=g(g({},r),n[0]);if(!l.mode)l.mode=0;else if(d(l.mode)&&l.mode.length===1&&"0123".includes(l.mode))l.mode=+l.mode;else if(!A(l.mode)||l.mode<0||l.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 ".concat(l.mode," (type ").concat(typeof l.mode,")"));return $("left",t,e,l,n.slice(1))}return d(n[0])?$("left",t,e,r,n):$("left",t,e,r,n.slice(1))}function Y(t,e,...n){if(!n.length||n.length===1&&h(n[0]))return null;let r={mode:0};if(h(n[0])){let l=g(g({},r),n[0]);if(!l.mode)l.mode=0;else if(d(l.mode)&&/^[0-3]$/.test(l.mode))l.mode=Number.parseInt(l.mode,10);else if(!A(l.mode)||l.mode<0||l.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 ".concat(l.mode," (type ").concat(typeof l.mode,")"));return $("right",t,e,l,n.slice(1))}return d(n[0])?$("right",t,e,r,n):$("right",t,e,r,n.slice(1))}return q(X);})(); /** * @name codsen-utils * @fileoverview Various utility functions * @version 1.8.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/codsen-utils/} */