Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Step 1: Favorite Animals

We keep track of our visitors' favorite animals by a combination of informal polling and park ticket data.
The program we use to work with that data stores that list of favorites as an array of strings... duplicated in multiple places.
We have to modify all those places whenever the list needs updating.

Could you please use your IDE to extract the array into a shared constant, and re-use that constant?

## Files

- `index.ts`: Refactor the functions here
- `index.test.ts`: Tests verifying the refactored functions
- `solution.ts`: Solution code
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { describe, expect, it } from "@jest/globals";

import * as index from "./index";
import * as solution from "./solution";

const { checkIsAnyAnimalFavorite, getFavoriteAnimals, logFavoriteAnimals } =
process.env.TEST_SOLUTIONS ? solution : index;

describe(checkIsAnyAnimalFavorite, () => {
it("returns true for a favorite animal", () => {
expect(checkIsAnyAnimalFavorite("parakeet")).toBe(true);
});

it("returns true for a favorite animal then a non-favorite animal", () => {
expect(checkIsAnyAnimalFavorite("parakeet", "snake")).toBe(true);
});

it("returns true for a non-favorite animal then a favorite animal", () => {
expect(checkIsAnyAnimalFavorite("snake", "parakeet")).toBe(true);
});

it("returns false for a non-favorite animal", () => {
expect(checkIsAnyAnimalFavorite("snake")).toBe(false);
});

it("does not include its own list of animals", () => {
expect(checkIsAnyAnimalFavorite.toString()).not.toMatch(/parakeet/);
});
});

describe(getFavoriteAnimals, () => {
it("returns all favorite animals by default", () => {
expect(getFavoriteAnimals()).toEqual([
"parakeet",
"macaw",
"cat",
"monkey",
"elephant",
"alpaca",
"fox",
]);
});

it("returns a limited quantity of favorite animals when a quantity is provided", () => {
expect(getFavoriteAnimals(3)).toEqual(["parakeet", "macaw", "cat"]);
});

it("does not include its own list of animals", () => {
expect(getFavoriteAnimals.toString()).not.toMatch(/parakeet/);
});
});

describe(logFavoriteAnimals, () => {
it("does not include its own list of animals", () => {
expect(logFavoriteAnimals.toString()).not.toMatch(/parakeet/);
});
});
35 changes: 35 additions & 0 deletions projects/using-ide-features/typearium/01-favorite-animals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Refactor here! ✨

export function checkIsAnyAnimalFavorite(...animals: string[]) {
const favoriteAnimalsUnique = new Set([
"parakeet",
"macaw",
"cat",
"monkey",
"elephant",
"alpaca",
"fox",
]);

return animals.some((animal) => favoriteAnimalsUnique.has(animal));
}

export function getFavoriteAnimals(max = Infinity) {
return [
"parakeet",
"macaw",
"cat",
"monkey",
"elephant",
"alpaca",
"fox",
].slice(0, max);
}

