locate-character
Get the line and column number of a particular character in a string.
Installation
npm install locate-character
, or get it from unpkg.com/locate-character.
Usage
To search for a particular character, using the index or a search string, use locate
:
import { locate } from 'locate-character';
const sample = `
A flea and a fly in a flue
Were imprisoned, so what could they do?
Said the fly, "let us flee!"
"Let us fly!" said the flea.
So they flew through a flaw in the flue.
`.trim();
const index = sample.indexOf('fly');
locate(sample, index);
locate(sample, 'fly');
locate(sample, 'fly', { startIndex: 14 });
If you will be searching the same string repeatedly, it's much faster if you use getLocator
:
import { getLocator } from 'locate-character';
const locate = getLocator(sample);
let location = locate(13);
location = locate('fly', { startIndex: location.character + 1 });
location = locate('fly', { startIndex: location.character + 1 });
In some situations (for example, dealing with sourcemaps), you need one-based line numbers:
getLocator(sample, { offsetLine: 1 });
locate(sample, { offsetLine: 1 });
There's also an offsetColumn
option which is less useful in real-world situations.
License
MIT