UNPKG

string-left-right

Version:

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

19 lines (17 loc) 10.3 kB
/** * @name string-left-right * @fileoverview Looks up the first non-whitespace character to the left/right of a given index * @version 6.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-left-right/} */ "use strict";var stringLeftRight=(()=>{var H=Object.create;var O=Object.defineProperty;var W=Object.getOwnPropertyDescriptor;var x=Object.getOwnPropertyNames,C=Object.getOwnPropertySymbols,Q=Object.getPrototypeOf,I=Object.prototype.hasOwnProperty,_=Object.prototype.propertyIsEnumerable;var j=(n,e,t)=>e in n?O(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t,p=(n,e)=>{for(var t in e||(e={}))I.call(e,t)&&j(n,t,e[t]);if(C)for(var t of C(e))_.call(e,t)&&j(n,t,e[t]);return n};var K=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports),Z=(n,e)=>{for(var t in e)O(n,t,{get:e[t],enumerable:!0})},L=(n,e,t,u)=>{if(e&&typeof e=="object"||typeof e=="function")for(let l of x(e))!I.call(n,l)&&l!==t&&O(n,l,{get:()=>e[l],enumerable:!(u=W(e,l))||u.enumerable});return n};var F=(n,e,t)=>(t=n!=null?H(Q(n)):{},L(e||!n||!n.__esModule?O(t,"default",{value:n,enumerable:!0}):t,n)),z=n=>L(O({},"__esModule",{value:!0}),n);var A=K((ae,k)=>{"use strict";k.exports=G;function d(n){return n instanceof Buffer?Buffer.from(n):new n.constructor(n.buffer.slice(),n.byteOffset,n.length)}function G(n){if(n=n||{},n.circles)return Y(n);let e=new Map;if(e.set(Date,r=>new Date(r)),e.set(Map,(r,a)=>new Map(u(Array.from(r),a))),e.set(Set,(r,a)=>new Set(u(Array.from(r),a))),n.constructorHandlers)for(let r of n.constructorHandlers)e.set(r[0],r[1]);let t=null;return n.proto?m:l;function u(r,a){let o=Object.keys(r),s=new Array(o.length);for(let c=0;c<o.length;c++){let i=o[c],f=r[i];typeof f!="object"||f===null?s[i]=f:f.constructor!==Object&&(t=e.get(f.constructor))?s[i]=t(f,a):ArrayBuffer.isView(f)?s[i]=d(f):s[i]=a(f)}return s}function l(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return u(r,l);if(r.constructor!==Object&&(t=e.get(r.constructor)))return t(r,l);let a={};for(let o in r){if(Object.hasOwnProperty.call(r,o)===!1)continue;let s=r[o];typeof s!="object"||s===null?a[o]=s:s.constructor!==Object&&(t=e.get(s.constructor))?a[o]=t(s,l):ArrayBuffer.isView(s)?a[o]=d(s):a[o]=l(s)}return a}function m(r){if(typeof r!="object"||r===null)return r;if(Array.isArray(r))return u(r,m);if(r.constructor!==Object&&(t=e.get(r.constructor)))return t(r,m);let a={};for(let o in r){let s=r[o];typeof s!="object"||s===null?a[o]=s:s.constructor!==Object&&(t=e.get(s.constructor))?a[o]=t(s,m):ArrayBuffer.isView(s)?a[o]=d(s):a[o]=m(s)}return a}}function Y(n){let e=[],t=[],u=new Map;if(u.set(Date,o=>new Date(o)),u.set(Map,(o,s)=>new Map(m(Array.from(o),s))),u.set(Set,(o,s)=>new Set(m(Array.from(o),s))),n.constructorHandlers)for(let o of n.constructorHandlers)u.set(o[0],o[1]);let l=null;return n.proto?a:r;function m(o,s){let c=Object.keys(o),i=new Array(c.length);for(let f=0;f<c.length;f++){let h=c[f],g=o[h];if(typeof g!="object"||g===null)i[h]=g;else if(g.constructor!==Object&&(l=u.get(g.constructor)))i[h]=l(g,s);else if(ArrayBuffer.isView(g))i[h]=d(g);else{let w=e.indexOf(g);w!==-1?i[h]=t[w]:i[h]=s(g)}}return i}function r(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return m(o,r);if(o.constructor!==Object&&(l=u.get(o.constructor)))return l(o,r);let s={};e.push(o),t.push(s);for(let c in o){if(Object.hasOwnProperty.call(o,c)===!1)continue;let i=o[c];if(typeof i!="object"||i===null)s[c]=i;else if(i.constructor!==Object&&(l=u.get(i.constructor)))s[c]=l(i,r);else if(ArrayBuffer.isView(i))s[c]=d(i);else{let f=e.indexOf(i);f!==-1?s[c]=t[f]:s[c]=r(i)}}return e.pop(),t.pop(),s}function a(o){if(typeof o!="object"||o===null)return o;if(Array.isArray(o))return m(o,a);if(o.constructor!==Object&&(l=u.get(o.constructor)))return l(o,a);let s={};e.push(o),t.push(s);for(let c in o){let i=o[c];if(typeof i!="object"||i===null)s[c]=i;else if(i.constructor!==Object&&(l=u.get(i.constructor)))s[c]=l(i,a);else if(ArrayBuffer.isView(i))s[c]=d(i);else{let f=e.indexOf(i);f!==-1?s[c]=t[f]:s[c]=a(i)}}return e.pop(),t.pop(),s}}});var ie={};Z(ie,{chompLeft:()=>ue,chompRight:()=>se,left:()=>V,leftSeq:()=>M,leftStopAtNewLines:()=>le,leftStopAtRawNbsp:()=>oe,right:()=>S,rightSeq:()=>P,rightStopAtNewLines:()=>te,rightStopAtRawNbsp:()=>re,version:()=>ee});var J=F(A(),1);var fe=(0,J.default)();function b(n){if(n==null||typeof n!="object")return!1;let e=Object.getPrototypeOf(n);return e!==null&&e!==Object.prototype&&Object.getPrototypeOf(e)!==null?!1:!(Symbol.iterator in n)&&!(Symbol.toStringTag in n)}function y(n){return typeof n=="string"}function R(n){return Number.isSafeInteger(n)&&n>=0}var B=F(A(),1);var U="6.1.0";var $=(0,B.default)(),ee=U,E="\xA0";function ne(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:t=!1,stopAtRawNbsp:u=!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()||t&&"\n\r".includes(n[e+1])||u&&n[e+1]===E))return e+1;if(n[e+2]&&(n[e+2].trim()||t&&"\n\r".includes(n[e+2])||u&&n[e+2]===E))return e+2;for(let l=e+1,m=n.length;l<m;l++)if(n[l].trim()||t&&"\n\r".includes(n[l])||u&&n[l]===E)return l;return null}function S(n,e=0){return v({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!1})}function te(n,e){return v({str:n,idx:e,stopAtNewlines:!0,stopAtRawNbsp:!1})}function re(n,e){return v({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!0})}function T({str:n,idx:e,stopAtNewlines:t,stopAtRawNbsp:u}){if(typeof n!="string"||!n.length||((!e||typeof e!="number")&&(e=0),e<1))return null;if(n[~-e]&&(n[~-e].trim()||t&&"\n\r".includes(n[~-e])||u&&n[~-e]===E))return~-e;if(n[e-2]&&(n[e-2].trim()||t&&"\n\r".includes(n[e-2])||u&&n[e-2]===E))return e-2;for(let l=e;l--;)if(n[l]&&(n[l].trim()||t&&"\n\r".includes(n[l])||u&&n[l]===E))return l;return null}function V(n,e=0){return T({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!1})}function le(n,e){return T({str:n,idx:e,stopAtNewlines:!0,stopAtRawNbsp:!1})}function oe(n,e){return T({str:n,idx:e,stopAtNewlines:!1,stopAtRawNbsp:!0})}function q(n,e,t,u,l){if(typeof e!="string"||!e.length||(typeof t!="number"&&(t=0),n==="right"&&!e[t+1]||n==="left"&&!e[~-t]))return null;let m=t,r=[],a,o,s,c=0;for(;c<l.length;){if(!y(l[c])||!l[c].length){c+=1;continue}let{value:i,optional:f,hungry:h}=ne(l[c]),g=n==="right"?S(e,m):V(e,m);if(u.i&&e[g].toLowerCase()===i.toLowerCase()||!u.i&&e[g]===i){let w=n==="right"?S(e,g):V(e,g);h&&(u.i&&e[w].toLowerCase()===i.toLowerCase()||!u.i&&e[w]===i)?s=!0:c+=1,typeof g=="number"&&n==="right"&&g>m+1?r.push([m+1,g]):n==="left"&&typeof g=="number"&&g<~-m&&r.unshift([g+1,m]),m=g,n==="right"?(a===void 0&&(a=g),o=g):(o===void 0&&(o=g),a=g)}else if(f){c+=1;continue}else if(s){c+=1,s=void 0;continue}else return null}return a===void 0||o===void 0?null:{gaps:r,leftmostChar:a,rightmostChar:o}}var N={i:!1};function M(n,e,...t){if(!(t!=null&&t.length))throw new Error("string-left-right/leftSeq(): only two input arguments were passed! Did you intend to use left() method instead?");let u;return b(t[0])?u=p(p({},N),t.shift()):u=N,q("left",n,e,u,Array.from(t).reverse())}function P(n,e,...t){if(!(t!=null&&t.length))throw new Error("string-left-right/rightSeq(): only two input arguments were passed! Did you intend to use right() method instead?");let u;return b(t[0])?u=p(p({},N),t.shift()):u=N,q("right",n,e,u,t)}function D(n,e,t,u,l=[]){var o,s;if(typeof e!="string"||!e.length||((!t||typeof t!="number")&&(t=0),n==="right"&&!e[t+1]||n==="left"&&+t==0))return null;let m=null,r=null;do m=n==="right"?P(e,typeof r=="number"?r:t,...l):M(e,typeof r=="number"?r:t,...l),m!==null&&(r=n==="right"?m.rightmostChar:m.leftmostChar);while(m);if(r!=null&&n==="right"&&(r+=1),r===null)return null;if(n==="right"){if((o=e[r])!=null&&o.trim())return r;let c=S(e,r);if(!u||u.mode===0){if(c===r+1)return r;if(e.slice(r,c||e.length).trim()||e.slice(r,c||e.length).includes("\n")||e.slice(r,c||e.length).includes("\r")){for(let i=r,f=e.length;i<f;i++)if("\n\r".includes(e[i]))return i}else return c?~-c:e.length}else{if(u.mode===1)return r;if(u.mode===2){let i=e.slice(r);if(i.trim()||i.includes("\n")||i.includes("\r")){for(let f=r,h=e.length;f<h;f++)if(e[f].trim()||"\n\r".includes(e[f]))return f}return e.length}}return c||e.length}if(e[r]&&((s=e[~-r])!=null&&s.trim()))return r;let a=V(e,r);if(!u||u.mode===0){if(a===r-2)return r;if(e.slice(0,r).trim()||e.slice(0,r).includes("\n")||e.slice(0,r).includes("\r")){for(let c=r;c--;)if("\n\r".includes(e[c])||e[c].trim())return c+1+(e[c].trim()?1:0)}return 0}if(u.mode===1)return r;if(u.mode===2){let c=e.slice(0,r);if(c.trim()||c.includes("\n")||c.includes("\r")){for(let i=r;i--;)if(e[i].trim()||"\n\r".includes(e[i]))return i+1}return 0}return a!==null?a+1:0}function ue(n,e,...t){if(!t.length||t.length===1&&b(t[0]))return null;let u={mode:0};if(b(t[0])){let l=p(p({},u),$(t[0]));if(!l.mode)l.mode=0;else if(y(l.mode)&&l.mode.length===1&&"0123".includes(l.mode))l.mode=+l.mode;else if(!R(l.mode))throw new Error("string-left-right/chompLeft(): [THROW_ID_01] 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 D("left",n,e,l,$(t).slice(1))}return y(t[0])?D("left",n,e,u,$(t)):D("left",n,e,u,$(t).slice(1))}function se(n,e,...t){if(!t.length||t.length===1&&b(t[0]))return null;let u={mode:0};if(b(t[0])){let l=p(p({},u),$(t[0]));if(!l.mode)l.mode=0;else if(y(l.mode)&&"0123".includes(l.mode))l.mode=Number.parseInt(l.mode,10);else if(!R(l.mode))throw new Error("string-left-right/chompRight(): [THROW_ID_02] 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 D("right",n,e,l,$(t).slice(1))}return y(t[0])?D("right",n,e,u,$(t)):D("right",n,e,u,$(t).slice(1))}return z(ie);})(); /** * @name codsen-utils * @fileoverview Various utility functions * @version 1.7.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/codsen-utils/} */