1 | 'use strict';
|
2 |
|
3 | var extract = require('extract-github').url
|
4 | , toString = Object.prototype.toString
|
5 | , crypto = require('crypto')
|
6 | , url = require('url');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | exports.type = function type(of) {
|
21 | return toString.call(of).slice(8, -1).toLowerCase();
|
22 | };
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | exports.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 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 |
|
57 | exports.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;
|
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 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | exports.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 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
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 | };
|