str-indexes-of-plus2.10.11

Like indexOf but returns array and counts per-grapheme

§ Quick Take

import { strict as assert } from "assert";
import indx from "str-indexes-of-plus";

// searches for string in a string, returns array:
assert.deepEqual(indx("abc-abc-abc-abc", "abc"), [
  0,
  4,
  8,
  12,
]);

// all graphemes are counted as one, emoji too:
assert.deepEqual(
  indx("🐴-🦄", "🦄"),
  [2] // not [3] considering unicorn is 2-characters long
);

// you can offset the start of a search:
assert.deepEqual(indx("abczabc", "abc", 3), [4]);

§ Compared to Others

method / programreturnsindex system based on
String.prototype.indexOf() opens in a new tabindex where the first finding startscode point count
str-indexes-of opens in a new tabarray of indexes where each finding startscode point count
📦 This package,
str-indexes-of-plus
array of indexes where each finding startsgrapheme count

See this opens in a new tab article about Unicode, graphemes and code points.

§ API

indx(str, searchValue, [fromIndex])

In other words, it's a function which takes three arguments, third one is optional (marked by square brackets).

Output: an array of zero or more numbers, each indicating the index of each finding's first character. Unicode astral characters are counted, as one character-long.

Input argumentTypeObligatory?Description
strstringyesSource string, where to search.
searchValuestringyesWhat to search for.
fromIndexnatural number or zeronoIf set, the searching will start from this index.

§ Trivia

Roy asked ~Shinnn opens in a new tab is it all right to create grapheme-count-based-index alternative of his str-indexes-of opens in a new tab and he said it's OK.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 edit-package-json 0.1.36
Edit package.json without parsing, as string, to keep the formatting intact
📦 easy-replace 3.8.0
Replace strings with optional lookarounds, but without regexes
📦 email-all-chars-within-ascii 2.9.72
Scans all characters within a string and checks are they within ASCII range
📦 js-row-num 2.7.28
Update all row numbers in all console.logs in JS code
📦 string-collapse-leading-whitespace 3.0.2
Collapse the leading and trailing whitespace of a string
📦 string-remove-thousand-separators 3.0.72
Detects and removes thousand separators (dot/comma/quote/space) from string-type digits