From 2c9f47629b641ba86b5865f7f1fe20aa42e954fe Mon Sep 17 00:00:00 2001 From: Tyler Reitz Date: Tue, 19 Jun 2018 13:09:05 -0700 Subject: [PATCH] feat: adds ability to remove contribution types when editing existing user (#94) * adds ability to remove contribution types when editing existing user * removes .swp file * refactor: concat types in all cases except when removing --- src/cli.js | 2 +- src/contributors/__tests__/add.js | 19 +++++++++++++++++++ src/contributors/add.js | 14 +++++++++++--- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/cli.js b/src/cli.js index ee98b0e..b83a5b6 100755 --- a/src/cli.js +++ b/src/cli.js @@ -135,7 +135,7 @@ function promptForCommand(argv) { message: 'What do you want to do?', choices: [ { - name: 'Add a new contributor or add a new contribution type', + name: 'Add new contributor or edit contribution type', value: 'add', }, { diff --git a/src/contributors/__tests__/add.js b/src/contributors/__tests__/add.js index b5cbbdf..2e8f71c 100644 --- a/src/contributors/__tests__/add.js +++ b/src/contributors/__tests__/add.js @@ -188,3 +188,22 @@ test(`should update an existing contributor's contributions if a new type is add }, ) }) + +test(`should update an existing contributor's contributions if an existing type is removed`, () => { + const {options} = fixtures() + const username = 'login2' + const contributions = ['code'] + + return addContributor(options, username, contributions, mockInfoFetcher).then( + contributors => { + expect(contributors.length).toBe(options.contributors.length) + expect(contributors[1]).toEqual({ + login: 'login2', + name: 'Some name', + avatar_url: 'www.avatar.url', + profile: 'www.profile.url', + contributions: ['code'], + }) + }, + ) +}) diff --git a/src/contributors/add.js b/src/contributors/add.js index 5554313..d45a2d6 100644 --- a/src/contributors/add.js +++ b/src/contributors/add.js @@ -4,15 +4,23 @@ function uniqueTypes(contribution) { return contribution.type || contribution } -function formatContributions(options, existing, types) { +function formatContributions(options, existing = [], types) { + const same = _.intersectionBy(uniqueTypes, existing, types) + const remove = types.length < existing.length && same.length + if (options.url) { - return (existing || []).concat( + return existing.concat( types.map(type => { return {type, url: options.url} }), ) } - return _.uniqBy(uniqueTypes, (existing || []).concat(types)) + + if (remove) { + return same + } + + return _.uniqBy(uniqueTypes, existing.concat(types)) } function updateContributor(options, contributor, contributions) {