<?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: Ndimah Tchougoua</title>
    <description>The latest articles on DEV Community by Ndimah Tchougoua (@jospinevans).</description>
    <link>https://dev.to/jospinevans</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%2F228185%2Fe5936422-a0d0-491e-b330-5d22111b796a.png</url>
      <title>DEV Community: Ndimah Tchougoua</title>
      <link>https://dev.to/jospinevans</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jospinevans"/>
    <language>en</language>
    <item>
      <title>You don't need to set the time-out</title>
      <dc:creator>Ndimah Tchougoua</dc:creator>
      <pubDate>Sun, 11 Aug 2024 17:20:06 +0000</pubDate>
      <link>https://dev.to/jospinevans/you-dont-need-to-set-the-time-out-5bfo</link>
      <guid>https://dev.to/jospinevans/you-dont-need-to-set-the-time-out-5bfo</guid>
      <description>&lt;p&gt;I know timers has been for a while a feature that a lot of folks use in their day-to-day tasks. In the JavaScript world, timers are often implemented with the &lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;setInterval&lt;/code&gt; functions, the bad news for you if you are doing it is that it's not a good practice and I will try to explain why.&lt;/p&gt;

&lt;p&gt;Before I begin to explain my thought, I have a question for you: &lt;strong&gt;&lt;em&gt;can you use a watch giving the wrong time ?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your answer is yes, I'm sorry to have wasted your precious time because this article does not belong to you.&lt;/p&gt;

&lt;p&gt;On the other hand, if your answer is negative, I will explain why using &lt;code&gt;setTimeout&lt;/code&gt; or &lt;code&gt;setInterval&lt;/code&gt; is like using a damaged watch to get the time.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem with these functions
&lt;/h2&gt;

&lt;p&gt;To begin, let's consider the following snippet&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;test&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;hello&lt;/span&gt;&lt;span class="dl"&gt;"&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="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nx"&gt;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;number &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="nx"&gt;i&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="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;world&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;if you run this snippet in your browser console, you will have the following result &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzyk32d1ksrebwxcivo6.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmzyk32d1ksrebwxcivo6.gif" alt="output of the execution, showing how set Timeout has low priority" width="800" height="419"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This behavior is due to the fact that &lt;code&gt;setTimeout&lt;/code&gt; is adding the callback in the browser's queue so that it should process it once it is idle &lt;em&gt;(does not have a task to do)&lt;/em&gt; in other words the callback passed to the &lt;code&gt;setTimeout&lt;/code&gt; has low priority&lt;/p&gt;

&lt;p&gt;Now, knowing this, I imagine that it will be hard for you to implement timers using the &lt;code&gt;setTimeout&lt;/code&gt; function because you can have 2 or even 10 ticks (depending on how busy your browser is) at the same time. This will be a nightmare to debug, but do we have a better solution ?&lt;/p&gt;
&lt;h2&gt;
  
  
  A way to avoid these functions
&lt;/h2&gt;

&lt;p&gt;To provide a better way of implementing timers, we should use &lt;code&gt;requestAnimationFrame&lt;/code&gt; function because it tells the browser to execute a callback before the next paint &lt;em&gt;(in other words before any change in the UI occurs)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;The difference here is pretty subtle, so it's better to understand it through the code. Let's take back our previous snippet and tweak it a little bit to compare &lt;code&gt;setTimeout&lt;/code&gt; and &lt;code&gt;requestAnimationFrame&lt;/code&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;function&lt;/span&gt; &lt;span class="nf"&gt;testWithAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;hello&lt;/span&gt;&lt;span class="dl"&gt;"&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="nf"&gt;requestAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;tell me the next paint&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;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;number &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;i&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="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;world&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;In this example, we can see that when running on Chrome, the &lt;code&gt;setTimeout&lt;/code&gt; runs before &lt;code&gt;requestAnimationFrame&lt;/code&gt; &lt;em&gt;(though in some rare cases the inverse happens)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanti38k1ldxr65h0gd11.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fanti38k1ldxr65h0gd11.gif" alt="screenshot showing how set timeout has priority over request animation frame when no painting is happening in Chrome" width="800" height="464"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But if you run it on Firefox, this will be the output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fll6uauzo5ns3ihmutxk5.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fll6uauzo5ns3ihmutxk5.gif" alt="screenshot showing how request animation frame has priority over set timeout when no painting is happening in Firefox" width="800" height="403"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can seem confusing but if you pay a bit of attention, you will realize that no painting is happening during the execution, so how this scenario is handled depends on the browser.&lt;/p&gt;

