<?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: Steve Cruz</title>
    <description>The latest articles on DEV Community by Steve Cruz (@stevescruz).</description>
    <link>https://dev.to/stevescruz</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%2F394376%2Fdcf542be-f7b2-40b1-9aa1-236983ae247b.png</url>
      <title>DEV Community: Steve Cruz</title>
      <link>https://dev.to/stevescruz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stevescruz"/>
    <language>en</language>
    <item>
      <title>TIL: Destructuring from Object | Undefined</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Tue, 05 Jan 2021 11:45:08 +0000</pubDate>
      <link>https://dev.to/stevescruz/til-destructuring-from-object-undefined-1col</link>
      <guid>https://dev.to/stevescruz/til-destructuring-from-object-undefined-1col</guid>
      <description>&lt;h1&gt;
  
  
  Destructure from a value that can be undefined
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;updateUserProfile&lt;/code&gt;&lt;/strong&gt; function allows me to retrieve the &lt;strong&gt;&lt;code&gt;user&lt;/code&gt;&lt;/strong&gt; by making a query to the database with the provided &lt;strong&gt;&lt;code&gt;user_id&lt;/code&gt;&lt;/strong&gt;, then use another database query to guarantee that the provided &lt;strong&gt;&lt;code&gt;email&lt;/code&gt;&lt;/strong&gt; does not belong to another user and finally update the user in the database sending a query with the provided &lt;strong&gt;&lt;code&gt;name&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;email&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The problem&lt;/strong&gt; was that when checking if we already had a user with the provided &lt;strong&gt;&lt;code&gt;email&lt;/code&gt;&lt;/strong&gt; in the database the result could be a &lt;strong&gt;&lt;code&gt;User&lt;/code&gt;&lt;/strong&gt; object or &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;, depending on if we found the user or not.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;&lt;code&gt;User&lt;/code&gt;&lt;/strong&gt; object contains many properties (&lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;name&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;email&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;avatar&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;password&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;created_at&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;updated_at&lt;/code&gt;&lt;/strong&gt;), but I only needed the &lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt; property to compare with the provided &lt;strong&gt;&lt;code&gt;user_id&lt;/code&gt;&lt;/strong&gt; to guarantee that the email did not belong to any user.&lt;/p&gt;

&lt;p&gt;I was not able to use &lt;strong&gt;destructuring&lt;/strong&gt; to unpack only the &lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt;  from the &lt;strong&gt;result&lt;/strong&gt; nor rename it to &lt;strong&gt;&lt;code&gt;findEmailOwner&lt;/code&gt;&lt;/strong&gt; because the result could be a &lt;strong&gt;&lt;code&gt;User&lt;/code&gt;&lt;/strong&gt; object or &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;, so I got the following &lt;strong&gt;TypeScript error message&lt;/strong&gt;: &lt;strong&gt;&lt;code&gt;"Property 'id' does not exist on type 'User | undefined'&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TLDR&lt;/strong&gt;: I need to obtain &lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt; by &lt;strong&gt;destructuring&lt;/strong&gt; a &lt;strong&gt;value&lt;/strong&gt; that can be an &lt;strong&gt;&lt;code&gt;object&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateUserProfile&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;usersRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AppError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The user was not found.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;findEmailOwner&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;usersRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// error message: "Property 'id' does not exist on type 'User | undefined'.&lt;/span&gt;

  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;findEmailOwner&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;findEmailOwner&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AppError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`This email cannot be used.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  
  &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;usersRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;We can use &lt;strong&gt;short circuit evaluation&lt;/strong&gt; to supply a &lt;strong&gt;default&lt;/strong&gt; if &lt;strong&gt;&lt;code&gt;user&lt;/code&gt;&lt;/strong&gt; is a &lt;strong&gt;falsy value&lt;/strong&gt; (&lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;0&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;-0&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;0n&lt;/code&gt;&lt;/strong&gt;, &lt;strong&gt;&lt;code&gt;""&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;NaN&lt;/code&gt;&lt;/strong&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE 1&lt;/strong&gt;: I can use this approach in my application because the &lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt; &lt;strong&gt;property&lt;/strong&gt; that I want to retrieve with destructuring &lt;strong&gt;cannot be assigned to any falsy value in my database&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE 2&lt;/strong&gt;: BUT if I was retrieving the &lt;strong&gt;&lt;code&gt;avatar&lt;/code&gt; property&lt;/strong&gt; that can be assigned to &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt; in the database, this approach would not work.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```tsx
