<?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: Mubashir Hassan</title>
    <description>The latest articles on DEV Community by Mubashir Hassan (@mhm13dev).</description>
    <link>https://dev.to/mhm13dev</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%2F289879%2F90c8f85a-f70b-487e-8870-a81ea799954c.jpeg</url>
      <title>DEV Community: Mubashir Hassan</title>
      <link>https://dev.to/mhm13dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mhm13dev"/>
    <language>en</language>
    <item>
      <title>I spent hours debugging an issue which was not even in my code! Docker could have saved my time</title>
      <dc:creator>Mubashir Hassan</dc:creator>
      <pubDate>Fri, 07 Jun 2024 18:28:10 +0000</pubDate>
      <link>https://dev.to/mhm13dev/i-spent-hours-debugging-an-issue-which-was-not-even-in-my-code-docker-could-have-saved-my-time-1al9</link>
      <guid>https://dev.to/mhm13dev/i-spent-hours-debugging-an-issue-which-was-not-even-in-my-code-docker-could-have-saved-my-time-1al9</guid>
      <description>&lt;p&gt;Have you ever faced issues like &lt;strong&gt;&lt;em&gt;It works on my machine but not on yours&lt;/em&gt;&lt;/strong&gt;? Or you wrote some code some time back and now you are not able to run it because of the environment setup? Or you are working on multiple projects and each project requires a different environment setup?&lt;/p&gt;

&lt;p&gt;Almost 2 years ago, I wrote a script with Node.js v14 for interacting with Ethereum Smart Contract. After few months, I needed to run that script again but I was getting some weird clue-less error. I spent a lot of time debugging the issue and found that I was using a &lt;strong&gt;&lt;em&gt;package which required Node.js v14&lt;/em&gt;&lt;/strong&gt; to run and &lt;strong&gt;&lt;em&gt;I had Node.js v16 installed on my machine&lt;/em&gt;&lt;/strong&gt;. Maybe, some specific method that was used in that package was deprecated Node.js v16. So, I had to downgrade my Node.js version to v14 to run the script.&lt;/p&gt;

&lt;p&gt;Imagine if that script was dockerized, I would not have wasted my time debugging an issue which was not even present in my code, rather in that package. I would have just run the script in a Docker container with Node.js v14 installed and it would have run without any issues.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Docker?
&lt;/h2&gt;

&lt;p&gt;Docker is a platform to bundle your application code along with the environment it needs to run, so that &lt;strong&gt;&lt;em&gt;if it works on your machine, it will work on any machine that has Docker installed&lt;/em&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Docker bundles application code, operating system, libraries, and other dependencies in one package called a &lt;code&gt;docker image&lt;/code&gt; which can be used to run multiple instances of the application in isolated environments called &lt;code&gt;docker containers&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Consistent Environment&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Docker containers provide a consistent environment for your application to run, so you can be sure that when your application is ported to another developer's machine or to a server, it will behave the exact same way.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It helps developers to work on different projects without worrying about the environment setup.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;E.g. If you have an old Node.js application that requires Node.js v18 to run, you can create a Docker container with Node.js v18 installed and run the application in that container, without worrying about the version of Node.js installed on your machine.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Imagine running an application which was intended for Node.js v18, on a machine with Node.js v22 installed (which may have some breaking changes). But if that is dockerized, it will run in the same environment as it was intended to (Node.js v18).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Isolation&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Docker containers are isolated from each other and from the host system, so they offer a clean and consistent environment to run your application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This isolation helps to prevent conflicts between different applications running on the same machine. Some applications might be running on Debian with Node.js v16, while others might be running on Ubuntu with Node.js v18, v20, etc. inside their respective containers.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker containers are secure and can be used to run untrusted code in a sandboxed environment, reducing the risk of security vulnerabilities because the code is isolated from the host system.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Portability&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Docker images are portable and can run on any machine that has Docker installed, regardless of the underlying operating system.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;E.g. if a developer is working on a Windows machine, they can create a Docker image with Ubuntu OS and Node.js v18 and run their application in that container. Then, they can share the image with another developer who is working on a Mac or Linux machine, and the application will run the same way on their machine as well.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Scalability&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Docker containers are scalable and can be used to run multiple instances of the same application on the same machine or across different machines.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Docker containers can be easily deployed to cloud platforms like AWS, Google Cloud, Azure, etc., and can be scaled up or down based on the demand for the application.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;E.g. if your application is getting more traffic, you can scale up the number of containers running the application to handle the increased load, and scale them down when the traffic decreases.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is Dockerfile, Docker Image, Docker Container?
&lt;/h3&gt;

