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

map-line-to-diff

v1.0.0-beta.2

Published

Map file lines to Git diff locations.

Downloads

46

Readme

Map Line to Diff

When submitting a pull request review via the GitHub API, comments must be mapped to their position within the diff:

Note: To comment on a specific line in a file, you need to first determine the position of that line in the diff.... The position value equals the number of lines down from the first "@@" hunk header in the file you want to add a comment. The line just below the "@@" line is position 1, the next line is position 2, and so on. The position in the diff continues to increase through lines of whitespace and additional hunks until the beginning of a new file.

Installation

$ yarn install map-line-to-diff

Usage

Given a string representation of a Git diff, the name of a file in the diff, and a line number in the file's current state, mapLineToDiff returns the position of that line within the diff relative to the file's first @@ hunk header, as defined by the GitHub API guidelines above. If the file does not appear in the diff, or the line is not contained in any of the hunks, the function returns -1.

diff --git a/example.txt b/example.txt
--- a/example.txt
+++ b/example.txt
@@ -1,3 +1,3 @@
 It's
 a
-test.
+trap!
@@ -10,1 +10,2 @@
 Hello
+world
diff --git a/testing.txt b/testing.txt
--- /dev/null
+++ b/testing.txt
@@ -0,0 +1,1 @@
+Testing.
// ES2015 modules import/export
import { mapLineToDiff } from 'map-line-to-diff';
// CommonJS require()
const { mapLineToDiff } = require('map-line-to-diff');


// Assume `diff` contains the diff above.
const diff = '...';

// Lines 1 and 2 aren't changed.
mapLineToDiff(diff, 'example.txt', 1); // => 1
mapLineToDiff(diff, 'example.txt', 2); // => 2

// Line 3 in the current file was changed to "trap!" on line 4 of the diff.
mapLineToDiff(diff, 'example.txt', 3); // => 4

// Lines 4-9 aren't in the diff.
mapLineToDiff(diff, 'example.txt', 4); // => -1
mapLineToDiff(diff, 'example.txt', 9); // => -1

// Line 10, "Hello", is unchanged 6 lines after the first hunk's @@ header.
mapLineToDiff(diff, 'example.txt', 10); // => 6
// Line 11, "world", is was added in the diff.
mapLineToDiff(diff, 'example.txt', 11); // => 7

// Lines greater than 11 aren't in the diff.
mapLineToDiff(diff, 'example.txt', 12); // => -1

// A new file resets the line count to 1 at "Testing." in testing.txt.
mapLineToDiff(diff, 'testing.txt', 1); // => 1

// README.md is not in the diff.
mapLineToDiff(diff, 'README.md', 1); // -1

License

MIT © Brandon Mills