<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Josue Jaramillo</title>
    <description>The latest articles on DEV Community by Josue Jaramillo (@josuews303).</description>
    <link>https://dev.to/josuews303</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1072281%2F45052cde-6bd2-42af-a5de-8ad85eb6a779.jpeg</url>
      <title>DEV Community: Josue Jaramillo</title>
      <link>https://dev.to/josuews303</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/josuews303"/>
    <language>en</language>
    <item>
      <title>Don't use magic strings!</title>
      <dc:creator>Josue Jaramillo</dc:creator>
      <pubDate>Wed, 03 May 2023 22:24:32 +0000</pubDate>
      <link>https://dev.to/josuews303/dont-use-magic-strings-2aol</link>
      <guid>https://dev.to/josuews303/dont-use-magic-strings-2aol</guid>
      <description>&lt;p&gt;Hi everyone, this is something I recently learned and I would like to share with everyone. It is something small but at the same time it becomes a good practice that can greatly improve our code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Magic Strings
&lt;/h2&gt;

&lt;p&gt;We can define this as using hardcoded code that contains a specific string, either to handle a state, compare a value, or store a value that we, for whatever reason, need to have hardcoded.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(this.userStatus === 'online'){
  //any
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  The problem
&lt;/h2&gt;

&lt;p&gt;If you need to check this status in multiple parts of your code by any reason, someone can easily make a typo error and write something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(this.userStatus === 'onlien'){
  //any
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the code won't work as expected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution
&lt;/h2&gt;

&lt;p&gt;The most obvious solutions should be to create a constant and use this constant when necessary, but what if we have a lot of constants related to that specific functionality? We should create an object with all possible words.... &lt;strong&gt;A Dictionary&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const USER_STATUS = {
  online: 'online',
  offline: 'offline',
  connecting: 'connecting'
}

export default class Example {
  checkStatus() {
    if(this.userStatus === USER_STATUS.online){
      //any
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Mutability
&lt;/h2&gt;

&lt;p&gt;We still have a problem here, even if it's a constant, we could edit the content of our object and overwrite the value by mistake.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;USER_STATUS.online = 'hi'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To avoid this we can improve the code making it immutable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Immutability
&lt;/h2&gt;

&lt;p&gt;Freezing an object prevents extensions and makes existing properties non-writable and non-configurable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const USER_STATUS = Object.freeze({
  online: "online",
  offline: "offline",
  connecting: "connecting"
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Point of improvement
&lt;/h2&gt;

&lt;p&gt;At this point our dictionary is ready, however there are cases where we use these values only internally in our application, i.e. we don't need any specific text, it can be a number or anything.&lt;/p&gt;

&lt;p&gt;In these cases we could use symbol and avoid the fatigue of adding different strings or numbers for each part of our dictionary. Symbol will always make sure to assign a different value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const USER_STATUS = Object.freeze({
  online: Symbol(),
  offline: Symbol(),
  connecting: Symbol()
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's all, thanks for reading and I hope this helps you organize your code better.&lt;/p&gt;

&lt;p&gt;You can also see an small example here:&lt;br&gt;
&lt;a href="https://codesandbox.io/s/runtime-dust-zbkfth"&gt;https://codesandbox.io/s/runtime-dust-zbkfth&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Bye bye!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Simple Vanilla One-Time Password (OTP) module from scratch.</title>
      <dc:creator>Josue Jaramillo</dc:creator>
      <pubDate>Thu, 27 Apr 2023 03:17:05 +0000</pubDate>
      <link>https://dev.to/josuews303/simple-vanilla-one-time-password-0tp-module-from-scratch-4mn0</link>
      <guid>https://dev.to/josuews303/simple-vanilla-one-time-password-0tp-module-from-scratch-4mn0</guid>
      <description>&lt;p&gt;Hello people! As you can see in the title, this post is a guide about how to develop an OTP module. Why? Because it sounds easy but there are some things that we need to take into account when developing.&lt;/p&gt;

&lt;h2&gt;
  
  
  Initial HTML
&lt;/h2&gt;

&lt;p&gt;To begin we will put together the basic structure of this, we will add our section, a title, a descriptive message and obviously our form with a single input to enter the code. Nothing difficult right? It should work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;section class="auth-block container" aria-labelledby="auth-block-title-id"&amp;gt;
      &amp;lt;h1 id="auth-block-title-id" class="auth-block__title"&amp;gt;
        Enter your authentication code
      &amp;lt;/h1&amp;gt;
      &amp;lt;p&amp;gt;
        We sent a message to your phone. Please add the code and click on
        continue
      &amp;lt;/p&amp;gt;
      &amp;lt;form action="/check-code"&amp;gt;
        &amp;lt;label for="auth-block-single-input-id"&amp;gt;OTP Code:&amp;lt;/label&amp;gt;
        &amp;lt;input id="auth-block-single-input-id" /&amp;gt;
        &amp;lt;button type="reset"&amp;gt;Clear&amp;lt;/button&amp;gt;
        &amp;lt;button type="submit"&amp;gt;Continue&amp;lt;/button&amp;gt;
      &amp;lt;/form&amp;gt;
    &amp;lt;/section&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it works! &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--dEwYc-fm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0vzntqg1b2stttucq31.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--dEwYc-fm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f0vzntqg1b2stttucq31.png" alt="Image description" width="569" height="146"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But... it can't be that simple, right? Well, no. There are certain things we need to consider when making an OTP input.&lt;/p&gt;

&lt;p&gt;First of all these codes are usually numeric, so we should validate this. And as you can imagine, the easiest way would be to update the input to a &lt;code&gt;type="number"&lt;/code&gt; input. But this will add some controls that don't make much sense when entering a code, I don't think someone will use the arrows until they get to their 6 digit number.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E1Uw60HV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56fwvp67ry3qk8ufzy77.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E1Uw60HV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/56fwvp67ry3qk8ufzy77.png" alt="Image description" width="551" height="149"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To avoid this, we will use a text input, but we will validate the type of text we expect, in this case, it is numeric. To do this we will take advantage of the pattern and add a simple regular expression.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;pattern="[0-9]{6}"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--krPEKyY_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wiaqa9kip3lr5o50o04w.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--krPEKyY_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wiaqa9kip3lr5o50o04w.png" alt="Image description" width="551" height="200"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But is it enough? Thanks to our pattern we can validate the length and prevent form submission, but from my point of view it would be better if the user can't enter letters, only numbers. So we'll add some JavaScript code :)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;handleInput(e) {
    const { target } = e;
    target.value = target.value.replace(/[^0-9]/g, "");
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code takes advantage of a regular expression to replace anything that is not number with empty &lt;code&gt;""&lt;/code&gt;. In this way, when the user tries to write text instead of numbers, it will be impossible.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mobile
&lt;/h2&gt;

&lt;p&gt;At this point we might think that we are done, if we try it on our computer it seems that everything is correct, but what if we try it on our phone? Don't you find it annoying that the full keyboard opens to enter numbers? We should only open the numeric keyboard. And we do that by adding an &lt;code&gt;inputmode="numeric"&lt;/code&gt; to our input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mHT71nmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4phfuo9ct200lkzz2mwh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mHT71nmy--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4phfuo9ct200lkzz2mwh.png" alt="Image description" width="616" height="394"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Why always iOS?
&lt;/h2&gt;

&lt;p&gt;If you already have some experience developing sites and you care that they look good on all devices and browsers, you will know that on many occasions you have to make small changes for iOS. And this is not the exception LOL.&lt;/p&gt;

&lt;p&gt;If you pay attention to the previous image, you will see a blank space right in the middle, this is where iOS has a cool feature that means that when you receive a message with an OTP code, it shows it directly as an autocomplete, so you don't you have to go to the messages app, copy the message, come back and paste it.&lt;/p&gt;

&lt;p&gt;To achieve this we just need to add &lt;code&gt;autocomplete="one-time-code"&lt;/code&gt; to the input.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TX1-j2in--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cesitk8d6ugpr79ninh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TX1-j2in--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2cesitk8d6ugpr79ninh.png" alt="Image description" width="701" height="805"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And voila, we could continue adding styles, improving accessibility, make it required, etc. &lt;/p&gt;

&lt;p&gt;But that will be for another post in which I'll explain how to make a more advanced OTP module like the one used by Epic Games and a lot of sites that has multiple inputs, one per number. However for now this code complies with the basic functionality that an input for OTP should have.&lt;/p&gt;

&lt;p&gt;You can see the complete code example in the following link:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://codesandbox.io/s/boring-neumann-0z3rki"&gt;https://codesandbox.io/s/boring-neumann-0z3rki&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
