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