From d9a5abaa988f26052653614287e4e83d2f16c39d Mon Sep 17 00:00:00 2001 From: synzen Date: Sat, 6 Feb 2021 09:52:04 -0500 Subject: [PATCH] Support any n/p comparisons to be added --- src/commands/compare.js | 33 +++++++++++++++++-------- src/tests/commands/unit_compare.test.js | 32 ++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/tests/commands/unit_compare.test.js diff --git a/src/commands/compare.js b/src/commands/compare.js index c9fec56ad..39031fd35 100644 --- a/src/commands/compare.js +++ b/src/commands/compare.js @@ -7,14 +7,20 @@ const getConfig = require('../config.js').get const createLogger = require('../util/logger/create.js') /** - * @param {string} str + * @param {string} command */ -function getValidInputs (str) { - const parts = str.split(' ') +function getValidInputs (command) { + const parts = command.split(' ') + // Remove the command name, such as rss.compare parts.shift() const cleanedParts = parts .map(p => p.trim()) - .filter((p, index) => p && parts.indexOf(p) === index && p.length > 1 && (p.startsWith('+') || p.startsWith('-'))) + .filter((p, index, array) => { + const exists = !!p + const isNotDupe = parts.indexOf(p) === index + const hasCorrectSymbols = p.startsWith('+') || p.startsWith('-') + return exists && isNotDupe && hasCorrectSymbols + }) return cleanedParts } @@ -53,10 +59,6 @@ module.exports = async (message, command) => { const stringified = `\`${invalids.join('`,`')}\`` return message.channel.send(translate('commands.compare.invalid', { errors: stringified })) } - // Temporary check - if (validProperties.length !== 1 || validProperties[0] !== '-title') { - return message.channel.send(translate('commands.compare.onlyTitle')) - } } const selectFeedNode = new PromptNode(commonPrompts.selectFeed.prompt) const { selectedFeed: feed } = await runWithFeedsProfile(selectFeedNode, message) @@ -75,6 +77,7 @@ module.exports = async (message, command) => { } const log = createLogger(message.guild.shard.id) + if (reset) { feed.ncomparisons = [] await feed.save() @@ -83,12 +86,22 @@ module.exports = async (message, command) => { guild: message.guild }, 'Comparisons have been reset') } else { - feed.ncomparisons = ['title'] + const newPValues = validProperties + .filter((prop) => prop.startsWith('+')) + .map((s) => s.replace('+', '')) + const newNValues = validProperties + .filter((prop) => prop.startsWith('-')) + .map((s) => s.replace('-', '')) + console.log(validProperties.join('\n')) + feed.ncomparisons = newNValues + feed.pcomparisons = newPValues await feed.save() const str = `\`${validProperties.join('`\n`')}\`` await message.channel.send(translate('commands.compare.success', { added: str, url: feed.url })) log.info({ guild: message.guild - }, `Comparisons have set ${JSON.stringify(feed.ncomparisons)}`) + }, `Comparisons have set ${JSON.stringify(validProperties)}`) } } + +module.exports.getValidInputs = getValidInputs diff --git a/src/tests/commands/unit_compare.test.js b/src/tests/commands/unit_compare.test.js new file mode 100644 index 000000000..bc7c91822 --- /dev/null +++ b/src/tests/commands/unit_compare.test.js @@ -0,0 +1,32 @@ +const compare = require('../../commands/compare') + +describe('commands/compare', () => { + describe('getValidInputs', () => { + it('returns the the properties correctly', () => { + const input = 'rss.compare +title -description +author +date' + const parts = compare.getValidInputs(input) + expect(parts).toEqual([ + '+title', + '-description', + '+author', + '+date' + ]) + }) + it('ignores invalid input', () => { + const input = 'rss.compare +title description author +date' + const parts = compare.getValidInputs(input) + expect(parts).toEqual([ + '+title', + '+date' + ]) + }) + it('ignores duplicates', () => { + const input = 'rss.compare +title +date +title' + const parts = compare.getValidInputs(input) + expect(parts).toEqual([ + '+title', + '+date' + ]) + }) + }) +})