<?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>Domain-driven Design (DDD): File Structure</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Tue, 01 Sep 2020 22:51:39 +0000</pubDate>
      <link>https://dev.to/stevescruz/domain-driven-design-ddd-file-structure-4pja</link>
      <guid>https://dev.to/stevescruz/domain-driven-design-ddd-file-structure-4pja</guid>
      <description>&lt;h1&gt;
  
  
  Project Current File Structure
&lt;/h1&gt;

&lt;p&gt;&lt;em&gt;src&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;config&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;database&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;errors&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;middlewares&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;models&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;repositories&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;routes&lt;/em&gt; folder&lt;br&gt;
 -&lt;em&gt;services&lt;/em&gt; folder&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;src&lt;/em&gt; folder should contain our application &lt;em&gt;modules&lt;/em&gt;. Everything in it impacts directly &lt;strong&gt;how our application functions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We currently &lt;strong&gt;separate&lt;/strong&gt; our application by &lt;strong&gt;file types&lt;/strong&gt;. So files of the service type are stored in the services folder.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;problem&lt;/strong&gt; with this is: if we had 10 models, we would have around 40–50 services. Having so many files that do not deal with the &lt;strong&gt;same module&lt;/strong&gt;, or in other words, do not deal with the &lt;strong&gt;same domain&lt;/strong&gt; becomes confusing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Domain
&lt;/h1&gt;

&lt;p&gt;Is the &lt;em&gt;sphere of knowledge&lt;/em&gt; that involves that &lt;strong&gt;particular file&lt;/strong&gt; or &lt;strong&gt;module&lt;/strong&gt;. A set of &lt;strong&gt;business rules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So it is not always good to build our project structure around separating files by their type.&lt;/p&gt;

&lt;p&gt;A good way to build our project structure is to isolate things according to their &lt;strong&gt;domain&lt;/strong&gt;, by using &lt;strong&gt;modules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So services related to &lt;em&gt;users&lt;/em&gt; such as Authenticate User Service, Create Appointment Service, Create User Service, Update User Avatar Service should all belong to the &lt;strong&gt;User Domain&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There would be an &lt;strong&gt;Appointment Domain&lt;/strong&gt; that should only take care of &lt;strong&gt;business logic&lt;/strong&gt; related to &lt;em&gt;appointments&lt;/em&gt;, be it to create appointments, list appointments, check available appointments or cancel appointments.&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%2Fachtyekeojee4h8edu2c.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%2Fachtyekeojee4h8edu2c.png" alt="DDD can divide an ecommerce in the the delivery context with order, product and customer and the finance context." width="800" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  DDD (Domain-driven Design)
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Scrum&lt;/strong&gt; is an &lt;em&gt;agile methodology&lt;/em&gt; that allows the team to organize and execute their tasks in an agile manner.&lt;/p&gt;

&lt;p&gt;But &lt;strong&gt;Scrum&lt;/strong&gt; does not work in the same way for all teams and projects, we implement the ideas and concepts in the way that makes more sense for our context. &lt;strong&gt;DDD&lt;/strong&gt; works in the same way, there are some concepts that only make sense in enterprise applications (very big applications).&lt;/p&gt;

&lt;h1&gt;
  
  
  Modules
&lt;/h1&gt;

&lt;p&gt;What are the &lt;strong&gt;modules&lt;/strong&gt; (&lt;em&gt;spheres of knowledge&lt;/em&gt;) that  we have in our application? The &lt;strong&gt;user domain&lt;/strong&gt; and the &lt;strong&gt;appointment domain&lt;/strong&gt;.&lt;br&gt;
We must create a &lt;strong&gt;&lt;em&gt;modules&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;folder&lt;/strong&gt; to accommodate our modules and inside it we create the &lt;strong&gt;&lt;em&gt;users&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;appointments&lt;/em&gt;&lt;/strong&gt; &lt;strong&gt;subfolders&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can we store inside the modules/users folder?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;DTOs&lt;/li&gt;
&lt;li&gt;Entities/Models&lt;/li&gt;
&lt;li&gt;Repositories (including our fake/mock implementations of them for TDD), services&lt;/li&gt;
&lt;li&gt;Providers that are specific to users (a hash provider that hashes the user password)&lt;/li&gt;
&lt;li&gt;Infra that is specific to users (we will see below what the infrastructure layer means).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Shared
&lt;/h1&gt;

