From ce5df179c05d1a9d2f7311f90aba1cf6d7212f9a Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Wed, 28 Sep 2022 13:17:27 -0400 Subject: [PATCH] fix: allow slack usernames with spaces (#12) --- cypress/e2e/utils/parse-channel.cy.js | 32 +++++++++++++++++++++++++++ cypress/expected.json | 6 ++++- src/utils.js | 5 ++++- 3 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 cypress/e2e/utils/parse-channel.cy.js diff --git a/cypress/e2e/utils/parse-channel.cy.js b/cypress/e2e/utils/parse-channel.cy.js new file mode 100644 index 0000000..2f86d61 --- /dev/null +++ b/cypress/e2e/utils/parse-channel.cy.js @@ -0,0 +1,32 @@ +/// +const { getChannelAndPeople } = require('../../../src/utils') + +it('parses channel name with usernames', () => { + const { channel, people } = getChannelAndPeople( + '#channel-name @user1 @user2 @user3', + ) + expect(channel, 'channel').to.equal('#channel-name') + expect(people, 'people').to.deep.equal(['@user1', '@user2', '@user3']) +}) + +it('parses channel name with usernames with spaces', () => { + const { channel, people } = getChannelAndPeople( + '#channel-name-1 @john doe @mary ann', + ) + expect(channel, 'channel').to.equal('#channel-name-1') + expect(people, 'people').to.deep.equal(['@john doe', '@mary ann']) +}) + +it('ignores multiple spaces', () => { + const { channel, people } = getChannelAndPeople( + ' #channel-name-1 @john doe @mary ann ', + ) + expect(channel, 'channel').to.equal('#channel-name-1') + expect(people, 'people').to.deep.equal(['@john doe', '@mary ann']) +}) + +it('does not care about casing', () => { + const { channel, people } = getChannelAndPeople(' #channel-NAME-1 @Mary') + expect(channel, 'channel').to.equal('#channel-NAME-1') + expect(people, 'people').to.deep.equal(['@Mary']) +}) diff --git a/cypress/expected.json b/cypress/expected.json index 97ace5e..30b7c0f 100644 --- a/cypress/expected.json +++ b/cypress/expected.json @@ -5,5 +5,9 @@ "fails in the subfolder sub": "fail", "finds the right channel by the filename end": "pass", "finds the right channel by minimatch": "pass", - "has single target shortcut": "pass" + "has single target shortcut": "pass", + "parses channel name with usernames": "pass", + "parses channel name with usernames with spaces": "pass", + "ignores multiple spaces": "pass", + "does not care about casing": "pass" } diff --git a/src/utils.js b/src/utils.js index bd71d51..11e5d33 100644 --- a/src/utils.js +++ b/src/utils.js @@ -33,7 +33,10 @@ function getChannelAndPeople(s) { if (typeof s !== 'string') { throw new Error(`expected a string, got "${s}"`) } - const parts = s.split(' ') + const parts = s + .split(/\s(?=@)/g) + .map((s) => s.trim()) + .filter(Boolean) const channel = parts.find((s) => s.startsWith('#')) const people = parts.filter((s) => s.startsWith('@')) return { channel, people }