Skip to content

Commit

Permalink
fix: allow slack usernames with spaces (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov authored Sep 28, 2022
1 parent 4a5b165 commit ce5df17
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
32 changes: 32 additions & 0 deletions cypress/e2e/utils/parse-channel.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference types="cypress" />
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'])
})
6 changes: 5 additions & 1 deletion cypress/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
5 changes: 4 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit ce5df17

Please sign in to comment.