Skip to content

Commit 0262e51

Browse files
committed
Metadata description improvements
1 parent c1a3df4 commit 0262e51

9 files changed

Lines changed: 162 additions & 183 deletions

File tree

  • src
    • _data
    • blog
      • 2021-05-23-generics-arent-as-scary-as-you-think
      • 2021-06-09-how-to-create-custom-hooks
      • 2021-07-03-mocking-in-jest-with-typescript-and-react
      • 2021-10-09-eslint-vs-prettier
      • 2022-01-03-what-is-a-number
      • 2022-02-28-what-is-memoization
      • 2022-10-24-default-options-pattern-in-typescript
    • landingpage

src/_data/site.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module.exports = {
22
url: "https://rupertmckay.com",
33
title: "Rupert Foggo McKay",
4-
description: "Rupert's website.",
4+
description: "Technical blog exploring TypeScript, React, JavaScript, and computer science fundamentals. In-depth articles on software development, testing, and mathematical concepts.",
55
author: {
66
name: "Rupert Foggo McKay",
77
email: "rupert@rupertmckay.com",

src/blog/2021-05-23-generics-arent-as-scary-as-you-think/index.md

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: Generics aren't as scary as you think
3-
description: You already understand generics... you just don't know you do.
3+
description: Learn TypeScript generics through familiar examples like Arrays and Promises. Discover how you're already using generics without realizing it.
44
date: 2021-05-23
55
layout: layouts/post.njk
66
---
7+
78
I'll be demonstrating generics using TypeScript, but the core principles of generics are the same in any programming language.
89

910
## First things first
@@ -39,16 +40,16 @@ Now let's pretend `filter` didn't already exist, and we wanted to make our own f
3940

4041
```ts
4142
function numberFilter(
42-
array: Array<number>,
43-
callback: (num: number) => boolean
43+
array: Array<number>,
44+
callback: (num: number) => boolean,
4445
) {
45-
const result: Array<number> = [];
46-
for (const num of array) {
47-
if (callback(num)) {
48-
result.push(num);
49-
}
50-
}
51-
return result;
46+
const result: Array<number> = [];
47+
for (const num of array) {
48+
if (callback(num)) {
49+
result.push(num);
50+
}
51+
}
52+
return result;
5253
}
5354

5455
const numbers = [1, 31, 12, 40];
@@ -64,16 +65,16 @@ To really prove this point, lets show what a custom string filter function might
6465

6566
```ts
6667
function stringFilter(
67-
array: Array<string>,
68-
callback: (num: string) => boolean
68+
array: Array<string>,
69+
callback: (num: string) => boolean,
6970
) {
70-
const result: Array<string> = [];
71-
for (const str of array) {
72-
if (callback(str)) {
73-
result.push(str);
74-
}
75-
}
76-
return result;
71+
const result: Array<string> = [];
72+
for (const str of array) {
73+
if (callback(str)) {
74+
result.push(str);
75+
}
76+
}
77+
return result;
7778
}
7879

7980
const strings = ["cat", "horse", "dog", "dinosaur"];
@@ -87,16 +88,16 @@ What if we could abstract the implementation from the specific type of element t
8788

8889
```ts
8990
function genericFilter<Element>(
90-
array: Array<Element>,
91-
callback: (element: Element) => boolean
91+
array: Array<Element>,
92+
callback: (element: Element) => boolean,
9293
) {
93-
const result: Array<T> = [];
94-
for (const element of array) {
95-
if (callback(element)) {
96-
result.push(element);
97-
}
98-
}
99-
return result;
94+
const result: Array<T> = [];
95+
for (const element of array) {
96+
if (callback(element)) {
97+
result.push(element);
98+
}
99+
}
100+
return result;
100101
}
101102

102103
// Works with numbers!

src/blog/2021-06-09-how-to-create-custom-hooks/index.md

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: How to Create Custom Hooks
3-
description: DRY component logic and separation of rendering from state management.
3+
description: Master React custom hooks to encapsulate reusable functionality, keep components clean, and separate rendering from state management.
44
date: 2021-06-09
55
layout: layouts/post.njk
66
---
7+
78
React hooks are a powerful tool for managing state in your React components. In this post I'll be demonstrating how we can compose existing React hooks to make custom hooks that encapsulate reusable functionality.
89

910
React's built-in hooks provide a great deal of power and flexibility, between `useState`, `useEffect` and `useCallback` we can cover very many use cases. But often times we'll find ourselves repeating a pattern of hooks over and over again, or maybe we'd simply prefer to pull out a custom hook to keep our component definitions short and expressive.
@@ -28,7 +29,7 @@ Here we have a `WelcomePage` component which makes a call to some utility method
2829

2930
```ts
3031
const useOnMount = (callback: () => void) => {
31-
React.useEffect(callback, []);
32+
React.useEffect(callback, []);
3233
};
3334
```
3435

@@ -50,10 +51,10 @@ For simple pieces of boolean state we typically only want a way to read the curr
5051

5152
```ts
5253
const useToggle = (initialValue: boolean) => {
53-
const [value, setValue] = React.useState(initialValue);
54-
const toggle = () => setValue(!value);
54+
const [value, setValue] = React.useState(initialValue);
55+
const toggle = () => setValue(!value);
5556

56-
return { value, toggle };
57+
return { value, toggle };
5758
};
5859
```
5960

@@ -75,11 +76,11 @@ This next example is a little more complex. Perhaps you have a list of images in
7576

7677
```ts
7778
const useCircularIndex = (length: number) => {
78-
const [index, setIndex] = React.useState(0);
79-
const next = () => setIndex(index === length - 1 ? 0 : index + 1);
80-
const prev = () => setIndex(index === 0 ? length - 1 : index - 1);
79+
const [index, setIndex] = React.useState(0);
80+
const next = () => setIndex(index === length - 1 ? 0 : index + 1);
81+
const prev = () => setIndex(index === 0 ? length - 1 : index - 1);
8182

82-
return { index, next, prev };
83+
return { index, next, prev };
8384
};
8485
```
8586

@@ -189,19 +190,19 @@ type ReadyState = { state: "ready"; userDetails: UserDetails };
189190
type UserDetailsResponse = LoadingState | ErrorState | ReadyState;
190191

191192
const useGetUserDetails = (userId: string) => {
192-
const [response, setResponse] = React.useState<UserDetailsResponse>({
193-
state: "loading",
194-
});
195-
196-
React.useEffect(() => {
197-
getUserDetails(userId)
198-
.then((response) =>
199-
setResponse({ state: "ready", userDetails: response })
200-
)
201-
.catch((error) => setResponse({ state: "error", error }));
202-
}, [userId, getUserDetails]);
203-
204-
return { response };
193+
const [response, setResponse] = React.useState<UserDetailsResponse>({
194+
state: "loading",
195+
});
196+
197+
React.useEffect(() => {
198+
getUserDetails(userId)
199+
.then((response) =>
200+
setResponse({ state: "ready", userDetails: response }),
201+
)
202+
.catch((error) => setResponse({ state: "error", error }));
203+
}, [userId, getUserDetails]);
204+
205+
return { response };
205206
};
206207
```
207208

src/blog/2021-07-03-mocking-in-jest-with-typescript-and-react/index.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: Mocking in Jest with TypeScript and React
3-
description: It is functions all the way down.
3+
description: Practical guide to mocking in Jest with TypeScript. Learn type-safe mock functions and how to mock React components effectively.
44
date: 2021-07-03
55
layout: layouts/post.njk
66
---
7+
78
Key takeaways:
89

910
- `jest.mock` covers many use cases
@@ -109,8 +110,8 @@ import { getUserDetails } from "./user-storage";
109110

110111
// Declare that our mock implementation must return a valid "getUserDetails" implementation
111112
jest.mock("./user-storage", (): { getUserDetails: typeof getUserDetails } => ({
112-
// TypeScript will now correctly tell us this doesn't match the expected type
113-
getUserDetails: () => ({ naaaaaaaame: "rupert" }),
113+
// TypeScript will now correctly tell us this doesn't match the expected type
114+
getUserDetails: () => ({ naaaaaaaame: "rupert" }),
114115
}));
115116
```
116117
@@ -122,8 +123,8 @@ import * as UserStorage from "./user-storage";
122123

123124
// And use the 'typeof' that namespace to enforce our mock matches
124125
jest.mock("./user-storage", (): typeof UserStorage => ({
125-
// Correctly tells us this doesn't match the expected type
126-
getUserDetails: () => ({ naaaaaaaame: "rupert" }),
126+
// Correctly tells us this doesn't match the expected type
127+
getUserDetails: () => ({ naaaaaaaame: "rupert" }),
127128
}));
128129
```
129130
@@ -239,15 +240,15 @@ One other thing we really need to watch out for here though is making sure we cl
239240
240241
```ts
241242
it("something is fishy here", () => {
242-
expect(mockGetUserDetails).toHaveBeenCalledWith("1234");
243+
expect(mockGetUserDetails).toHaveBeenCalledWith("1234");
243244
});
244245
```
245246
246247
That test will pass! But how? It doesn't do anything, yet somehow it is still true that the mock was called with that argument. Well, just like mock implementations persist through the whole test file, so too does the mock's "memory" of when it has been called. To prevent this confusing behavior, we should clear the "memory" of mocks between tests:
247248
248249
```ts
249250
beforeEach(() => {
250-
jest.clearAllMocks();
251+
jest.clearAllMocks();
251252
});
252253
```
253254
@@ -356,8 +357,8 @@ But when we inevitably do want to test a component rendered within a context, I
356357
357358
```ts
358359
expect(mockThirdPartyWidget).toHaveBeenCalledWith(
359-
{ userId: "1234" },
360-
expect.any({}) // Ignore React contexts
360+
{ userId: "1234" },
361+
expect.any({}), // Ignore React contexts
361362
);
362363
```
363364
@@ -430,9 +431,9 @@ Rupert
430431
431432
Library versions used when writing this post:
432433
433-
| Dependency | Version |
434-
| ---------------------- | ------- |
434+
| Dependency | Version |
435+
| ------------------------ | ------- |
435436
| `jest` | 27.0.6 |
436437
| `react` | 17.0.2 |
437438
| `typescript` | 4.3.5 |
438-
| `@testing-library/react` | 12.0.0 |
439+
| `@testing-library/react` | 12.0.0 |

src/blog/2021-10-09-eslint-vs-prettier/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: ESLint vs Prettier
3-
description: Why I don't use the 'fix' option.
3+
description: Understanding the distinction between ESLint's semantic error detection and Prettier's syntactic formatting. Why automated tools can't fix everything.
44
date: 2021-10-09
55
layout: layouts/post.njk
66
---

src/blog/2022-01-03-what-is-a-number/index.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
---
22
title: What is a number?
3-
description: Rounding errors are inevitable.
3+
description: Explore why JavaScript's 0.1 * 0.2 doesn't equal 0.02, understanding floating-point arithmetic and the fundamental limits of numeric representation.
44
date: 2022-01-03
55
layout: layouts/post.njk
66
---
7+
78
TL;DR:
89

910
- No datatype will ever be able to represent all numeric values perfectly
@@ -129,16 +130,19 @@ We call the leading bits the "Mantissa" and the power of 2 the "Exponent".
129130
To demonstrate this, consider the following numbers:
130131

131132
5 in binary is 101:
133+
132134
- Mantissa: 1.01
133135
- Exponent: 10
134136

135137
> Note that the exponent is _also_ in binary so "10" here is not "ten" but rather "two".
136138
137139
9 in binary is 1001:
140+
138141
- Mantissa: 1.001
139142
- Exponent: 11
140143

141144
7/32 in binary is 0.00111:
145+
142146
- Mantissa: 1.11
143147
- Exponent: -11
144148

@@ -149,10 +153,12 @@ This format has the advantage of being very flexible in accomodating both very l
149153
First, we convert the numbers to their floating-point representations:
150154

151155
0.1 in binary is 0.00011001100...:
156+
152157
- Mantissa: 1.1001100...
153158
- Exponent: -100
154159

155-
0.2 in binary is 0.0011001100...:
160+
0.2 in binary is 0.0011001100...:
161+
156162
- Mantissa: 1.1001100...
157163
- Exponent: -11
158164

@@ -161,6 +167,7 @@ You might already see a problem, that our numeric representations have become an
161167
Then to complete our computation, we multiply the mantissas together and combine the exponents, which leaves us with:
162168

163169
0.0000010100011110101...
170+
164171
- Mantissa: 1.0100011110101...
165172
- Exponent: -110
166173

0 commit comments

Comments
 (0)