export function logFavoriteAnimals() {
["parakeet", "macaw", "cat", "monkey", "elephant", "alpaca", "fox"].forEach(
(animal, i) => {
console.log(`I like ${animal} number ${i}!`);
}
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Refactor here! ✨

const favoriteAnimals = [
"parakeet",
"macaw",
"cat",
"monkey",
"elephant",
"alpaca",
"fox",
];

export function checkIsAnyAnimalFavorite(...animals: string[]) {
const favoriteAnimalsUnique = new Set(favoriteAnimals);

return animals.some((animal) => favoriteAnimalsUnique.has(animal));
}

export function getFavoriteAnimals(quantity = Infinity) {
return favoriteAnimals.slice(0, quantity);
}

export function logFavoriteAnimals() {
favoriteAnimals.forEach((animal, i) => {
console.log(`I like ${animal} number ${i}!`);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Step 2: Species Collections

Lovely, now we can log our favorite animals much more easily!
Thank you!

...

right click to move to new file

add nesting for fauna/\*, flora/\*

typescript names the files like "MammalsSettings.ts" - change name to just "fauna/mammals.ts"

...

:::tip
once you're done, use find-all-references on `onlyTruthy` and the various settings interfaces to practice navigating with IDE features.
:::

## Files

- `index.ts`: Refactor the functions here
- `index.test.ts`: Tests verifying the refactored functions
- `solution.ts`: Solution code
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { getMammals, MammalsSettings } from "./fauna/mammals.solution";
import { getReptiles, ReptilesSettings } from "./fauna/reptiles.solution";

export interface FaunaSettings {
mammals?: MammalsSettings;
reptiles?: ReptilesSettings;
}

export function getFauna(settings?: FaunaSettings) {
return [getMammals(settings?.mammals), getReptiles(settings?.reptiles)];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { onlyTruthy } from "../utils/onlyTruthy.solution";

export interface MammalsSettings {
cute?: boolean;
deadly?: boolean;
}

export function getMammals(settings?: MammalsSettings) {
return onlyTruthy(
settings?.cute && [
"cats",
"dogs",
settings?.deadly && "monty python rabbit",
],
settings?.deadly && ["lion", "tiger"]
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onlyTruthy } from "../utils/onlyTruthy.solution";

export interface ReptilesSettings {
ferocious?: boolean;
small?: boolean;
}

export function getReptiles(settings?: ReptilesSettings) {
return onlyTruthy(
settings?.ferocious && "dragon",
settings?.small && ["frog", "gecko"]
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { FlowersSettings, getFlowers } from "./flora/flowers.solution";
import { TreesSettings, getTrees } from "./flora/trees.solution";

export interface FloraSettings {
flowers?: FlowersSettings;
trees?: TreesSettings;
}

export function getFlora(settings?: FloraSettings) {
return [getFlowers(settings?.flowers), getTrees(settings?.trees)];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onlyTruthy } from "../utils/onlyTruthy.solution";

export interface FlowersSettings {
colorful?: boolean;
prickly?: boolean;
}

export function getFlowers(settings?: FlowersSettings) {
return onlyTruthy(
settings?.colorful && ["carnation", "lilac", "tulip"],
settings?.colorful && settings?.prickly && "rose"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { onlyTruthy } from "../utils/onlyTruthy.solution";

export interface TreesSettings {
evergreen?: boolean;
fruitBearing?: boolean;
}

export function getTrees(settings?: TreesSettings) {
return onlyTruthy(
settings?.evergreen && "pine",
settings?.fruitBearing && ["apple", "pear"]
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Refactor here! ✨

import { FaunaSettings, getFauna } from "./fauna.solution";
import { FloraSettings, getFlora } from "./flora.solution";
import { onlyTruthy } from "./utils/onlyTruthy.solution";

export interface EverythingSettings {
fauna?: FaunaSettings;
flora?: FloraSettings;
}

export function getEverything(settings?: EverythingSettings) {
return onlyTruthy(getFauna(settings?.fauna), getFlora(settings?.flora));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { describe, expect, it, test } from "@jest/globals";

import * as index from "./index";
import * as solution from "./index.solution";

const { getEverything } = process.env.TEST_SOLUTIONS ? solution : index;

describe(getEverything, () => {
it("returns nothing with no settings", () => {
expect(getEverything()).toMatchInlineSnapshot(`Array []`);
});

it("returns everything with all settings", () => {
expect(
getEverything({
fauna: {
mammals: {
cute: true,
deadly: true,
},

reptiles: {
ferocious: true,
small: true,
},
},

flora: {
flowers: {
colorful: true,
prickly: true,
},

trees: {
evergreen: true,
fruitBearing: true,
},
},
})
).toMatchInlineSnapshot(`
Array [
"cats",
"dogs",
"monty python rabbit",
"lion",
"tiger",
"dragon",
"frog",
"gecko",
"carnation",
"lilac",
"tulip",
"rose",
"pine",
"apple",
"pear",
]
`);
});
});
Loading