https://github.com/mxjp/line-map
Convert between line/column and offsets
https://github.com/mxjp/line-map
Last synced: about 2 months ago
JSON representation
Convert between line/column and offsets
- Host: GitHub
- URL: https://github.com/mxjp/line-map
- Owner: mxjp
- License: mit
- Created: 2021-11-22T17:59:04.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2021-12-31T22:20:02.000Z (over 3 years ago)
- Last Synced: 2025-02-04T14:49:34.416Z (3 months ago)
- Language: TypeScript
- Size: 129 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @mpt/line-map
Convert between lines/columns and offsets.## Why?
+ Has a one-to-one mapping between positions and offsets:
+ For every position there is exactly one offset and vice versa.
+ Converting positions and offsets that do not exist will return undefined.
+ Position objects are compatible with vscode's extension api.
+ Can be used to find all line start offsets in a string.
+ Supports custom line separators.
+ By default `\n` is used, which will also work with `\r\n`.
+ Conversions using an existing line map are very fast:
+ Converting position to offset has a complexity of `O(1)`.
+ Converting offset to position has a time complexity of `O(log n)` where n is the number of lines.
+ Exports both CommonJS and ES modules.## Usage
```ts
import { LineMap } from "@mpt/line-map";const map = new LineMap("\n Hello World!\n");
map.getPosition(4);
// => { line: 1, character: 1 }map.getOffset({ line: 1, character: 1 });
// => 4map.offsets
// => [0, 1, 16]map.text
// => "\n Hello World!\n"
```