&lt;p&gt;Now if we can tweak our snippet to make the browser repaint the page, let's see what will happen&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;testWithAnimationFrame2&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;hello&lt;/span&gt;&lt;span class="dl"&gt;"&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="nf"&gt;requestAnimationFrame&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;tell me the next paint&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;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;number &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;i&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="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;backgroundColor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;red&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="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;world&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;Here is the output on Chrome&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8nnmbkpgvtvt6jaivid7.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8nnmbkpgvtvt6jaivid7.gif" alt="screenshot showing how request animation frame has priority over set timeout when a painting occurs in Chrome" width="800" height="512"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And here is the output in Firefox&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1fgn6b904brk5aisnbv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy1fgn6b904brk5aisnbv.gif" alt="screenshot showing how request animation frame has priority over set timeout when a painting occurs in Firefox" width="800" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see in the logs, when the browser in making changes in the UI, the &lt;code&gt;requestAnimationFrame&lt;/code&gt; function will always have priority over other scheduled callbacks.&lt;/p&gt;

&lt;p&gt;Because on the web, we are constantly performing repaints, &lt;code&gt;requestAnimationFrame&lt;/code&gt; is an obvious choice to implement timers.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding the &lt;code&gt;requestAnimationFrame&lt;/code&gt; function
&lt;/h2&gt;

&lt;p&gt;The function only takes a callback as parameter. To provide context to the callback, it should take a timestamp indicating the time at which the previous frame has ended based on the time of the initial rendering of the page.&lt;/p&gt;

&lt;p&gt;The function will return an integer representing the request's identifier, this can be useful if you wish to cancel the request with the &lt;code&gt;cancelAnimationFrame&lt;/code&gt; function.&lt;/p&gt;
&lt;h2&gt;
  
  
  A simple implementation of a Stopwatch in JavaScript
&lt;/h2&gt;

&lt;p&gt;To implement a stopwatch, there are some requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We should know after which amount of time it should tick (generally   a second)&lt;/li&gt;
&lt;li&gt;We should know the delay of time after which the stopwatch should stop ticking&lt;/li&gt;
&lt;li&gt;The ticking intervals should be lesser than the delay&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Taking all these requirements in considerations, the following code snippet will create a stopwatch for you&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;h2&gt;
  
  
  Outro
&lt;/h2&gt;

&lt;p&gt;I know it might have been a long read but believe that you have enjoyed it. Anyway, if you have any questions or if you have any suggestion, feel free to reach me out.&lt;/p&gt;

&lt;p&gt;Thank you for reading it and goodbye 👋&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>timers</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Your website deserves to look good on social medias</title>
      <dc:creator>Ndimah Tchougoua</dc:creator>
      <pubDate>Thu, 04 Jul 2024 12:17:05 +0000</pubDate>
      <link>https://dev.to/jospinevans/your-website-deserves-to-look-good-on-social-medias-305e</link>
      <guid>https://dev.to/jospinevans/your-website-deserves-to-look-good-on-social-medias-305e</guid>
      <description>&lt;p&gt;I know sometime you might have wondered how websites such as &lt;a href="https://github.com" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; and &lt;a href="https://dev.to"&gt;Dev&lt;/a&gt; do to make their image and description appear when you share their links through social medias on even some messaging applications as illustrated here in WhatsApp.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fski7ypwuev5u9h9u811k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fski7ypwuev5u9h9u811k.png" alt="An overview of how the https://github.com/jay-ike link looks on WhatsApp, showcasing the user avatar and the user bio on the GitHub platform" width="754" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I have good news for you, it's not that complicated, it's even simple (&lt;em&gt;not kidding I really mean it&lt;/em&gt;). All what they do is implementing the &lt;strong&gt;opengraph protocol&lt;/strong&gt; to make it happen so that social medias robots can parse it to get some information in order to render it that way.&lt;/p&gt;

