npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@flex-development/vfile-location

v1.1.0

Published

utility to convert between point (line/column) and offset (range) based locations

Downloads

13

Readme

vfile-location

github release npm codecov module type: esm license conventional commits typescript vitest yarn

vfile utility to convert between point (line/column) and offset (range) based locations

Contents

What is this?

This is a tiny but useful package that facilitates conversions between points and offsets in a file.

When should I use this?

This utility is useful when adding positional information to unist nodes, or when building packages that require location data, such as a set of lint rules.

Install

This package is ESM only.

In Node.js (version 18+) with yarn:

yarn add @flex-development/vfile-location

In Deno with esm.sh:

import { Location } from 'https://esm.sh/@flex-development/vfile-location'

In browsers with esm.sh:

<script type="module">
  import { Location } from 'https://esm.sh/@flex-development/vfile-location'
</script>

Use

import { Location, type Point } from '@flex-development/vfile-location'
import { read } from 'to-vfile'
import type * as unist from 'unist'
import type { VFile, Value } from 'vfile'

const point: Point = { column: 1, line: 21, offset: 474 }
const pt: Point = { column: 2, line: 47, offset: 1124 }

const file: VFile = await read('hrt.ts')
const val: Value = String(file).slice(point.offset, pt.offset + 1)

const location: Location = new Location(file)
const loc: Location = new Location(val, point)

console.log(location.offset({ ...point, offset: undefined })) // => point.offset
console.log(location.point(point.offset)) // => point

console.log(loc.offset({ ...pt, offset: undefined })) // => pt.offset
console.log(loc.point(pt.offset)) // => pt

API

This package exports the identifier Location. There is no default export.

Location([file][, start])

Create a new location index to translate between point and offset based locations in file.

Pass a start point to make relative conversions. Any point or offset accessed will be relative to the given point.

An incremental index can be built when file is null or undefined, in which case indices (and place) must be updated manually.

  • file (Value | VFile | null | undefined) — file to index
  • start (Point | null | undefined) — point before first character

Location#indices

(Indices) Map, where each key/value pair is either the index of a character in the file (offset) and a point, or a line and column in the file and an offset. Both the key and value are relative to start.

Location#offset([point])

Get an offset for point.

👉 The offset for point is greater than or equal to 0 when point is valid, and -1 when point is invalid.

Parameters
  • point (unist.Point | null | undefined) — place in file
Returns

(Offset) Index of character in file or -1.

Location#place

(Point) Current point.

👉 Useful for building an incremental index. This point is deeply equal to start when a file is auto-indexed and never altered.

Location#point([offset])

Get a point for offset.

👉 point.column and point.line are greater than or equal to 1 when offset is valid, and -1 when offset is invalid.

Parameters
  • offset (Offset | null | undefined) — index of character in file
Returns

(Point) Place in file.

Location#start

(Readonly<Point>) Point before first character in file.

Column

Column in a source file (1-indexed integer) (TypeScript type).

type Column = number

Indices

Map, where each key/value pair is either the index of a character in a source file (offset) and a point, or a line and column in the source file and an offset (TypeScript type).

type Indices = { [offset: Offset]: Point; [point: SerializedPoint]: Offset }

Line

Line in a source file (1-indexed integer) (TypeScript type).

type Line = number

Offset

Index of a character in a source file (0-indexed integer) (TypeScript type).

type Offset = number

Point

One place in a source file (TypeScript interface).

Properties

  • column (Column) — column in source file (1-indexed integer)
  • line (Line) — line in source file (1-indexed integer)
  • offset (Offset) — index of character in source file (0-indexed integer)

SerializedPoint

String representing one place in a source file (TypeScript type).

type SerializedPoint = `${Line}:${Column}`

Types

This package is fully typed with TypeScript.

Contribute

See CONTRIBUTING.md.

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