UNPKG

3.38 kBJavaScriptView Raw
1'use strict';
2
3var extract = require('extract-github').url
4 , toString = Object.prototype.toString
5 , crypto = require('crypto')
6 , url = require('url');
7
8//
9// to.js is a small collection of parsers and utilities that makes it easier to
10// normalize the data structures that are returned from The npm Registry API.
11//
12
13/**
14 * Get accurate type information for the given JavaScript class.
15 *
16 * @param {Mixed} of The thing who's type class we want to figure out.
17 * @returns {String} lowercase variant of the name.
18 * @api private
19 */
20exports.type = function type(of) {
21 return toString.call(of).slice(8, -1).toLowerCase();
22};
23
24/**
25 * Decide the best way of merging license data.
26 *
27 * @param {Mixed} data
28 * @param {Mixed} fallback
29 * @returns {Mixed} The one that should be merged.
30 */
31exports.licenses = function licenses(data, fallback) {
32 var fblicenses = fallback.licenses
33 , dlicenses = data.licenses
34 , fbok, dok;
35
36 fbok = Array.isArray(fblicenses) && fblicenses.every(function every(license) {
37 return 'string' === exports.type(license);
38 });
39
40 dok = Array.isArray(dlicenses) && dlicenses.every(function every(license) {
41 return 'string' === exports.type(license);
42 });
43
44 if (dok && !fbok) return dlicenses;
45 if (fbok && !dok) return fblicenses;
46
47 return dlicenses || fblicenses;
48};
49
50/**
51 * Create a gravatar for the given email.
52 *
53 * @param {Object} data Object that has an `email` property.
54 * @returns {Object} The data object.
55 * @private
56 */
57exports.gravatar = function gravatar(data) {
58 var email = (
59 'string' === typeof data.email
60 ? data.email
61 : ''
62 ).toLowerCase().trim();
63
64 if (!email || (data.gravatar && !~data.gravatar.indexOf('?'))) {
65 return data; // Gravatar's are constructed from email addresses.
66 }
67
68 data.gravatar_id = crypto.createHash('md5').update(email).digest('hex');
69 data.gravatar = 'https://secure.gravatar.com/avatar/'+ data.gravatar_id;
70
71 return data;
72};
73
74/**
75 * Default to something from github.
76 *
77 * @param {String} path The path we should append to the string.
78 * @returns {Function}
79 * @api public
80 */
81exports.github = function github(path) {
82 return function githubtransform(data) {
83 var type = exports.type(data)
84 , existing = extract(data);
85
86 if (!existing) {
87 if (!this.github) return undefined;
88
89 existing = 'https://github.com/'+ this.github.user +'/'+ this.github.repo;
90 if (path) existing += '/'+ path;
91 } else {
92 //
93 // Normalize the URL to something useful. As it could be that we were
94 // given some of the following URL structures:
95 //
96 // - git@github.com:primus/primus.git
97 // - git://github.com/primus/primus.git#branch
98 // - https://github.com/primus/primus.git
99 // - git+ssh://git@github.com/primus/primus.git#branch
100 //
101 existing = existing.replace(/^git@/, '').replace(/com\:/, 'com/');
102 existing = url.parse(existing);
103 existing.protocol = !existing.protocol || 'git:' === existing.protocol ? 'http:' : existing.protocol;
104 existing.slashes = true;
105 existing.path = existing.pathname = (existing.pathname || existing.path || '').replace(/\.git$/, '');
106 existing.hash = null;
107 existing = url.format(existing);
108 }
109
110 if ('object' !== type) return {
111 url: existing
112 };
113
114 data.url = existing;
115 delete data.web;
116
117 return data;
118 };
119};