Installation
1. Install the latest version with yarn:
yarn add str-indexes-of-plus
or with npm:
npm install str-indexes-of-plus
2. Import it in your code:
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() | ||
String.prototype.indexOf() | index where the first finding starts | code point count |
str-indexes-of | ||
str-indexes-of | array of indexes where each finding starts | code point count |
📦 This package, str-indexes-of-plus | ||
📦 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()
The main function strIndexesOfPlus()
is imported like this:
import { strIndexesOfPlus } from "str-indexes-of-plus";
It’s a function which takes three input arguments:
function strIndexesOfPlus (
str: string,
searchValue: string,
fromIndex?: string | number
): number[];
Input argument | Type | Obligatory | Description |
---|---|---|---|
str Type: string Obligatory: yes | |||
str | string | yes | Source string, where to search. |
searchValue Type: string Obligatory: yes | |||
searchValue | string | yes | What to search for. |
fromIndex Type: natural number or zero Obligatory: no | |||
fromIndex | natural number or zero | no | If set, the searching will start from this index. |
Function will return 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.
API — version
You can import version
:
import { version } from "str-indexes-of-plus";
console.log(version);
// => "5.0.3"
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.