Skip to content

Commit

Permalink
fix: make find/add case insensitive (#64)
Browse files Browse the repository at this point in the history
Closes #62
  • Loading branch information
GantMan authored and machour committed Nov 10, 2017
1 parent 9f72f9a commit a79f9d4
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/contributors/add.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
};

46 changes: 46 additions & 0 deletions lib/contributors/add.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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';
Expand All @@ -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';
Expand Down

0 comments on commit a79f9d4

Please sign in to comment.