&lt;p&gt;👉 &lt;code&gt;Dockerfile&lt;/code&gt; is a text file that contains a set of instructions to build a &lt;code&gt;Docker image&lt;/code&gt;. It contains instructions to install the necessary software packages, copy application code into the &lt;code&gt;Docker image&lt;/code&gt;, set environment variables, expose ports, and define the command to run the application.&lt;/p&gt;

&lt;p&gt;👉 A &lt;code&gt;Docker image&lt;/code&gt; is a template that contains the application code, environment and dependencies required to run an application inside a &lt;code&gt;Docker container&lt;/code&gt;. It is created from a &lt;code&gt;Dockerfile&lt;/code&gt; and can be used to create multiple instances of a &lt;code&gt;Docker container&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Docker image&lt;/code&gt; can be shared, stored, and reused across different environments. It is portable and can be used to run &lt;code&gt;docker containers&lt;/code&gt; on any machine that has Docker installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 A &lt;code&gt;Docker container&lt;/code&gt; is a lightweight, standalone instance created from a &lt;code&gt;Docker image&lt;/code&gt; that can be run. Multiple containers of same or different images can run on the same machine, isolated from each other and from the host system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let's Talk About Docker Compose
&lt;/h2&gt;

&lt;p&gt;Docker Compose is a tool for defining and running multi-container Docker applications. It uses a YAML file to configure the application's services, networks, and volumes.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;With Docker Compose, you can define a multi-container application in a single file and run it with a single command.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 When you have a web application that has an API, a database, a frontend. Then you also need to define the network between these services (so they can communicate with each other), the volumes to store data (so that when you remove and recreate containers, the data should persist). Docker Compose helps you define all these in a single file and run them together with a single command &lt;code&gt;docker compose up&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;👉 Docker Compose is useful for development, testing, and staging environments where you need to run multiple services together.&lt;/p&gt;

&lt;p&gt;That's all for an overview of Docker and Docker Compose.&lt;/p&gt;

&lt;p&gt;You can find the example project here:&lt;br&gt;
&lt;a href="https://github.com/mhm13dev/lerna-with-docker-compose"&gt;https://github.com/mhm13dev/lerna-with-docker-compose&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>docker</category>
      <category>productivity</category>
      <category>devops</category>
    </item>
    <item>
      <title>Streamlining AWS S3 Object Handling and Optimizing User Experience</title>
      <dc:creator>Mubashir Hassan</dc:creator>
      <pubDate>Fri, 29 Sep 2023 11:15:49 +0000</pubDate>
      <link>https://dev.to/mhm13dev/streamlining-aws-s3-object-handling-and-optimizing-user-experience-3a7e</link>
      <guid>https://dev.to/mhm13dev/streamlining-aws-s3-object-handling-and-optimizing-user-experience-3a7e</guid>
      <description>&lt;p&gt;Have you ever wondered how large files like images or videos are efficiently served over the internet, ensuring a smooth user experience without excessive delays? Recently, I delved into the world of AWS SDK APIs, particularly focusing on the Amazon S3 API, and discovered some fascinating insights into handling and optimizing the delivery of objects like images and videos.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generating Presigned URLs
&lt;/h3&gt;

&lt;p&gt;The journey began with understanding how to generate presigned URLs for S3 objects. These URLs are like golden tickets, granting temporary access to upload or download objects. Presigned URLs are incredibly useful for securely sharing files while maintaining control over access.&lt;/p&gt;

&lt;h3&gt;
  
  
  Streamlining Object Retrieval
&lt;/h3&gt;

&lt;p&gt;But it wasn't just about generating URLs; I was eager to figure out how to retrieve S3 objects efficiently, especially for large media files. The challenge was to avoid waiting for the entire file to download on the server before serving it, as this can be slow and inefficient.&lt;/p&gt;

&lt;p&gt;That's when I stumbled upon a game-changing feature in the AWS SDK: the ability to get a readable stream of the object directly from the S3 bucket. With this stream in hand, I could stream data chunk by chunk to the client side without delay, significantly improving efficiency and speed.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting the Right Headers
&lt;/h3&gt;

