locate-character

WebJar for locate-character

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

locate-character
Last Version

Last Version

2.0.5
Release Date

Release Date

Type

Type

jar
Description

Description

locate-character
WebJar for locate-character
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/Rich-Harris/locate-character

Download locate-character

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/locate-character/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>locate-character</artifactId>
    <version>2.0.5</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/locate-character/
implementation 'org.webjars.npm:locate-character:2.0.5'
// https://jarcasting.com/artifacts/org.webjars.npm/locate-character/
implementation ("org.webjars.npm:locate-character:2.0.5")
'org.webjars.npm:locate-character:jar:2.0.5'
<dependency org="org.webjars.npm" name="locate-character" rev="2.0.5">
  <artifact name="locate-character" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='locate-character', version='2.0.5')
)
libraryDependencies += "org.webjars.npm" % "locate-character" % "2.0.5"
[org.webjars.npm/locate-character "2.0.5"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

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

// 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:

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:

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

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

License

MIT

Versions

Version
2.0.5