§ Quick Take
import { strict as assert } from "assert";
import { strIndexesOfPlus } from "str-indexes-of-plus";
// searches for string in a string, returns array:
assert.deepEqual(
strIndexesOfPlus("abc-abc-abc-abc", "abc"),
[0, 4, 8, 12]
);
// all graphemes are counted as one, emoji too:
assert.deepEqual(
strIndexesOfPlus("🐴-🦄", "🦄"),
[2] // not [3] considering unicorn is 2-characters long
);
// you can offset the start of a search:
assert.deepEqual(
strIndexesOfPlus("abczabc", "abc", 3),
[4]
);
§ Compared to Others
method / program | returns | index system based on |
---|---|---|
String.prototype.indexOf() | index where the first finding starts | code point count |
str-indexes-of | array of indexes where each finding starts | code point count |
📦 This package,str-indexes-of-plus | array of indexes where each finding starts | grapheme count |
See this article about Unicode, graphemes and code points.
§ API
strIndexesOfPlus( 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 argument | Type | Obligatory? | Description |
---|---|---|---|
str | string | yes | Source string, where to search. |
searchValue | string | yes | What to search for. |
fromIndex | natural number or zero | no | If set, the searching will start from this index. |
§ Trivia
Roy asked ~Shinnn is it all right to create grapheme-count-based-index alternative of his str-indexes-of
and he said it's OK.
§ Changelog
See it in the monorepo , on GitHub.
§ Contributing
To report bugs or request features or assistance, raise an issue on GitHub .
Any code contributions welcome! All Pull Requests will be dealt promptly.
§ Licence
Copyright © 2010–2021 Roy Revelt and other contributors
Related packages:
📦 str-indexes-of
Like String#indexOf() but return all indexes
📦 edit-package-json 0.3.6
Edit package.json without parsing, as string, to keep the formatting intact
📦 easy-replace 4.0.6
Replace strings with optional lookarounds, but without regexes
📦 email-all-chars-within-ascii 3.0.6
Scans all characters within a string and checks are they within ASCII range
📦 js-row-num 4.0.6
Update all row numbers in all
console.log
s in JS code📦 line-column-mini 1.1.6
Convert string index to line-column position
📦 string-remove-duplicate-heads-tails 5.0.6
Detect and (recursively) remove head and tail wrappings around the input string