fix: make find/add case insensitive (#64)

Closes #62
This commit is contained in:
Gant Laborde 2017-11-10 02:55:26 -06:00 committed by Mehdi Achour
parent 9f72f9aabc
commit a79f9d4ea6
2 changed files with 54 additions and 2 deletions

View file

@ -23,7 +23,7 @@ function updateContributor(options, contributor, contributions) {
function updateExistingContributor(options, username, contributions) {
return options.contributors.map(function (contributor) {
if (username !== contributor.login) {
if (username.toLowerCase() !== contributor.login.toLowerCase()) {
return contributor;
}
return updateContributor(options, contributor, contributions);
@ -41,8 +41,14 @@ function addNewContributor(options, username, contributions, infoFetcher) {
}
module.exports = function addContributor(options, username, contributions, infoFetcher) {
if (_.find({login: username}, options.contributors)) {
// case insensitive find
var exists = _.find(function (contributor) {
return contributor.login.toLowerCase() === username.toLowerCase();
}, options.contributors);
if (exists) {
return Promise.resolve(updateExistingContributor(options, username, contributions));
}
return addNewContributor(options, username, contributions, infoFetcher);
};

View file

@ -34,6 +34,21 @@ function fixtures() {
return {options};
}
function caseFixtures() {
const options = {
contributors: [{
login: 'Login1',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
contributions: [
'code'
]
}]
};
return {options};
}
test('should callback with error if infoFetcher fails', t => {
const {options} = fixtures();
const username = 'login3';
@ -100,6 +115,17 @@ test(`should not update an existing contributor's contributions where nothing ha
});
});
test(`should not update an existing contributor's contributions where nothing has changed but the casing`, t => {
const {options} = caseFixtures();
const username = 'login1';
const contributions = ['code'];
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
t.deepEqual(contributors, options.contributors);
});
});
test(`should update an existing contributor's contributions if a new type is added`, t => {
const {options} = fixtures();
const username = 'login1';
@ -120,6 +146,26 @@ test(`should update an existing contributor's contributions if a new type is add
});
});
test(`should update an existing contributor's contributions if a new type is added with different username case`, t => {
const {options} = caseFixtures();
const username = 'login1';
const contributions = ['bug'];
return addContributor(options, username, contributions, mockInfoFetcher)
.then(contributors => {
t.is(contributors.length, 1);
t.deepEqual(contributors[0], {
login: 'Login1',
name: 'Some name',
avatar_url: 'www.avatar.url',
profile: 'www.profile.url',
contributions: [
'code',
'bug'
]
});
});
});
test(`should update an existing contributor's contributions if a new type is added with a link`, t => {
const {options} = fixtures();
const username = 'login1';