Skip to content

Commit

Permalink
Support any n/p comparisons to be added
Browse files Browse the repository at this point in the history
  • Loading branch information
synzen committed Feb 6, 2021
1 parent 4bf43e5 commit d9a5aba
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/commands/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
Expand All @@ -75,6 +77,7 @@ module.exports = async (message, command) => {
}

const log = createLogger(message.guild.shard.id)

if (reset) {
feed.ncomparisons = []
await feed.save()
Expand All @@ -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
32 changes: 32 additions & 0 deletions src/tests/commands/unit_compare.test.js
Original file line number Diff line number Diff line change
@@ -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'
])
})
})
})

0 comments on commit d9a5aba

Please sign in to comment.