Skip to content

Instantly share code, notes, and snippets.

View jfhector's full-sized avatar

J.F. Hector jfhector

View GitHub Profile

react-intl: When to use defineMessage and why

It's generally better to define messages inline as arguments of intl.formatMessage(...). (Because linting only works that way.)

But this pageH1Message needs to be used in two places. So we assign that message to a variable using defineMessage, then use that message in two places.

(If we had defined the same message twice inline, we'd have ended up with two identical messages in the translation file, with makes managing translation harder).

@jfhector
jfhector / mockExpressEndpoint.ts
Created March 23, 2022 22:43
mockExpressEndpoint
import express from 'express';
import cors from 'cors';
const PORT = 7890;
const server = express();
server.use(cors())
server.get('/me', (req, res) => {
console.log("A request was just made");
@jfhector
jfhector / README.md
Created March 6, 2022 14:22
Implementing simple Monads

Implementing simple Monads

From the book "Mastering Functional Programming"

@jfhector
jfhector / README.md
Last active March 5, 2022 14:15
Lenses in JS

Lenses in JS

From "Mastering JavaScript Functional Programming"

@jfhector
jfhector / README.md
Last active March 2, 2022 20:50
The tower of Hanoi problem solved using recursion
@jfhector
jfhector / README.md
Created February 24, 2022 14:23
Getting a value from a request header, and using that value to show a banner – both Server-Side and Client-Side rendered

Getting a value from a request header, and using that value to show a banner – both Server-Side and Client-Side rendered

How this works

### In the renderApp Express route handler

  1. We look at the value of a request header with the visitor's location country.
  2. We pass that value to <App visitorLocationCountry={visitorLocationCountry} /> (i.e. App being called when rendered to string on the server).
  3. We generate a HTML meta tag string using that visitor code, and put that in the head of the generated index.html document
@jfhector
jfhector / assertHasProperty.test.ts
Created February 24, 2022 14:07
TypeScript assertion functions: assertHasProperty and assertIsAnObject
/* eslint-disable @typescript-eslint/no-empty-function */
import { assertHasProperty } from '../';
test(`Given it's called with an object that has the specified property
And that property does NOT hold 'undefined'
Then it does NOT throw`, () => {
expect(() => {
assertHasProperty('nameOfThePropertyImLookingFor', {
nameOfThePropertyImLookingFor: {},
@jfhector
jfhector / definition.feature
Created February 19, 2022 20:30
wrapInAsyncTryCatch
Feature: wrapInAsyncTryCatch
Scenario: The function returned by wrapInAsyncTryCatch calls the function provided to wrapInAsyncTryCatch with the same arguments
Given wrapInAsyncTryCatch is called with an asynchronous function that does not throw
When the function returned by wrapInAsyncTryCatch is called with some arguments
Then the function provided to wrapInAsyncTryCatch gets called with the same arguments
And the function returned by wrapInAsyncTryCatch returns the same thing as the provided function
Scenario: It catches any error that the provided function throws
Given wrapInAsyncTryCatch is called with an asynchronous function that throws when called with some arguments
@jfhector
jfhector / definition.feature
Last active February 19, 2022 20:23
wrapInSynchronousTryCatch
Feature: wrapInSynchronousTryCatch
Scenario: The function returned by wrapInSynchronousTryCatch calls the function provided to wrapInSynchronousTryCatch with the same arguments
Given wrapInSynchronousTryCatch is called with a synchronous function that does not throw
When the function returned by wrapInSynchronousTryCatch is called with some arguments
Then the function provided to wrapInSynchronousTryCatch gets called with the same arguments
And the function returned by wrapInSynchronousTryCatch returns the same thing as the provided function
Scenario: It catches any error that the provided function throws
Given wrapInSynchronousTryCatch is called with a synchronous function that throws when called with some arguments

This wiki is built in Notion. Here are all the tips you need to contribute.

The Basics

Create a Page

In your sidebar, click the + that appears next to the word Workspace on hover. A new page will appear. Give it a title and start typing like you would in any other document.

Headings