vfile-location

vfile utility to convert between positional (line and column-based) and offset (range-based) locations

Usage no npm install needed!

<script type="module">
  import vfileLocation from 'https://cdn.skypack.dev/vfile-location';
</script>

README

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