string-range-expander
Version:
Expands string index ranges within whitespace boundaries until letters are met
12 lines (10 loc) • 6.04 kB
JavaScript
/**
* @name string-range-expander
* @fileoverview Expands string index ranges within whitespace boundaries until letters are met
* @version 4.0.17
* @author Roy Revelt, Codsen Ltd
* @license MIT
* {@link https://codsen.com/os/string-range-expander/}
*/
import{isStr as s,isInt as f,isPlainObject as u}from"codsen-utils";var h="4.0.17";var I=h,a={str:"",from:0,to:0,ifLeftSideIncludesThisThenCropTightly:"",ifLeftSideIncludesThisCropItToo:"",ifRightSideIncludesThisThenCropTightly:"",ifRightSideIncludesThisCropItToo:"",extendToOneSide:!1,wipeAllWhitespaceOnLeft:!1,wipeAllWhitespaceOnRight:!1,addSingleSpaceToPreventAccidentalConcatenation:!1};function S(e){let d=/^[0-9a-zA-Z]+$/;function l(t){return s(t)&&!t.trim()}if(u(e)){if(!Object.keys(e).length)throw new Error(`string-range-expander: [THROW_ID_02] Input must be a plain object but it's been given as a plain object without any keys. However, "from" and "to" settings are obligatory!`)}else{let t;throw e===void 0?t="but it is missing completely.":e===null?t="but it was given as null.":t=`but it was given as ${typeof e}, equal to:
${JSON.stringify(e,null,4)}.`,new Error(`string-range-expander: [THROW_ID_01] Input must be a plain object ${t}`)}if(!f(e.from))throw new Error(`string-range-expander: [THROW_ID_03] The input's "from" value resolvedOpts.from, is not a number! It's been given as ${typeof e.from}, equal to ${JSON.stringify(e.from,null,0)}`);if(!f(e.to))throw new Error(`string-range-expander: [THROW_ID_04] The input's "to" value resolvedOpts.to, is not a number! It's been given as ${typeof e.to}, equal to ${JSON.stringify(e.to,null,0)}`);if(e?.str&&!e.str[e.from]&&e.from!==e.to)throw new Error(`string-range-expander: [THROW_ID_05] The given input string resolvedOpts.str ("${e.str}") must contain the character at index "from" ("${e.from}")`);if(e?.str&&!e.str[e.to-1])throw new Error(`string-range-expander: [THROW_ID_06] The given input string, resolvedOpts.str ("${e.str}") must contain the character at index before "to" ("${e.to-1}")`);if(e.from>e.to)throw new Error(`string-range-expander: [THROW_ID_07] The given "from" index, "${e.from}" is greater than "to" index, "${e.to}". That's wrong!`);if(e.extendToOneSide===null||s(e.extendToOneSide)&&e.extendToOneSide!=="left"&&e.extendToOneSide!=="right"||!s(e.extendToOneSide)&&e.extendToOneSide!==void 0&&e.extendToOneSide)throw new Error(`string-range-expander: [THROW_ID_08] The options value "extendToOneSide" is not recognisable! It's set to: "${e.extendToOneSide}" (${typeof e.extendToOneSide}). It has to be either Boolean "false" or one of strings: "left" or "right"`);if(e?.ifLeftSideIncludesThisThenCropTightly&&!s(e.ifLeftSideIncludesThisThenCropTightly))throw new Error(`string-range-expander: [THROW_ID_09] The option "ifLeftSideIncludesThisThenCropTightly", is not a string! It's been given as ${typeof e.ifLeftSideIncludesThisThenCropTightly}, equal to ${JSON.stringify(e.ifLeftSideIncludesThisThenCropTightly,null,0)}`);if(e?.ifLeftSideIncludesThisCropItToo&&!s(e.ifLeftSideIncludesThisCropItToo))throw new Error(`string-range-expander: [THROW_ID_10] The option "ifLeftSideIncludesThisCropItToo", is not a string! It's been given as ${typeof e.ifLeftSideIncludesThisCropItToo}, equal to ${JSON.stringify(e.ifLeftSideIncludesThisCropItToo,null,0)}`);if(e?.ifRightSideIncludesThisThenCropTightly&&!s(e.ifRightSideIncludesThisThenCropTightly))throw new Error(`string-range-expander: [THROW_ID_11] The option "ifRightSideIncludesThisThenCropTightly", is not a string! It's been given as ${typeof e.ifRightSideIncludesThisThenCropTightly}, equal to ${JSON.stringify(e.ifRightSideIncludesThisThenCropTightly,null,0)}`);if(e?.ifRightSideIncludesThisCropItToo&&!s(e.ifRightSideIncludesThisCropItToo))throw new Error(`string-range-expander: [THROW_ID_12] The option "ifRightSideIncludesThisCropItToo", is not a string! It's been given as ${typeof e.ifRightSideIncludesThisCropItToo}, equal to ${JSON.stringify(e.ifRightSideIncludesThisCropItToo,null,0)}`);let i={...a,...e},n=i.str,r=i.from,o=i.to;if(i.extendToOneSide!=="right"&&(l(n[r-1])&&(l(n[r-2])||i.ifLeftSideIncludesThisCropItToo.includes(n[r-2]))||n[r-1]&&i.ifLeftSideIncludesThisCropItToo.includes(n[r-1])||i.wipeAllWhitespaceOnLeft&&l(n[r-1]))){for(let t=r;t--;)if(!i.ifLeftSideIncludesThisCropItToo.includes(n[t])){if(n[t].trim()){i.wipeAllWhitespaceOnLeft||i.ifLeftSideIncludesThisCropItToo.includes(n[t+1])?r=t+1:r=t+2;break}else if(t===0){i.wipeAllWhitespaceOnLeft?r=0:r=1;break}}}if(i.extendToOneSide!=="left"&&(l(n[o])&&(i.wipeAllWhitespaceOnRight||l(n[o+1]))||i.ifRightSideIncludesThisCropItToo.includes(n[o]))){for(let t=o,g=n.length;t<g;t++)if(!i.ifRightSideIncludesThisCropItToo.includes(n[t])){if(n[t].trim()){i.wipeAllWhitespaceOnRight||i.ifRightSideIncludesThisCropItToo.includes(n[t-1])?o=t:o=t-1;break}else if(t===n.length-1){i.wipeAllWhitespaceOnRight?o=n.length:o=n.length-1;break}}}return(i.extendToOneSide!=="right"&&s(i.ifLeftSideIncludesThisThenCropTightly)&&i.ifLeftSideIncludesThisThenCropTightly&&(n[r-2]&&i.ifLeftSideIncludesThisThenCropTightly.includes(n[r-2])||n[r-1]&&i.ifLeftSideIncludesThisThenCropTightly.includes(n[r-1]))||i.extendToOneSide!=="left"&&s(i.ifRightSideIncludesThisThenCropTightly)&&i.ifRightSideIncludesThisThenCropTightly&&(n[o+1]&&i.ifRightSideIncludesThisThenCropTightly.includes(n[o+1])||n[o]&&i.ifRightSideIncludesThisThenCropTightly.includes(n[o])))&&(i.extendToOneSide!=="right"&&l(n[r-1])&&!i.wipeAllWhitespaceOnLeft&&(r-=1),i.extendToOneSide!=="left"&&l(n[o])&&!i.wipeAllWhitespaceOnRight&&(o+=1)),i.addSingleSpaceToPreventAccidentalConcatenation&&n[r-1]?.trim()&&n[o]?.trim()&&(!i.ifLeftSideIncludesThisThenCropTightly&&!i.ifRightSideIncludesThisThenCropTightly||!((!i.ifLeftSideIncludesThisThenCropTightly||i.ifLeftSideIncludesThisThenCropTightly.includes(n[r-1]))&&(!i.ifRightSideIncludesThisThenCropTightly||n[o]&&i.ifRightSideIncludesThisThenCropTightly.includes(n[o]))))&&(d.test(n[r-1])||d.test(n[o]))?[r,o," "]:[r,o]}export{a as defaults,S as expander,I as version};