&lt;p&gt;Efficient object retrieval wasn't the only piece of the puzzle. I also had to ensure that the client understood the response correctly. This meant setting a plethora of HTTP headers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Content-Type&lt;/strong&gt;: Defines the type of content being sent.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content-Disposition&lt;/strong&gt;: Guides the client on how to handle the response, whether to display it or download it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content-Length&lt;/strong&gt;: Specifies the size of the object.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cache-Control&lt;/strong&gt;: Indicates how long the object can be cached.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accept-Ranges&lt;/strong&gt;: Informs the client of support for partial requests, crucial for efficient downloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content-Range&lt;/strong&gt;: Helps the client track how much of the object it has received, useful for resumable downloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ETag&lt;/strong&gt;: Notifies the client of object changes, reducing unnecessary downloads.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Last-Modified&lt;/strong&gt;: Indicates the last modification date, crucial for caching and conditional requests.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Expires&lt;/strong&gt;: Specifies when the object will expire.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content-Encoding&lt;/strong&gt;: Tells the client how the object is encoded, often used for compression.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Content-Language&lt;/strong&gt;: Informs the client about the language of the content.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;x-amz-version-id&lt;/strong&gt;: Identifies the version of the object, vital for versioning.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each of these headers plays a specific role in ensuring a seamless user experience. For instance, &lt;code&gt;ETag&lt;/code&gt; helps avoid unnecessary downloads, while &lt;code&gt;Content-Disposition&lt;/code&gt; controls how the client handles the response, whether to download or display it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling Range Requests
&lt;/h3&gt;

&lt;p&gt;One particularly fascinating aspect was handling range requests. By utilizing the &lt;code&gt;Range&lt;/code&gt; header, I could send only the requested part of the object to the client. This is incredibly valuable for large files like videos. Clients can download the video in chunks and begin playback while the download is in progress, creating a smoother user experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Private Object Access
&lt;/h3&gt;

&lt;p&gt;This approach also opens up possibilities for role-based access to objects. For instance, imagine a private user profile with content accessible only to followers. With this method, we can check if a user is a follower and serve the object accordingly. It's a powerful way to manage and serve private content efficiently.&lt;/p&gt;

&lt;p&gt;In conclusion, my journey through AWS S3 object handling and optimizing user experiences was not only educational but also empowering. I gained a deep understanding of how HTTP headers influence client-server interactions and how they can be leveraged to streamline content delivery.&lt;/p&gt;

&lt;p&gt;Moreover, implementing these techniques has significantly improved the performance of serving files, making the user experience smoother and more efficient. Whether you're handling images, videos, or other types of content, mastering the art of object handling and HTTP headers is a valuable skill in the world of web development. I'm excited to apply these lessons in our app, and I hope you find them as insightful and empowering as I did.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>aws</category>
      <category>s3</category>
      <category>cloud</category>
    </item>
    <item>
      <title>Array.sort() in JavaScript - I was asked about this in an interview</title>
      <dc:creator>Mubashir Hassan</dc:creator>
      <pubDate>Tue, 13 Dec 2022 21:55:50 +0000</pubDate>
      <link>https://dev.to/mhm13dev/arraysort-in-javascript-i-was-asked-about-this-in-an-interview-387</link>
      <guid>https://dev.to/mhm13dev/arraysort-in-javascript-i-was-asked-about-this-in-an-interview-387</guid>
      <description>&lt;p&gt;I was in an interview and I was given an array of strings.&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;arr&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;karachi&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;lahore&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;kolachi&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;islamabad&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;&lt;strong&gt;He asked me to sort it in alphabatical order.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I tried:&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;a&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;b&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;He said it will work for numbers (actually it won't) but does not work for strings.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Then I tried&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charAt&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="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charAt&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;He said it will work for just the first characters (actually it won't), then how &lt;code&gt;karachi&lt;/code&gt; and &lt;code&gt;kolachi&lt;/code&gt; starting with &lt;code&gt;k&lt;/code&gt; will be sorted?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I was blank.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;He said what are &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I said &lt;code&gt;a&lt;/code&gt; is the current element (the element at the index of current iteration) and &lt;code&gt;b&lt;/code&gt; is the next element (the element at the current iteration + 1 index).&lt;/p&gt;

&lt;p&gt;👉 But actually, it is the opposite. &lt;code&gt;a&lt;/code&gt; is the next element and &lt;code&gt;b&lt;/code&gt; is the current element.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then he asked, does the sort modify the original array or returns a new array.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Honestly, most of the time I am working with &lt;code&gt;.map()&lt;/code&gt;, &lt;code&gt;.filter()&lt;/code&gt;, &lt;code&gt;.some()&lt;/code&gt;, and &lt;code&gt;.every()&lt;/code&gt;. So I knew the behaviour of these methods but I don't remember when was the last time I used &lt;code&gt;.sort()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I said, it does not modify the original array, rather returns a new array like &lt;code&gt;.map()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;👉 But it is the opposite. &lt;code&gt;.sort()&lt;/code&gt; modifies the original array and returns the reference to the original array, which is now sorted.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does actually &lt;code&gt;.sort()&lt;/code&gt; work?
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Array.sort()&lt;/code&gt; accepts a an optional compare function as an argument. &lt;/p&gt;

