<?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: Varit Patel</title>
    <description>The latest articles on DEV Community by Varit Patel (@varit05).</description>
    <link>https://dev.to/varit05</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%2F63134%2Fde165d29-99b4-4db9-bcad-04ea83189a95.jpeg</url>
      <title>DEV Community: Varit Patel</title>
      <link>https://dev.to/varit05</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varit05"/>
    <language>en</language>
    <item>
      <title>LeetCode Question 29</title>
      <dc:creator>Varit Patel</dc:creator>
      <pubDate>Sat, 09 Mar 2024 23:15:47 +0000</pubDate>
      <link>https://dev.to/varit05/leetcode-question-29-4bm2</link>
      <guid>https://dev.to/varit05/leetcode-question-29-4bm2</guid>
      <description>&lt;p&gt;&lt;a href="https://leetcode.com/problems/divide-two-integers/description/"&gt;29. Divide Two Integers&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Given two integers dividend and divisor, divide two integers without using &lt;strong&gt;&lt;em&gt;multiplication, division, and mod operator&lt;/em&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's solve it :-)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var divide = function(dividend, divisor) {
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Function divide has two parameters dividend and divisor. &lt;/p&gt;

&lt;p&gt;First of all, Let's check some weird scenarios: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Question has a note which says 32-bit signed integer range: [−2*&lt;em&gt;31, 2&lt;/em&gt;*31 − 1] so this indicates the max and min(on Negative side) number which we need to process&lt;/li&gt;
&lt;li&gt;What if the dividend and divisor both have negative value&lt;/li&gt;
&lt;li&gt;What if the dividend or divisor has negative value&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's code it ;-)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; const signedIntegerValue = 2**31
 var divide = function(dividend, divisor) {
    const retIsNegative = Math.sign(divisor) !== Math.sign(dividend);
    dividend = Math.abs(dividend)
    divisor = Math.abs(divisor)

    let result = 0

    // Logic here below...

    return retIsNegative ? -result : result
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Define the 32 bit signed integer range as constant at above &lt;/p&gt;

&lt;p&gt;Now inside function calculate weather we have one of the value as negative or not. &lt;/p&gt;

&lt;p&gt;If both signs are not same then, return value is negative. &lt;/p&gt;

&lt;p&gt;Next lines, Makes both of them to the absolute value as it will be easy to handle the sign later. &lt;/p&gt;

&lt;p&gt;And predefined result value 0 and added result value as the return value for the function.&lt;/p&gt;

&lt;p&gt;_Logic _&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    while (divisor &amp;lt;= dividend) {
        let value = divisor
        let multiple = 1
        while (value + value &amp;lt;= dividend) {
            value += value
            multiple += multiple
        }
        dividend = dividend - value
        result += multiple
    }

    if (result &amp;gt; (signedIntegerValue - 1)) {
        return retIsNegative ? -signedIntegerValue : signedIntegerValue - 1
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Consider dividend value is 10 and divisor value is 3. &lt;br&gt;
Running a while loop till the divisor is less than dividend. &lt;/p&gt;

&lt;p&gt;Assigning divisor value to value variable&lt;/p&gt;

&lt;p&gt;Initially, considering multiple value is 1. &lt;/p&gt;

&lt;p&gt;Running a nest while loop when (*&lt;em&gt;value + value *&lt;/em&gt;) which is 2 * divisor value is less than dividend - This loop runs for the logarithmic purpose to remove the values from dividend &lt;/p&gt;

&lt;p&gt;so now the &lt;strong&gt;value&lt;/strong&gt; = value + value which is (3 + 3) = 6 &lt;/p&gt;

&lt;p&gt;and &lt;strong&gt;multiple&lt;/strong&gt; value is now 2&lt;/p&gt;

&lt;p&gt;Now, nested while loop check the condition &lt;/p&gt;

&lt;p&gt;so new value of the &lt;strong&gt;value&lt;/strong&gt; = value + value which is (6 + 3) = 9 and 9 &amp;lt; divisor 10  &lt;/p&gt;

&lt;p&gt;New value of value is 9 and &lt;strong&gt;multiple&lt;/strong&gt; stands as 3&lt;/p&gt;

&lt;p&gt;Now, nested while loop check the condition, where 12 &amp;lt;= 10 which is false. &lt;/p&gt;

&lt;p&gt;Hence nested loop is over and next line after the loop starts executing. &lt;/p&gt;

&lt;p&gt;Hence new value of dividend is 10 - 9 = &lt;strong&gt;1&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;return value is result(0) + multiple(3) = &lt;strong&gt;3&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Checking the return value with the signed 32 integer value &lt;/p&gt;

&lt;p&gt;and returning according to the signatures for the max and min signed 32 integer value. &lt;/p&gt;

&lt;p&gt;If the return is not greater than signed integer value then it returns as the signed value suggests. &lt;/p&gt;

&lt;p&gt;Full Code as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const signedIntegerValue = 2 ** 31
var divide = function (dividend, divisor) {
    const retIsNegative = Math.sign(divisor) !== Math.sign(dividend);
    dividend = Math.abs(dividend)
    divisor = Math.abs(divisor)

    let result = 0
    while (divisor &amp;lt;= dividend) {
        let value = divisor
        let multiple = 1
        while (value + value &amp;lt;= dividend) {
            value += value
            multiple += multiple
        }
        dividend = dividend - value
        result += multiple
    }

    if (result &amp;gt; (signedIntegerValue - 1)) {
        return retIsNegative ? -signedIntegerValue : signedIntegerValue - 1
    }
    return retIsNegative ? -result : result
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Hope this helps. Happy Coding!&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>javascript</category>
      <category>iterative</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The better way to Validate the props in Vue</title>
      <dc:creator>Varit Patel</dc:creator>
      <pubDate>Sun, 19 Apr 2020 07:19:17 +0000</pubDate>
      <link>https://dev.to/varit05/better-way-to-validate-the-props-in-vue-55m8</link>
      <guid>https://dev.to/varit05/better-way-to-validate-the-props-in-vue-55m8</guid>
      <description>&lt;p&gt;Well, Whenever we communicate with data, we need to validate the data in order to achieve the expected behavior and these apply to any type of communication that happens within the application and when it comes to communicating from Parent to Child using &lt;code&gt;props&lt;/code&gt; is no exception.&lt;/p&gt;

&lt;p&gt;Let's dig into props in detail to write better and predictable code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Array syntax&lt;/li&gt;
&lt;li&gt;Object syntax&lt;/li&gt;
&lt;li&gt;Object syntax with custom validator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, let's define the parent component which is responsible to pass the data to the child.&lt;/p&gt;

&lt;h3&gt;
  
  
  Parent component&lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Parent component consists of the movies list which basically passes the list of the movies to the child component. For better understanding will pass static props to more focus on how to write props better.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MovieList&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Movie 1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;image&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;assets/images/movie-1.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="nx"&gt;rating&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;7.5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; 
&lt;span class="nx"&gt;watchlist&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Array syntax &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Using array syntax, props can be directly passed using the name of the props and that will be used to display the data in the template.&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="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&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;MovieList&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&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;name&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;image&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;rating&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;watchlist&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;Basically, It can be helpful for prototyping of the app but for big and scalable this approach is not enough to make code more scalable and reliable. Below are lists of disadvantages of using this syntax.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cons
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;No Type checking&lt;/li&gt;
&lt;li&gt;No Information indicates on mandatory and optional props&lt;/li&gt;
&lt;li&gt;No Default value&lt;/li&gt;
&lt;li&gt;No Appropriate custom validator&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Well, All the shortcomings discussed above will be handled in the below examples to make our code more predictable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object syntax &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;

&lt;p&gt;Alternate to Array syntax, props can also be defined with object syntax and this syntax can facilitate us to use three key properties with prop attributes which helps us to write better code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;type&lt;/code&gt; checks the data type of prop.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;required&lt;/code&gt; indicates whether the prop is required or not.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;default&lt;/code&gt; indicates the value of prop if the parent component failed to pass that prop.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&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="s2"&gt;MovieList&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&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="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;required&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="na"&gt;rating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;required&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="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="na"&gt;wishlist&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;default&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="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/assets/default-poster.png&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="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;Below are the things which we can notice from the above code. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Props are now supporting types. The list of available types includes some of the JavaScript data types &amp;amp; others are as below.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;String&lt;/li&gt;
&lt;li&gt;Number&lt;/li&gt;
&lt;li&gt;Object&lt;/li&gt;
&lt;li&gt;Array&lt;/li&gt;
&lt;li&gt;Boolean&lt;/li&gt;
&lt;li&gt;Date&lt;/li&gt;
&lt;li&gt;Function&lt;/li&gt;
&lt;li&gt;Symbol&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A prop can accept multiple data types as used in the &lt;code&gt;rating&lt;/code&gt; prop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Required props can be easily identified. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The default value is used in a case where the parent failed to pass the prop.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If default property is set, the required properties become true automatically hence even not writing would make sense. ( e.g. wishlist &amp;amp; image prop)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, The prop with object syntax is much better than the array syntax as it overcomes a lot of cons that come with object syntax, which we discussed above. but still, there is a catch in code with the image prop.&lt;/p&gt;

&lt;p&gt;Imagine, the Parent component somehow passed the image URL wrong.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;Movie-List&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"Movie with incorrect image path"&lt;/span&gt; &lt;span class="na"&gt;image=&lt;/span&gt;&lt;span class="s"&gt;"../assets/default.png"&lt;/span&gt;&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In order to handle such cases, &lt;code&gt;prop&lt;/code&gt; with object syntax have another property called &lt;code&gt;validator&lt;/code&gt; to validate the incoming prop and if our requirement for the prop doesn't match, it throws an error. Thus, it helps to write more reliable and scalable code. Let's go through the example below&lt;/p&gt;

&lt;h3&gt;
  
  
  Object syntax with custom validator &lt;a&gt;&lt;/a&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/assets/default-poster.png&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="na"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;propValue&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;hasImagesDir&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;propValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;indexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@/assets/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&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;listOfAvailableExt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.jpeg&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="s2"&gt;.jpg&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="s2"&gt;.png&lt;/span&gt;&lt;span class="dl"&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;isValidExt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;listOfAvailableExt&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class="nx"&gt;propValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ext&lt;/span&gt;&lt;span class="p"&gt;)&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;hasImagesDir&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isValidExt&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="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;Here, the &lt;code&gt;validator&lt;/code&gt; function accepts a prop value and if the value passes the given validations, then the prop image is visible otherwise it will throw an error in the console as below.&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Vue&lt;/span&gt; &lt;span class="nx"&gt;warn&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;Invalid&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;custom&lt;/span&gt; &lt;span class="nx"&gt;validator&lt;/span&gt; &lt;span class="nx"&gt;check&lt;/span&gt; &lt;span class="nx"&gt;failed&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;prop&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can simply correct the path of the image which renders the correct image.&lt;/p&gt;

&lt;p&gt;Here is the working &lt;a href="https://codesandbox.io/s/prop-validator-j5ss7"&gt;codesandbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/j5ss7"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;NOTE: The &lt;code&gt;props&lt;/code&gt; are validated before the component instance is created, hence the instance properties such as &lt;code&gt;data&lt;/code&gt;, &lt;code&gt;computed&lt;/code&gt;, etc will not be available to use inside &lt;code&gt;default&lt;/code&gt; or &lt;code&gt;validator&lt;/code&gt; function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I hope it helps to write better code.🤞&lt;/p&gt;

&lt;p&gt;Feel free to post any questions 📝 on the comments below. I would be more than happy to answer them.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>The need for composition API in Vue</title>
      <dc:creator>Varit Patel</dc:creator>
      <pubDate>Thu, 20 Feb 2020 04:23:28 +0000</pubDate>
      <link>https://dev.to/varit05/the-need-for-composition-api-in-vue-2ii4</link>
      <guid>https://dev.to/varit05/the-need-for-composition-api-in-vue-2ii4</guid>
      <description>&lt;p&gt;Well, Presently in Vue world, The hot🔥 topic is the Composition API, a function-based API introduced in Vue 3.0. In this article, We will learn what was the need for this new API and then how to use function-based API. I try to explain it in as much simple as I can to understand it for beginners also.&lt;/p&gt;

&lt;p&gt;This article summarises the need for Composition API described in &lt;a href="https://github.com/vuejs/rfcs/pull/42"&gt;#RFC&lt;/a&gt; &amp;amp; some examples.&lt;/p&gt;

&lt;p&gt;So, Let's start 💻&lt;/p&gt;

&lt;p&gt;Before we deep dive, There is no need to panic that the composition API will change the Vue drastically. Composition API is add-ons to the existing feature hence there are no breaking changes going into Vue 3.0&lt;br&gt;
Moreover, the Vue team has created a plugin (&lt;a href="https://github.com/vuejs/composition-api"&gt;vue-composition&lt;/a&gt;) which is compatible with Vue 2.x.&lt;/p&gt;

&lt;p&gt;Let's see some questions first!&lt;/p&gt;
&lt;h3&gt;
  
  
  What was the need for composition API?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;As popularity increases for Vue day by day, People started to adopt Vue for large &amp;amp; Enterprise level application too. Due to which there were many cases where components for such applications grow gradually with the time and there is the moment when It was hard to maintain &amp;amp; read using Single File component architect. Hence the need for braking component in a logical manner arises and with Vue's existing API this was not possible. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Instead of adding in another new concept, Composition APIs are proposed which basically expose Vue's core capabilities - such as creating and observing reactive state - as standalone functions and this API helps to write clean and reusable code among multiple components.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  What were the drawbacks of the alternative approaches that Vue has?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Before the introduction of the new API, Vue has other alternatives that offer reusability among components such as mixins, HOC(High order component), scoped slot but with all approaches comes with their own shortcomings due to which they are not widely used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Mixins - It was hard to maintain once the app has some numbers of mixins. The developer needs to visit each mixin to see data is coming from which mixin.&lt;/p&gt;

&lt;p&gt;HOC - This pattern doesn't go well with the Vue SFC approach, thus it is not widely recommended or popular to use among Vue developers.&lt;/p&gt;

&lt;p&gt;Scoped slot - Even though slot hides the drawbacks of other concepts can't but with this approach, the developer ended up having a lot of reusable components and putting more and more login in Template which was again hard to maintain in longtime.&lt;/p&gt;

&lt;p&gt;Kudos to Vue Team 👏&lt;/p&gt;

&lt;p&gt;In Simple words, composition API helps to&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use low-level APIs of Vue which was not the case with Vue 2.x&lt;/li&gt;
&lt;li&gt;Efficiently Organise and Write reusable code, as API is function-based.&lt;/li&gt;
&lt;li&gt;Improves code readability by separating shared logic to functions.&lt;/li&gt;
&lt;li&gt;Achieve code separation &amp;amp; DRY principle.&lt;/li&gt;
&lt;li&gt;Use TypeScript better with Vue application.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Note: As Vue is very easy to start, so is the composition API.&lt;/p&gt;

&lt;p&gt;Let's build one simple app to go through the API.&lt;/p&gt;

&lt;p&gt;1) Import vue-composition plugin&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install @vue/composition-api --save
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2) Install plugin to the Vue app before importing any other APIs. The idea to register it before any other plugin is that composition API can be used in other plugins just as it is a built-in feature of Vue.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import VueCompositionApi from '@vue/composition-api';

