<?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: Prateek</title>
    <description>The latest articles on DEV Community by Prateek (@prateek13727).</description>
    <link>https://dev.to/prateek13727</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%2F197272%2F8cef8499-8bcd-4c09-80f6-1add0000a919.jpeg</url>
      <title>DEV Community: Prateek</title>
      <link>https://dev.to/prateek13727</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prateek13727"/>
    <language>en</language>
    <item>
      <title>Date Interface for Frontend</title>
      <dc:creator>Prateek</dc:creator>
      <pubDate>Fri, 28 May 2021 09:47:31 +0000</pubDate>
      <link>https://dev.to/prateek13727/date-interface-for-frontend-1i0c</link>
      <guid>https://dev.to/prateek13727/date-interface-for-frontend-1i0c</guid>
      <description>&lt;p&gt;Hello fellas,&lt;/p&gt;

&lt;p&gt;The aim of the writeup here is to validate the thought process behind creating a custom date interface to be used across multiple frontnend projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  What does an application of date library on frontend looks like ?
&lt;/h2&gt;

&lt;p&gt;An example of a date utility on frontend would be, say&lt;br&gt;
My API gives me date as a string in &lt;code&gt;DD/MM/YYYY&lt;/code&gt;in response and I want to use it for validating a Create operation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (date &amp;lt; date.now() + 180 days) 
 allow create
else
 don't
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  How do I achieve this ?
&lt;/h4&gt;

&lt;p&gt;I would convert the date string into a date object. Once I have the date object, I can add 180 days to it and comapre this new date object with the current date.&lt;/p&gt;

&lt;p&gt;Like this we have numerous applications of Date on frontend&lt;/p&gt;

&lt;h2&gt;
  
  
  What is date interface and why it makes sense ?
&lt;/h2&gt;

&lt;p&gt;Date Interface is the face of Date library for all my consumer applications. The consumer (the project) say wants to convert date string into a Date object ? Now the developer, does not need to think what should be the type of date object or which date library I should use, etc etc.&lt;/p&gt;

&lt;p&gt;Instead the Date Interface exposes the below funtion&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let make: (~date: string, ~format: string) =&amp;gt; Date.t
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It takes a date string and a valid date format (we can further optimize the &lt;code&gt;format&lt;/code&gt; argument to accept only valid formats) and returns a type &lt;code&gt;Date.t&lt;/code&gt; Now the developer can use this type to interact with other functions exposed by this date interface, like a toString function to convert date object to a specific format for display.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let toString: (~date: Date.t, ~format: string) =&amp;gt; string

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Am I rewriting a date library here ?
&lt;/h2&gt;

&lt;p&gt;The answer is, I am rewiting only the interface. Underlying the interface I would be using a library like Moment or DateFns to perform my operations. &lt;/p&gt;

&lt;h4&gt;
  
  
  Why?
&lt;/h4&gt;

&lt;p&gt;The consumer will be unware of the implementation details.&lt;br&gt;
They don;t need to worry about the Date library being used and skim through the documentatiin&lt;/p&gt;

&lt;p&gt;I can easily plug and play libraries without affecting my end consumer. Every function in my interface is a pure function, with defined input and outputs. My custom type &lt;code&gt;Date.t&lt;/code&gt; helps me achieve this. Until that contract holds, I can try out a lot of stuff inside. Sounds cool :), right?&lt;/p&gt;

&lt;p&gt;Under the hood we may take best of both worlds, and use two different date libraries to achive two different things. Not &lt;br&gt;
a very ideal scenario but yes. Quite helpful when you are migrating libraires. Some functions are on old libraries and the other are on the new one.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
