index-to-position

Convert a string index to its line and column position

MIT 6 个版本
安装
npm install index-to-position
yarn add index-to-position
pnpm add index-to-position
bun add index-to-position
README

index-to-position

Convert a string index to its line and column position

Install

npm install index-to-position

Usage

import indexToPosition from 'index-to-position';

indexToPosition('hello\nworld\n!', 7);
//=> {line: 1, column: 1}

API

indexToPosition(text, index, options?)

text

Type: string

The text in which to find the line and column position.

index

Type: number

The index in the string for which to find the line and column position.

options

Type: object

oneBased

Type: boolean
Default: false

Whether to use 1-based or 0-based indexing for the result.

oneBasedLine

Type: boolean
Default: false

Whether to use 1-based or 0-based line indexing for the result.

oneBasedColumn

Type: boolean
Default: false

Whether to use 1-based or 0-based column indexing for the result.

版本列表
1.2.0 2025-09-25
1.1.0 2025-04-08
1.0.0 2024-02-28
0.1.2 2023-11-09
0.1.1 2023-11-09
0.1.0 2023-11-02