&lt;p&gt;Files with logic that is shared between all modules or multiple modules should be saved in the &lt;em&gt;shared folder&lt;/em&gt; that is outside of the modules folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  What can we store inside the shared folder?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Errors&lt;/li&gt;
&lt;li&gt;Shared database files(connections and migrations)&lt;/li&gt;
&lt;li&gt;Shared routes&lt;/li&gt;
&lt;li&gt;Shared middleware&lt;/li&gt;
&lt;li&gt;Shared providers (storage provider, email provider).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Infrastructure Layer vs Domain
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Domain&lt;/strong&gt;: Is the &lt;strong&gt;&lt;em&gt;sphere of knowledge&lt;/em&gt;&lt;/strong&gt; that involves that particular module. A set of &lt;strong&gt;business rules&lt;/strong&gt;. It takes care of how the application should work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Infrastructure Layer&lt;/strong&gt;: Is how data that is initially held by in the domain's entities (memory) is persisted in databases or another persistent store. the infrastructure layer must not "contaminate" the domain model layer. &lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: It contains our application's technical decisions&lt;/p&gt;

&lt;p&gt;When creating &lt;a href="https://github.com/stevescruz/ebarber_web" rel="noopener noreferrer"&gt;eBarber&lt;/a&gt; a &lt;strong&gt;meeting with the barbers or the users&lt;/strong&gt; will help you better determine the Domain/Business Logic. But a barber does not have the expertise to tell you what tools you should use: if you should use an ORM or a Query Builder.&lt;/p&gt;

&lt;p&gt;A CTO and/or senior engineers decide things that are related to the infrastructure layer.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;user domain&lt;/strong&gt; knows that when a user signs up he should receive an email, but it does not know what tool is being used to send emails. That is the role of the Infrastructure Layer.&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%2Fh134ezqol2olgh6h8ojs.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%2Fh134ezqol2olgh6h8ojs.png" alt="The domain layer (service, repository, domain object) should not depend on the infrastructure layer nor the application layer" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Read: &lt;a href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/microservice-ddd-cqrs-patterns/ddd-oriented-microservice" rel="noopener noreferrer"&gt;Microsoft &amp;amp; DDD-oriented Microservices&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Infra
&lt;/h2&gt;

&lt;p&gt;To accommodate the infrastructure layer we can create multiple &lt;em&gt;infra&lt;/em&gt; folders. We can have an &lt;em&gt;infra&lt;/em&gt; folder inside our &lt;em&gt;shared&lt;/em&gt; folder and inside each of our &lt;em&gt;modules&lt;/em&gt; (&lt;em&gt;users&lt;/em&gt; and &lt;em&gt;appointments&lt;/em&gt;).&lt;/p&gt;

&lt;p&gt;We can create a &lt;em&gt;database&lt;/em&gt; folder inside the &lt;em&gt;infra&lt;/em&gt; folder to deal with things related to our database. We should give it a specific name, since our project uses TypeORM, we should name our &lt;em&gt;database&lt;/em&gt; folder as &lt;strong&gt;&lt;em&gt;typeorm&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We can also create a &lt;em&gt;http&lt;/em&gt; folder inside the &lt;em&gt;infra&lt;/em&gt; folder to deal with things related to our http requests and responses such as &lt;em&gt;server.ts&lt;/em&gt;, routes and middleware.&lt;br&gt;
&lt;strong&gt;NOTE&lt;/strong&gt;: Once again we should give it a specific name. If we were using another type of protocol such as &lt;strong&gt;gRPC&lt;/strong&gt; we would give it another name.&lt;/p&gt;

&lt;h2&gt;
  
  
  Additional Notes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Infra will store all information that is responsible for a specific package or library.
Example: TypeORM.&lt;/li&gt;
&lt;li&gt;Errors may be shared but they are not part of the infra layer.&lt;/li&gt;
&lt;li&gt;It is good to include things that could change in the infra folder.

&lt;ul&gt;
&lt;li&gt;Example 1: Express routes and middleware.&lt;/li&gt;
&lt;li&gt;Example 2: Imagine if we need to change the communication protocol from HTTP to gRPC.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Project File Structure using DDD
&lt;/h1&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%2F6oxzwmipotzwlrkcmjv4.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%2F6oxzwmipotzwlrkcmjv4.png" alt="Alt Text" width="325" height="571"&gt;&lt;/a&gt;&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%2Fvhwtp9azq29evtl0ade1.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%2Fvhwtp9azq29evtl0ade1.png" alt="Alt Text" width="291" height="730"&gt;&lt;/a&gt;&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%2F1c4ftl981yqwdpyndcwv.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%2F1c4ftl981yqwdpyndcwv.png" alt="Alt Text" width="300" height="729"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Final considerations
&lt;/h1&gt;