&lt;p&gt;If we do not provide any compare function, the sort method converts all the &lt;code&gt;non-undefined&lt;/code&gt; elements to string, and then compare their sequences of UTF-16 code units values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does "compare their sequences of UTF-16 code units values" means?&lt;/strong&gt;&lt;br&gt;
To put it simple, let's say we write character &lt;code&gt;a&lt;/code&gt; which is encoded as UTF-16 in JavaScript. In decimal it's value will be &lt;code&gt;97&lt;/code&gt;. For &lt;code&gt;b&lt;/code&gt; it will be &lt;code&gt;98&lt;/code&gt;. i.e.&lt;br&gt;
&lt;code&gt;A&lt;/code&gt; = &lt;code&gt;65&lt;/code&gt;&lt;br&gt;
&lt;code&gt;B&lt;/code&gt; = &lt;code&gt;66&lt;/code&gt;&lt;br&gt;
&lt;code&gt;C&lt;/code&gt; = &lt;code&gt;67&lt;/code&gt;&lt;br&gt;
and so on.&lt;br&gt;
I expect you know the ASCII table.&lt;/p&gt;

&lt;p&gt;So basically the array of strings will automatically be sorted by &lt;code&gt;.sort()&lt;/code&gt; method correctly without passing any compare function.&lt;/p&gt;

&lt;p&gt;👉 In case of numbers, the behaviour is same;&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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100000&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&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="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// expected output: [1, 100000, 21, 30, 4]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because each number is first converted into string, and then compared with respect to their UTF-16 code units values.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But If we provide a compare function to sort based on numbers:&lt;/strong&gt;&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;arr&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&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="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="nx"&gt;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;nextValue&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;prevValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// if returnValue &amp;gt; 0, move nextValue after the prevValue&lt;/span&gt;
  &lt;span class="c1"&gt;// if returnValue &amp;lt; 0, move the nextValue before the prevValue&lt;/span&gt;
  &lt;span class="c1"&gt;// if returnValue === 0, keep the original order, do not move any value&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nextValue&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;prevValue&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;So, the sort method can be thought of like this:&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;function&lt;/span&gt; &lt;span class="nf"&gt;compareFunction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&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;a&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;less&lt;/span&gt; &lt;span class="nx"&gt;than&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;by&lt;/span&gt; &lt;span class="nx"&gt;some&lt;/span&gt; &lt;span class="nx"&gt;ordering&lt;/span&gt; &lt;span class="nx"&gt;criterion&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="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="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;a&lt;/span&gt; &lt;span class="nx"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;greater&lt;/span&gt; &lt;span class="nx"&gt;than&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="nx"&gt;by&lt;/span&gt; &lt;span class="nx"&gt;the&lt;/span&gt; &lt;span class="nx"&gt;ordering&lt;/span&gt; &lt;span class="nx"&gt;criterion&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="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="nx"&gt;must&lt;/span&gt; &lt;span class="nx"&gt;be&lt;/span&gt; &lt;span class="nx"&gt;equal&lt;/span&gt; &lt;span class="nx"&gt;to&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;
  &lt;span class="k"&gt;return&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope this will make the things a bit clear about &lt;code&gt;Array.sort()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;That's it for this post. Write your thoughts in the comments below!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>arrays</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>My first post on dev.to - I created a webpack plugin</title>
      <dc:creator>Mubashir Hassan</dc:creator>
      <pubDate>Sun, 23 Aug 2020 08:29:16 +0000</pubDate>
      <link>https://dev.to/mhm13dev/my-first-post-on-dev-to-i-created-a-webpack-plugin-201e</link>
      <guid>https://dev.to/mhm13dev/my-first-post-on-dev-to-i-created-a-webpack-plugin-201e</guid>
      <description>&lt;p&gt;I created a Webpack plugin for extracting output assets filenames into a separate JSON file according to the &lt;code&gt;Entrypoints&lt;/code&gt; specified in the webpack configuration.&lt;/p&gt;