// Case 1 - id (cannot contain falsy values)

// user does not exist
const user = undefined
const { id } = user || {}
console.log(id) // undefined (what we expect)

// user exists
const user = {
    id: 'aaaa-aaaa-aaaa-aaaa',
};
const { id } = user || {}
console.log(id) // 'aaaa-aaaa-aaaa-aaaa' (what we expect)

// Result: SUCCESS

//-----------------------------------------

// Case 2 - avatar (can contain null a falsy values)

const user = undefined
const { avatar } = user || {}
console.log(avatar) // undefined (what we expect)

const user = {
    avatar: 'photo.jpg',
};
const { avatar } = user || {}
console.log(avatar) // 'photo.jpg' (what we expect)

const user = {
    avatar: null,
};
const { avatar } = user || {}
console.log(avatar) // undefined (not good, we needed this to be null)

// Result: FAILURE
```
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Another approach is to spread the &lt;strong&gt;&lt;code&gt;user&lt;/code&gt;&lt;/strong&gt; into an object before destructuring it, because &lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt; &lt;strong&gt;values&lt;/strong&gt; are ignored.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE 1&lt;/strong&gt;: I would use this approach if was retrieving the &lt;strong&gt;&lt;code&gt;avatar&lt;/code&gt; property&lt;/strong&gt; that can be assigned to a &lt;strong&gt;falsy value&lt;/strong&gt; (&lt;strong&gt;&lt;code&gt;null&lt;/code&gt;&lt;/strong&gt;) in the database since the first approach would not work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE 2&lt;/strong&gt;: This approach is &lt;strong&gt;less idiomatic&lt;/strong&gt;, so I would not use it for cases where the first approach works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE 3&lt;/strong&gt;: This approach would also work for &lt;strong&gt;&lt;code&gt;id&lt;/code&gt;&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//Case - avatar (can contain null a falsy values)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;avatar&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;//undefined (what we expect)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;picture.jpg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;avatar&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 'picture.jpg' (what we expect)&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;avatar&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;avatar&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// null (what we expect)&lt;/span&gt;

&lt;span class="c1"&gt;// Result: SUCCESS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Applying the short circuit evaluation approach to our code&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;updateUserProfile&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;usersRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AppError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`The user was not found.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;findEmailOwner&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;usersRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;findByEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt; &lt;span class="c1"&gt;// 1st approach&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;findEmailOwner&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;findEmailOwner&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AppError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`This email cannot be used.`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;401&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;usersRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  TLDR
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Retrieving a &lt;strong&gt;property&lt;/strong&gt; (that cannot be falsy) with destructuring from a value that can be an &lt;strong&gt;&lt;code&gt;object&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt; - use &lt;strong&gt;short circuit evaluation&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Retrieving a &lt;strong&gt;property&lt;/strong&gt; (that can be falsy) with destructuring from a value that can be an &lt;strong&gt;&lt;code&gt;object&lt;/code&gt;&lt;/strong&gt; or &lt;strong&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/strong&gt; - use the &lt;strong&gt;spread operator&lt;/strong&gt; on the &lt;strong&gt;value&lt;/strong&gt; that can be an object or undefined.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Additional Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://stackoverflow.com/questions/48433008/js-es6-destructuring-of-undefined"&gt;JS/ES6: Destructuring of undefined on Stack Overflow&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Keep in touch
&lt;/h1&gt;

&lt;p&gt;Contact me through my social media. Let's talk about DDD, TDD, good practices and the new Wonder Woman 1982 movie, be it on &lt;a href="https://www.linkedin.com/in/stevescruz/"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://github.com/stevescruz"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Tell me what you learned today.&lt;/p&gt;

