UNPKG

string-remove-duplicate-heads-tails

Version:

Detect and (recursively) remove head and tail wrappings around the input string

11 lines (9 loc) 3.05 kB
/** * @name string-remove-duplicate-heads-tails * @fileoverview Detect and (recursively) remove head and tail wrappings around the input string * @version 7.1.0 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/string-remove-duplicate-heads-tails/} */ import{isPlainObject as w}from"codsen-utils";import{arrayiffy as p}from"arrayiffy-if-string";import{matchLeftIncl as E,matchRightIncl as d}from"string-match-left-right";import{Ranges as S}from"ranges-push";import{rApply as R}from"ranges-apply";import{trimSpaces as b}from"string-trim-spaces-only";var k="7.1.0";var L=k,A={heads:["{{"],tails:["}}"]};function P(t,$){let T=Object.prototype.hasOwnProperty;if(t===void 0)throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_01] The input is missing!");if(typeof t!="string")return t;if($&&!w($))throw new Error(`string-remove-duplicate-heads-tails: [THROW_ID_03] The given options are not a plain object but ${typeof $}!`);let i={...$};if(i&&T.call(i,"heads"))if(p(i.heads).every(e=>typeof e=="string"||Array.isArray(e)))typeof i.heads=="string"&&(i.heads=p(i.heads));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_04] The resolvedOpts.heads contains elements which are not string-type!");if(i&&T.call(i,"tails"))if(p(i.tails).every(e=>typeof e=="string"||Array.isArray(e)))typeof i.tails=="string"&&(i.tails=p(i.tails));else throw new Error("string-remove-duplicate-heads-tails: [THROW_ID_05] The resolvedOpts.tails contains elements which are not string-type!");let O=b(t).res;if(O.length===0)return t;t=O;let r={...A,...i};r.heads=r.heads.map(e=>e.trim()),r.tails=r.tails.map(e=>e.trim());let f=!1,D=!1,m=new S({limitToBeAddedWhitespace:!0}),l=new S({limitToBeAddedWhitespace:!0}),a=!0,u=!0,h="";function y(e,c){let n;return d(e,0,c.heads,{trimBeforeMatching:!0,cb:(o,g,s)=>(n=s,!0)})&&d(e,n,c.tails,{trimBeforeMatching:!0,cb:(o,g,s)=>(n=s,!0)})?e.slice(n):e}for(;t!==y(t,r);)t=b(y(t,r)).res;function M(e,c){let n;return!E(e,e.length-1,c.tails,{trimBeforeMatching:!0,cb:(o,g,s)=>(n=s,!0)})||!n?e:E(e,n,c.heads,{trimBeforeMatching:!0,cb:(o,g,s)=>(n=s,!0)})?e.slice(0,n+1):e}for(;t!==M(t,r);)t=b(M(t,r)).res;if(!r.heads.length||!d(t,0,r.heads,{trimBeforeMatching:!0})||!r.tails.length||!E(t,t.length-1,r.tails,{trimBeforeMatching:!0}))return b(t).res;for(let e=0,c=t.length;e<c;e++)if(t[e].trim()!==""){let n;if(d(t,e,r.heads,{trimBeforeMatching:!0,cb:(o,g,s)=>(n=s,!0)})&&n){u=!0,a&&(a=!0);let o;d(t,n,r.tails,{trimBeforeMatching:!0,cb:(s,J,I)=>(o=I,!0)})&&m.push(e,o),l.current()&&f&&h!=="tails"&&m.push(l.current()),f||l.current()&&(m.push(l.current()),l.wipe()),l.push(e,n),h="heads",e=n-1;continue}if(d(t,e,r.tails,{trimBeforeMatching:!0,cb:(o,g,s)=>(n=Number.isInteger(s)?s:t.length,!0)})&&n){u=!0,a?(h==="heads"&&l.wipe(),a=!1):l.push(e,n),h="tails",e=n-1;continue}a&&(a=!0),u&&!f?(f=!0,u=!1):u&&!D?(D=!0,a=!0,u=!1,h==="heads"&&l.wipe()):u&&D&&l.wipe()}return l.current()&&m.push(l.current()),m.current()?R(t,m.current()).trim():t.trim()}export{A as defaults,P as remDup,L as version};