Skip to content

Commit

Permalink
Adding the year to the bot day
Browse files Browse the repository at this point in the history
  • Loading branch information
O-Mutt committed Oct 9, 2020
1 parent cf98974 commit de415a5
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 31 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hubot-plusplus-expanded",
"version": "1.1.3",
"version": "1.1.4",
"description": "A hubot script for micro praise",
"main": "index.js",
"engines": {
Expand Down
27 changes: 23 additions & 4 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ function createUpDownVoteRegExp() {
return new RegExp(`${votedObject}${allowSpaceAfterObject}${operator}${reasonForVote}${eol}`, 'i');
}

function getMessageForNewScore(score, name, messageOperator, reason, reasonScore, robotName = '', cakeDay = false) {
function getMessageForNewScore(score, name, messageOperator, reason, reasonScore, cakeDay, robotName = '') {
// if we got a score, then display all the things and fire off events!
if (typeof score !== 'undefined' && score !== null) {
if (name === 'heat') {
Expand Down Expand Up @@ -134,8 +134,9 @@ function getMessageForNewScore(score, name, messageOperator, reason, reasonScore
}
}

if (cakeDay) {
cakeDayStr = `\n:birthday: Today is ${name}'s ${robotName} day! :birthday:`;
if (this.isCakeDay(cakeDay)) {
const yearsAsString = this.getYearsAsString(cakeDay);
cakeDayStr = `\n:birthday: Today is ${name}'s ${yearsAsString} ${robotName} day! :birthday:`;
}
return `${scoreStr}${reasonStr}${cakeDayStr}`;
}
Expand All @@ -146,14 +147,31 @@ function isCakeDay(dateObject) {
try {
const robotDay = new Date(dateObject);
const today = new Date();
if (robotDay.getDay() === today.getDay() && robotDay.getMonth() === today.getMonth()) {
if (robotDay.getDate() === today.getDate() && robotDay.getMonth() === today.getMonth()) {
return true;
}
// eslint-disable-next-line no-empty
} catch (e) { }
return false;
}

function getYearsAsString(dateObj) {
const robotDay = new Date(dateObj);
const today = new Date();
const years = today.getFullYear() - robotDay.getFullYear();
const lastDigit = years.toString().split('').pop();
if (lastDigit === '1') {
return `${years}st`;
}
if (lastDigit === '2') {
return `${years}nd`;
}
if (lastDigit === '3') {
return `${years}rd`;
}
return `${years}th`;
}

const helpers = {
cleanName,
cleanAndEncode,
Expand All @@ -168,6 +186,7 @@ const helpers = {
positiveOperators: positiveOperatorsString,
negativeOperators,
isCakeDay,
getYearsAsString,
};

module.exports = helpers;
9 changes: 3 additions & 6 deletions src/plusplus.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ module.exports = function plusPlus(robot) {
return;
}

const isCakeDay = helper.isCakeDay(userObject[`${robot.name}Day`]) || false;
const message = helper.getMessageForNewScore(newScore, name, operator, reason, reasonScore, robot.name, isCakeDay);
const message = helper.getMessageForNewScore(newScore, name, operator, reason, reasonScore, userObject[`${robot.name}Day`], robot.name);

if (message) {
msg.send(message);
Expand Down Expand Up @@ -139,14 +138,12 @@ module.exports = function plusPlus(robot) {
results = cleanNames.map(async (name) => {
const [newScore, reasonScore, userObject] = await scoreKeeper.add(name, from, room, encodedReason);
robot.logger.debug(`clean names map [${name}]: ${newScore}, the reason ${reasonScore}`);
const isCakeDay = helper.isCakeDay(userObject[`${robot.name}Day`]) || false;
return helper.getMessageForNewScore(newScore, name, operator, encodedReason, reasonScore, robot.name, isCakeDay);
return helper.getMessageForNewScore(newScore, name, operator, encodedReason, reasonScore, userObject[`${robot.name}Day`], robot.name);
});
} else if (`(${helper.negativeOperators})`.match(operator)) {
results = cleanNames.map(async (name) => {
const [newScore, reasonScore, userObject] = await scoreKeeper.subtract(name, from, room, encodedReason);
const isCakeDay = helper.isCakeDay(userObject[`${robot.name}Day`]) || false;
return helper.getMessageForNewScore(newScore, name, operator, encodedReason, reasonScore, robot.name, isCakeDay);
return helper.getMessageForNewScore(newScore, name, operator, encodedReason, reasonScore, userObject[`${robot.name}Day`], robot.name);
});
}
messages = await Promise.all(results);
Expand Down
41 changes: 22 additions & 19 deletions test/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,26 +144,29 @@ describe('Helpers', function () {
mockHelpers.returnsArg(0);
});
forEach([
[undefined, undefined, undefined, undefined, undefined, false, ''],
[1, 'matt', undefined, undefined, undefined, false, 'matt has 1 point.'],
[2, 'matt', undefined, undefined, undefined, false, 'matt has 2 points.'],
[100, 'matt', undefined, undefined, undefined, false, ':100: matt has 100 points :100:'],
[1000, 'matt', undefined, undefined, undefined, false, ':1000: matt has 1000 points :1000:'],
[300, 'matt', undefined, undefined, undefined, false, ':300: matt has 300 points :300:'],
[45, 'matt', '++', 'winning', 1, false, 'matt has 45 points, 1 of which is for winning.'],
[1, 'matt', '++', 'cool runnings!', 1, false, 'matt has 1 point for cool runnings!.'],
[1, 'matt', '++', 'cool runnings!', 1, true, 'matt has 1 point for cool runnings!.\n:birthday: Today is matt\'s testBot day! :birthday:'],
[99, 'matt', '++', 'cool runnings!', 'none', false, 'matt has 99 points, none of which are for cool runnings!.'],
[1, 'matt', '++', 'cool runnings!', 99, false, 'matt has 1 point, 99 of which are for cool runnings!.'], // this doesn't make sense but the message doesn't care
[145, 'matt', '++', 'cool runnings!', 99, false, 'matt has 145 points, 99 of which are for cool runnings!.'],
[200, 'matt', '++', 'cool runnings!', 99, false, ':200: matt has 200 points :200:, 99 of which are for cool runnings!.'],
[0, 'matt', '++', undefined, 0, false, ':zero: matt has 0 points :zero:'],
[28, 'heat', '++', undefined, 0, false, 'podríamos subir un gradin la calefa???\nLa temperatura debería estar en 28 ℃.'],
[28, 'heat', '--', undefined, 0, false, 'podríamos bajar un gradin la calefa???\nLa temperatura debería estar en 28 ℃.'],
[undefined, undefined, undefined, undefined, undefined, undefined, ''],
[1, 'matt', undefined, undefined, undefined, undefined, 'matt has 1 point.'],
[2, 'matt', undefined, undefined, undefined, undefined, 'matt has 2 points.'],
[100, 'matt', undefined, undefined, undefined, undefined, ':100: matt has 100 points :100:'],
[1000, 'matt', undefined, undefined, undefined, undefined, ':1000: matt has 1000 points :1000:'],
[300, 'matt', undefined, undefined, undefined, undefined, ':300: matt has 300 points :300:'],
[45, 'matt', '++', 'winning', 1, undefined, 'matt has 45 points, 1 of which is for winning.'],
[1, 'matt', '++', 'cool runnings!', 1, undefined, 'matt has 1 point for cool runnings!.'],
[1, 'matt', '++', 'cool runnings!', 1, new Date(), 'matt has 1 point for cool runnings!.\n:birthday: Today is matt\'s 1st testBot day! :birthday:'],
[99, 'matt', '++', 'cool runnings!', 'none', undefined, 'matt has 99 points, none of which are for cool runnings!.'],
[1, 'matt', '++', 'cool runnings!', 99, undefined, 'matt has 1 point, 99 of which are for cool runnings!.'], // this doesn't make sense but the message doesn't care
[145, 'matt', '++', 'cool runnings!', 99, undefined, 'matt has 145 points, 99 of which are for cool runnings!.'],
[200, 'matt', '++', 'cool runnings!', 99, undefined, ':200: matt has 200 points :200:, 99 of which are for cool runnings!.'],
[0, 'matt', '++', undefined, 0, undefined, ':zero: matt has 0 points :zero:'],
[28, 'heat', '++', undefined, 0, undefined, 'podríamos subir un gradin la calefa???\nLa temperatura debería estar en 28 ℃.'],
[28, 'heat', '--', undefined, 0, undefined, 'podríamos bajar un gradin la calefa???\nLa temperatura debería estar en 28 ℃.'],
])
.it('should take the score %j, name %j, operator %j, reason %j, reason score %j and print %j',
(score, name, messageOperator, reason, reasonScore, isCake, expectedMessage) => {
const message = helpers.getMessageForNewScore(score, name, messageOperator, reason, reasonScore, 'testBot', isCake);
.it('should take the score %j, name %j, operator %j, reason %j, reason score %j with cake day %j and print %j',
(score, name, messageOperator, reason, reasonScore, cakeDay, expectedMessage) => {
if (cakeDay !== undefined) {
cakeDay.setFullYear((new Date().getFullYear() - 1));
}
const message = helpers.getMessageForNewScore(score, name, messageOperator, reason, reasonScore, cakeDay, 'testBot');
expect(message).to.equal(expectedMessage);
});
});
Expand Down

0 comments on commit de415a5

Please sign in to comment.