The main goal of this library is to convert a string index to its corresponding line and column position. This makes it easy to find the location of a character in a multi-line string.
How to use it:
1. Install and import the index-to-position.
# NPM $ npm i index-to-position
import indexToPosition from 'index-to-position'; 2. The library takes a string and index as input and then counts newlines to determine the line number and character position within each line to find the column number.
indexToPosition('CSSnScriptn!', 7)3. The output is a JavaScript object with the discovered line and column numbers (default: zero indexed).
{
line: 1, // line 2
column: 3, // column 4
}4. Determine whether to use 0-based or 1-based indexing. Default: true.
indexToPosition('CSSnScriptn!', 7, {
oneBased: true
})5. Determine whether to use 1-based or 0-based line indexing for the result. Default: false.
indexToPosition('CSSnScriptn!', 7, {
oneBasedLine: false
})6. Determine whether to use 1-based or 0-based column indexing for the result. Default: false.
indexToPosition('CSSnScriptn!', 7, {
oneBasedColumn: false
})Changelog:
v1.2.0 (09/25/2025)
- Add oneBasedLine and oneBasedColumn options
v1.1.0 (04/08/2025)
- Improve performance
- Allow index to equal text length
v1.0.0 (02/28/2024)
- Strictly validate the parameters
The post Find the Line and Column Position of Any Character within a String – index-to-position appeared first on CSS Script.
Discover more from RSS Feeds Cloud
Subscribe to get the latest posts sent to your email.
