<?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>Forem: saransh kataria</title>
    <description>The latest articles on Forem by saransh kataria (@saranshk).</description>
    <link>https://forem.com/saranshk</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%2F206018%2Ff6190cc3-cda8-4178-b743-4a7cb17086b5.jpg</url>
      <title>Forem: saransh kataria</title>
      <link>https://forem.com/saranshk</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/saranshk"/>
    <language>en</language>
    <item>
      <title>Copy/Pasting output from the terminal</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Wed, 15 May 2024 22:42:22 +0000</pubDate>
      <link>https://forem.com/saranshk/copypasting-output-from-the-terminal-1c6g</link>
      <guid>https://forem.com/saranshk/copypasting-output-from-the-terminal-1c6g</guid>
      <description>&lt;p&gt;Manually copy-pasting the output of a terminal command with a mouse/trackpad feels tedious. It is more convenient to use commands to do so. And we can save the effort by using the built-in commands.&lt;/p&gt;

&lt;h2&gt;
  
  
  Mac
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pwd | pbcopy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can use pbcopy and pbpaste to copy and paste from the Mac terminal.&lt;/p&gt;

&lt;p&gt;We can pipe the output of a command to copy its output to the clipboard. For example, to copy the current directory path, we can use&lt;/p&gt;

&lt;p&gt;Or if we want to copy the contents of a file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat ~/Desktop/example.txt | pbcopy
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Similarly, to paste the clipboard output in a file, we can use:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pbpaste &amp;gt; ~/Documents/example.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Windows
&lt;/h2&gt;

&lt;p&gt;The commands to use on a Windows machine are clip and powershell get-clipboard.&lt;/p&gt;

&lt;p&gt;For copying the standard output to the clipboard, we use the command line command:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;some command&amp;gt; | clip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For copying the current directory, we need to convert it into a command. Since %cd% is the environment variable that stores that value, we can echo that value and pipe it into the clip command.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo %cd% | clip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Similarly, for copying a file’s contents to the clipboard, we can use&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat example.txt | clip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;To paste the clipboard into a file, we can use the powershell get-clipboard command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;powershell get-clipboard &amp;gt; example.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Linux
&lt;/h2&gt;

&lt;p&gt;For Linux machines, we can use the terminal commands xclip or xsel.&lt;/p&gt;

&lt;p&gt;These need to be installed first:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install xclip
sudo apt-get install xsel
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Using xsel, the command for copying something is&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xsel --clipboard --input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So, copy the current directory, we can use:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pwd | xsel --clipboard --input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And for copying the contents of a file to the clipboard:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cat example.txt | xsel --clipboard --input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And for pasting the clipboard contents in a file, we can use:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;xsel --clipboard --output &amp;gt; example.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That is all there is to copy/pasting output from the terminal to the system clipboard. If you have any questions, let us know in the comments below.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>terminal</category>
      <category>command</category>
      <category>developer</category>
    </item>
    <item>
      <title>How To Get The Hash of A File In Node.js</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Fri, 26 Apr 2024 16:24:11 +0000</pubDate>
      <link>https://forem.com/saranshk/how-to-get-the-hash-of-a-file-in-nodejs-1bdk</link>
      <guid>https://forem.com/saranshk/how-to-get-the-hash-of-a-file-in-nodejs-1bdk</guid>
      <description>&lt;p&gt;While working on a project, I wanted to do an integrity check of a file that I was referencing. So, I needed to know how to get the hash of a file in Node.js. And this post is about that.&lt;/p&gt;

&lt;p&gt;We will use the fs and crypto modules that are available in Node.js to get the hash of a file. We will be using the createReadStream method of the fs module to read the file and get its contents. After we are done reading it, we will call the the getHash() method of the crypto module to calculate the hash of the file.&lt;/p&gt;

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

