Skip to content

Commit 0555354

Browse files
committed
Rebrand as Caesar!
1 parent 57fd806 commit 0555354

File tree

5 files changed

+149
-149
lines changed

5 files changed

+149
-149
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
3+
import "./index.ts";
4+
5+
describe("String.prototype.caesar", () => {
6+
it("defaults to the classic Caesar cipher", () => {
7+
expect("ABCDEFGHIJKLMNOPQRSTUVWXYZ".caesar()).toBe(
8+
"BCDEFGHIJKLMNOPQRSTUVWXYZA",
9+
);
10+
expect("abcdefghijklmnopqrstuvwxyz".caesar()).toBe(
11+
"bcdefghijklmnopqrstuvwxyza",
12+
);
13+
});
14+
15+
it("can rotate backwards", () => {
16+
expect("ABCDEFGHIJKLMNOPQRSTUVWXYZ".caesar(-1)).toBe(
17+
"ZABCDEFGHIJKLMNOPQRSTUVWXY",
18+
);
19+
expect("abcdefghijklmnopqrstuvwxyz".caesar(-1)).toBe(
20+
"zabcdefghijklmnopqrstuvwxy",
21+
);
22+
});
23+
24+
it("ignores non-Latin alphabet characters", () => {
25+
expect("Alea iacta est!".caesar(12)).toBe("Mxqm umofm qef!");
26+
expect("Veni, vidi, vici".caesar(2)).toBe("Xgpk, xkfk, xkek");
27+
expect("Et tu, Brute?".caesar(-1)).toBe("Ds st, Aqtsd?");
28+
expect("Civis Romanus sum.".caesar()).toBe("Djwjt Spnbovt tvn.");
29+
});
30+
31+
// TODO: test ROT-13
32+
33+
// TODO: test Unicode characters
34+
35+
// TODO: test inverse
36+
37+
// TODO: more test cases for the existing tests
38+
39+
// TODO: test zero
40+
});

workspaces/adventure-pack/goodies/typescript/String.prototype.rot/index.ts renamed to workspaces/adventure-pack/goodies/typescript/String.prototype.caesar/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import "../String.prototype.ord/index.ts";
44

55
declare global {
66
interface String {
7-
rot(this: String): string;
8-
rot(this: String, delta: number): string;
7+
caesar(this: String): string;
8+
caesar(this: String, delta: number): string;
99
}
1010
}
1111