Vue.use(VueCompositionApi);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;3) Using composition API, Let's create a small functionality to understand the API better. Create a new folder, called &lt;code&gt;composition-fns&lt;/code&gt; and create a file called &lt;code&gt;toggle.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Here, we are importing ref from the API and using it we are declaring the variable isVisible and which has by default value false.&lt;/p&gt;

&lt;p&gt;In addition to it, There is a method called &lt;code&gt;toggle&lt;/code&gt; which is responsible&lt;br&gt;
for toggling the value of isVisible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ref } from "@vue/composition-api";

export const useToggle = () =&amp;gt; {
  const isVisible = ref(false);

  const toggle = () =&amp;gt; {
    return (isVisible.value = !isVisible.value);
  };

  return {
    isVisible,
    toggle
  };
};

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

&lt;/div&gt;



&lt;p&gt;4) At first, Import the &lt;code&gt;useToggle&lt;/code&gt; function and then to use the above composition API in a component, API provides &lt;code&gt;setUp()&lt;/code&gt; function. This setup function includes &lt;strong&gt;data, methods, computed &amp;amp; watcher&lt;/strong&gt; object of the Vue component. &lt;/p&gt;

&lt;p&gt;Since here we're using the useToggle composition fn, we can destructure the values and return it using setup method in order to use it for the template.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useToggle } from "./composition-fn/toggle";
setup() {
  const { isVisible, toggle } = useToggle();
  // expose to template
  return {
    isVisible,
    toggle
  };
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;setUp&lt;/code&gt; function can have two arguments. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;props - properties passed to the components and they are reactive since any change in props which results in re-render of the component.&lt;/li&gt;
&lt;li&gt;context - which has selected properties which was earlier passed to component using &lt;code&gt;this&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Context is having below functions/properties&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context.refs
context.emit
context.attrs
context.slots
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As &lt;code&gt;setUp&lt;/code&gt; function is heart of composition API, It is very crucial to understand it.&lt;/p&gt;

&lt;p&gt;5) In the end, Template is consists of the button and div to show and hide the toggle value.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div&amp;gt;{{ isVisible }}&amp;lt;/div&amp;gt;
&amp;lt;button type="button" @click="toggle"&amp;gt;
  {{isVisible ? 'hide' : 'show' }} Toggle
&amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is working &lt;a href="https://codesandbox.io/s/composition-api-4j9rn"&gt;codesandbox&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope this helps you to get started with composition API and journey towards Vue 3.0! &lt;/p&gt;

&lt;p&gt;Wish you good luck 😃&lt;/p&gt;

</description>
      <category>vue</category>
      <category>vuex</category>
      <category>javascript</category>
      <category>composition</category>
    </item>
    <item>
      <title>List of ways to merge arrays in javascript!</title>
      <dc:creator>Varit Patel</dc:creator>
      <pubDate>Sun, 15 Sep 2019 03:09:08 +0000</pubDate>
      <link>https://dev.to/varit05/list-of-ways-to-merge-arrays-in-javascript-10m6</link>
      <guid>https://dev.to/varit05/list-of-ways-to-merge-arrays-in-javascript-10m6</guid>
      <description>&lt;p&gt;Well, Imagine interviewer ask you to tell the ways to merge two arrays in JavaScript? There are various ways you can achieve it. so in this post, we will go through some of the ways to merge arrays.&lt;/p&gt;

&lt;p&gt;Let's see some ways!&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;const&lt;/span&gt; &lt;span class="nx"&gt;fruits1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍏&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="s2"&gt;🍎&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="s2"&gt;🍐&lt;/span&gt;&lt;span class="dl"&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;fruits2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍊&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="s2"&gt;🍋&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="s2"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * 1. Using concat method
 */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;concatFruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;fruits2&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;concat:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;concatFruits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Output
 * concat: ["🍏", "🍎", "🍐", "🍊", "🍋", "🍌"]
 */&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * 2. Using ES6 spread syntax
 */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;spreadFruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;fruits1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;fruits2&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;spread:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;spreadFruits&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Output
 * spread: ["🍏", "🍎", "🍐", "🍊", "🍋", "🍌"]
 */&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * 3. Using Push method
 * This method will mutate original array [fruits3]
 */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍏&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="s2"&gt;🍎&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="s2"&gt;🍐&lt;/span&gt;&lt;span class="dl"&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;fruits4&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍊&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="s2"&gt;🍋&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="s2"&gt;🍌&lt;/span&gt;&lt;span class="dl"&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;pushFruits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;fruits3&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;fruits4&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;push:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;fruits3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Output
 * push: ["🍏", "🍎", "🍐", "🍊", "🍋", "🍌"]
 */&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * 4. Using unshift method
 * This method will mutate original array [fruits6]
 */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits5&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍏&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="s2"&gt;🍎&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="s2"&gt;🍐&lt;/span&gt;&lt;span class="dl"&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;fruits6&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍊&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="s2"&gt;🍋&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="s2"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;fruits6&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;fruits5&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;unshift:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;fruits6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Output
 * push: ["🍏", "🍎", "🍐", "🍊", "🍋", "🍌"]
 */&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * 5. Using splice method
 * This method will mutate original array [fruits7]
 */&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fruits7&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍏&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="s2"&gt;🍎&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="s2"&gt;🍐&lt;/span&gt;&lt;span class="dl"&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;fruits8&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;🍊&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="s2"&gt;🍋&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="s2"&gt;🍌&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;fruits7&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;splice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;fruits8&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="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;splice:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nx"&gt;fruits7&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="cm"&gt;/**
 * Output
 * push: ["🍏", "🍎", "🍐", "🍊", "🍋", "🍌"]
 */&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://jsfiddle.net/varit05/96hn0k1d/"&gt;Play with jsfiddle&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Based on the above implementation, Recommend and convenient ways are &lt;br&gt;
    - option 1: &lt;code&gt;Array.prototype.concat()&lt;/code&gt; method  or &lt;br&gt;
    - option 2: &lt;code&gt;Spread syntax&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Feel free to comment if you know any other way to do so.&lt;/p&gt;

