unified

Project: vfile/vfile-location

Package: vfile-location@4.0.1

  1. Dependents: 38
  2. vfile utility to convert between positional (line and column-based) and offset (range-based) locations
  1. util 143
  2. utility 139
  3. file 34
  4. virtual 19
  5. vfile 17
  6. vfile-util 15
  7. position 9
  8. location 4
  9. point 3

vfile-location

Build Coverage Downloads Size Sponsors Backers Chat

Convert between positional (line and column-based) and offsets (range-based) locations in a virtual file.

Install

This package is ESM only: Node 12+ is needed to use it and it must be imported instead of required.

npm:

npm install vfile-location

Use

import {VFile} from 'vfile'
import {location} from 'vfile-location'

var place = location(new VFile('foo\nbar\nbaz'))

var offset = place.toOffset({line: 3, column: 3}) // => 10
place.toPoint(offset) // => {line: 3, column: 3, offset: 10}

API

This package exports the following identifiers: place. There is no default export.

place = location(doc)

Get transform functions for the given doc (string) or file.

Returns an object with toOffset and toPoint.

place.toOffset(point)

Get the offset (number) for a line and column-based point in the bound file. Returns -1 when given invalid or out of bounds input.

place.toPoint(offset)

Get the line and column-based point for offset in the bound file.

Contribute

See contributing.md in vfile/.github for ways to get started. See support.md for ways to get help.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.

License

MIT © Titus Wormer