<?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: Petros Koulianos</title>
    <description>The latest articles on DEV Community by Petros Koulianos (@petroskoulianos).</description>
    <link>https://dev.to/petroskoulianos</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%2F348806%2F1ab20f0d-d5fe-4f53-9eb8-d070af799309.jpg</url>
      <title>DEV Community: Petros Koulianos</title>
      <link>https://dev.to/petroskoulianos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/petroskoulianos"/>
    <language>en</language>
    <item>
      <title>2 Tips to Clean Ugly if Statements</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Mon, 18 Jan 2021 18:03:50 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/2-tips-to-clean-ugly-if-statements-19k</link>
      <guid>https://dev.to/petroskoulianos/2-tips-to-clean-ugly-if-statements-19k</guid>
      <description>&lt;p&gt;Conditional statements are the backbone of programming but lot of times business requirements can lead to create long nested and ugly if statements.&lt;br&gt;
This post demonstrates some tips to clean those ugly situations.&lt;/p&gt;
&lt;h1&gt;
  
  
  #1 Complex condition expressions
&lt;/h1&gt;

&lt;p&gt;Long and complex condition expressions are obvious an ugly situation&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// weird 😣😣&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&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="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;gusts&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&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="nx"&gt;snowing&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
&lt;span class="c1"&gt;//code block&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Solution create a separate function to return a boolean that represents the long condition&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// better 😁😁&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isColdOutside&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;windGusts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;snowing&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
&lt;span class="c1"&gt;//code block&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isColdOutside&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;temp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;windGusts&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;snowing&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="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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="nx"&gt;snowing&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;windGusts&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  #2 Ternary into ternary
&lt;/h1&gt;

&lt;p&gt;This is another situation that is ugly and the human brain struggle to parse&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// weird 😣😣&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;windGusts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isFreezingOutside&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;windGusts&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&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;snowing&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Solution again here we can create smaller functions to make it cleaner&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// better 😁😁&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;windGusts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;isFreezingOutside&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;temp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;isSnowing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;snowing&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;isWindStrong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;windGusts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isWindStrong&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;windGusts&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="nx"&gt;windGusts&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;isSnowing&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;snowing&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="nx"&gt;snowing&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This was two quick tips to clean ugly if statements.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Thanks for reading 😎😎😎&lt;/p&gt;

&lt;p&gt;twitter &lt;a href="https://twitter.com/PetrosKoulianos"&gt;@petroskoulianos&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>3 password REGEX for your next project</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Fri, 15 Jan 2021 08:55:17 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/3-password-regex-for-your-next-project-53fn</link>
      <guid>https://dev.to/petroskoulianos/3-password-regex-for-your-next-project-53fn</guid>
      <description>&lt;p&gt;Regular expressions are cryptic and hard to understand and build one from scratch.&lt;br&gt;
But don't panic with those cryptic symbols, the dev community can help.&lt;br&gt;
Here i am writing 3 password regular expressions to use it at your next JavaScript front end app or your next nodeJs back end application.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// PASSWORD REGEX FOR YOUR NEXT JAVASCRIPT APP&lt;/span&gt;

&lt;span class="c1"&gt;// regex for a basic password must be&lt;/span&gt;
&lt;span class="c1"&gt;// more than 8 chars &lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PASSWORD_REGEX_1&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;A-Za-z0-9&lt;/span&gt;&lt;span class="se"&gt;]\w{8,}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// more secure regex password must be&lt;/span&gt;
&lt;span class="c1"&gt;// more than 8 chars &lt;/span&gt;
&lt;span class="c1"&gt;// at least one number&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PASSWORD_REGEX_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;\d)(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;])(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;A-Z&lt;/span&gt;&lt;span class="se"&gt;])&lt;/span&gt;&lt;span class="sr"&gt;.&lt;/span&gt;&lt;span class="se"&gt;{8,}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// more secure regex password must be :&lt;/span&gt;
&lt;span class="c1"&gt;// more than 8 chars  &lt;/span&gt;
&lt;span class="c1"&gt;// at least one number&lt;/span&gt;
&lt;span class="c1"&gt;// at least one special character&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;PASSWORD_REGEX_3&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;  &lt;span class="sr"&gt;/^&lt;/span&gt;&lt;span class="se"&gt;(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;\d)(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;a-z&lt;/span&gt;&lt;span class="se"&gt;])(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;A-Z&lt;/span&gt;&lt;span class="se"&gt;])(?=&lt;/span&gt;&lt;span class="sr"&gt;.*&lt;/span&gt;&lt;span class="se"&gt;[&lt;/span&gt;&lt;span class="sr"&gt;!@#$%^&amp;amp;*&lt;/span&gt;&lt;span class="se"&gt;])&lt;/span&gt;&lt;span class="sr"&gt;.&lt;/span&gt;&lt;span class="se"&gt;{8,}&lt;/span&gt;&lt;span class="sr"&gt;$/&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can combine all 3 regular expressions to show proper warning messages.&lt;/p&gt;

