Today I Learned
This page contains short notes and sometimes code snippets, of interesting things I just found out.Entries can be opened by tapping/clicking them.
Node can run Typescript files now (permalink)
It’s possible since a while with
--experimental-strip-typesflag, but since Node v22.18 it is possible just like this under many circumstances. Check the official docs on how to run typescript natively to learn more!Javascript's Array.from is neat! (permalink)
Because it simply executes a function arg1.length times, one can do things like:
const randomNumbers = Array.from({ length: 75 }, Math.random)Rob Pike's 5 Rules of Programming (source, permalink)
- You can’t tell where a program is going to spend its time. Bottlenecks occur in surprising places, so don’t try to second guess and put in a speed hack until you’ve proven that’s where the bottleneck is.
- Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest.
- Fancy algorithms are slow when n is small, and n is usually small. Fancy algorithms have big constants. Until you know that n is frequently going to be big, don’t get fancy. (Even if n does get big, use Rule 2 first.)
- Fancy algorithms are buggier than simple ones, and they’re much harder to implement. Use simple algorithms as well as simple data structures.
- Data dominates. If you’ve chosen the right data structures and organized things well, the algorithms will almost always be self-evident. Data structures, not algorithms, are central to programming.
Pike’s rules 1 and 2 restate Tony Hoare’s famous maxim “Premature optimization is the root of all evil.” Ken Thompson rephrased Pike’s rules 3 and 4 as “When in doubt, use brute force.”. Rules 3 and 4 are instances of the design philosophy KISS. Rule 5 was previously stated by Fred Brooks in The Mythical Man-Month. Rule 5 is often shortened to “write stupid code that uses smart objects”.
Disallowed Focussed Tests and how it saved my day (permalink)
Today I was about to push a focussed test. A focussed test, you ask?
In Jest (and others) one can run only a specific test, by writing
it.only(.... Pushing this to production might create some funny or not so funny side effects though. Luckily there is theno-focussed-testslinter rule in eslint-plugin-jest.Jest mocks are ...different (permalink)
If you want to mock an imported function in Jest in a way that allows you to check if it has been called, you can not do the seemingly straighforward:
// mock prefix necessary btw const mockThatFunction = jest.fn(() => 'stuff') jest.mock('@/path/to/module', () => ({ thatFunction: mockThatFunction, })) // ...in test descriptions expect(mockThatFunction).toHaveBeenCalled()This way thatFunction will be undefined without anyone telling you why.
What you need to do instead:
import { thatFunction } from '@/path/to/module' jest.mock('@/path/to/module', () => ({ thatFunction: jest.fn(() => 'stuff'), })) // ...in test descriptions expect(thatFunction).toHaveBeenCalled()There is a file system for EFI vars now (source, permalink)
On kernel updates I saw a recurring “EFI variables are not supported on this system”, so I investigated and learned that the new EFI variables are provided via a file system that needs to be mounted first:
mount -t efivarfs efivarfs /sys/firmware/efi/efivarsAdding aliases in vite with typescript needs the same alias in tsconfig (permalink)
For example:
The following vite.config.ts:
import { fileURLToPath, URL } from "url" import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ plugins: [vue()], resolve: { alias: { "~": fileURLToPath(new URL("./src", import.meta.url)), "~component": fileURLToPath(new URL("./src/components", import.meta.url)), "~composable": fileURLToPath(new URL("./src/composables", import.meta.url)), "~lib": fileURLToPath(new URL("./src/lib", import.meta.url)), } } })will need this in tsconfig.json:
{ "compilerOptions": { "paths": { "~/*": [ "./src/*" ], "~component/*": [ "./src/components/*" ], "~composable/*": [ "./src/composables/*" ], "~lib/*": [ "./src/lib/*" ] } } }The asterixes in the syntax are important (
alias/*=>./path/*).