Skip to content

Commit

Permalink
rename variable, extract variable
Browse files Browse the repository at this point in the history
  • Loading branch information
NovemLinguae committed Dec 8, 2024
1 parent b250839 commit e2cff9f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
9 changes: 5 additions & 4 deletions modules/twinkletag.js
Original file line number Diff line number Diff line change
Expand Up @@ -2017,15 +2017,16 @@ Twinkle.tag.callbacks = {
/**
* Given an array of incompatible tags, check if we have two or more selected
*
* @param {Array} conflictsToCheckFor
* @param {Array} incompatibleTags
* @param {Array} tagsToCheck
* @param {string} [extraMessage]
* @return {true|undefined}
*/
Twinkle.tag.checkIncompatible = function(conflictsToCheckFor, tagsToCheck, extraMessage = null) {
const count = conflictsToCheckFor.reduce((sum, tag) => sum += tagsToCheck.indexOf(tag) !== -1, 0);
Twinkle.tag.checkIncompatible = function(incompatibleTags, tagsToCheck, extraMessage = null) {
const count = incompatibleTags.reduce((sum, tag) => sum += tagsToCheck.indexOf(tag) !== -1, 0);
if (count > 1) {
let message = 'Please select only one of: {{' + conflictsToCheckFor.join('}}, {{') + '}}.';
const incompatibleTagsString = '{{' + incompatibleTags.join('}}, {{') + '}}';
let message = 'Please select only one of: ' + incompatibleTagsString + '.';
message += extraMessage ? ' ' + extraMessage : '';
alert(message);
return true;
Expand Down
28 changes: 14 additions & 14 deletions tests/twinkletag.test.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
describe('modules/twinkletag', () => {
describe('checkIncompatible', () => {
test('no conflicts', () => {
const conflictsToCheckFor = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const incompatibleTags = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const tagsToCheck = ['Better source requested'];
const expected = undefined;
expect(Twinkle.tag.checkIncompatible(conflictsToCheckFor, tagsToCheck)).toBe(expected);
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck)).toBe(expected);
});

test('conflictsToCheckFor, tagsToCheck in alphabetical order', () => {
const conflictsToCheckFor = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
test('incompatibleTags, tagsToCheck in alphabetical order', () => {
const incompatibleTags = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const tagsToCheck = ['Bad GIF', 'Bad JPEG'];
const expected = true;
expect(Twinkle.tag.checkIncompatible(conflictsToCheckFor, tagsToCheck)).toBe(expected);
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck)).toBe(expected);
});

test('conflictsToCheckFor, tagsToCheck not in alphabetical order', () => {
const conflictsToCheckFor = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
test('incompatibleTags, tagsToCheck not in alphabetical order', () => {
const incompatibleTags = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const tagsToCheck = ['Bad JPEG', 'Bad GIF'];
const expected = true;
expect(Twinkle.tag.checkIncompatible(conflictsToCheckFor, tagsToCheck)).toBe(expected);
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck)).toBe(expected);
});

test('conflicts mixed with non-conflicts', () => {
const conflictsToCheckFor = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const incompatibleTags = ['Bad GIF', 'Bad JPEG', 'Bad SVG', 'Bad format'];
const tagsToCheck = ['Better source requested', 'Bad GIF', 'Maybe free media', 'Bad JPEG', 'Copy to Commons'];
const expected = true;
expect(Twinkle.tag.checkIncompatible(conflictsToCheckFor, tagsToCheck)).toBe(expected);
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck)).toBe(expected);
});

test('extra param, no conflicts', () => {
const conflictsToCheckFor = ['Merge', 'Merge from', 'Merge to'];
const incompatibleTags = ['Merge', 'Merge from', 'Merge to'];
const tagsToCheck = ['One source'];
const extraMessage = 'If several merges are required, use {{Merge}} and separate the article names with pipes (although in this case Twinkle cannot tag the other articles automatically).';
const expected = undefined;
expect(Twinkle.tag.checkIncompatible(conflictsToCheckFor, tagsToCheck, extraMessage)).toBe(expected);
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck, extraMessage)).toBe(expected);
});

test('extra param, conflicts', () => {
const conflictsToCheckFor = ['Merge', 'Merge from', 'Merge to'];
const incompatibleTags = ['Merge', 'Merge from', 'Merge to'];
const tagsToCheck = ['Merge from', 'Merge to'];
const extraMessage = 'If several merges are required, use {{Merge}} and separate the article names with pipes (although in this case Twinkle cannot tag the other articles automatically).';
const expected = true;
expect(Twinkle.tag.checkIncompatible(conflictsToCheckFor, tagsToCheck, extraMessage)).toBe(expected);
expect(Twinkle.tag.checkIncompatible(incompatibleTags, tagsToCheck, extraMessage)).toBe(expected);
});
});
});

0 comments on commit e2cff9f

Please sign in to comment.