</description>
      <category>todayilearned</category>
      <category>javascript</category>
      <category>todayisearched</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>JavaScript - Leetcode: Check If N and Its Double Exist</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Sun, 30 Aug 2020 11:54:06 +0000</pubDate>
      <link>https://dev.to/stevescruz/leetcode-check-if-n-and-its-double-exist-43j</link>
      <guid>https://dev.to/stevescruz/leetcode-check-if-n-and-its-double-exist-43j</guid>
      <description>&lt;p&gt;I have been solving LeetCode problems to practice my knowledge of algorithms and data structures for job interviews and decided to share my JavaScript solutions for them.&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: You can also read this in &lt;a href="https://leetcode.com/problems/check-if-n-and-its-double-exist/discuss/791983/JavaScript-Hash-Table-or-Set-Space-Time-Complexity-Analysis"&gt;LeetCode&lt;/a&gt;.&lt;/p&gt;
&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;Problem: Check if N and Its Double Exist&lt;br&gt;
Difficulty: Easy&lt;/p&gt;

&lt;p&gt;Given an array arr of integers, check if there exists two integers N and M such that N is the double of M ( i.e. N = 2 * M).&lt;/p&gt;

&lt;p&gt;More formally check if there exists two indices i and j such that :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;i != j&lt;/li&gt;
&lt;li&gt;0 &amp;lt;= i, j &amp;lt; arr.length&lt;/li&gt;
&lt;li&gt;arr[i] == 2 * arr[j]&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Inputs
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: arr = [10,2,5,3]
Output: true
Explanation: N = 10 is the double of M = 5,that is, 10 = 2 * 5.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: arr = [7,1,14,11]
Output: true
Explanation: N = 14 is the double of M = 7,that is, 14 = 2 * 7.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: arr = [3,1,7,11]
Output: false
Explanation: In this case does not exist N and M, such that N = 2 * M.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Constraints
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;2 &amp;lt;= arr.length &amp;lt;= 500&lt;/li&gt;
&lt;li&gt;-10^3 &amp;lt;= arr[i] &amp;lt;= 10^3&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Naive solution
&lt;/h1&gt;

&lt;p&gt;We could use a for  loop nested in a for loop to check for each element if there is a corresponding number that is its double.&lt;/p&gt;

