UNPKG

string-trim-spaces-only

Version:

Like String.trim() but you can choose granularly what to trim

13 lines (11 loc) 1.12 kB
/** * @name string-trim-spaces-only * @fileoverview Like String.trim() but you can choose granularly what to trim * @version 5.0.12 * @author Roy Revelt, Codsen Ltd * @license MIT * {@link https://codsen.com/os/string-trim-spaces-only/} */ var i="5.0.12";var g=i,p={classicTrim:!1,cr:!1,lf:!1,tab:!1,space:!0,nbsp:!1};function d(e,l){if(typeof e!="string")throw new Error(`string-trim-spaces-only: [THROW_ID_01] input must be string! It was given as ${typeof e}, equal to: ${JSON.stringify(e,null,4)}`);let n={...p,...l};function o(t){return n.classicTrim&&!t.trim()||!n.classicTrim&&(n.space&&t===" "||n.cr&&t==="\r"||n.lf&&t===` `||n.tab&&t===" "||n.nbsp&&t==="\xA0")}let r,s;if(e.length){if(o(e[0]))for(let t=0,c=e.length;t<c;t++){if(!o(e[t])){r=t;break}if(t===e.length-1)return{res:"",ranges:[[0,e.length]]}}if(o(e[e.length-1])){for(let t=e.length;t--;)if(!o(e[t])){s=t+1;break}}return r?s?{res:e.slice(r,s),ranges:[[0,r],[s,e.length]]}:{res:e.slice(r),ranges:[[0,r]]}:s?{res:e.slice(0,s),ranges:[[s,e.length]]}:{res:e,ranges:[]}}return{res:"",ranges:[]}}export{p as defaults,d as trimSpaces,g as version};