UNPKG

str-indexes-of-plus

Version:

Like indexOf but returns array and counts per-grapheme

11 lines (9 loc) 1.07 kB
/** * @name str-indexes-of-plus * @fileoverview Like indexOf but returns array and counts per-grapheme * @version 5.1.3 * @author Roy Revelt * @license MIT * {@link https://codsen.com/os/str-indexes-of-plus/} */ var o="5.1.3";var f=o;function g(p,u,e=0){if(typeof p!="string")throw new TypeError(`str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: ${typeof p}`);if(typeof u!="string")throw new TypeError(`str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: ${typeof u}`);if(isNaN(+e)||typeof e=="string"&&!/^\d*$/.test(e))throw new TypeError(`str-indexes-of-plus/strIndexesOfPlus(): third input argument must be a natural number! Currently it's: ${e}`);let s=Array.from(p),r=Array.from(u);if(s.length===0||r.length===0||e!=null&&+e>=s.length)return[];e||(e=0);let l=[],i=!1,n;for(let t=e,d=s.length;t<d;t++)i&&(s[t]===r[t-+n]?t-+n+1===r.length&&l.push(+n):(n=null,i=!1)),i||s[t]===r[0]&&(r.length===1?l.push(t):(i=!0,n=t));return l}export{g as strIndexesOfPlus,f as version};