File size: 1,691 Bytes
b5ea024
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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](https://unpkg.com/locate-character).

## Usage

To search for a particular character, using the index or a search string, use `locate`:

```js
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();

// Using a character index
const index = sample.indexOf('fly');
locate(sample, index);
// -> { line: 0, column: 13, character: 13 }

// Using the string itself
locate(sample, 'fly');
// -> { line: 0, column: 13, character: 13 }

// Using the string with a start index
locate(sample, 'fly', { startIndex: 14 });
// -> { line: 2, column: 9, character: 76 }
```

If you will be searching the same string repeatedly, it's much faster if you use `getLocator`:

```js
import { getLocator } from 'locate-character';

const locate = getLocator(sample);

let location = locate(13);
// -> { line: 0, column: 13, character: 13 }

location = locate('fly', { startIndex: location.character + 1 });
// -> { line: 2, column: 9, character: 76 }

location = locate('fly', { startIndex: location.character + 1 });
// -> { line: 3, column: 8, character: 104 }
```

In some situations (for example, dealing with sourcemaps), you need one-based line numbers:

```js
getLocator(sample, { offsetLine: 1 });
locate(sample, { offsetLine: 1 });
```

There's also an `offsetColumn` option which is less useful in real-world situations.

## License

MIT