Skip to content

50

Remove multiple event listeners at once using AbortController.

// React
useEffect(() => {
const controller = new AbortController()
window.addEventListener('dragStart', handleDragStart, {
signal: controller.signal,
})
window.addEventListener('dragMove', handleDragMove, {
signal: controller.signal,
})
window.addEventListener('dragEnd', handleDragEnd, {
signal: controller.signal,
})
return () => {
controller.abort()
}
}, [])

49

Use AbortSignal.timeout() dispatches abort event after timeout duration has passed.

fetch(url, {
// Abort this request automatically after 2 seconds.
signal: AbortSignal.timeout(2000),
})

48

Edit text on any website with the designMode property.

document.designMode = 'on'; // run this in the browser console

More info on MDN.

47

Automatically refresh HTML document every X seconds:

<head>
<meta http-equiv="refresh" content="30" />
</head>

46

Test Double is a generic term for any case where you replace a production object for testing purposes.

Under test doubles, there are various types like stubs, spies and indeed mocks! Check out Martin Fowler’s post for more detail.

45

When creating an array in Go, we can put ... in place of array’s length to determine the length of the array automatically based on the number of elements provided.

nums := [...]int{1, 2, 3} // [3]int{1, 2, 3}

44

When working with session cookie, it’s important to be aware that using SameSite=Strict will block the session cookie being sent by the user’s browser for all cross-site usage — including safe requests with HTTP methods like GET and HEAD.

While that might be the safest setting, the downside is that the session cookie won’t be sent when a user clicks on a link to your application from another website. Your application will initially treat the user as ‘not logged in’ even if they have an active session.

So if your application will potentially have other websites linking to it (or even links shared in emails or private messaging services), then SameSite=Lax is generally the more appropriate setting.

43

Javascript compatible valid email regex recommended by W3C and Web Hypertext Application Technology Working Group.

/^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/

42

In Go, the syntax for SQL parameter placeholder differs depending on your database. MySQL, SQL Server and SQLite use the ? notation, but PostgreSQL uses the $N notation.

For example:

-- MySQL
INSERT INTO ... VALUES (?, ?, ?);
-- PostgreSQL
INSERT INTO ... VALUES ($1, $2, $3);

41

To remove a package in a Go project:

  • Run go get and postfix the package path with @none.

    Terminal window
    $ go get github.com/path/to/package@none
  • Remove all references to the package in your code, then run go mod tidy, which will automatically remove any unused packages from your go.mod and go.sum files.

    Terminal window
    $ go mod tidy -v