Войдите или зарегистрируйтесь
Вы сможете писать комментарии и посты, ставить лайки и другое
Поиск
Тёмная тема

Misleading naming in JavaScript: atob() and btoa()

2 дн. назад
JavaScript has two globally available metods for working with Base64: atob() and btoa(). Their names clearly look like they were borrowed from older languages. In C, for instance, the standard library includes functions like atoi and atof:

#include <stdlib.h>

char str[] = "123";
int num = atoi(str); // 123

atoi means ASCII to integer, and atof means ASCII to float (though in reality it returns a double).

So what do you think the atob function does in JavaScript? ASCII to Base64? In other words, converting a regular string into a Base64 string? Nope! It does the exact opposite: it converts a Base64 string into a "regular" string. And btoa, in turn, converts a regular string into Base64!

console.log(btoa('Famabara')); // 'RmFtYWJhcmE='
console.log(atob('RmFtYWJhcmE')); // 'Famabara'

Who thought it was a good idea to swap the names like that? Love JS.
+1
4

The expectation paradox: he wanted a boy, but kept having girls

недавно
A man has five children - all girls. He dreams of a son, an heir! Now his wife is pregnant again. Let's skip the psychology behind all this and focus on the probabilities.

Emotionally, it feels like after five girls in a row, a boy is due. Surely, the odds of finally having a son are much higher now.

But this is a textbook example of the gambler's fallacy - the false belief that if something happens repeatedly, the opposite is bound to happen soon. That's not how probability works.

According to math, the chance of having exactly five girls in a row followed by another girl is 1 in 64, or about 1.6%. But those five girls are already here. The only thing that matters now is the next birth.

And the odds of having a boy? Still 50/50. Every birth is an independent event (assuming randomness), so the previous outcomes don't change the next one.

So yes, there's still a 50% chance they'll have another girl.
+3
60

Copenhagen - the city without lamp posts

недавно
Did you know that there are almost no lamp posts in Copenhagen? Open Google Maps and see for yourself.
Instead of posts, the city uses lamps suspended over the middle of the road.
Показать полностью...
+2
11

Nuxt 3 and 4: issue with multiple navigateTo calls

недавно
At work, on one of our projects, I ran into an issue in Nuxt 4 where a page component needs to perform different redirects via navigateTo() based on various conditions during the execution of the setup function. Something like this:

<template>
  Some page
</template>

<script lang="ts" setup>
if (1 + 1 === 2) {
  await navigateTo('/?abc=1', {
    redirectCode: 302,
  });
}

if (2 + 2 === 4) {
  await navigateTo('/?abc=2', {
    redirectCode: 302,
  });
}

if (3 + 3 === 6) {
  await navigateTo('/?abc=3', {
    redirectCode: 302,
  });
}

if (4 + 4 === 8) {
  await navigateTo('/?abc=4', {
    redirectCode: 302,
  });
}
</script>

This is where the problem appears: in the end, the redirect always goes to the very last URL, i.e. in the example above the navigation ends up at /?abc=4. I dug into the documentation but couldn't find anything built-in to prevent subsequent navigateTo calls from being triggered. It seems this behavior is related to vue router.

You can't write it like this either:
if (1 + 1 === 2) {
  await navigateTo('/?abc=1', {
    redirectCode: 302,
  });
  return; // not allowed in script setup
}

In the end, the only solution that worked was introducing a boolean variable and checking it before calling navigateTo:
let isAlreadyRedirected = false;

if (1 + 1 === 2) {
  await navigateTo('/?abc=1', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}

if (!isAlreadyRedirected && 2 + 2 === 4) {
  await navigateTo('/?abc=2', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}

if (!isAlreadyRedirected && 3 + 3 === 6) {
  await navigateTo('/?abc=3', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}

if (!isAlreadyRedirected && 4 + 4 === 8) {
  await navigateTo('/?abc=4', {
    redirectCode: 302,
  });
  isAlreadyRedirected = true;
}
Once again: this is the only thing that solved the problem for me. If you have any other solution, please let me know.
Показать полностью...
+2
5