Skip to content

Commit bcf62ec

Browse files
authored
Merge pull request #1903 from FranciscoPombal/rot13_numbers_negative_shift_amount
fix: ROT13 - shifting numbers by negative amounts
2 parents 004ef7f + 7e5eb01 commit bcf62ec

File tree

2 files changed

+89
-11
lines changed

2 files changed

+89
-11
lines changed

src/core/operations/ROT13.mjs

+4-3
Original file line numberDiff line numberDiff line change
@@ -59,23 +59,24 @@ class ROT13 extends Operation {
5959
rot13Upperacse = args[1],
6060
rotNumbers = args[2];
6161
let amount = args[3],
62-
chr;
62+
amountNumbers = args[3];
6363

6464
if (amount) {
6565
if (amount < 0) {
6666
amount = 26 - (Math.abs(amount) % 26);
67+
amountNumbers = 10 - (Math.abs(amountNumbers) % 10);
6768
}
6869

6970
for (let i = 0; i < input.length; i++) {
70-
chr = input[i];
71+
let chr = input[i];
7172
if (rot13Upperacse && chr >= 65 && chr <= 90) { // Upper case
7273
chr = (chr - 65 + amount) % 26;
7374
output[i] = chr + 65;
7475
} else if (rot13Lowercase && chr >= 97 && chr <= 122) { // Lower case
7576
chr = (chr - 97 + amount) % 26;
7677
output[i] = chr + 97;
7778
} else if (rotNumbers && chr >= 48 && chr <= 57) { // Numbers
78-
chr = (chr - 48 + amount) % 10;
79+
chr = (chr - 48 + amountNumbers) % 10;
7980
output[i] = chr + 48;
8081
}
8182
}

tests/operations/tests/Rotate.mjs

+85-8
Original file line numberDiff line numberDiff line change
@@ -135,32 +135,65 @@ TestRegister.addTests([
135135
},
136136
],
137137
},
138+
{
139+
name: "ROT13: no shift amount",
140+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
141+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
142+
recipeConfig: [
143+
{
144+
op: "ROT13",
145+
args: [true, true, true, 0]
146+
},
147+
],
148+
},
138149
{
139150
name: "ROT13: normal",
140-
input: "The Quick Brown Fox Jumped Over The Lazy Dog.",
141-
expectedOutput: "Gur Dhvpx Oebja Sbk Whzcrq Bire Gur Ynml Qbt.",
151+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
152+
expectedOutput: "Gur Dhvpx Oebja Sbk Whzcrq Bire Gur Ynml Qbt. 3456789012",
142153
recipeConfig: [
143154
{
144155
op: "ROT13",
145156
args: [true, true, true, 13]
146157
},
147158
],
148159
},
160+
{
161+
name: "ROT13: negative shift amount",
162+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
163+
expectedOutput: "Gur Dhvpx Oebja Sbk Whzcrq Bire Gur Ynml Qbt. 7890123456",
164+
recipeConfig: [
165+
{
166+
op: "ROT13",
167+
args: [true, true, true, -13]
168+
},
169+
],
170+
},
149171
{
150172
name: "ROT13: full loop",
151-
input: "The Quick Brown Fox Jumped Over The Lazy Dog.",
152-
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog.",
173+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
174+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 6789012345",
153175
recipeConfig: [
154176
{
155177
op: "ROT13",
156178
args: [true, true, true, 26]
157179
},
158180
],
159181
},
182+
{
183+
name: "ROT13: full loop (negative shift amount)",
184+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
185+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 4567890123",
186+
recipeConfig: [
187+
{
188+
op: "ROT13",
189+
args: [true, true, true, -26]
190+
},
191+
],
192+
},
160193
{
161194
name: "ROT13: lowercase only",
162-
input: "The Quick Brown Fox Jumped Over The Lazy Dog.",
163-
expectedOutput: "Tur Qhvpx Bebja Fbk Jhzcrq Oire Tur Lnml Dbt.",
195+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
196+
expectedOutput: "Tur Qhvpx Bebja Fbk Jhzcrq Oire Tur Lnml Dbt. 0123456789",
164197
recipeConfig: [
165198
{
166199
op: "ROT13",
@@ -170,15 +203,59 @@ TestRegister.addTests([
170203
},
171204
{
172205
name: "ROT13: uppercase only",
173-
input: "The Quick Brown Fox Jumped Over The Lazy Dog.",
174-
expectedOutput: "Ghe Duick Orown Sox Wumped Bver Ghe Yazy Qog.",
206+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
207+
expectedOutput: "Ghe Duick Orown Sox Wumped Bver Ghe Yazy Qog. 0123456789",
175208
recipeConfig: [
176209
{
177210
op: "ROT13",
178211
args: [false, true, false, 13]
179212
},
180213
],
181214
},
215+
{
216+
name: "ROT13: numbers only",
217+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
218+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 5678901234",
219+
recipeConfig: [
220+
{
221+
op: "ROT13",
222+
args: [false, false, true, 5]
223+
},
224+
],
225+
},
226+
{
227+
name: "ROT13: numbers only (negative shift amount)",
228+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
229+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 5678901234",
230+
recipeConfig: [
231+
{
232+
op: "ROT13",
233+
args: [false, false, true, 5]
234+
},
235+
],
236+
},
237+
{
238+
name: "ROT13: numbers only loop",
239+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
240+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
241+
recipeConfig: [
242+
{
243+
op: "ROT13",
244+
args: [false, false, true, 10]
245+
},
246+
],
247+
},
248+
{
249+
name: "ROT13: numbers only loop (negative shift amount)",
250+
input: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
251+
expectedOutput: "The Quick Brown Fox Jumped Over The Lazy Dog. 0123456789",
252+
recipeConfig: [
253+
{
254+
op: "ROT13",
255+
args: [false, false, true, -10]
256+
},
257+
],
258+
},
182259
{
183260
name: "ROT47: nothing",
184261
input: "",

0 commit comments

Comments
 (0)