&lt;p&gt;The main purpose of writing this plugin was that I had a project structure where I was working with NodeJS, Express, Pug (view engine) and webpack. So basically, webpack was building my assets with names having random content hashes like &lt;code&gt;index.d53b3te33yi3y.js&lt;/code&gt; and it was difficult for me to inject those assets into my views e.g. &lt;code&gt;index.pug&lt;/code&gt;. That's why I came up with &lt;code&gt;webpack-get-files-plugin&lt;/code&gt; that extracts the filenames of the output assets into a &lt;code&gt;GetFiles.json&lt;/code&gt; file and I can easily inject them into my views.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install webpack-get-files-plugin --save-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;The Webpack Configuartion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const  path  =  require('path');

// Requiring webpack-get-files-plugin
const  GetFilesPlugin  =  require('webpack-get-files-plugin');

const  MiniCssExtractPlugin  =  require('mini-css-extract-plugin');
const { CleanWebpackPlugin } =  require('clean-webpack-plugin');

module.exports  = {
    mode:  'production',
    entry: {
        home:  path.join(__dirname, 'src', 'home.js'),
    },
    output: {
        path:  path.join(__dirname, 'dist'),
        filename:  'js/[name].[contentHash].js',
    },
    plugins: [
        // Using the Plugin
        new  GetFilesPlugin(),

        new  CleanWebpackPlugin(),
        new  MiniCssExtractPlugin({
            filename:  'css/[name].[contentHash].css',
        }),
    ],
    module: {
        rules: [
            {
                test:  /\.css$/,
                use: [MiniCssExtractPlugin.loader, 'css-loader'],

            },
            {
                test: /\.(png|jpg|svg|gif|jpeg)/,
                use: {
                    loader: 'file-loader',
                    options: {
                        name: 'images/[name].[contentHash].[ext]',
                    },
                },
            },
        ],
    },
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After running the webpack build process, this plugin will emit a file named &lt;code&gt;GetFiles.json&lt;/code&gt; in the &lt;code&gt;root directory&lt;/code&gt; of your project.&lt;/p&gt;

&lt;h3&gt;
  
  
  Structure of GetFiles.json
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "entrypoints": ["home"],
    "files": {
        "home": {
            "filenames": [
                "css/home.c43adcd817b4eaa62b97.css",
                "js/home.5e994fb65e62d205d1c5.js",
                "{\"name\": \"webpack-logo.png\"}??gffm??images/webpack-logo.3b7bf087cbac835e6f7d4b7dc9711e72.png",
                "{\"name\": \"github.svg\"}??gffm??images/github.16a9304e38fd8167989291ab92544e14.svg"
                ],
            "assets": {
                "css": ["css/home.c43adcd817b4eaa62b97.css"],
                "js": ["js/home.5e994fb65e62d205d1c5.js"],
                "images": {
                    "webpack-logo.png":  "images/webpack-logo.3b7bf087cbac835e6f7d4b7dc9711e72.png",
                    "github.svg":  "images/github.16a9304e38fd8167989291ab92544e14.svg"
                }
            }
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Demo Project
&lt;/h3&gt;

&lt;p&gt;I have created a demo project in &lt;code&gt;demo-webpack-get-files-plugin&lt;/code&gt; directory to help you better understand how this plugin works. This demo project &lt;code&gt;does not&lt;/code&gt; contain example of working with NodeJS, Express and Pug.&lt;/p&gt;

&lt;p&gt;For that I have a separate boilerplate project where I have used this plugin. &lt;a href="https://github.com/mhm13dev/node-express-webpack-pug"&gt;@mhm13dev/node-express-webpack-pug&lt;/a&gt; &lt;/p&gt;

&lt;h3&gt;
  
  
  To See How This Plugin Works in Action:
&lt;/h3&gt;

&lt;p&gt;Clone this repo:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone https://github.com/mhm13dev/webpack-get-files-plugin.git
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change into repo's directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd webpack-get-files-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Change into demo project directory&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd demo-webpack-get-files-plugin
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Install the dependencies&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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the webpack build process&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm run webpack
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then have a look at the dist directory and &lt;code&gt;GetFiles.json&lt;/code&gt; file inside &lt;code&gt;demo-webpack-get-files-plugin&lt;/code&gt; directory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Link to the Github Rep:
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/mhm13dev/webpack-get-files-plugin"&gt;@mhm13/webpack-get-files-plugin&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For queries and suggestions, comment below.&lt;/p&gt;

&lt;p&gt;If you liked this post then please give it a heart! ❤&lt;/p&gt;

</description>
      <category>webpack</category>
      <category>javascript</category>
      <category>npm</category>
    </item>
  </channel>
</rss>
