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.

  1. Node can run Typescript files now (permalink)

    It’s possible since a while with --experimental-strip-types flag, 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!

  2. 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)
    
  3. Reading speed is 238 words per minute on average (source, permalink)

    And speech time averages at 183 wpm.

  4. Sort list of strings in Javascript (source, permalink)
    users.sort((a, b) => a.firstname.localeCompare(b.firstname))
    

    or reversed order:

    users.sort((a, b) => a.firstname.localeCompare(b.firstname) * -1)
    
  5. Rob Pike's 5 Rules of Programming (source, permalink)
    1. 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.
    2. Measure. Don’t tune for speed until you’ve measured, and even then don’t unless one part of the code overwhelms the rest.
    3. 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.)
    4. Fancy algorithms are buggier than simple ones, and they’re much harder to implement. Use simple algorithms as well as simple data structures.
    5. 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”.

  6. 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 the no-focussed-tests linter rule in eslint-plugin-jest.

  7. 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()
    
  8. pwdx command shows the working path of a process (source, permalink)

    For example:

    % pwdx 1984
    > 1984: /home/george/ttlctrl
    
  9. 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/efivars
    
  10. Adding 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/*).

  11. E-Mail that isn't spam is called ham! (source, permalink)
  12. git fetch repo_url remote_branch:new_local_branch (source, permalink)

    git fetch $repo_url $remote_branch:$new_local_branch

  13. Bush refused offer to discuss Osama Bin Laden handover (source, permalink)
  14. There is a HTML tag for "Word Break Opportunity" (source, permalink)

    For example: Kauf<wbr/>haus.