UNPKG

str-indexes-of-plus

Version:

Like indexOf but returns array and counts per-grapheme

11 lines (9 loc) 1.57 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/} */ "use strict";var strIndexesOfPlus=(()=>{var o=Object.defineProperty;var a=Object.getOwnPropertyDescriptor;var m=Object.getOwnPropertyNames;var f=Object.prototype.hasOwnProperty;var g=(s,t)=>{for(var e in t)o(s,e,{get:t[e],enumerable:!0})},y=(s,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of m(t))!f.call(s,r)&&r!==e&&o(s,r,{get:()=>t[r],enumerable:!(n=a(t,r))||n.enumerable});return s};var h=s=>y(o({},"__esModule",{value:!0}),s);var j={};g(j,{strIndexesOfPlus:()=>v,version:()=>x});var d="5.1.3";var x=d;function v(s,t,e=0){if(typeof s!="string")throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): first input argument must be a string! Currently it's: ".concat(typeof s));if(typeof t!="string")throw new TypeError("str-indexes-of-plus/strIndexesOfPlus(): second input argument must be a string! Currently it's: ".concat(typeof t));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: ".concat(e));let n=Array.from(s),r=Array.from(t);if(n.length===0||r.length===0||e!=null&&+e>=n.length)return[];e||(e=0);let l=[],u=!1,p;for(let i=e,c=n.length;i<c;i++)u&&(n[i]===r[i-+p]?i-+p+1===r.length&&l.push(+p):(p=null,u=!1)),u||n[i]===r[0]&&(r.length===1?l.push(i):(u=!0,p=i));return l}return h(j);})();