UNPKG

qsu

Version:

qsu is a utility library that contains useful and frequently used functions. Start with your preferred language and the modern development environment.

1 lines 1.21 kB
export function getGroupKeys(r,e,t,n=!1){if(!r)return[];if(!e||!t)throw new Error("`groupStart` and `groupEnd` must be non-empty.");if(e===t)throw new Error("`groupStart` and `groupEnd` must be different.");if(1===e.length&&1!==t.length)throw new Error("When `groupStart` is a single character, `groupEnd` must also be a single character.");if(1===t.length&&1!==e.length)throw new Error("When `groupEnd` is a single character, `groupStart` must also be a single character.");const i=[],u=r.length,o=e.length,f=t.length,l=e=>{let t=0;for(let n=e-1;n>=0&&"\\"===r[n];n--)t++;return t%2==1},s=1===o&&1===f,a=s?e:"",c=s?t:"",g=t=>{if(t<0||t+o>u)return!1;if(r.slice(t,t+o)!==e)return!1;if(l(t))return!1;if(s){if(t>0&&r[t-1]===a)return!1;if(t+1<u&&r[t+1]===a)return!1}return!0},h=e=>{if(e<0||e+f>u)return!1;if(r.slice(e,e+f)!==t)return!1;if(l(e))return!1;if(s){if(e>0&&r[e-1]===c)return!1;if(e+1<u&&r[e+1]===c)return!1}return!0},p=r=>!r.includes("\n")&&!r.includes("\r")&&(!r.includes(e)&&!r.includes(t)&&(!!n||/^[A-Za-z0-9_$-]*$/.test(r)));for(let e=0;e<=u-o;e++){if(!g(e))continue;const t=e+o;let n=-1;for(let r=t;r<=u-f;r++)if(h(r)){n=r;break}if(-1===n)continue;const l=r.slice(t,n);p(l)&&i.push(l),e=n+f-1}return i}