all-contributors-cli/lib/contributors/add.js
2016-03-03 00:03:03 +01:00

57 lines
1.7 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
var _ = require('lodash/fp');
function matchContribution(type) {
return function(existing) {
return type === existing || type === existing.type;
};
}
function uniqueTypes(contribution) {
return contribution.type || contribution;
}
function formatContributions(options, existing, newTypes) {
var types = newTypes.split(',');
if (options.url) {
return (existing || []).concat(types.map(function(type) {
return { type: type, url: options.url };
}));
}
return _.uniqBy(uniqueTypes, (existing || []).concat(types));
}
function updateContributor(options, contributor, contributions) {
return _.assign(contributor, {
contributions: formatContributions(options, contributor.contributions, contributions)
});
}
function updateExistingContributor(options, username, contributions) {
return options.contributors.map(function(contributor, index) {
if (username !== contributor.login) {
return contributor;
}
return updateContributor(options, contributor, contributions);
});
}
function addNewContributor(options, username, contributions, infoFetcher, cb) {
infoFetcher(username, function(error, userData) {
if (error) {
return cb(error);
}
var contributor = _.assign(userData, {
contributions: formatContributions(options, [], contributions)
});
return cb(null, options.contributors.concat(contributor));
});
}
module.exports = function addContributor(options, username, contributions, infoFetcher, cb) {
if (_.find({login: username}, options.contributors)) {
return cb(null, updateExistingContributor(options, username, contributions));
}
return addNewContributor(options, username, contributions, infoFetcher, cb);
}