UNPKG

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