&lt;p&gt;So if you are looking for a quick snippet, you can stop here to grab the following&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="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&lt;/span&gt; &lt;span class="na"&gt;lang=&lt;/span&gt;&lt;span class="s"&gt;"en"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt; &lt;span class="na"&gt;Other&lt;/span&gt; &lt;span class="na"&gt;infos&lt;/span&gt; &lt;span class="na"&gt;of&lt;/span&gt; &lt;span class="na"&gt;the&lt;/span&gt; &lt;span class="na"&gt;head&lt;/span&gt; &lt;span class="na"&gt;goes&lt;/span&gt; &lt;span class="na"&gt;here&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt; &lt;span class="na"&gt;Open&lt;/span&gt; &lt;span class="na"&gt;Graph&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:type"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"website"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:url"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"https://ike-designo-website.vercel.app/"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:locale"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"en_US"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:title"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"Designo a digital agency for your digital needs"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:description"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"With over 10 years in the industry, we are experienced
        in creating fully responsive websites, app design, and engaging brand experiences.
        Find out more about our services."&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:image"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"https://ike-designo-website.vercel.app/assets/home-og-image.png"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:image:width"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"1110"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:image:height"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"610"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;property=&lt;/span&gt;&lt;span class="s"&gt;"og:image:alt"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"the screen shot of the web page at https://ike-designo-website.vercel.app"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;&lt;/span&gt;&lt;span class="err"&gt;!&lt;/span&gt; &lt;span class="na"&gt;the&lt;/span&gt; &lt;span class="na"&gt;body&lt;/span&gt; &lt;span class="na"&gt;goes&lt;/span&gt; &lt;span class="na"&gt;here&lt;/span&gt; &lt;span class="na"&gt;--&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, if you get this far, it means that you're looking for a thorough explanation. As you've seen, everything is listed in the head of the page as metadata. So I will try to explain it step by step.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you have some knowledge about the meta tag you should've noticed that the &lt;strong&gt;property&lt;/strong&gt; attribute is non-standard in the context of HTML. This can be explained by the will to avoid conflicts with the language &lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  og:type
&lt;/h2&gt;

&lt;p&gt;This represents the type of content you are providing, in our case it's a website &lt;/p&gt;

&lt;h2&gt;
  
  
  og:url
&lt;/h2&gt;

&lt;p&gt;This is the URL of your website, it will be used by the robot as an identifier of your website&lt;/p&gt;

&lt;h2&gt;
  
  
  og:locale
&lt;/h2&gt;

&lt;p&gt;This property is used to provide information about the primary language of your website (for internationalization purpose)&lt;/p&gt;

&lt;h2&gt;
  
  
  og:title
&lt;/h2&gt;

&lt;p&gt;The title of the website as it will appear to the potential visitors&lt;/p&gt;

&lt;h2&gt;
  
  
  og:description
&lt;/h2&gt;