const getHash = path =&amp;gt; new Promise((resolve, reject) =&amp;gt; {
 const hash = crypto.createHash('sha256');
 const rs = fs.createReadStream(path);
 rs.on('error', reject);
 rs.on('data', chunk =&amp;gt; hash.update(chunk));
 rs.on('end', () =&amp;gt; resolve(hash.digest('hex')));
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, we can use the getHash method to get the hash of a file. It is worth mentioning that we could have used various algorithms for hashing our file, like md5, sha1 and sha256. sha256 would be the more robust algorithm but a bit slower than the other less secure ones. For the digest method, we could have used hex or base64 depending on how we want to output the hash. We can use the above method like so:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(async () =&amp;gt; {
  try {
    const hashValue = await getHash('path/to/file');
    console.log(hashValue);
  } catch (error) {
    console.error('Error:', error);
  }
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that is all to the code and its explanation. If you have any questions, feel free to drop a comment below.&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Native Popover API in HTML</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Wed, 24 Apr 2024 16:28:42 +0000</pubDate>
      <link>https://forem.com/saranshk/native-popover-api-in-html-4kce</link>
      <guid>https://forem.com/saranshk/native-popover-api-in-html-4kce</guid>
      <description>&lt;p&gt;Popovers have been a problem that was typically solved by using a third-party solution. But that is no longer the case. We now have a native popover API in HTML supported by all browsers (Firefox added preview browser support on 16th April 2024 in version 125).&lt;/p&gt;

&lt;p&gt;Let us take a look at how the native popover API works.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a popover element
&lt;/h2&gt;

&lt;p&gt;Popovers are window-like components shown when a user interacts with an element (that serves as a trigger for showing them). It usually provides additional information or context. They are typically used as tooltips, context menus, toast menus, notifications, onboarding tours, etc.&lt;/p&gt;

&lt;p&gt;First, look at the HTML needed to create a popover using the native popover API.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;button popovertarget="wg-popover-container"&amp;gt;Toggle popover&amp;lt;/button&amp;gt;
&amp;lt;div id="wg-popover-container" popover&amp;gt;Popover content goes here&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;By default, the container element is hidden (thanks to the popover attribute) and is only visible when a user action triggers it. We can write some JavaScript to do that. However, we can simply use native popover API in HTML without needing any JavaScript. All we need is the custom attribute called &lt;code&gt;popovertarget&lt;/code&gt;. It controls the display/hide logic of the div with the same id as the value specified for the attribute. The corresponding element gets hidden or shown when the button gets clicked.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; The popover attribute has the value auto by default, meaning that the popover force closes other popovers (except ancestors) when triggered. It can be set to &lt;code&gt;popover="manual"&lt;/code&gt; to not force the closing of elements, and manual popovers need to be explicitly closed programmatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  Popover API and JavaScript
&lt;/h2&gt;

&lt;p&gt;Even though JavaScript is not needed for usage, the popover API does provide a way to trigger popovers using JavaScript for it. The &lt;code&gt;HTMLElement.togglePopover()&lt;/code&gt; method can be used to toggle the popover state programmatically. There also are methods for &lt;code&gt;hidePopover()&lt;/code&gt; and &lt;code&gt;showPopover()&lt;/code&gt; if we want to do the actions independently. Additionally, there are two events available &lt;code&gt;beforetoggle&lt;/code&gt; and &lt;code&gt;toggle&lt;/code&gt; which are fired before and after a transition state of a popover element.&lt;/p&gt;

&lt;h2&gt;
  
  
  Popover API and CSS
&lt;/h2&gt;

&lt;p&gt;It is worth mentioning that the popover elements are stacked on a different layer above the rest of the page. So we do not need to worry about z-index’s. In addition to that, there are pseudo-classes, such as the &lt;code&gt;:popover-open&lt;/code&gt; that allows us to target the element when it is opened and &lt;code&gt;::backdrop&lt;/code&gt; allows us to add CSS to content behind the popover.&lt;/p&gt;

&lt;p&gt;And that is all there is to the native popover API in HTML. It is also worth noting that there is no built-in support for responsiveness in the popover API, and it needs to be handled manually. There is no built-in animation support yet, but it is in the works.&lt;/p&gt;

&lt;p&gt;Let us know in the comments if you have any questions!&lt;/p&gt;

</description>
      <category>html</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>news</category>
    </item>
    <item>
      <title>Node.js 20.6 adds built-in support for .env files</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Fri, 12 Apr 2024 14:19:47 +0000</pubDate>
      <link>https://forem.com/saranshk/nodejs-206-adds-built-in-support-for-env-files-1741</link>
      <guid>https://forem.com/saranshk/nodejs-206-adds-built-in-support-for-env-files-1741</guid>
      <description>&lt;p&gt;Node.js 20.6 added built-in support for the .env file. This is an excellent addition to the platform and gives us the ability to load environment variables from .env files directly without using third-party packages. While it is great to see first-class support, some caveats remain.&lt;/p&gt;

&lt;p&gt;Let us look at how it works. Assuming that you are running Node 20.6, create a .env file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;API_KEY="KEY"
DATABASE_URL="URL"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And then, you can run node using the following command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node --env-file .env index.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This would give you access to the variables defined in the .env file in your JavaScript code.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// index.js
console.log(`Hello ${process.env.DATABASE_URL}`)

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

&lt;/div&gt;

&lt;p&gt;That is it! Want to have a different production configuration? Just create a new file and point it to a .env.production file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Order when running the command matters
&lt;/h2&gt;

&lt;p&gt;A minor detail to remember when executing the script is that the env file needs to be passed in before the file name. Ideally, it should have been interchangeable, but that is not the case. The env file gets ignored if we use the command:&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .env file gets ignored in this case&lt;br&gt;
node inex.js --env-file .env&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Caveats&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;As with all experimental things, a few things are missing. Some of these might lead to people using dotenv until support for these gets added. I will mention them here and let you see if they are dealbreakers. You can also follow the &lt;a href="https://github.com/nodejs/node/issues/49148"&gt;GitHub issue&lt;/a&gt; to track missing feature support.&lt;/p&gt;

&lt;h2&gt;
  
  
  No Multiline Support
&lt;/h2&gt;

&lt;p&gt;Multiline environment variables are not supported currently. If you add one, it will be undefined.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .env&lt;br&gt;
WORLD="Hello&lt;br&gt;
World"

&lt;p&gt;// index.js&lt;br&gt;
console.log(&lt;code&gt;Hello ${process.env.WORLD}&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;// running the script&lt;br&gt;
node --env-file=.env index.js&lt;br&gt;
Hello undefined&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  The same variable is defined in both the environment and file&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;If the same variable is defined in the environment and the file, the value from the file takes precedence. There is no way to override it with the system environment variables.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .env&lt;br&gt;
WORLD="foo"

&lt;p&gt;// index.js&lt;br&gt;
console.log(&lt;code&gt;Hello ${process.env.WORLD}&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;// running the script&lt;br&gt;
export WORLD="bar"&lt;br&gt;
node --env-file=.env index.js&lt;br&gt;
Hello foo&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  No variable references/expansions&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Node does not support variable expansion currently. It will output the variable as a string if trying to reference another variable using $variable. This is possible in dotenv using the &lt;a href="https://github.com/motdotla/dotenv-expand"&gt;dotenv-expand&lt;/a&gt; library.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// .env&lt;br&gt;
WORLD="foo"&lt;br&gt;
WORLD_BAZ=$WORLD

&lt;p&gt;// index.js&lt;br&gt;
console.log(&lt;code&gt;Hello ${process.env.WORLD_BAZ}&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;// running the script&lt;br&gt;
node --env-file=.env index.js&lt;br&gt;
Hello $WORLD&lt;br&gt;
&lt;/p&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  No .env.vault support&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/dotenv-org/dotenv-vault"&gt;dotenv-vault&lt;/a&gt; is another popular package that lets you encrypt your secret and decrypt the file just in time. They are quite helpful for production and CIT environments but are not supported currently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Node.js 20.6 adding built-in support for .env files is a huge step forward for the Node community. Hopefully, it does not stay experimental in the near future, and we can start using it in our applications soon!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>webdev</category>
      <category>devops</category>
    </item>
    <item>
      <title>Object destructuring in TypeScript</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Wed, 03 Apr 2024 14:22:39 +0000</pubDate>
      <link>https://forem.com/saranshk/object-destructuring-in-typescript-587d</link>
      <guid>https://forem.com/saranshk/object-destructuring-in-typescript-587d</guid>
      <description>&lt;p&gt;Object destructuring is a powerful ES 6 feature that can help developers write cleaner code. It allows us to extract properties from a JavaScript object into variables.&lt;/p&gt;

&lt;p&gt;If you are unfamiliar with it, read our post about &lt;a href="https://www.wisdomgeek.com/development/web-development/javascript/rest-and-spread-operator-three-dots-that-changed-javascript/"&gt;object destructuring&lt;/a&gt; to know more about what is possible using it.&lt;/p&gt;

&lt;p&gt;When it comes to object destructuring in TypeScript, the following statement doesn’t work.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { title: string, comments: number } = blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Technically, the way it works is that it assigns the title property of the object to a variable named string and the comments property to the number variable. This happens because JavaScript assumes that we want to destructure and rename the variables, which is valid ES6 syntax.&lt;/p&gt;

&lt;p&gt;The right way to do object destructuring in TypeScript would be&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { title, comments } : { title: string; comments: number }  = blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Another alternative can be to use an interface or a type declaration:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;interface Blog {
  title: string
  comments: number
}

const { title, comments }: Blog = blog
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;By default, the types usually get inferred, and this type of object destructuring is usually not needed. But for the cases when we do, we now know how to. I hope this post was helpful. If you have any questions, feel free to drop a comment below.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/object-destructuring-in-typescript/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on March 28, 2024.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
      <category>learning</category>
    </item>
    <item>
      <title>Improve git clone performance in a CI pipeline</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Tue, 19 Mar 2024 13:46:03 +0000</pubDate>
      <link>https://forem.com/saranshk/improve-git-clone-performance-in-a-ci-pipeline-52pf</link>
      <guid>https://forem.com/saranshk/improve-git-clone-performance-in-a-ci-pipeline-52pf</guid>
      <description>&lt;p&gt;Have you felt particularly annoyed by the time it takes to clone a large repository, especially one with a huge commit history? This post will discuss a simple but powerful technique to significantly improve git clone performance.&lt;/p&gt;

&lt;p&gt;The solution is called shallow cloning, which uses the --depth=1 flag with git clone. The reason why this improves git clone performance is that it creates a clone of the git repository with history up to the depth field that we specify. So, in this case, only the most recent commit (and some necessary metadata) is fetched, and nothing else. This helps skip any older commits, tags, branches, etc. Skipping everything else helps reduce the download size and the time it takes. This is particularly useful to improve git clone performance on a CI server. All we need to do is use&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git clone --depth=1 &amp;lt;repository-url&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is worth mentioning that while shallow cloning does improve the clone performance, it probably is not useful if you want access to the full commit history. It also means there is no branch history, so switching to a different branch would require additional fetches. So shallow cloning is ideal for continuous integration/deployment pipelines, automated build systems, and cases where you want a quick setup for testing or performing code reviews.&lt;/p&gt;

&lt;p&gt;If you are looking for other git-related hacks, do check out our posts about &lt;a href="https://www.wisdomgeek.com/development/how-to-permanently-remove-a-file-from-git-history/"&gt;removing file from git history&lt;/a&gt; and &lt;a href="https://www.wisdomgeek.com/development/delete-git-branches-that-do-not-exist-on-remote/"&gt;deleting git branches that do not exist on remote&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/improve-git-clone-performance-in-a-ci-pipeline/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on March 19, 2024.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Fix: Hydration failed because the initial UI does not match what was rendered on the server</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Wed, 13 Mar 2024 18:18:26 +0000</pubDate>
      <link>https://forem.com/saranshk/fix-hydration-failed-because-the-initial-ui-does-not-match-what-was-rendered-on-the-server-41di</link>
      <guid>https://forem.com/saranshk/fix-hydration-failed-because-the-initial-ui-does-not-match-what-was-rendered-on-the-server-41di</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/web-development/react/fix-hydration-failed-because-the-initial-ui-does-not-match-what-was-rendered-on-the-server/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on March 13, 2024.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Within a React or Next.js app, if you encounter the error &lt;em&gt;“Hydration failed because the initial UI does not match what was rendered on the server”&lt;/em&gt;, there could be a few reasons for it.&lt;/p&gt;

&lt;p&gt;Let us look at all the possible reasons and potential fixes for the error.&lt;/p&gt;

&lt;h2&gt;
  
  
  Potential issues
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Incorrect HTML formatting
&lt;/h3&gt;

&lt;p&gt;The most common cause for the error is incorrect HTML formatting in our code.&lt;/p&gt;

&lt;p&gt;But why does the browser not tell you that but instead throws the error “Hydration failed because the initial UI does not match what was rendered on the server”? Because the browser tries to fix these on its own and then does a comparison of the HTML it has from what the server sent, it fails to match the two. Thus it throws the generic error.&lt;/p&gt;

&lt;p&gt;Some examples of invalid HTML are as follows.&lt;/p&gt;

&lt;p&gt;Having a div element inside a paragraph is not allowed.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const IncorrectComponent = () =&amp;gt; {
  return (
    &amp;lt;p&amp;gt;
      &amp;lt;div&amp;gt;Incorrect HTML&amp;lt;/div&amp;gt;
    &amp;lt;/p&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A paragraph inside another paragraph is erroneous too. The same goes for an H1, or a list item too.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const IncorrectComponent = () =&amp;gt; {
  return (
    &amp;lt;p&amp;gt;
      &amp;lt;p&amp;gt;p inside paragraph is invalid&amp;lt;/p&amp;gt;
      &amp;lt;h1&amp;gt;H1 inside paragraph is invalid too&amp;lt;/h1&amp;gt;
    &amp;lt;/p&amp;gt;
  )
}


export const AnotherIncorrectComponent = () =&amp;gt; {
  return (
    &amp;lt;ul&amp;gt;
      &amp;lt;p&amp;gt;
        &amp;lt;li&amp;gt;Incorrect HTML&amp;lt;/li&amp;gt;
      &amp;lt;/p&amp;gt;
    &amp;lt;/ul&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The Link tag that is provided by Next.js is already rendered as an anchor component. Trying to add another anchor inside it will throw an error too, since an anchor cannot be placed inside another.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const IncorrectComponent = () =&amp;gt; {
  return (
    &amp;lt;Link&amp;gt;
      &amp;lt;a&amp;gt;Link tag already creates an anchor tag, so this errors too&amp;lt;/a&amp;gt;
    &amp;lt;/Link&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A &lt;/p&gt;
&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt; must have a &lt;thead&gt; or a &lt;/thead&gt;
&lt;tbody&gt; before adding any table rows (&lt;tr&gt; ).
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const IncorrectComponent = () =&amp;gt; {
  return (
    &amp;lt;table&amp;gt;
      tbody is needed inside a table
    &amp;lt;/table&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Images inside an anchor tag throw an error too.&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const IncorrectComponent = () =&amp;gt; {
  return (
    &amp;lt;a&amp;gt;
      &amp;lt;img /&amp;gt;
    &amp;lt;/a&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;
  
  
  Locale objects
&lt;/h3&gt;

&lt;p&gt;When working with locale objects, like date or time objects that are being rendered in the component, it could lead to a mismatch in the output of the component. Who doesn’t love timezones, right?&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const IncorrectComponent = () =&amp;gt; {
  return (
    {new Date().toLocaleString()}
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;
  
  
  Incorrect CSS formatting
&lt;/h3&gt;

&lt;p&gt;Incorrect CSS formatting can also lead to the error. For example, if you forget to add quotes when specifying background image&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.image {
  background-image: url(https://www.wisdomgeek.com)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;
  
  
  Extensions
&lt;/h3&gt;

&lt;p&gt;Another possible reason is that an extension might be inserting something in the client page that might lead to this error since the rendered HTML is different than what the server sent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Accessing the window object
&lt;/h3&gt;

&lt;p&gt;If our component tries to access the window object, that is not going to be available on the server. That can lead to this error. This is pretty common when using a third-party component. Even checks like typeof window !== 'undefined' can lead to this error.&lt;/p&gt;

&lt;p&gt;Sometimes you still need to access some of the properties mentioned above and there is no workaround. For those cases, there are a couple of possible solutions to fix it. Let us take a look at those next.&lt;/p&gt;

&lt;h2&gt;
  
  
  Potential workaround solutions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use a useEffect to run only on the client side
&lt;/h3&gt;

&lt;p&gt;We can intentionally render something different on the client side by using a variable that gets updated inside a useEffect hook.&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useState, useEffect } from 'react'

export default function App() {
  const [isClient, setIsClient] = useState(false)

  useEffect(() =&amp;gt; {
    setIsClient(true);
  }, [])

  return (
    &amp;lt;&amp;gt;
      {isClient &amp;amp;&amp;amp; &amp;lt;&amp;gt;{/* Window related checks or components can go here */}&amp;lt;/&amp;gt;}
      {!isClient &amp;amp;&amp;amp; "Pre-rendered content"}
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;
  
  
  Disable SSR
&lt;/h3&gt;

&lt;p&gt;Another potential solution is to disable prerendering altogether for a component. In Next.js, we can do so using the following:&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import dynamic from 'next/dynamic'

const DynamicComponent = dynamic(() =&amp;gt; import('./Component'), { ssr: false })

export default NewComponent = () =&amp;gt; {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;DynamicComponent /&amp;gt;
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;
  
  
  Use suppressHydrationWarning
&lt;/h3&gt;

&lt;p&gt;We can suppress the warning altogether by adding the HTML attribute suppressHydrationWarning to the component or element. For example, if we have a custom component that we want to suppress the warning for:&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export default Component = () =&amp;gt; {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;CustomComponent suppressHydrationWarning={true} /&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And those are all the potential solutions for fixing the “Hydration failed because the initial UI does not match what was rendered on the server” error. Let us know in the comments if you are still facing the error.&lt;/p&gt;


&lt;/tr&gt;
&lt;br&gt;
&lt;/tbody&gt;
&lt;br&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
      <category>javascript</category>
      <category>nextjs</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>How to Convert a React Component to an Image</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Thu, 08 Sep 2022 18:44:33 +0000</pubDate>
      <link>https://forem.com/saranshk/how-to-convert-a-react-component-to-an-image-2jon</link>
      <guid>https://forem.com/saranshk/how-to-convert-a-react-component-to-an-image-2jon</guid>
      <description>&lt;p&gt;Sometimes you want to give the users the ability to download a part of the web application as an image. In that case, you want a way to convert a React component to an image. And it can be straightforward by using a third-party NPM package called html2canvas. Let us look at how to do it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;

&lt;p&gt;We want to first mark the div in HTML markup that we want to be downloaded when the user hits the download button. This could be a chart, user data, a table, or anything we want. We will allocate an id to that element.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div id="print"&amp;gt;This will be downloaded as an image&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We can wrap this as a React component with an event handler attached to a download button:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const App = () =&amp;gt; {
  const handleImageDownload = () =&amp;gt; {
    // TODO: add logic here
  };
  return (
    &amp;lt;&amp;gt;
      &amp;lt;button type="button" onClick={handleImageDownload}&amp;gt;Download&amp;lt;/button&amp;gt;
      &amp;lt;div id="print"&amp;gt;This will be downloaded as an image&amp;lt;/div&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h2&gt;
  
  
  Programming logic
&lt;/h2&gt;

&lt;p&gt;As we discussed earlier, we will install thehtml2canvas NPM package.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install html2canvas
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, all we need to do is use the package to fetch the corresponding div that we want to convert to an image. We then create a link in memory to download the image, click it programmatically and then remove the link from the DOM.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const handleDownloadImage = () =&amp;gt; {
    const element = document.getElementById('print'),
    canvas = await html2canvas(element),
    data = canvas.toDataURL('image/jpg'),
    link = document.createElement('a');

    link.href = data;
    link.download = 'downloaded-image.jpg';

    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
  };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And that is all we need to do to convert a React component to an image! Hope you found this post useful.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/web-development/react/how-to-convert-a-react-component-to-an-image/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on September 28, 2021.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Specify a Node Version in Repl.it</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Wed, 17 Aug 2022 21:24:08 +0000</pubDate>
      <link>https://forem.com/saranshk/how-to-specify-a-node-version-in-replit-6bg</link>
      <guid>https://forem.com/saranshk/how-to-specify-a-node-version-in-replit-6bg</guid>
      <description>&lt;p&gt;I was recently trying to use a later version of Node on Repl.it. I wanted to use a package that supported ES Modules, and the default version did not have support for it. So I wanted to use the latest node version in Repl.it. And found that there was no direct way of doing so. But it is still possible with a few custom steps.&lt;/p&gt;

&lt;h2&gt;
  
  
  The setup
&lt;/h2&gt;

&lt;p&gt;Repl.it allows specifying the node.js version as part of the package.json itself. But it does not use the installed version by default when running the script. But before we get to that, we need to install our node version in repl.it. To do so, we go to the package.json and add the version we want. Or we could have used the package manager interface to do so:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8Xd2bBbu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/0%2Ajc1i99XFq2pRqkkU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8Xd2bBbu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/0%2Ajc1i99XFq2pRqkkU.png" alt="" width="800" height="295"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Configuring Repl.it
&lt;/h2&gt;

&lt;p&gt;Once this is set up, we need to use this version instead of the default Repl.it Node.js version. We need to make use of a configuration for Repl.it by creating a file named .replit. You can read more about it &lt;a href="https://docs.replit.com/programming-ide/configuring-run-button"&gt;here&lt;/a&gt; if you are interested.&lt;/p&gt;

&lt;p&gt;In this file, we will add the contents:&lt;/p&gt;

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

&lt;/div&gt;

&lt;p&gt;This will execute Node from the shell instead of the console. And then all that is left to do is configure the start script in our package.json file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": { "start": "node ." }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we wanted to start using the latest version for the shell as well, we can execute the following command in the shell:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm config set prefix=$(pwd)/node_modules/node &amp;amp;&amp;amp; export PATH=$(pwd)/node_modules/node/bin:$PATH
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Optionally, we might want to re-install the packages installed if we want to target them to the higher version of node.js that we just installed.&lt;/p&gt;

&lt;p&gt;And that should be it. Our custom Node version in Repl.it should be ready and good to go. As soon as we hit the Run button, the index.js script will be executed using the node version we specified in our package.json.&lt;/p&gt;

&lt;p&gt;If you have any questions regarding this, feel free to drop a comment below!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/web-development/javascript/specifying-a-node-version-in-repl-it/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on September 13, 2021.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Access the Clipboard in JavaScript</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Thu, 21 Jul 2022 00:12:57 +0000</pubDate>
      <link>https://forem.com/saranshk/access-the-clipboard-in-javascript-2co6</link>
      <guid>https://forem.com/saranshk/access-the-clipboard-in-javascript-2co6</guid>
      <description>&lt;p&gt;Developers are probably the laziest people on the planet. And of all the things, copy-paste is our favorite keyboard shortcut. But what is better than hitting CTRL + C? Having a button do the copying for you! And that is now possible using an asynchronous version of the clipboard API in JavaScript.&lt;/p&gt;

&lt;p&gt;You would have probably come across this while copying code off of a website, or an API key, or copying links from Google Drive:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KUJMBrUL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/0%2A0YVD368IPc5PUYWV.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KUJMBrUL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/0%2A0YVD368IPc5PUYWV.png" alt="" width="800" height="155"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The clipboard API
&lt;/h2&gt;

&lt;p&gt;document.execCommand() has been available to copy text way before the clipboard API became a thing. But it was a synchronous call, did not work correctly across all browsers (permission access was not consistent either), and had some security risks associated with it.&lt;/p&gt;

&lt;p&gt;The newer asynchronous clipboard API is supported by all browsers and is more secure (only works on HTTPS pages by default, and is not available to background tabs).&lt;/p&gt;

&lt;h2&gt;
  
  
  Implementing using JavaScript
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// copying to clipboard
navigator.clipboard.writeText(SOME_VALUE)
    .then(() =&amp;gt; alert("Text is now stored your cliboard!"));

// copying from clipboard
await readText = await navigator.clipboard.readText();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We will need more code to detect support in the browser and error handling. But that is the gist of the API.&lt;/p&gt;

&lt;p&gt;And both the methods are &lt;a href="https://caniuse.com/?search=navigator.clipboard"&gt;supported&lt;/a&gt; in all modern browsers. And that is it. Hope this was helpful and that you start using it in the relevant places.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/web-development/javascript/accessing-the-clipboard-in-javascript/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on September 9, 2021.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Using counters in CSS to number elements automatically</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Thu, 28 Apr 2022 21:51:58 +0000</pubDate>
      <link>https://forem.com/saranshk/using-counters-in-css-to-number-elements-automatically-18e</link>
      <guid>https://forem.com/saranshk/using-counters-in-css-to-number-elements-automatically-18e</guid>
      <description>&lt;p&gt;Ordered lists have been an important part of web design for quite a while now. If we needed more control over the appearance of numbers, we had to add more HTML and/or JavaScript to do so until now. Counters in CSS save us much of that trouble.&lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges with ol
&lt;/h2&gt;

&lt;p&gt;An ordered list can be rendered as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Item one&lt;/li&gt;
&lt;li&gt;Item one&lt;/li&gt;
&lt;li&gt;Item one&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What if we wanted complex numbering? What if we wanted dynamic pagination? Or style the numbers in a particular manner?&lt;/p&gt;

&lt;p&gt;Instead of doing &lt;/p&gt;
&lt;li&gt;Item one&lt;/li&gt;, we would have had to replace it by &lt;span&gt;1.&lt;/span&gt;Item one instead.

&lt;p&gt;And then apply styles to the span tags. But we are developers. We don’t like hard-coded numbers and repetition! Counters in CSS come to our rescue.&lt;/p&gt;

&lt;h2&gt;
  
  
  Counters in CSS
&lt;/h2&gt;

&lt;p&gt;CSS counters are variables whose values can be changed by using CSS rules. They are scoped to the current web page.&lt;/p&gt;

&lt;p&gt;Before using counters in CSS, we need to understand its properties. counter-reset, counter-increment and counter() properties are what make the dynamic nature of counters a possibility. There are a couple of other properties as well, but these are sufficient for a basic counter.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;counter-reset is used to reset or initialize the counter. A counter needs to be created with this property in order for it to be used.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;counter-increment is used to increment the value of a counter.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;counter does the heavy lifting. It should be used inside the content property in a :before or :after pseudo selector to increment counts.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Putting these together, we will first initialize our list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div.list {
  counter-reset: numbered-list;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We are creating a variable called numbered-list to store our counters. Next, we want to increment the variable value every time we encounter a div inside this list. To do that, we will do:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div.list div {
  counter-increment: list-number;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This sets up our counter. We need to now add it to the DOM using the content property.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div.list div:before {
  content: counter(numbered-list);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And if we apply this CSS to a list:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="list"&amp;gt;
  &amp;lt;div&amp;gt;Item 1&amp;lt;/div&amp;gt;
  &amp;lt;div&amp;gt;Item 2&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We get the output:&lt;br&gt;
1Item 1&lt;br&gt;
2Item 2&lt;/p&gt;

&lt;p&gt;We can then style thediv.list div:before section of the element as we saw fit.&lt;/p&gt;

&lt;p&gt;We can even specify a custom starting point to the counter by initializing it as:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;counter-reset: list-number 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Or specify the increment value:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;counter-increment: list-number 10; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Browser support for counters is pretty good too:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2ZrZaM0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/0%2A4qiQF8i73o2xBMk1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2ZrZaM0o--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://cdn-images-1.medium.com/max/2048/0%2A4qiQF8i73o2xBMk1.png" alt="" width="800" height="259"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So we can start counters in CSS without much hassle.&lt;/p&gt;

&lt;p&gt;There are other functions such as counters() which allow nested counters and the counter function also takes a second parameter to specify the format of the counter which are good additions if you are looking for them.&lt;/p&gt;

&lt;p&gt;And that is all you need to know about counters in CSS. Feel free to drop a comment below if you have any questions.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/web-development/using-counters-in-css-to-number-elements-automatically/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on September 1, 2021.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>css</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Remove a Property from a JavaScript Object</title>
      <dc:creator>saransh kataria</dc:creator>
      <pubDate>Thu, 21 Apr 2022 02:09:13 +0000</pubDate>
      <link>https://forem.com/saranshk/how-to-remove-a-property-from-a-javascript-object-4gg</link>
      <guid>https://forem.com/saranshk/how-to-remove-a-property-from-a-javascript-object-4gg</guid>
      <description>&lt;p&gt;There are two ways to remove a property from a JavaScript object: one is the mutable way of doing it by using the delete operator. And the second one is the immutable way of doing it by using &lt;a href="https://www.wisdomgeek.com/development/web-development/rest-and-spread-operator-three-dots-that-changed-javascript/"&gt;object restructuring&lt;/a&gt;. Let us go through each of these:&lt;/p&gt;

&lt;h2&gt;
  
  
  1. The delete operator
&lt;/h2&gt;

&lt;p&gt;delete is a JavaScript instruction that allows us to remove a property from a JavaScript object. There are a couple of ways to use it:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;delete object.property;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;delete object[‘property’];&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The operator deletes the corresponding property from the object.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let blog = {name: 'Wisdom Geek', author: 'Saransh Kataria'};
const propToBeDeleted = 'author';
delete blog[propToBeDeleted];
console.log(blog); // {name: 'Wisdom Geek'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The delete operation modifies the original object. Therefore it is a mutable operation.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Object destructuring
&lt;/h2&gt;

&lt;p&gt;Using the object restructuring and rest syntax, we can destructure the object with the property to be removed and create a new copy of it. After the destructuring, a new copy of the object would be created and assigned to a new variable without the property that we chose to remove.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const { property, ...remainingObject } = object;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let blog = {name: 'Wisdom Geek', author: 'Saransh Kataria'};
const { author, ...blogRest } = blog;
console.log(blogRest) // {name: 'Wisdom Geek'};
console.log(blog); // {name: 'Wisdom Geek', author: 'Saransh Kataria'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we want to do this dynamically, we can do this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const name = 'propertToBeRemoved';
const { [name]: removedProperty, ...remainingObject } = object;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is also possible to remove multiple properties using the same syntax.&lt;/p&gt;

&lt;p&gt;And those are the 2 ways to remove a property from a JavaScript object. If you have any questions, feel free to drop a comment below!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://www.wisdomgeek.com/development/web-development/javascript/how-to-remove-a-property-from-a-javascript-object/"&gt;https://www.wisdomgeek.com&lt;/a&gt; on August 29, 2021.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
