3 [![Build][build-badge]][build]
4 [![Coverage][coverage-badge]][coverage]
5 [![Downloads][downloads-badge]][downloads]
6 [![Size][size-badge]][size]
7 [![Sponsors][sponsors-badge]][collective]
8 [![Backers][backers-badge]][collective]
9 [![Chat][chat-badge]][chat]
11 Convert between positions (line and column-based) and offsets (range-based)
12 locations in a [virtual file][vfile].
19 npm install vfile-location
25 var vfile = require('vfile')
26 var vfileLocation = require('vfile-location')
28 var location = vfileLocation(vfile('foo\nbar\nbaz'))
30 var offset = location.toOffset({line: 3, column: 3}) // => 10
31 location.toPosition(offset) // => {line: 3, column: 3, offset: 10}
36 ### `location = vfileLocation(doc)`
38 Get transform functions for the given `doc` (`string`) or [`file`][vfile].
40 Returns an object with [`toOffset`][to-offset] and [`toPosition`][to-position].
42 ### `location.toOffset(position)`
44 Get the `offset` (`number`) for a line and column-based [`position`][position]
46 Returns `-1` when given invalid or out of bounds input.
48 ### `location.toPosition(offset)`
50 Get the line and column-based [`position`][position] for `offset` in the bound
55 See [`contributing.md`][contributing] in [`vfile/.github`][health] for ways to
57 See [`support.md`][support] for ways to get help.
59 This project has a [Code of Conduct][coc].
60 By interacting with this repository, organisation, or community you agree to
65 [MIT][license] © [Titus Wormer][author]
69 [build-badge]: https://img.shields.io/travis/vfile/vfile-location.svg
71 [build]: https://travis-ci.org/vfile/vfile-location
73 [coverage-badge]: https://img.shields.io/codecov/c/github/vfile/vfile-location.svg
75 [coverage]: https://codecov.io/github/vfile/vfile-location
77 [downloads-badge]: https://img.shields.io/npm/dm/vfile-location.svg
79 [downloads]: https://www.npmjs.com/package/vfile-location
81 [size-badge]: https://img.shields.io/bundlephobia/minzip/vfile-location.svg
83 [size]: https://bundlephobia.com/result?p=vfile-location
85 [sponsors-badge]: https://opencollective.com/unified/sponsors/badge.svg
87 [backers-badge]: https://opencollective.com/unified/backers/badge.svg
89 [collective]: https://opencollective.com/unified
91 [chat-badge]: https://img.shields.io/badge/join%20the%20community-on%20spectrum-7b16ff.svg
93 [chat]: https://spectrum.chat/unified/vfile
95 [npm]: https://docs.npmjs.com/cli/install
97 [contributing]: https://github.com/vfile/.github/blob/master/contributing.md
99 [support]: https://github.com/vfile/.github/blob/master/support.md
101 [health]: https://github.com/vfile/.github
103 [coc]: https://github.com/vfile/.github/blob/master/code-of-conduct.md
107 [author]: https://wooorm.com
109 [vfile]: https://github.com/vfile/vfile
111 [to-offset]: #locationtooffsetposition
113 [to-position]: #locationtopositionoffset
115 [position]: https://github.com/syntax-tree/unist#position