Content Warning

This website contains age-restricted materials including nudity and explicit depictions of sexual activity.

By entering, you affirm that you are at least 18 years of age or the age of majority in the jurisdiction you are accessing the website from and you consent to viewing sexually explicit content.

Lemmy NSFW
  • Communities
  • Create Post
  • Create Community
  • heart
    Support Lemmy
  • search
    Search
  • Login
  • Sign Up
KaKi87@jlai.lu to JavaScript@programming.dev · 6 days ago

Ternary-based pattern matching in JS

blog.kaki87.net

external-link
message-square
6
link
fedilink
6
external-link

Ternary-based pattern matching in JS

blog.kaki87.net

KaKi87@jlai.lu to JavaScript@programming.dev · 6 days ago
message-square
6
link
fedilink
[Code] Ternary-based pattern matching in JS - KaKi's blog
blog.kaki87.net
external-link
# Ternary-based pattern matching in JS Folks are jealous of other programming languages having this kind of pattern matching abilities : kotlin fun compareNumbers( a: Int, b: Int ) = when { a > b -> "A is greater than B" b > a -> "B is greater than A" else -> "A and B are equal" } But, so do we : typescript const compareNumbers = ( a: number, b: number ) => a > b ? 'A is greater than B' : b > a ? 'B is greater than A' : 'A and B are equal'; And this example is at identical character count btw. For example, I use it here [https://git.kaki87.net/KaKi87/dynapt/src/commit/9c89046de317295d7cd2e20e96b1a877a7ccfc72/src/updatePackages.js#L42-L44] in dynapt(1) : javascript url = app.url ? app.url : app.github ? `https://api.github.com/repos/$%7Bapp.github.repo%7D/releases/latest` : undefined To define an app download url that is either already provided if arbitrary, or requires being generated if from a GitHub / string. I use it again in the same file a little lower [https://git.kaki87.net/KaKi87/dynapt/src/commit/9c89046de317295d7cd2e20e96b1a877a7ccfc72/src/updatePackages.js#L64-L66] : javascript isUnmodified = app.url ? response.status === 304 : app.github ? response.status === 304 || cache[app.id]?.id === response.data['id'] : undefined; To determine whethere the download file from the abovementioned URL is modified or not, which works a little differently for GitHub because of its API. In another project that uses Discord.JS and TypeScript, I use this syntax for a conditional return type : typescript createSelectRow = ({ type // ... }: { type: type, // ... }) : type extends 'channel' ? ActionRowBuilder : type extends 'role' ? ActionRowBuilder : type extends 'user' ? ActionRowBuilder : ActionRowBuilder => { // ... } Etc. — (1) dynapt is a dynamic APT repository. More information about that project here [https://gist.github.com/KaKi87/f620788e9901abbfef4978eb7ad358b4].
  • KaKi87@jlai.luOP
    link
    fedilink
    arrow-up
    1
    ·
    5 days ago

    Probably just my general dislike of verbosity 😅

JavaScript@programming.dev

javascript@programming.dev

Subscribe from Remote Instance

Create a post
You are not logged in. However you can subscribe from another Fediverse account, for example Lemmy or Mastodon. To do this, paste the following into the search field of your instance: [email protected]
Visibility: Public
globe

This community can be federated to other instances and be posted/commented in by their users.

  • 2 users / day
  • 32 users / week
  • 52 users / month
  • 251 users / 6 months
  • 4 local subscribers
  • 2.42K subscribers
  • 175 Posts
  • 348 Comments
  • Modlog
  • mods:
  • Erlingur@programming.dev
  • Ategon@programming.dev
  • nick@programming.dev
  • UI: 0.19.11-nsfw
  • BE: 0.19.11
  • Modlog
  • Legal
  • Instances
  • Docs
  • Code
  • join-lemmy.org