&lt;p&gt;But even though we would have a constant space complexity of O(1), we would have a quadratic time complexity of O(n²) which is not good and should be avoided if possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//JavaScript
var checkIfExist = function(arr) {
    for(let i = 0; i &amp;lt; arr.length; i ++) {
      const currentValue = arr[i];
      for(let j = 0; j &amp;lt; arr.length; j ++) {
        const possibleValue = arr[j];
        if(possibleValue === 2 * currentValue &amp;amp;&amp;amp; i !== j) {
          return true;
        }
      }
    }
  return false;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Solution 1: Hash Table
&lt;/h1&gt;

&lt;p&gt;Another possible solution is to use the Hash Table data structure that in JavaScript can be represented as an object. Its main advantage is that we can assume that it takes constant time of O(1) to retrieve each stored element, so it is fast.&lt;/p&gt;

&lt;p&gt;It also allows us to solve this problem by traversing the array only once:&lt;/p&gt;

&lt;p&gt;In each iteration of a for statement we check if the current value already exists as a key in our object.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If so, a number and its double exist in the array, we must return true.&lt;/li&gt;
&lt;li&gt;If not, store key/value pairs where one pair has the current element divided by 2 as a key and the other pair has the current element multiplied by 2 as a key. Notice that the values we store with each key do not matter, since we only check the keys.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If the for loop ends without finding a match, it means that the array does not contain a number and its double, we must return false.&lt;/p&gt;

&lt;p&gt;Since we created a Hash Table with a size that scales linearly according to the size of our input array, it has a linear space complexity of O(n).&lt;/p&gt;

&lt;p&gt;This time we only traverse the array once, so it has a linear time complexity of O(n).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//JavaScript
var checkIfExist = function(arr) {
    const hashTable = {};

    for(let i = 0; i &amp;lt; arr.length; i ++) {
      const currValue = arr[i];

      if(hashTable[currValue] !== undefined) {
        return true
      }
      hashTable[currValue / 2] = currValue;
      hashTable[currValue * 2] = currValue;
    }

  return false;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Map
&lt;/h2&gt;

&lt;p&gt;This Hash Table approach could also be implemented by using the JavaScript built in Map data collection.&lt;/p&gt;

&lt;p&gt;The main difference in our use case would be that instead of storing each key in the Hash Table as a string, we would store each key in a Map as a number. An Object only supports string and symbol as a key, but a Map supports objects and any primitive type as keys.&lt;/p&gt;

&lt;h1&gt;
  
  
  Solution 2: Set
&lt;/h1&gt;

&lt;p&gt;The problem with using a Hash Table (object) or Map is that when we insert a key/value pair, the key is required but its value is not.&lt;/p&gt;

&lt;p&gt;When we need a Hash Table data structure's properties to solve the problem, but we only need keys instead of key/value pairs it makes sense to use a Set data collection.&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: Keep in mind that a JavaScript built in Set only stores unique values.&lt;/p&gt;

&lt;p&gt;Similar to an object and a Map, we can assume that we can retrieve a value from a Set with a constant time complexity of O(1).&lt;/p&gt;

&lt;p&gt;We created a Set with a size that scales linearly according to the size of our input array, it has a linear space complexity of O(n).&lt;/p&gt;

&lt;p&gt;Just like our previous solution we only traverse the array once, so it has a linear time complexity of O(n).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//JavaScript
var checkIfExist = function(arr) {
    const set = new Set();

    for(let i = 0; i &amp;lt; arr.length; i ++) {
      const currValue = arr[i];

      if(set.has(currValue)) {
        return true
      }
      set.add(currValue / 2);
      set.add(currValue * 2);
    }

  return false;
};
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  Keep in touch
&lt;/h1&gt;

&lt;p&gt;Contact me through my social media. Let's talk about algorithms, data structures and LeetCode problems, be it on &lt;a href="https://www.linkedin.com/in/stevescruz/"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://github.com/stevescruz"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Share with us your solutions for this problem.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>leetcode</category>
      <category>datastructures</category>
    </item>
    <item>
      <title>Stop Guessing: What is a JWT?</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Sun, 23 Aug 2020 20:41:50 +0000</pubDate>
      <link>https://dev.to/stevescruz/stop-guessing-what-is-a-jwt-47d9</link>
      <guid>https://dev.to/stevescruz/stop-guessing-what-is-a-jwt-47d9</guid>
      <description>&lt;h1&gt;
  
  
  Stop Guessing: What is a JWT?
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tmkgz4iA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uoie4izbvw839zxfu9o6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tmkgz4iA--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uoie4izbvw839zxfu9o6.png" alt="A string of english characters separated by dots form a JSON Web Token that has three parts: header, payload and signature."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  JSON Web Token (JWT)
&lt;/h2&gt;

&lt;p&gt;A JWT Is an open standard that defines a compact and self-contained way for performing Authentication in REST APIs where information is securely transmitted between both parties as a JSON object.&lt;/p&gt;

&lt;p&gt;This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with HMAC algorithm) or a public/private key pair using RSA.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE 1&lt;/strong&gt;: We consider it compact because of its size, it is possible to send it through an URL, POST parameter, or inside an HTTP header. Also due to its size its transmission is fast.&lt;br&gt;
&lt;strong&gt;NOTE 2&lt;/strong&gt;: We consider it self-contained because we do not need to query the database more than once, the payload contains all the necessary information about the user.&lt;/p&gt;


&lt;h2&gt;
  
  
  When to use JWT?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Authentication&lt;/strong&gt;: After the user is signed in, each subsequent request includes the JWT. This allows the user to access routes, services, and resources that require that token.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Information Exchange&lt;/strong&gt;: JWTs are a secure way of transmitting information between parties, because you can be sure that the sender is who they say they are, since they can be signed (possibly by using a public/private key pair). You can also verify that the content has not changed, since the signature is created using the header and the payload.&lt;/p&gt;


&lt;h2&gt;
  
  
  JWT Structure
&lt;/h2&gt;

&lt;p&gt;A JWT is formed by three parts separated by dots (.): a Header, a Payload, and a Signature. These parts follow this structure: xxxxx.yyyyy.zzzzz.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rKYLxAIU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/343jnps1u5dn0hi4deoc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rKYLxAIU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/343jnps1u5dn0hi4deoc.png" alt="The first part of a JWT is the header, the second the payload and the third the signature."&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Header
&lt;/h3&gt;

&lt;p&gt;Contains some information that usually include the token type (which is JWT) and the hashing algorithm (such as HMAC, SHA256 or RSA).&lt;/p&gt;

&lt;p&gt;Afterwards the JSON containing that Header is Base64Url encoded to form the first part of the JWT.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Example of a Header
{
  "alg": "HS256",
  "typ": "JWT"
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Payload
&lt;/h3&gt;

&lt;p&gt;Contains the claims that are statements about an entity (usually the user) and additional metadata.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Can not contain sensible information about a user like password, but it is ok to include user id, name or email.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of claims&lt;/strong&gt;: iss (issuer), exp (expiration time), sub (subject), aud (audience), among others.&lt;br&gt;
Afterwards the JSON containing the payload is then Base64Url encoded to form the second part of the JWT.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Example of a Payload
{
  "sub": "0987654321",
  "name": "Jane Doe",
  "admin": true 
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Signature
&lt;/h3&gt;

&lt;p&gt;Is used to verify that the sender of the JWT is who they claim to be and to ensure that the message was not changed while it was being transmitted.&lt;/p&gt;

&lt;p&gt;To create the signature take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Example of a Signature using the HMAC SHA256 algorithm
HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: A simple way to generate a secret is using &lt;a href="http://www.md5.cz/"&gt;http://www.md5.cz/&lt;/a&gt; to generate a MD5 hash of a string.&lt;/p&gt;

&lt;h3&gt;
  
  
  Uniting the three parts
&lt;/h3&gt;

&lt;p&gt;The output is three Base64 strings separated by dots: an encoded header, an encoded payload and it is signed with a secret. They can be passed in HTML and HTTP environments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--U6uDABPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1zi8p9d4nw85013plppc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U6uDABPZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/1zi8p9d4nw85013plppc.png" alt="Shows the jwt.io website where you can play with JWTs by encoding and decoding them. You can choose the hashing algorithm."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Go to jwt.io, a website that allows you to decode, verify and generate JWT.&lt;/p&gt;




&lt;h2&gt;
  
  
  How does a JWT work?
&lt;/h2&gt;

&lt;p&gt;1 - When the user is authenticated by successfully signing in using their credentials, a JWT will be returned.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C7flqHbz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wx8cifob01wrfcg8dker.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C7flqHbz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wx8cifob01wrfcg8dker.png" alt="Insomnia was used to generate a JWT by passing a http POST request with a body containing the user email and password."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--kAt_vaq4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kt6s88dmgp1l35dj7edx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kAt_vaq4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/kt6s88dmgp1l35dj7edx.png" alt="Insomnia receives a response that was sent previously. The response contains the JWT and information about the user."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: Keep in mind that tokens are credentials, so you must prevent security issues: do not keep tokens longer than required.&lt;/p&gt;

&lt;p&gt;2 - Whenever the user wants to access a protected route, its request should send the JWT, usually in the Authorization header using the Bearer schema: Authorization: Bearer .&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EgR-vNH1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wrjudrz212l324n75jyv.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EgR-vNH1--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/wrjudrz212l324n75jyv.png" alt="Insomnia sends a request to access a route that requires a JWT (an authenticated user) by including the JWT in the request."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt;: This authentication mechanism is stateless, because the user state is not saved in the server memory. Instead, the server's protected routes check for a valid JWT in the Authorization header, and only allows the user if this condition is fulfilled. As a result it is not necessary to query the database multiple times as JWTs are self-contained, so it already has all the necessary information.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Should You Use JWT?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;They are stateless&lt;/strong&gt;: Since tokens are self-contained they have all the information that is needed for authentication. This is good for scalability as your server does not have to store session state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They can be generated from anywhere&lt;/strong&gt;: Token generation and token verification are decoupled. This allows you to handle the signing of tokens on a separate server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;They allow access control&lt;/strong&gt;: Within the payload it is possible to specify user roles and permissions. You can also define the resources that the user can access.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Let tokens expire&lt;/strong&gt;: When a token is signed it will never expire unless you change the signing key or explicitly set an expiration. This could pose potential issues so it is necessary to have a strategy for expiring and/or revoking tokens.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Do not store sensitive data in the payload&lt;/strong&gt;: Tokens can be easily decoded, their goal is to protect against manipulation with their signature. So only add the necessary number of claims to the payload to have the best possible performance and security.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Be a good magician, do not reveal your secret&lt;/strong&gt;: Only reveal the signing key to services that really need it. It should be treated like any other credentials.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Utilize HTTPS&lt;/strong&gt;: On non-HTTPS connections the requests can be intercepted and tokens compromised more easily.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Keep in touch
&lt;/h1&gt;

&lt;p&gt;Contact me through my social media. Let's talk about security, authentication and programming in general, be it on &lt;a href="https://www.linkedin.com/in/stevescruz/"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://github.com/stevescruz"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Share with us what JWT good practices you advocate for.&lt;/p&gt;

</description>
      <category>security</category>
      <category>jwt</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Awesome Jest Tip: Coverage Report</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Mon, 17 Aug 2020 19:56:49 +0000</pubDate>
      <link>https://dev.to/stevescruz/awesome-jest-tip-coverage-report-h5j</link>
      <guid>https://dev.to/stevescruz/awesome-jest-tip-coverage-report-h5j</guid>
      <description>&lt;h1&gt;
  
  
  How much testing is enough?
&lt;/h1&gt;

&lt;p&gt;Sometimes we create some unit tests for our application to test our services, but we do not know the answers to these questions: Did we create enough unit tests? Did we create too many tests?&lt;/p&gt;

&lt;h1&gt;
  
  
  Jest: Coverage Report
&lt;/h1&gt;

&lt;p&gt;Popular JavaScript frameworks can use Facebook’s Jest to perform unit tests.&lt;/p&gt;

&lt;p&gt;Jest has the Coverage Report feature that allows us to check if our code covers all lines of the files we choose by generating an HTML file that we can open.&lt;/p&gt;

&lt;p&gt;In my case I chose to cover services that are in the services folder. These services are related to Users and Appointments, such as AuthenticateUser, UpdateUserAvatar and CreateAppointment.&lt;/p&gt;

&lt;p&gt;Our HTML file shows us that we almost have enough unit tests for our services related to Users and that we do not have any unit tests for our services related to Appointments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Dm7deO1H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lhf4ng03sd07svvqr82b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Dm7deO1H--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/lhf4ng03sd07svvqr82b.png" alt="appointments/services includes statistics on all services related to Appointments, while users/services includes statistics on all services related to Users."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we click in appointments/services we will see more in depth information showing coverage statistics on each service related to Appointments.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Branches&lt;/strong&gt; represent if statements which conditions have been fulfilled at least once during the unit tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functions&lt;/strong&gt; represent functions that have been called at least once during the unit tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Lines&lt;/strong&gt; represent code lines that have executed at least once during the unit tests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Statements&lt;/strong&gt; represent instructions that have been executed at least once during the unit tests. For example, we can have a line that contains two statements:

&lt;code&gt;var age= 18; console.log(age)&lt;/code&gt;

this contains a variable declaration statement and a statement that executes the log function that belongs to the console object.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RKPKtITK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pznivaz6u938n8v9kssx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RKPKtITK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pznivaz6u938n8v9kssx.png" alt="Contains statistics about each individual service related to Appointments, in this case we only have CreateAppointment."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we click on CreateAppointmentService.ts we are able to see every line of code contained in that file.&lt;/p&gt;

&lt;p&gt;Lines &lt;strong&gt;highlighted in pink&lt;/strong&gt; are statements that are not covered by the unit test. This helps us create tests for parts that are missing them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9vicDX_u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9p7bfwfbjfmmwcxpnhdq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9vicDX_u--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9p7bfwfbjfmmwcxpnhdq.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we return to the initial screen and click on users/services we will see more in depth information showing coverage statistics on each service related to Users. It is possible to see that some are completely covered and others are partially covered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DeKP9ES_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/29wqhcbg9gpzjfwudxlu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DeKP9ES_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/29wqhcbg9gpzjfwudxlu.png" alt="Contains statistics of each individual service related to Users, in this case we have CreateUser, UpdateUserAvatar, AuthenticateUser and SendEmailPasswordReset.&amp;lt;br&amp;gt;
"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we analyze the lines of code in the CreateAppointmentService.ts file we can see &lt;strong&gt;1x at the left hand side&lt;/strong&gt;, it means that we executed that part of the code one time during our unit tests. This happens because I only tested one of the functional requirements on my unit tests: “Should not allow unauthenticated users to change their avatar”.&lt;/p&gt;

&lt;p&gt;Notice there is an &lt;strong&gt;E symbol&lt;/strong&gt; at line 31. It means that we have not entered it during our unit tests (else path not taken).&lt;/p&gt;

&lt;p&gt;Also notice that there is a part of line 42 that is &lt;strong&gt;highlighted in yellow&lt;/strong&gt;. It means that the possible branch is not covered.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KpIDu57j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o0lccg511s0s36nvmtnn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KpIDu57j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o0lccg511s0s36nvmtnn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If we analyze the lines of code in the CreateUserService.ts file we can see &lt;strong&gt;3x and 4x in different places at the left hand side&lt;/strong&gt;, it means that we executed some parts of the code differently during our unit tests. This happens because I tested multiple different functional requirements on my unit tests. Examples of my requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;“Should be able to create a new appointment”.&lt;/li&gt;
&lt;li&gt;“Should not be able to create two appointments at the same time”.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EfkW60Dw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/khan3e4brlqg1ywppunc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EfkW60Dw--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/khan3e4brlqg1ywppunc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Keep in touch
&lt;/h1&gt;

&lt;p&gt;Contact me through my social media. Let's talk about unit tests and programming in general, be it on &lt;a href="https://www.linkedin.com/in/stevescruz/"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://github.com/stevescruz"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Share with us what tools you use to improve your tests.&lt;/p&gt;

</description>
      <category>jest</category>
      <category>testing</category>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>4 Tips to help people with Visual Impairments (a11y)</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Thu, 23 Jul 2020 22:55:16 +0000</pubDate>
      <link>https://dev.to/stevescruz/4-tips-to-help-people-with-visual-impairments-a11y-bgb</link>
      <guid>https://dev.to/stevescruz/4-tips-to-help-people-with-visual-impairments-a11y-bgb</guid>
      <description>&lt;p&gt;26.9 million American Adults age 18 and older reported experiencing vision loss according to a 2017 study from the American Foundation for Blind.&lt;/p&gt;

&lt;p&gt;Here are 5 things we can do to help promote web accessibility:&lt;/p&gt;

&lt;h1&gt;
  
  
  1. Be careful with Alert Boxes, Error Messages, and Pop-up Windows
&lt;/h1&gt;

&lt;p&gt;Do not use JavaScript or other client-side scripting to hide warnings, disclaimers, or error messages.&lt;/p&gt;

&lt;p&gt;When HTML elements are removed from the DOM the screen reader does not have access to them anymore.&lt;/p&gt;

&lt;p&gt;Important messages should be available to all site visitors.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm2ob9mgax681xjxhhe1z.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fm2ob9mgax681xjxhhe1z.gif" alt="hiding tooltip with opacity 0 instead of visibility: none"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this case I hid the tooltip using the CSS property &lt;code&gt;opacity: 0&lt;/code&gt;. Had I used JavaScript to remove the tooltip from the DOM or the CSS property &lt;code&gt;display: none&lt;/code&gt; most screen readers would not be able to read the tooltip without the visually impaired user hovering it because it was removed from the DOM.&lt;br&gt;
NOTE: &lt;code&gt;visibility: hidden&lt;/code&gt; does not remove the element from the DOM, remove them from the accessibility tree so screen readers cannot read it too.&lt;/p&gt;

&lt;p&gt;Another possible solution to this problem is to develop a server-side solution that rebuilds and serves a modified page with the error message embedded in it.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Do not use Color as the only way to convey information
&lt;/h1&gt;

&lt;p&gt;Color can be used for denoting required fields on a form or any input errors. However, some users may be color blind and have a hard time perceiving color, colors are also a problem to those who are blind so, when color is used, do not use it as the only means of conveying information.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2s5hwfkc83ti9yxakzsk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2s5hwfkc83ti9yxakzsk.png" alt="Twitter uses the red color with text to convey that an error has occurred"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It is common to use red as a color to convey error and green to convey success, but some websites only use a red border around the input to convey an input error without any additional cues.&lt;/p&gt;

&lt;p&gt;Twitter solves this by also displaying text instead of just using the red color to convey that an error has occurred, this approach is good for people with visual impairments.&lt;/p&gt;

&lt;h1&gt;
  
  
  3. Provide adequate Contrast
&lt;/h1&gt;

&lt;p&gt;There must be enough contrast between text and its background. This allows people with reduced vision to read and navigate the website site.&lt;/p&gt;

&lt;p&gt;The minimum contrast ratio between most text and its background must be 4.5:1. Exceptions include text that is larger and/or bold, they may have a contrast ratio of 3:1.&lt;br&gt;
NOTE: Large can be defined as 18-point text, bold is a 14-point bold text.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7c6kbzug5whqnjcyfhd1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7c6kbzug5whqnjcyfhd1.jpg" alt="Adequate contrast makes things easier to read"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Image: &lt;a href="https://www.brandeis.edu/web-accessibility/understanding/color.html" rel="noopener noreferrer"&gt;https://www.brandeis.edu/web-accessibility/understanding/color.html&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Free tools to check contrast
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.paciellogroup.com/resources/contrastanalyser/" rel="noopener noreferrer"&gt;Color Contrast Analyser&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://chrome.google.com/webstore/detail/color-contrast-analyzer/dagdlcijhfbmgkjokkjicnnfimlebcll" rel="noopener noreferrer"&gt;Color Contrast Analyzer for Chrome&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://snook.ca/technical/colour_contrast/colour.html#fg=33FF33,bg=333333" rel="noopener noreferrer"&gt;Color Contrast Check&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://contrast-ratio.com/" rel="noopener noreferrer"&gt;Contrast Ratio&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  4. Visible Focus
&lt;/h1&gt;

&lt;p&gt;Focus emphasizes the element that is currently selected. It allows users to know that they are about to activate a link, button, form control, etc.&lt;/p&gt;

&lt;p&gt;This helps people who have reduced vision or other print-related disabilities have a clear indicator of where they are on a web page.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0svhdw2ud8dab7zzegvy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F0svhdw2ud8dab7zzegvy.png" alt="Elements in the twitter page have visible focus"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: If a border is used to indicate focus on an element, it must have sufficient contrast so it can be clearly noticed.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to check if your page's elements have visible focus?
&lt;/h3&gt;

&lt;p&gt;Use the tab key to cycle through all the page's elements. As each element receives focus, there must be a clearly visible change to the element.&lt;/p&gt;

&lt;h1&gt;
  
  
  Keep in touch
&lt;/h1&gt;

&lt;p&gt;Contact me through my social media. Let's talk about accessibility and programming in general, be it on &lt;a href="https://www.linkedin.com/in/stevescruz/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; or &lt;a href="https://github.com/stevescruz" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Share with us what you do to improve accessibility for those with visual impairments.&lt;/p&gt;

</description>
      <category>a11y</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>css</category>
    </item>
  </channel>
</rss>
