Github - package.json dependency linker

When viewing a package.json file, automatically links dependencies to their NPM page

Από την 08/10/2014. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name           Github - package.json dependency linker
// @description    When viewing a package.json file, automatically links dependencies to their NPM page
// @author         James Skinner <[email protected]> http://userscripts.org/users/55684
// @namespace      http://spiralx.org/
// @license        BSD
// @version        0.0.2
// @include        https://github.com/*/package.json
// @require        https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.js
// ==/UserScript==

function format(formatString, data) {
  data = arguments.length == 2 && typeof data === "object" && !Array.isArray(data)
    ? data
    : [].slice.call(arguments, 1);

  return formatString
    .replace(/\{\{/g, String.fromCharCode(0))
    .replace(/\}\}/g, String.fromCharCode(1))
    .replace(/\{([^}]+)\}/g, function(match, path) {
      try {
        var p = path.replace(/\[(-?\w+)\]/g, '.$1').split('.');
        //console.log('path="%s" (%s), data=%s', path, p.toSource(), data.toSource());
        return String(p.reduce(function(o, n) {
          return o.slice && !isNaN(n) ? o.slice(n).shift() :  o[n];
        }, data));
      }
      catch (ex) {
        return match;
      }
    })
    .replace(/\x00/g, "{")
    .replace(/\x01/g, "}");
}

// --------------------------------------------------------------------------

var $code = $('.highlight .js-file-line'),
  pkg = JSON.parse($code.text()),
  depKeys = [
    "dependencies",
    "devDependencies",
    "peerDependencies",
    "bundleDependencies",
    "bundledDependencies",
    "optionalDependencies"
  ];
  
depKeys.forEach(function(k) {
  var deps = pkg[k] || {};
  
  for (var module in deps) {
    var
      url = 'https://www.npmjs.org/package/' + module,
      // jsonUrl = 'http://registry.npmjs.org/' + module + '/latest',
      t = '"' + module + '"';
      
    $code
      .find('.nt')
      .filter(function() {
        return $(this).text().trim() === t
      })
      .html(format('"<a href="{0}">{1}</a>"', url, module));
  }
});