Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 741b8b6

Browse files
committed
todo filtering
1 parent 0752b48 commit 741b8b6

File tree

2 files changed

+91
-27
lines changed

2 files changed

+91
-27
lines changed

support.js

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,15 @@ const actions = [
6565
regexp: /debug/i,
6666
reply: (message, output) => JSON.stringify(output, null, 4),
6767
},
68+
{
69+
action: (tickets, _message, username) => [tickets, username.toLowerCase()],
70+
regexp: /todo ?(\w*)/i,
71+
reply: (message, output) => showTickets(output[0], ['open'], output[1]),
72+
},
6873
{
6974
action: (tickets, _message) => tickets,
70-
regexp: /todo/i,
71-
reply: (message, output) => showTickets(output, ['open']),
75+
regexp: /mine/i,
76+
reply: (message, output) => showTickets(output, ['open'], message.userName),
7277
},
7378
{
7479
action: (tickets, _message) => tickets,
@@ -111,7 +116,7 @@ function closeTicket(tickets, id) {
111116
function openTicket(tickets, message, content) {
112117
const newTicket = createTicket()
113118
tickets.lastId += 1
114-
newTicket.id = tickets.lastId
119+
newTicket.id = `${tickets.lastId}`
115120
newTicket.created = new Date()
116121
newTicket.content = content
117122
newTicket.roomName = message.roomName
@@ -124,16 +129,28 @@ function showTicket(ticket) {
124129
return `${ticket.content} requested by ${ticket.requester} (${ticket.roomName}) assigned to ${ticket.assignee}`
125130
}
126131

127-
function showTickets(tickets, states) {
132+
function compareUserName(userA, userB) {
133+
if(!userA || !userB) {
134+
return false
135+
}
136+
return userA.toLowerCase().startsWith(userB.toLowerCase())
137+
}
138+
139+
function showTickets(tickets, states, userName) {
128140
var count = 0;
129141
var output = '\n'
130-
for(var i = 1; i < tickets.lastId + 1; i++) {
131-
if(states.indexOf(tickets[i].status) != -1) {
132-
output += `ticket #${i}: ${showTicket(tickets[i])}\n`
133-
count++
142+
for(var i = 1; i <= tickets.lastId; i++) {
143+
const strIndex = `${i}`
144+
if(states.indexOf(tickets[strIndex].status) != -1) {
145+
if(!userName ||
146+
(compareUserName(tickets[strIndex].assignee, userName) ||
147+
compareUserName(tickets[strIndex].requester, userName))) {
148+
output += `ticket #${i}: ${showTicket(tickets[strIndex])}\n`
149+
count++
150+
}
134151
}
135152
}
136-
if(!count) {
153+
if(count === 0) {
137154
return 'nothing TODO!'
138155
}
139156
return output

test/support_test.js

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,46 @@ const sample = {
1111
userName: 'Ricky',
1212
}
1313

14-
const tickets = {
15-
'1': {
16-
assignee: null,
17-
closed: null,
18-
content: 'content',
19-
id: 1,
20-
requester: 'Ricky',
21-
roomName: 'MyRoom',
22-
status: 'open',
23-
},
24-
lastId: 1,
14+
function oneTicket() {
15+
return {
16+
'1': {
17+
assignee: null,
18+
closed: null,
19+
content: 'content',
20+
id: '1',
21+
requester: 'Ricky',
22+
roomName: 'MyRoom',
23+
status: 'open',
24+
},
25+
lastId: 1,
26+
}
2527
}
2628

29+
function twoTickets() {
30+
return {
31+
'1': {
32+
assignee: null,
33+
closed: null,
34+
content: 'content',
35+
id: '1',
36+
requester: 'Ricky',
37+
roomName: 'MyRoom',
38+
status: 'open',
39+
},
40+
'2': {
41+
assignee: null,
42+
closed: null,
43+
content: 'content',
44+
id: '2',
45+
requester: 'Dmitry',
46+
roomName: 'MyRoom',
47+
status: 'open',
48+
},
49+
lastId: 2,
50+
}
51+
}
52+
53+
2754
function copy(sampleObject) {
2855
return JSON.parse(JSON.stringify(sampleObject))
2956
}
@@ -34,52 +61,72 @@ function message(content) {
3461
return msg
3562
}
3663

64+
/* eslint-disable no-sync */
3765
const TEST_FILE = tmp.fileSync().name
3866

3967
describe('support', function() {
4068
it('ping', function() {
41-
const reply = support.process(tickets, message(''))
69+
const reply = support.process(oneTicket(), message(''))
4270
assert.equal(reply, '#candra: Yes Ricky?')
4371
})
4472

4573
it('close', function() {
46-
const reply = support.process(tickets, message('close #1'))
74+
const reply = support.process(oneTicket(), message('close #1'))
4775
assert.equal(reply, '#candra: ticket #1 is closed')
4876
})
4977

5078
it('debug', function() {
79+
const tickets = oneTicket()
5180
const reply = support.process(tickets, message('debug'))
5281
assert.deepEqual(JSON.parse(reply.slice('#candra: '.length)), tickets)
5382
})
5483

5584
it('invalid', function() {
56-
const reply = support.process(tickets, message('handsome'))
85+
const reply = support.process(oneTicket(), message('handsome'))
5786
assert.equal(reply, '#candra: I don\'t understand: handsome, can you try again?')
5887
})
5988

6089
it('help', function() {
61-
const reply = support.process(tickets, message('help'))
90+
const reply = support.process(oneTicket(), message('help'))
6291
assert.isTrue(reply.indexOf('please') != -1)
6392
})
6493

6594
it('forget it', function() {
95+
const tickets = oneTicket()
6696
const reply = support.process(tickets, message('forget it'))
6797
assert.equal(reply, '#candra: deleted 1 tickets')
6898
assert.equal(tickets.lastId, 0)
6999
})
70100

71101
it('gimme a compliment', function() {
72-
const reply = support.process(tickets, message('gimme a compliment'))
73-
assert.match(reply, /[\.!\?]$/)
102+
const reply = support.process({}, message('gimme a compliment'))
103+
assert.match(reply, /[\.!?)]$/)
74104
})
75105

76106
it('gimme something that does not exist', function() {
77-
const reply = support.process(tickets, message('gimme a unknown'))
107+
const reply = support.process({}, message('gimme a unknown'))
78108
assert.equal(reply, "I don't know how to give you unknown!")
79109
})
80110

81111
it('load and store working', function() {
82112
support.store(TEST_FILE, {var: 4})
83113
assert.equal(support.load(TEST_FILE).var, 4)
84114
})
115+
116+
it('TODO empty', function() {
117+
const reply = support.process({}, message('todo'))
118+
assert.equal(reply, '#candra: nothing TODO!')
119+
})
120+
121+
it('TODO with ticket specific mine', function() {
122+
const reply = support.process(twoTickets(), message('mine'))
123+
assert.match(reply, /ticket #1/)
124+
assert.notMatch(reply, /ticket #2/)
125+
})
126+
127+
it('TODO with ticket specific other user', function() {
128+
const reply = support.process(twoTickets(), message('TODO dmitry'))
129+
assert.notMatch(reply, /ticket #1/)
130+
assert.match(reply, /ticket #2/)
131+
})
85132
})

0 commit comments

Comments
 (0)