&lt;p&gt;The &lt;strong&gt;main advantage&lt;/strong&gt; of Domain Driven Design is being able to create code with well-defined components that have &lt;strong&gt;clear contracts&lt;/strong&gt; between them. This allows us to better define their responsibilities, makes &lt;strong&gt;updating&lt;/strong&gt; or &lt;strong&gt;replacing&lt;/strong&gt; one of these components much easier with &lt;strong&gt;less impact&lt;/strong&gt; on the overall system.&lt;/p&gt;

&lt;p&gt;The key &lt;strong&gt;disadvantage&lt;/strong&gt; is that DDD assumes that you have a fairly clear picture of the solution you are trying to produce, but that is not always the case. A solution to this problem is to create a small prototype, possibly iterate over it multiple times until you have enough understanding to come up with the reasonable design. This can delay the start of the project, but is likely to result in a more maintainable solution.&lt;/p&gt;

&lt;p&gt;This post only touches the tip of the iceberg, I encourage you to &lt;strong&gt;read more about DDD&lt;/strong&gt; including about the &lt;strong&gt;application layer&lt;/strong&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 DDD, TDD and good practices, 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 how you decide your project file structure.&lt;/p&gt;

</description>
      <category>ddd</category>
      <category>javascript</category>
      <category>architecture</category>
      <category>computerscience</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>
    <item>
      <title>Accessibility &amp; Technology: Connecting People</title>
      <dc:creator>Steve Cruz</dc:creator>
      <pubDate>Mon, 13 Jul 2020 17:37:45 +0000</pubDate>
      <link>https://dev.to/stevescruz/accessibility-technology-connecting-people-491a</link>
      <guid>https://dev.to/stevescruz/accessibility-technology-connecting-people-491a</guid>
      <description>&lt;h1&gt;
  
  
  The purpose of technology
&lt;/h1&gt;

&lt;p&gt;Technology evolves at an ever increasing pace, every year there is a new iPhone product with better lens and more features.&lt;/p&gt;

&lt;p&gt;In a not so distant past insulin was harnessed from pigs and cows, but since the 80s recombinant DNA  is used to produce this medication by inserting a human gene into the genetic material of a common bacterium.&lt;/p&gt;

&lt;p&gt;So what is the purpose of technology? Is it profit? Is it to increase life expectancy and eradicate disease? Guarantee a sustainable future for the planet and the future generations?&lt;/p&gt;

&lt;p&gt;It is possible to strive for all these, but a very important purpose of technology is to connect people.&lt;/p&gt;

&lt;h1&gt;
  
  
  Technology connects people
&lt;/h1&gt;

&lt;p&gt;Humans have been looking for ways of connecting people as far back as Ancient Greece. In those times people reunited in the agora, a public space, to shop, discuss different political views. It is hard to imagine nowadays a marketplace and a courtroom side by side where everyone is welcome to speak and consider views other than their own. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: Agora can be literally translated to "gathering place" or "assembly".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://imgbb.com/"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OFcB-YRQ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/djS9wkn/greek-agora.jpg" alt="Greek"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But, even though there was a lot of engagement between people and their local community, which is something that many believe is lacking nowadays, distance was a problem, for the average citizen it was hard to connect with and maintain contact with people far away, news traveled slowly.&lt;/p&gt;

&lt;p&gt;Humans are obsessed with solving problems, we have harnessed technology to communicate in many different ways, the printing press allowed newspapers to connect people, afterwards the telephone, the radio, the tv, the internet with computers and cellphones. &lt;/p&gt;

&lt;p&gt;But even with all these advances there is a significant amount of the human population that become excluded without being able to enjoy the benefits of technology to connect and learn. They are invisible to technology and most of the time their unique needs are not taken into account when developing these technologies.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://ibb.co/R7Rvshn"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rJd22OlV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/KmT0fNZ/communication-methodsjpg.jpg" alt="communication-methods"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Who are the people invisible to technology?
&lt;/h1&gt;

&lt;p&gt;The World Health Organization (WHO) conducted a study and published its results in the Disability and health fact sheet. According to it "Over a billion people, about 15% of the world's population, have some form of disability". It also states that "Between 110 million and 190 million adults have significant difficulties in functioning."&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.who.int/en/news-room/fact-sheets/detail/disability-and-health"&gt;https://www.who.int/en/news-room/fact-sheets/detail/disability-and-health&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Disabilities categories
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://ibb.co/XXhzY1y"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MtGdRw9l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/p2t3hcW/typesofdeficiency.jpg" alt="typesofdeficiency"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Visual impairments
&lt;/h3&gt;

