<?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: keogami</title>
    <description>The latest articles on DEV Community by keogami (@keogami).</description>
    <link>https://dev.to/keogami</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%2F469073%2F42405996-94c4-465f-a2c4-e0900bd4034d.jpg</url>
      <title>DEV Community: keogami</title>
      <link>https://dev.to/keogami</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/keogami"/>
    <language>en</language>
    <item>
      <title>Cascade-esq Notation in JavaScript?</title>
      <dc:creator>keogami</dc:creator>
      <pubDate>Wed, 16 Sep 2020 21:06:23 +0000</pubDate>
      <link>https://dev.to/keogami/cascade-esq-notation-in-javascript-2agp</link>
      <guid>https://dev.to/keogami/cascade-esq-notation-in-javascript-2agp</guid>
      <description>&lt;p&gt;A little while ago I was going through the &lt;a href="https://dart.dev/guides/language/language-tour"&gt;Dart's Language Tour&lt;/a&gt; and found this cool notation they got. They call it the &lt;code&gt;Cascade Notation&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's a code example from the tour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;querySelector('#confirm') // Get an object.
  ..text = 'Confirm' // Use its members.
  ..classes.add('important')
  ..onClick.listen((e) =&amp;gt; window.alert('Confirmed!'));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Which translates to:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var button = querySelector('#confirm');
button.text = 'Confirm';
button.classes.add('important');
button.onClick.listen((e) =&amp;gt; window.alert('Confirmed!'));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, ain't that pretty? 😆&lt;/p&gt;

&lt;p&gt;JavaScript's influence on Dart's syntax is quite evident. Its almost as if Dart is Javascript with nutella smeared all over it.&lt;br&gt;
And as such, Dart has some feature over JavaScript which have got me drooling! 🤤&lt;/p&gt;
&lt;h2&gt;
  
  
  My Problem
&lt;/h2&gt;

&lt;p&gt;Its pretty common in JavaScript to programmatically build elements and populate them in some &lt;code&gt;parent&lt;/code&gt; element. Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let anchor = document.createElement('a');
anchor.href = data.link;
anchor.innerText = data.caption
anchor.classList.add(data.type)
parent.appendChild(anchor)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It has always bugged me to have to type that &lt;code&gt;anchor.someProp&lt;/code&gt;. I wish we had something like that &lt;code&gt;Cascade Notation&lt;/code&gt;, but alas, we don't. And &lt;em&gt;that's&lt;/em&gt; my problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Solution - Chain 'em all!
&lt;/h2&gt;

&lt;p&gt;I will simply create a class that chains and have a &lt;code&gt;.build()&lt;/code&gt; method that returns the element. Something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;parent.appendChild(
  new Link()            // Returns a chainable builder
    .to(data.link)
    .text(data.caption)
    .type(data.type)
    .build()            // Finally, returns anchor element
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Now, how to implement?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Method_chaining"&gt;Chainable objects&lt;/a&gt; have been around for years and are pretty well known. And here's how I like to construct them.&lt;/p&gt;

&lt;p&gt;First, I create a helping curry-ed function(check &lt;a href="https://medium.com/javascript-scene/curry-and-function-composition-2c208d774983"&gt;Currying&lt;/a&gt;):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let chainer = (ctx) =&amp;gt; (mutator) =&amp;gt; (...args) =&amp;gt; {
  mutator.apply(ctx, args)
  return ctx
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here,&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;ctx&lt;/code&gt; is the object that is chained upon&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;mutator&lt;/code&gt; is a function that is used to actually make changes to the ctx&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;...args&lt;/code&gt; are the arguments provided to the mutator&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Then, I create the builder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let Link = function () {
  let linker = chainer(this)

  let config = {
    to: "",
    text: "",
    type: "",
  }

  this.config = config
  this.to = linker((link) =&amp;gt; {
    config.to = link
  })
  this.text = linker((text) =&amp;gt; {
    config.text = text
  })
  this.type = linker((type) =&amp;gt; {
    config.type = type
  })

  this.build = () =&amp;gt; {
    let a = document.createElement("a")
    a.href = config.to
    a.innerText = config.text
    !!config.type &amp;amp;&amp;amp; a.classList.add(config.type)
    return a
  }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Voila! we are done （︶^︶）&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;We can use them ubiquitously, like literally smearing nutella. 😋&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.body.appendChild(
  new Link()
    .to("http://localhost")
    .text("home sweet home")
    .build()
)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Wanna try?
&lt;/h2&gt;

&lt;p&gt;&lt;iframe src="https://jsfiddle.net/keogami/hufq9dcz/4//embedded/result,js//dark" width="100%" height="600"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h2&gt;
  
  
  End Note
&lt;/h2&gt;

&lt;p&gt;Yo reader! This was my way of trying to recreate the cascading syntax with JavaScript, if you got a better way... Share it with everyone!&lt;/p&gt;

&lt;p&gt;Cheers~&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>dart</category>
      <category>functional</category>
      <category>healthydebate</category>
    </item>
  </channel>
</rss>