&lt;p&gt;References: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat"&gt;MDN - concat&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax"&gt;MDN - Spread&lt;/a&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>array</category>
      <category>interview</category>
    </item>
    <item>
      <title>What is Partial &amp; Required utility types in TypeScript?</title>
      <dc:creator>Varit Patel</dc:creator>
      <pubDate>Wed, 11 Sep 2019 05:09:47 +0000</pubDate>
      <link>https://dev.to/varit05/what-is-partial-required-utility-types-in-typescript-466b</link>
      <guid>https://dev.to/varit05/what-is-partial-required-utility-types-in-typescript-466b</guid>
      <description>&lt;p&gt;In a real-world application, Basic types are used but there are scenarios where we need some types that can be derived from one or two types. TypeScript provides various utility types to make an easier transformation from the already defined type in our project. moreover, these utilities are globally available. Hence it can be accessed from anywhere.&lt;/p&gt;

&lt;p&gt;Basic understanding of TypeScript requires in order to understand the below concept.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Partial&lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make a type with all properties of &lt;code&gt;T&lt;/code&gt; set to Optional.&lt;/li&gt;
&lt;li&gt;Useful when some of the properties of the type &lt;code&gt;T&lt;/code&gt; to be updated.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&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;updateTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fieldsToUpdate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;fieldsToUpdate&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;todo1&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;organize desk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clear clutter&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="nx"&gt;todo2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updateTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;throw out trash&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;Above example, &lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt; denotes as a defined type that needs to be modified.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;Required&lt;code&gt;&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make a type with all properties of &lt;code&gt;T&lt;/code&gt; set to Required.&lt;/li&gt;
&lt;li&gt;Useful when all the properties of the object to set all the properties of &lt;code&gt;T&lt;/code&gt; to be updated.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nx"&gt;description&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&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;updateTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fieldsToUpdate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Required&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Todo&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Todo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;todo&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;fieldsToUpdate&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;todo1&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;organize desk&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;clear clutter&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="nx"&gt;todo2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;updateTodo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;todo1&lt;/span&gt;&lt;span class="p"&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;title updated&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;throw out trash&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;&lt;strong&gt;Important Points to note&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Below are the scenarios where these two utilities can have some tweaks.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Useful when &lt;em&gt;strictNullChecks&lt;/em&gt; flag is enabled.&lt;/li&gt;
&lt;li&gt;Works only on a single level, does not work with the nested level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And weirdly, If at all, &lt;code&gt;Partial&lt;/code&gt; &amp;amp; &lt;code&gt;Required&lt;/code&gt; are used together, the outer most will take higher priority(not useful in an ideal scenario, but mentioned just to understand it better).&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>utilitytypes</category>
    </item>
  </channel>
</rss>
