# locate-character

> Get the line and column number of a specific character in a string

Latest version **3.0.0** (published 2023-06-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install locate-character
pnpm add locate-character
yarn add locate-character
bun add locate-character
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2023-06-15 |
| First published | 2016-08-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM |
| Dependencies | 0 |
| Unpacked size | 5.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Rich Harris |
| Maintainers | rich_harris |
| Keywords | string, character, locate, line, column, location |

## Links

- npm: https://www.npmjs.com/package/locate-character
- Repository: https://gitlab.com/Rich-Harris/locate-character
- Homepage: https://gitlab.com/Rich-Harris/locate-character#README
- Issues: https://gitlab.com/Rich-Harris/locate-character/issues
- npm.io page: https://npm.io/package/locate-character

## Alternatives

- [localforage](https://npm.io/package/localforage.md) — 6.2M weekly downloads
- [localforage-observable](https://npm.io/package/localforage-observable.md) — 30.8K weekly downloads
- [@y/y](https://npm.io/package/@y/y.md) — 30.1K weekly downloads
- [@metaobjectsdev/render](https://npm.io/package/@metaobjectsdev/render.md) — 3.5K weekly downloads
- [@ledgerhq/coin-algorand](https://npm.io/package/@ledgerhq/coin-algorand.md) — 1.1K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2023-06-15
- 2.0.5 — 2018-01-14
- 2.0.4 — 2018-01-14
- 2.0.3 — 2017-11-27
- 2.0.2 — 2017-11-27
- 2.0.1 — 2017-05-21
- 2.0.0 — 2016-09-02
- 1.0.0 — 2016-08-07

## README

# 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

---
_Source: https://npm.io/package/locate-character · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