&lt;p&gt;A short description about the page to give an overview of the content (&lt;em&gt;it's important to make at most 2 sentences in order to not confuse your potential visitors&lt;/em&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  og:image
&lt;/h2&gt;

&lt;p&gt;a URL representing the image you want to showcase for your website (&lt;em&gt;This can be tricky to choose, but if you don't know what to choose, it is better to take a screenshot of your page&lt;/em&gt;)&lt;/p&gt;

&lt;h2&gt;
  
  
  og:image:alt
&lt;/h2&gt;

&lt;p&gt;A description of what is in the image, this is very important because it will provide information to people who cannot see or are having difficulties to see your image. So I urge you to never forget this one if you want to provide an image&lt;/p&gt;

&lt;h2&gt;
  
  
  og:image:width, og:image:height
&lt;/h2&gt;

&lt;p&gt;Provide information about the number of pixels wide and high of your image. This information is optional, but it's worth providing it&lt;/p&gt;

&lt;p&gt;All these data will produce the following result (&lt;em&gt;this is a site project I've worked on recently&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hzgjur7e1sh3hct3igs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3hzgjur7e1sh3hct3igs.png" alt="An overview of the landing page of the designo website on WhatsApp when sending the link https://ike-designo-website.vercel.app" width="754" height="353"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To sum up, thanks to the opengraph protocol, we can give an overview of our content when we share our link on social medias. So now it's your turn to make your website look good on social medias.&lt;/p&gt;

&lt;p&gt;Thank you for reading, and any feedback is appreciated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Further reading&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://ogp.me" rel="noopener noreferrer"&gt;The opengraph protocol documentation&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>html</category>
      <category>seo</category>
      <category>website</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Brash one-liner to rename files Matching a pattern</title>
      <dc:creator>Ndimah Tchougoua</dc:creator>
      <pubDate>Fri, 02 Sep 2022 14:20:33 +0000</pubDate>
      <link>https://dev.to/jospinevans/brash-one-liner-to-rename-files-matching-a-pattern-3890</link>
      <guid>https://dev.to/jospinevans/brash-one-liner-to-rename-files-matching-a-pattern-3890</guid>
      <description>&lt;p&gt;One day I found myself in the situation where I needed to rename a series of files by removing a pattern on Each file. Guess what ? They were around 150 PDF files, so no way for me to do it manually.&lt;br&gt;
While searching on the internet, I found the term brash one-liner. &lt;strong&gt;It's a kind of long series of bash command combined in order to achieve a goal.&lt;/strong&gt;&lt;br&gt;
So I had to define mine in order to solve my problem. After a lot of experimentations, I came out with this brash one-liner :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'some-pattern'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; find &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-iname&lt;/span&gt; &lt;span class="s2"&gt;"*&lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="s2"&gt;*"&lt;/span&gt; | xargs &lt;span class="nt"&gt;-0&lt;/span&gt; &lt;span class="nt"&gt;-I&lt;/span&gt;&lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"file=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;{}&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;; mv &lt;/span&gt;&lt;span class="se"&gt;\"\$&lt;/span&gt;&lt;span class="s2"&gt;file&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\"\$&lt;/span&gt;&lt;span class="s2"&gt;{file//&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;/} "&lt;/span&gt; | bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks cryptic? Yeah, you're right, it is a little bit.&lt;br&gt;
So let me explain my approach to solve this problem.&lt;br&gt;
My approach to solve it uses 4 steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Pattern Definition&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Files Searching&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Text-Based bash command definition&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Command Execution&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Pattern Definition
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   &lt;span class="nv"&gt;pattern&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'some-pattern'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;here I just keep my pattern in a variable to avoid repeating myself &lt;em&gt;(I'm a DRY guy)&lt;/em&gt;&lt;br&gt;
&lt;em&gt;Note: I use the single quotes here because my pattern don't need to be evaluated before execution by the shell&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Files Searching
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  find &lt;span class="nt"&gt;-type&lt;/span&gt; f &lt;span class="nt"&gt;-iname&lt;/span&gt; &lt;span class="s2"&gt;"*&lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="s2"&gt;*"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I search for all files matching my pattern, ignoring the case with the help of the find command &lt;br&gt;
&lt;strong&gt;Note: we have to ensure ourselves that the pattern only matches the files we are looking to rename, otherwise it's going to rename the unexpected files. Please try to run this separately first to verify it.&lt;/strong&gt; &lt;/p&gt;

&lt;h1&gt;
  
  
  Text-Based bash command definition
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   xargs &lt;span class="nt"&gt;-0&lt;/span&gt; &lt;span class="nt"&gt;-I&lt;/span&gt;&lt;span class="o"&gt;{}&lt;/span&gt; &lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"file=&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;{}&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;; mv &lt;/span&gt;&lt;span class="se"&gt;\"\$&lt;/span&gt;&lt;span class="s2"&gt;file&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="se"&gt;\"\$&lt;/span&gt;&lt;span class="s2"&gt;{file//&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="nv"&gt;$pattern&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;/} "&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the magic operate, so we have to use a little trick to define the new name of each file.&lt;br&gt;
Here, my approach is to use &lt;strong&gt;shell expansion&lt;/strong&gt;.&lt;br&gt;
I've chosen to use the xargs command because it will execute the same command for each file found.&lt;br&gt;
The &lt;strong&gt;-0&lt;/strong&gt; argument here is to end each entry with the null character.&lt;br&gt;
The &lt;strong&gt;-I{}&lt;/strong&gt; is to tell xargs to replace all occurrences of &lt;strong&gt;{}&lt;/strong&gt; in the command with the entry.&lt;br&gt;
Inside my text-based command, I define a so that I will substitute all occurrences of &lt;em&gt;$pattern&lt;/em&gt; with a blank character.&lt;br&gt;
I've used the escape character to avoid the &lt;strong&gt;file&lt;/strong&gt; variable to be evaluated before execution.&lt;br&gt;
&lt;em&gt;Note: I could've used the&lt;/em&gt; &lt;strong&gt;sed&lt;/strong&gt; &lt;em&gt;command, but I wanted to reduce typing. By the way, this is the place where we can build any complex command, so it's up to your imagination.&lt;/em&gt;&lt;/p&gt;

&lt;h1&gt;
  
  
  Command Execution
&lt;/h1&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;   bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is where the bash come to the game and execute the text-based bash command that it receives.&lt;/p&gt;

&lt;p&gt;At the end of the day, I hope that this little tutorial will help you if you face this type of problem. Feel free to send me any suggestion or improvement in the discussion, thank you for your attention.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>bash</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