&lt;p&gt;They include people that suffer with blindness, color-blindness and low vision, the World Health Organization estimates that "285 million people are estimated to be visually impaired worldwide".&lt;/p&gt;

&lt;p&gt;Many websites set font size with px instead of using the relative units, this overrides the browsers ability to allow a user with low vision to enlarge the font-size though its settings.&lt;/p&gt;

&lt;p&gt;Because of that web browsers took matter onto their own hands and now have zooming capabilities to help fix this problem. &lt;/p&gt;

&lt;p&gt;Some people use screen readers, software that reads text aloud, but for information to be read properly it requires the website to be optimized with semantic HTML. Using the &lt;code&gt;visibility: hidden&lt;/code&gt; CSS property hides an HTML element from the accessibility tree, so the screen reader cannot read the information increasing exclusion.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/IK97XMibEws"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Auditory impairments
&lt;/h3&gt;

&lt;p&gt;They include people that suffer with deafness and loss of hearing, the World Health Organization estimates that "466 million people worldwide have disabling hearing loss".&lt;br&gt;
These people use ATs that are Assistive Devices for People with Hearing, Voice, Speech, or Language Disorders. &lt;br&gt;
It is also essential for websites to provide textual alternatives to audio content:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Text transcripts that can be displayed with audio visual content.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Motor impairments
&lt;/h3&gt;

&lt;p&gt;Includes people that have disabilities related to movement.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Physical issues: related to a temporary or permanent paralysis or a loss of limbs.&lt;/li&gt;
&lt;li&gt;Neurological and/or genetic disorders: responsible for loss of control in limbs or weakness.&lt;/li&gt;
&lt;li&gt;Both&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Due to this many people cannot use a mouse, have a slow response time and/or limited fine motor control, The US Centers for Disease Control and Prevention's Disability and Functioning statistics estimate that in the US "Percent of adults with any physical functioning difficulty: 16.1%".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://www.cdc.gov/nchs/fastats/disability.htm"&gt;https://www.cdc.gov/nchs/fastats/disability.htm&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://imgbb.com/"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--yoYrLktg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://i.ibb.co/D4P6pkj/rsz-mouse-pointer.jpg" alt="rsz-mouse-pointer"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Some people have been paralyzed to such a degree that head pointers are necessary to utilize the computer. Picture from &lt;a href="https://www.performancehealth.com/baseball-cap-head-pointer"&gt;https://www.performancehealth.com/baseball-cap-head-pointer&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Cognitive impairments
&lt;/h3&gt;

&lt;p&gt;Includes people that have a broad range of disabilities, such as learning disabilities (dyslexia and attention deficit hyperactivity disorder), distractibility, inability to remember or focus on large amounts of information. It also includes people with mental illnesses, such as depression and schizophrenia.&lt;/p&gt;

&lt;p&gt;These impairments may introduce difficulty when interacting with websites, such as having a hard time understanding content or confusion created by inconsistent webpage layouts. An example of an inconsistent and unintuitive layout is a computer software that has its x button responsible for closing the application on the lower left side of the screen.&lt;/p&gt;

&lt;h1&gt;
  
  
  Accessibility and programming
&lt;/h1&gt;

&lt;p&gt;Each kind of impairment brings unique challenges and requires strategy in content design. Accessibility is keeping these unique challenges in mind and building strategies that have a usability that goes beyond people with disabilities. &lt;/p&gt;

&lt;p&gt;An example of this is that everyone enjoys websites that have intuitive navigation, useful illustrations, and content that is organized in a logical manner. &lt;/p&gt;

&lt;p&gt;Another example is that while users with disabilities need captions and transcripts, they are useful to anyone that is using multimedia in environments that are too noisy or in an environment that is silent when they do not have earbuds.&lt;/p&gt;

&lt;p&gt;With accessibility in mind the Web Content Accessibility Guidelines (WCAG) Overview were created to promote inclusion in the internet. In future posts I will use WCAG to talk more in depth about the unique challenges each type of impairment presents and strategies to tackle them.&lt;/p&gt;

&lt;h1&gt;
  
  
  Let's talk about this
&lt;/h1&gt;

&lt;p&gt;Contact me through my social media, seriously. Let's talk about accessibility 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;Technology can be accessible to a broader audience by having diverse people build while collaborating and sharing their unique needs and experiences.&lt;/p&gt;

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