12-
String.prototype.rot = function (this: String, delta: number = 1): string {
12+
String.prototype.caesar = function (this: String, delta: number = 1): string {
1313
return this.replaceAll(
14-
/([a-z])|([A-Z])/g,
15-
(letter: string, lower: string | undefined, upper: string | undefined) => {
14+
/([a-z])|[A-Z]/g,
15+
(letter: string, lower: string | undefined) => {
1616
const offset = (lower == null ? "A" : "a").ord();
1717
return ((letter.ord() - offset + delta).positiveMod(26) + offset).chr();
1818
},

workspaces/adventure-pack/goodies/typescript/String.prototype.rot/index.test.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

workspaces/adventure-pack/src/app/__tests__/__snapshots__/equip-test.ts.snap

Lines changed: 68 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2383,17 +2383,7 @@ Object.setUnsafe = function (obj, properties, value) {
23832383
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
23842384
`;
23852385

2386-
exports[`App can equip single goody: JavaScript String.prototype.chars 1`] = `
2387-
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
2388-
// Adventure Pack commit fake-commit-hash
2389-
// Running at: https://example.com/
2390-
2391-
String.prototype.chars = String.prototype[Symbol.iterator];
2392-
2393-
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
2394-
`;
2395-
2396-
exports[`App can equip single goody: JavaScript String.prototype.ord 1`] = `
2386+
exports[`App can equip single goody: JavaScript String.prototype.caesar 1`] = `
23972387
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
23982388
// Adventure Pack commit fake-commit-hash
23992389
// Running at: https://example.com/
@@ -2405,14 +2395,39 @@ function nullthrows(value, errorMessage = "Unexpected nullish value!") {
24052395
return value;
24062396
}
24072397

2398+
Number.prototype.chr = function () {
2399+
return String.fromCodePoint(Number(this));
2400+
};
2401+
2402+
Number.prototype.positiveMod = function (modulo) {
2403+
return ((Number(this) % modulo) + Math.abs(modulo)) % modulo;
2404+
};
2405+
24082406
String.prototype.ord = function () {
24092407
return nullthrows(this.codePointAt(0));
24102408
};
24112409

2410+
String.prototype.caesar = function (delta = 1) {
2411+
return this.replaceAll(/([a-z])|[A-Z]/g, (letter, lower) => {
2412+
const offset = (lower == null ? "A" : "a").ord();
2413+
return ((letter.ord() - offset + delta).positiveMod(26) + offset).chr();
2414+
});
2415+
};
2416+
24122417
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
24132418
`;
24142419

2415-
exports[`App can equip single goody: JavaScript String.prototype.rot 1`] = `
2420+
exports[`App can equip single goody: JavaScript String.prototype.chars 1`] = `
2421+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
2422+
// Adventure Pack commit fake-commit-hash
2423+
// Running at: https://example.com/
2424+
2425+
String.prototype.chars = String.prototype[Symbol.iterator];
2426+
2427+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
2428+
`;
2429+
2430+
exports[`App can equip single goody: JavaScript String.prototype.ord 1`] = `
24162431
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
24172432
// Adventure Pack commit fake-commit-hash
24182433
// Running at: https://example.com/
@@ -2424,25 +2439,10 @@ function nullthrows(value, errorMessage = "Unexpected nullish value!") {
24242439
return value;
24252440
}
24262441

2427-
Number.prototype.chr = function () {
2428-
return String.fromCodePoint(Number(this));
2429-
};
2430-
2431-
Number.prototype.positiveMod = function (modulo) {
2432-
return ((Number(this) % modulo) + Math.abs(modulo)) % modulo;
2433-
};
2434-
24352442
String.prototype.ord = function () {
24362443
return nullthrows(this.codePointAt(0));
24372444
};
24382445

2439-
String.prototype.rot = function (delta = 1) {
2440-
return this.replaceAll(/([a-z])|([A-Z])/g, (letter, lower, upper) => {
2441-
const offset = (lower == null ? "A" : "a").ord();
2442-
return ((letter.ord() - offset + delta).positiveMod(26) + offset).chr();
2443-
});
2444-
};
2445-
24462446
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
24472447
`;
24482448

@@ -5000,29 +5000,22 @@ Object.setUnsafe = function (
50005000
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
50015001
`;
50025002

5003-
exports[`App can equip single goody: TypeScript String.prototype.chars 1`] = `
5003+
exports[`App can equip single goody: TypeScript String.prototype.caesar 1`] = `
50045004
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
50055005
// Adventure Pack commit fake-commit-hash
50065006
// Running at: https://example.com/
50075007

50085008
declare global {
5009-
interface String {
5010-
chars(this: String): IterableIterator<string>;
5011-
}
5012-
}
5013-
5014-
String.prototype.chars = String.prototype[Symbol.iterator];
5015-
5016-
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
5017-
`;
5009+
interface Number {
5010+
chr(this: Number): string;
50185011

5019-
exports[`App can equip single goody: TypeScript String.prototype.ord 1`] = `
5020-
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
5021-
// Adventure Pack commit fake-commit-hash
5022-
// Running at: https://example.com/
5012+
positiveMod(this: Number, modulo: number): number;
5013+
}
50235014

5024-
declare global {
50255015
interface String {
5016+
caesar(this: String): string;
5017+
caesar(this: String, delta: number): string;
5018+
50265019
ord(this: String): number;
50275020
}
50285021
}
@@ -5037,30 +5030,55 @@ function nullthrows<T>(
50375030
return value;
50385031
}
50395032

5033+
Number.prototype.chr = function (this: Number): string {
5034+
return String.fromCodePoint(Number(this));
5035+
};
5036+
5037+
Number.prototype.positiveMod = function (this: Number, modulo: number): number {
5038+
return ((Number(this) % modulo) + Math.abs(modulo)) % modulo;
5039+
};
5040+
50405041
String.prototype.ord = function (this: String): number {
50415042
return nullthrows(this.codePointAt(0));
50425043
};
50435044

5045+
String.prototype.caesar = function (this: String, delta: number = 1): string {
5046+
return this.replaceAll(
5047+
/([a-z])|[A-Z]/g,
5048+
(letter: string, lower: string | undefined) => {
5049+
const offset = (lower == null ? "A" : "a").ord();
5050+
return ((letter.ord() - offset + delta).positiveMod(26) + offset).chr();
5051+
},
5052+
);
5053+
};
5054+
50445055
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
50455056
`;
50465057

5047-
exports[`App can equip single goody: TypeScript String.prototype.rot 1`] = `
5058+
exports[`App can equip single goody: TypeScript String.prototype.chars 1`] = `
50485059
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
50495060
// Adventure Pack commit fake-commit-hash
50505061
// Running at: https://example.com/
50515062

50525063
declare global {
5053-
interface Number {
5054-
chr(this: Number): string;
5055-
5056-
positiveMod(this: Number, modulo: number): number;
5064+
interface String {
5065+
chars(this: String): IterableIterator<string>;
50575066
}
5067+
}
5068+
5069+
String.prototype.chars = String.prototype[Symbol.iterator];
50585070

5071+
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
5072+
`;
5073+
5074+
exports[`App can equip single goody: TypeScript String.prototype.ord 1`] = `
5075+
"////////////////////////// BEGIN ADVENTURE PACK CODE ///////////////////////////
5076+
// Adventure Pack commit fake-commit-hash
5077+
// Running at: https://example.com/
5078+
5079+
declare global {
50595080
interface String {
50605081
ord(this: String): number;
5061-
5062-
rot(this: String): string;
5063-
rot(this: String, delta: number): string;
50645082
}
50655083
}
50665084

@@ -5074,28 +5092,10 @@ function nullthrows<T>(
50745092
return value;
50755093
}
50765094

5077-
Number.prototype.chr = function (this: Number): string {
5078-
return String.fromCodePoint(Number(this));
5079-
};
5080-
5081-
Number.prototype.positiveMod = function (this: Number, modulo: number): number {
5082-
return ((Number(this) % modulo) + Math.abs(modulo)) % modulo;
5083-
};
5084-
50855095
String.prototype.ord = function (this: String): number {
50865096
return nullthrows(this.codePointAt(0));
50875097
};
50885098

5089-
String.prototype.rot = function (this: String, delta: number = 1): string {
5090-
return this.replaceAll(
5091-
/([a-z])|([A-Z])/g,
5092-
(letter: string, lower: string | undefined, upper: string | undefined) => {
5093-
const offset = (lower == null ? "A" : "a").ord();
5094-
return ((letter.ord() - offset + delta).positiveMod(26) + offset).chr();
5095-
},
5096-
);
5097-
};
5098-
50995099
/////////////////////////// END ADVENTURE PACK CODE ////////////////////////////"
51005100
`;
51015101

0 commit comments

Comments
 (0)