Skip to content

Commit e3b1596

Browse files
✨ feat: add Using IDE Features > Typearium project (#97)
* ✨ feat: add Using IDE Features > Typearium project * Fix: actually import solution...
1 parent 49d49c0 commit e3b1596

19 files changed

Lines changed: 484 additions & 0 deletions
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Step 1: Favorite Animals
2+
3+
We keep track of our visitors' favorite animals by a combination of informal polling and park ticket data.
4+
The program we use to work with that data stores that list of favorites as an array of strings... duplicated in multiple places.
5+
We have to modify all those places whenever the list needs updating.
6+
7+
Could you please use your IDE to extract the array into a shared constant, and re-use that constant?
8+
9+
## Files
10+
11+
- `index.ts`: Refactor the functions here
12+
- `index.test.ts`: Tests verifying the refactored functions
13+
- `solution.ts`: Solution code
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { describe, expect, it } from "@jest/globals";
2+
3+
import * as index from "./index";
4+
import * as solution from "./solution";
5+
6+
const { checkIsAnyAnimalFavorite, getFavoriteAnimals, logFavoriteAnimals } =
7+
process.env.TEST_SOLUTIONS ? solution : index;
8+
9+
describe(checkIsAnyAnimalFavorite, () => {
10+
it("returns true for a favorite animal", () => {
11+
expect(checkIsAnyAnimalFavorite("parakeet")).toBe(true);
12+
});
13+
14+
it("returns true for a favorite animal then a non-favorite animal", () => {
15+
expect(checkIsAnyAnimalFavorite("parakeet", "snake")).toBe(true);
16+
});
17+
18+
it("returns true for a non-favorite animal then a favorite animal", () => {
19+
expect(checkIsAnyAnimalFavorite("snake", "parakeet")).toBe(true);
20+
});
21+
22+
it("returns false for a non-favorite animal", () => {
23+
expect(checkIsAnyAnimalFavorite("snake")).toBe(false);
24+
});
25+
26+
it("does not include its own list of animals", () => {
27+
expect(checkIsAnyAnimalFavorite.toString()).not.toMatch(/parakeet/);
28+
});
29+
});
30+
31+
describe(getFavoriteAnimals, () => {
32+
it("returns all favorite animals by default", () => {
33+
expect(getFavoriteAnimals()).toEqual([
34+
"parakeet",
35+
"macaw",
36+
"cat",
37+
"monkey",
38+
"elephant",
39+
"alpaca",
40+
"fox",
41+
]);
42+
});
43+
44+
it("returns a limited quantity of favorite animals when a quantity is provided", () => {
45+
expect(getFavoriteAnimals(3)).toEqual(["parakeet", "macaw", "cat"]);
46+
});
47+
48+
it("does not include its own list of animals", () => {
49+
expect(getFavoriteAnimals.toString()).not.toMatch(/parakeet/);
50+
});
51+
});
52+
53+
describe(logFavoriteAnimals, () => {
54+
it("does not include its own list of animals", () => {
55+
expect(logFavoriteAnimals.toString()).not.toMatch(/parakeet/);
56+
});
57+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Refactor here! ✨
2+
3+
export function checkIsAnyAnimalFavorite(...animals: string[]) {
4+
const favoriteAnimalsUnique = new Set([
5+
"parakeet",
6+
"macaw",
7+
"cat",
8+
"monkey",
9+
"elephant",
10+
"alpaca",
11+
"fox",
12+
]);
13+
14+
return animals.some((animal) => favoriteAnimalsUnique.has(animal));
15+
}
16+
17+
export function getFavoriteAnimals(max = Infinity) {
18+
return [
19+
"parakeet",
20+
"macaw",
21+
"cat",
22+
"monkey",
23+
"elephant",
24+
"alpaca",
25+
"fox",
26+
].slice(0, max);
27+
}
28+
29+
export function logFavoriteAnimals() {
30+
["parakeet", "macaw", "cat", "monkey", "elephant", "alpaca", "fox"].forEach(
31+
(animal, i) => {
32+
console.log(`I like ${animal} number ${i}!`);
33+
}
34+
);
35+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Refactor here! ✨
2+
3+
const favoriteAnimals = [
4+
"parakeet",
5+
"macaw",
6+
"cat",
7+
"monkey",
8+
"elephant",
9+
"alpaca",
10+
"fox",
11+
];
12+
13+
export function checkIsAnyAnimalFavorite(...animals: string[]) {
14+
const favoriteAnimalsUnique = new Set(favoriteAnimals);
15+
16+
return animals.some((animal) => favoriteAnimalsUnique.has(animal));
17+
}
18+
19+
export function getFavoriteAnimals(quantity = Infinity) {
20+
return favoriteAnimals.slice(0, quantity);
21+
}
22+
23+
export function logFavoriteAnimals() {
24+
favoriteAnimals.forEach((animal, i) => {
25+
console.log(`I like ${animal} number ${i}!`);
26+
});
27+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Step 2: Species Collections
2+
3+
Lovely, now we can log our favorite animals much more easily!
4+
Thank you!
5+
6+
...
7+
8+
right click to move to new file
9+
10+
add nesting for fauna/\*, flora/\*
11+
12+
typescript names the files like "MammalsSettings.ts" - change name to just "fauna/mammals.ts"
13+
14+
...
15+
16+
:::tip
17+
once you're done, use find-all-references on `onlyTruthy` and the various settings interfaces to practice navigating with IDE features.
18+
:::
19+
20+
## Files
21+
22+
- `index.ts`: Refactor the functions here
23+
- `index.test.ts`: Tests verifying the refactored functions
24+
- `solution.ts`: Solution code
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { getMammals, MammalsSettings } from "./fauna/mammals.solution";
2+
import { getReptiles, ReptilesSettings } from "./fauna/reptiles.solution";
3+
4+
export interface FaunaSettings {
5+
mammals?: MammalsSettings;
6+
reptiles?: ReptilesSettings;
7+
}
8+
9+
export function getFauna(settings?: FaunaSettings) {
10+
return [getMammals(settings?.mammals), getReptiles(settings?.reptiles)];
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { onlyTruthy } from "../utils/onlyTruthy.solution";
2+
3+
export interface MammalsSettings {
4+
cute?: boolean;
5+
deadly?: boolean;
6+
}
7+
8+
export function getMammals(settings?: MammalsSettings) {
9+
return onlyTruthy(
10+
settings?.cute && [
11+
"cats",
12+
"dogs",
13+
settings?.deadly && "monty python rabbit",
14+
],
15+
settings?.deadly && ["lion", "tiger"]
16+
);
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { onlyTruthy } from "../utils/onlyTruthy.solution";
2+
3+
export interface ReptilesSettings {
4+
ferocious?: boolean;
5+
small?: boolean;
6+
}
7+
8+
export function getReptiles(settings?: ReptilesSettings) {
9+
return onlyTruthy(
10+
settings?.ferocious && "dragon",
11+
settings?.small && ["frog", "gecko"]
12+
);
13+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { FlowersSettings, getFlowers } from "./flora/flowers.solution";
2+
import { TreesSettings, getTrees } from "./flora/trees.solution";
3+
4+
export interface FloraSettings {
5+
flowers?: FlowersSettings;
6+
trees?: TreesSettings;
7+
}
8+
9+
export function getFlora(settings?: FloraSettings) {
10+
return [getFlowers(settings?.flowers), getTrees(settings?.trees)];
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { onlyTruthy } from "../utils/onlyTruthy.solution";
2+
3+
export interface FlowersSettings {
4+
colorful?: boolean;
5+
prickly?: boolean;
6+
}
7+
8+
export function getFlowers(settings?: FlowersSettings) {
9+
return onlyTruthy(
10+
settings?.colorful && ["carnation", "lilac", "tulip"],
11+
settings?.colorful && settings?.prickly && "rose"
12+
);
13+
}

0 commit comments

Comments
 (0)