Skip to content

Instantly share code, notes, and snippets.

@joanna-liana
joanna-liana / demo.ts
Created February 27, 2024 16:02
TS method shorthand syntax - bivariance pitfall
interface Client {
pay(): void;
}
interface VIP extends Client {
fastPay(): void;
}
interface Cashier {
handleAnyClient(client: Client): void; // wider and narrower Clients allowed
@joanna-liana
joanna-liana / removeWatchLaterItems.js
Created June 28, 2023 14:29
Bulk remove items from YouTube's Watch Later playlist
// to be run in the browser
// at https://www.youtube.com/playlist?list=WL
// might need a few reruns based on the number of items
function removeWatchLaterItems() {
const items = Array.from(document.querySelector("div.ytd-playlist-video-list-renderer:nth-child(3)").children);
for (let i = 0; i < items.length; i++) {
const item = items[i];
setTimeout(() => {
@joanna-liana
joanna-liana / wallaby.js
Created June 21, 2023 06:20
NestJS Wallaby config (unit tests)
module.exports = function (_wallaby) {
return {
files: ['src/**/*.ts', '!src/**/*spec.ts'],
tests: ['src/**/*spec.ts'],
testFramework: 'jest',
env: {
type: 'node',
@joanna-liana
joanna-liana / defaults.ts
Created June 11, 2023 06:51
Bullet-proof defaults for object parameters
const withDefaults = (
{
a = 'a default',
b
}: {
a?: string;
b?: number;
}
) => console.log(a, b);
@joanna-liana
joanna-liana / commands.sh
Last active May 21, 2023 10:36
Check system architecture
dpkg --print-architecture
# amd64
uname -m
# x86_64
@joanna-liana
joanna-liana / log.ts
Created April 21, 2023 17:35
Logging a deeply nested object without JSON.stringify
const nested = { a: { b: { c: { d: 1 } } } };
// { a: { b: { c: [Object] } } }
console.log(nested);
// {
// a: { b: { c: { d: 1 } } }
// }
console.log(
util.inspect(nested, { depth: null, colors: true })
@joanna-liana
joanna-liana / ReadonlyRecord.ts
Created December 10, 2022 09:33
`ReadonlyRecord` type - to prevent hacks for mutating record properties, e.g. pushing to arrays or overriding nested props
type ReadonlyRecord<Record> = {
readonly [Key in keyof Record]: ReadonlyRecord<Record[Key]>
}
@joanna-liana
joanna-liana / update_go.sh
Last active August 30, 2023 15:58
Easy-peasy Go update; a bash script using the latest available version (default) or a specific archive name; ZSH is assumed to be the default shell.
#!/usr/bin/env bash
# Script adapted from https://golangcode.com/updating-go-on-ubuntu/
set -o errexit
main () {
if ! [[ -z "$1" ]]
then
printf "❕ To update Go to the latest version, you do not need to provide any arguments.\nAlternatively, provide just the name of the specific Linux archive, e.g. go1.19.linux-amd64.tar.gz\n\n"
@joanna-liana
joanna-liana / assertions.test.ts
Last active September 12, 2021 06:32
Jest's toBe assertion with custom message (expected value alias)
describe('Assertions with expected value alias', () => {
it('reports the failure with a meaningul message', async () => {
expect(7).toBe(42, 'The Answer to the Ultimate Question of Life, the Universe, and Everything');
});
it('given no custom value alias, it reports the failure with the standard message', async () => {
expect(7).toBe(42);
});
});
@joanna-liana
joanna-liana / docker-compose.yml
Created August 22, 2021 07:49
Docker Compose with Postgres config options
version: '2.4'
services:
postgres:
image: postgres:12.4
restart: always
command: [
"postgres",
"-c",
"log_statement=all",
"-c",