Skip to content

Instantly share code, notes, and snippets.

Avatar

André Vitor de Lima Matos andrevmatos

View GitHub Profile
View 01_ts2_assert.md

Flexible assert

assert functions and statements are so omnipresent in almost all programming languages in the last couple of decades, it's almost ironic JavaScript/TypeScript not having it built-in. But it's very easy to implement one. Usually, they receive a condition to test as first parameter, and an optional error to throw as second parameter.

We don't want to throw strings (bad practice), so we wanna wrap them with our custom AssertionFailed error class. But we may want to be more descriptive and provide some more details as a second parameter to such class. We don't want to construct these parameters every time an assertion is run, these can be expensive to fetch and would be needed only if the assertion failed. Uff, there's a lot of cases...

Your task is to write an assert function/guard, which can receive the error parameter in multiple ways:

  • an error string, to be fed to our custom class constructor
  • an error instance, to be thrown directly
  • a tuple containing the error message and
@andrevmatos
andrevmatos / issue
Created Apr 21, 2019
Arch Linux /etc/issue
View issue

      ### ### ##
    ## ##
┌ #### ### ###   ####   ## ### ## ### ### ### ### ###  ### ###
│  ##  ### ##  ##   ###  [1;37;
View keybase.md

Keybase proof

I hereby claim:

  • I am andrevmatos on github.
  • I am andrevmatos (https://keybase.io/andrevmatos) on keybase.
  • I have a public key whose fingerprint is 9A98 76D8 66A1 D4D5 BD12 A24E A48A 6B2D BAC1 EC29

To claim this, I am signing this object: