UNPKG

2.06 kBJavaScriptView Raw
1'use strict';
2
3var extract = require('extract-github')
4 , to = require('./to');
5
6/**
7 * Normalize user profile information.
8 *
9 * @param {Object} data The profile data.
10 * @returns {Object} The cleaned up data structure.
11 * @api public
12 */
13function users(data) {
14 if (!data || 'object' !== to.type(data)) return {};
15
16 //
17 // Clean up github information by extracting github user name from the URL and
18 // reconstructing it. People can put anything in here, so we just want to make
19 // sure that WE decided on the our internal structure, not the users.
20 //
21 if (data.github) {
22 if ('string' !== typeof data.github) {
23 delete data.github;
24 } else {
25 data.github = (extract(data.github) || {}).user;
26 }
27
28 //
29 // The user didn't specify a homepage, we're going to default to their
30 // Github profile as that is the most interesting for most developers.
31 //
32 if ('string' === typeof data.github && !data.homepage) {
33 data.homepage = 'https://github.com/'+ data.github;
34 }
35 }
36
37 //
38 // It's possible that the twitter profile is stored with an `@` prefix or with
39 // a full URL. We only need the twitter handle as we can construct the URL's
40 // our self.
41 //
42 if (data.twitter) {
43 var twitter = /twitter.com\/[#!@\/]{0,}([^\/]+)\/?/gi;
44
45 if ('string' !== typeof data.twitter) {
46 delete data.twitter;
47 } else if (data.twitter.indexOf('@') === 0) {
48 data.twitter = data.twitter.slice(1);
49 } else if (twitter.test(data.twitter)) {
50 data.twitter = twitter.exec(data.twiter)[1];
51 } else {
52 data.twitter = data.twitter
53 .replace(/^@*(.*)/, '$1')
54 .replace(/^https?:\/\/twitter.com\//i, '');
55 }
56 }
57
58 //
59 // Make sure we follow the same gravatar URL pattern as the maintainers in the
60 // packages.
61 //
62 to.gravatar(data);
63
64 //
65 // The fields is way to opinionated and should have existed in the npm's
66 // website instead. We're going to remove this as it's only duplicate
67 // bullshit.
68 //
69 delete data.fields;
70
71 return data;
72}
73
74module.exports = users;