&lt;p&gt;Great web sites to check your regular expressions :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://regex101.com"&gt;regex101&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://regexr.com"&gt;regexr&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.regextester.com"&gt;regextester&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Structuring an expressJS application</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Tue, 12 Jan 2021 12:21:07 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/structuring-an-expressjs-application-2690</link>
      <guid>https://dev.to/petroskoulianos/structuring-an-expressjs-application-2690</guid>
      <description>&lt;p&gt;This is my opinion for file structuring expressJS apps. &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9lbp1-1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/70ygs6d8hqtsr45mb4dn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9lbp1-1i--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/70ygs6d8hqtsr45mb4dn.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I am open to read any opinions and for further discussion.&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Looking for React Theme to Build a Dev Blog</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Thu, 13 Aug 2020 19:32:00 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/looking-for-react-theme-to-build-a-dev-blog-1fi</link>
      <guid>https://dev.to/petroskoulianos/looking-for-react-theme-to-build-a-dev-blog-1fi</guid>
      <description>&lt;p&gt;I am looking for a minimal React Theme to build a developer blog, any suggestions would be useful.&lt;br&gt;
Thanks 😀😀&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>node</category>
      <category>design</category>
    </item>
    <item>
      <title>5 Reasons to Use SQLite the Tiny GIANT for Your Next Project</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Fri, 07 Aug 2020 16:43:08 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/5-reasons-to-use-sqlite-the-tiny-giant-for-your-next-project-3lhb</link>
      <guid>https://dev.to/petroskoulianos/5-reasons-to-use-sqlite-the-tiny-giant-for-your-next-project-3lhb</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/swlh/5-reasons-to-use-sqlite-the-tiny-giant-for-your-next-project-a6bc384b2df4?source=rss-bba4d4e74108------2"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TNJ3JO9Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2592/0%2AOTLnmllxK-I1FGrG" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SQLite database is a tiny giant in the SQL area with a fully-featured SQL database engine and billions of deployments.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/swlh/5-reasons-to-use-sqlite-the-tiny-giant-for-your-next-project-a6bc384b2df4?source=rss-bba4d4e74108------2"&gt;Continue reading on The Startup »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>relationaldatabases</category>
      <category>programming</category>
      <category>sql</category>
      <category>software</category>
    </item>
    <item>
      <title>Proper Ways to Clone an Object in JavaScript</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Wed, 22 Jul 2020 07:31:40 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/proper-ways-to-clone-an-object-in-javascript-2i9p</link>
      <guid>https://dev.to/petroskoulianos/proper-ways-to-clone-an-object-in-javascript-2i9p</guid>
      <description>&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ba1-syeK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/0%2A3MBOGQB4P2dtrBud" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ba1-syeK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/0%2A3MBOGQB4P2dtrBud" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://unsplash.com/@bedeviere?utm_source=medium&amp;amp;utm_medium=referral"&gt;Bimata Prathama&lt;/a&gt; on &lt;a href="https://unsplash.com/?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Objects in JavaScript are reference values and can store complex key-value properties.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;story&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Proper Ways to Copy(Clone) an Object in JavaScript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;author&lt;/span&gt;&lt;span class="p"&gt;:{&lt;/span&gt;
            &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pkoulianos&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;petran@pkoulianos.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;tags&lt;/span&gt;&lt;span class="p"&gt;:[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Javascript&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;programming&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Copy an object can be a little tricky. But don’t worry about in this story we’ll cover how to copy an object in proper ways.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. The Fatal😡 Way to Copy an Object
&lt;/h3&gt;

&lt;p&gt;A fatal way to try copying an object is to use the assign = operator. The reason is that the assign operator will only pass the reference to the new variable.&lt;/p&gt;

&lt;p&gt;Let’s see a simple example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let car1 = { color:’white’, type:’4X4'};// fatal way to copy an object
let car2 = car1;//change the color property
car2.color = ‘red’;console.log(car1);
**//{ color: 'red', type: '4X4' }** 😂😂
console.log(car2);
**//{ color: 'red', type: '4X4' }** 😂😂
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we create a new object car1 and try to copy it with the = operator to a new variable car2 and we change the color property. Printing both objects we can see that is identical and the reason is that both car1 and car2 have the same reference of the object.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Get a Shallow💧 copy
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"&gt;&lt;em&gt;Shallow copy&lt;/em&gt;&lt;/a&gt; &lt;em&gt;will copy all&lt;/em&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable"&gt;&lt;em&gt;enumerable&lt;/em&gt;&lt;/a&gt; &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty"&gt;&lt;em&gt;own properties&lt;/em&gt;&lt;/a&gt; &lt;em&gt;from the source object to the target.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In simple terms, a shallow copy &lt;strong&gt;will not&lt;/strong&gt; truly copy :&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Arrays, Sets, etc&lt;/li&gt;
&lt;li&gt;Inner objects&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Get a shallow copy with Object.assign()&lt;/p&gt;

&lt;p&gt;Object.assign() will get you a shallow copy of your target object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let post = {
   title:'How to copy objects in JS',
   tags:['js','js-basics','programming'],
   date: new Date(),
   author:{
         name:'petros',
         email:'petran@pkoulianos.com'
   },
   getAuthorData: function(){
              return this.author.name+'-'+this.author.email;
   }
};let newPost = Object.assign({},post);
newPost.title = 'I love js'
newPost.tags[0] = 'web-programming'
newPost.author.name = 'Petran';
newPost.date = new Date(1970);console.log(post);
console.log(newPost);//console output
{ title: 'How to copy objects in JS',
  tags: ['web-programming', 'js-basics', 'programming'],
  date: 2020-07-21T18:48:29.112Z,
  author: { name: 'Petran', email: '[petran@pkoulianos.com](mailto:petran@pkoulianos.com)' },
  getAuthorData: [Function: getAuthorData] }
{ title: 'I love js',😀
  tags: ['web-programming', 'js-basics', 'programming'],😂
  date: 1970-01-01T00:00:01.970Z,😀
  author: { name: 'Petran', email: '[petran@pkoulianos.com](mailto:petran@pkoulianos.com)' },😂
  getAuthorData: [Function: getAuthorData] }😀
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we create a new object post and copy it with Object.assign() to a new variable newPost and we change all properties. Printing both objects we can see that the shallow copy newPost have copied properly the title ,date and getAuthorData but tags and author are pass by reference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get a shallow copy with …Spread operator
&lt;/h3&gt;

&lt;p&gt;The spread operator will get you also a shallow copy of your target object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ \*\*\* / 
**let newPost = {...post}**
/ \*\*\* /
console.log(post);
console.log(newPost);//console output
{ title: 'How to copy objects in JS',
  tags: ['web-programming', 'js-basics', 'programming'],
  date: 2020-07-21T18:48:29.112Z,
  author: { name: 'Petran', email: '[petran@pkoulianos.com](mailto:petran@pkoulianos.com)' },
  getAuthorData: [Function: getAuthorData] }
{ title: 'I love js',
  tags: ['web-programming', 'js-basics', 'programming'],
  date: 1970-01-01T00:00:01.970Z,
  author: { name: 'Petran', email: '[petran@pkoulianos.com](mailto:petran@pkoulianos.com)' },
  getAuthorData: [Function: getAuthorData] }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Get a Deep🌊 copy
&lt;/h3&gt;

&lt;p&gt;A deep copy of an object will solve the mystery of getting a proper copy of inner objects and arrays, sets, etc &lt;strong&gt;but date objects will be converted to string and functions will not be copied at all.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We can get a deep copy by using the JSON object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;let targetObj = JSON.parse(JSON.stringify(sourceObj));&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/ \*\*\* / 
let newPost = JSON.parse(JSON.stringify(post));
/ \*\*\* /
console.log(post);
console.log(newPost);//console output
{ title: 'How to copy objects in JS',
  tags: ['js', 'js-basics', 'programming'],
  date: 2020-07-21T18:54:35.964Z,
  author: { name: 'petros', email: '[petran@pkoulianos.com](mailto:petran@pkoulianos.com)' },
  getAuthorData: [Function: getAuthorData] }
{ title: 'I love js',
  tags: ['web-programming', 'js-basics', 'programming'],
  date: **'2020-07-21T18:54:35.964Z'** ,😂
  author: { name: 'Petran', email: '[petran@pkoulianos.com](mailto:petran@pkoulianos.com)' } }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Printing both objects we can see that the deep copy newPost have copied properly the title , tags and author but date is converted to string and getAuthorData are not copied at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Conclusion
&lt;/h3&gt;

&lt;p&gt;Both Shallow and Deep copies have their own pros and cons. Before we decide which copy is right, we have to be sure about the object properties.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




</description>
      <category>webdev</category>
      <category>beginners</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Build a Flush Message Middleware with Node.js from Scratch</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Wed, 22 Apr 2020 12:20:02 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/build-a-flush-message-middleware-with-node-js-from-scratch-4ahn</link>
      <guid>https://dev.to/petroskoulianos/build-a-flush-message-middleware-with-node-js-from-scratch-4ahn</guid>
      <description>&lt;p&gt;Learn how to build a flush messages middleware system with node.js and express.js from scratch&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Nas2RYoU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/0%2AU-cesLrLj4Rbc30o" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Nas2RYoU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://miro.medium.com/max/1400/0%2AU-cesLrLj4Rbc30o" alt=""&gt;&lt;/a&gt;Photo by &lt;a href="https://miro.medium.com/max/1400/0*U-cesLrLj4Rbc30o"&gt;Photo by Octavian Rosca&lt;/a&gt; on &lt;a href="https://miro.medium.com/max/1400/0*U-cesLrLj4Rbc30o"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  1.Introduction
&lt;/h3&gt;

&lt;p&gt;In this story, we’ll learn how to build a flush message middleware from scratch with node.js and express.js.&lt;/p&gt;

&lt;p&gt;But what is flush messages?&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Flush messages are small messages with information that inform the user-client about various cases, such as unsuccessful login, invalid input on a form field etc …&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://medium.com/javascript-in-plain-english/build-a-flush-message-middleware-with-node-js-from-scratch-843f6e9823ba"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>Regular Expressions Cheat Sheet in Node.js</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Tue, 07 Apr 2020 19:40:35 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/regular-expressions-cheat-sheet-in-node-js-d63</link>
      <guid>https://dev.to/petroskoulianos/regular-expressions-cheat-sheet-in-node-js-d63</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/javascript-in-plain-english/learn-to-use-regular-expressions-like-a-ninja-in-node-js-20cfb6806f26?source=rss-bba4d4e74108------2"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--jyfCPHrz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/0%2A_zDio712oJdK_J3h" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A detailed story to learn, write and execute regular expressions easily.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/javascript-in-plain-english/learn-to-use-regular-expressions-like-a-ninja-in-node-js-20cfb6806f26?source=rss-bba4d4e74108------2"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>How to Implement a Stack in Node.js</title>
      <dc:creator>Petros Koulianos</dc:creator>
      <pubDate>Tue, 31 Mar 2020 21:01:44 +0000</pubDate>
      <link>https://dev.to/petroskoulianos/how-to-implement-a-stack-in-node-js-1hn0</link>
      <guid>https://dev.to/petroskoulianos/how-to-implement-a-stack-in-node-js-1hn0</guid>
      <description>&lt;p&gt;&lt;a href="https://medium.com/javascript-in-plain-english/how-to-implement-a-stack-in-node-js-e7b43af282d4?source=rss-bba4d4e74108------2"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--U6X_4UsH--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/2600/0%2A2qmCy1qhKeVpPTv8" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Stack is one of the most important data structures in programming, but how we can implement a stack in javascript? Let’s dig in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://medium.com/javascript-in-plain-english/how-to-implement-a-stack-in-node-js-e7b43af282d4?source=rss-bba4d4e74108------2"&gt;Continue reading on JavaScript in Plain English »&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
    </item>
  </channel>
</rss>
