<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Casey Morris on Medium]]></title>
        <description><![CDATA[Stories by Casey Morris on Medium]]></description>
        <link>https://medium.com/@caseymorrisus?source=rss-c194ff39a976------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tMUyuM6DVeV420uP4J5TNA.jpeg</url>
            <title>Stories by Casey Morris on Medium</title>
            <link>https://medium.com/@caseymorrisus?source=rss-c194ff39a976------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 14 Nov 2026 08:14:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@caseymorrisus/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[We All Need Automated Tests (Rant.js)]]></title>
            <link>https://medium.com/dailyjs/we-all-need-automated-tests-rant-js-8b7db0a127c7?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/8b7db0a127c7</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Mon, 22 Oct 2018 10:45:28 GMT</pubDate>
            <atom:updated>2019-09-20T15:42:13.259Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BsNwPdWkr_HzT7n4HwPLTQ.png" /></figure><blockquote>Welcome to Rant.js, a new series I’m starting based on short, mostly opinions, on anything that interests me in the JavaScript world. These articles, while not entirely factual, may provide an interesting look into the day-to-day thought process of a JavaScript developer. These rants mostly stem from discussions with peers on Slack or GitHub. You can find the previous article here: <a href="https://medium.com/@caseymorrisus/rant-js-undefined-vs-null-7f90f203063b">JavaScript — Undefined vs. Null</a></blockquote><h3>My Opinion On Automated Tests</h3><p>I use automated testing to some extent in almost every single one of my projects. This includes personal, freelance/consulting, and full-time projects. I <em>never</em> strive for 100% coverage. I believe testing the least amount of things possible is the best approach. Tests must be maintained alongside your code, they’re an investment. I’ve seen many testing suites break down and fail because they were too verbose, annoying to update, or didn’t stop production bugs in critical areas due to a focus on total coverage.</p><h3>How Do You Know What To Test?</h3><p>It’s really hard to give specific advice without knowing specific business requirements or what a codebase looks like. Thankfully, there are a few common areas to check for testing when looking at a codebase.</p><h4>Business Critical</h4><p>The most important areas to test are those that are business-critical. If you’re working on an e-commerce site, this would be anything surround the checkout, cart, and adding/removing products. If any of these areas were to go down, you’ll definitely be getting a PagerDuty request to fix a P1 issue. <em>Say goodbye to relaxing at home on the weekend!</em></p><h4>High Error Occurrence</h4><p>Another way to add new testing coverage is to look into existing error reporting data. I’m generally able to trace a large amount of user errors to a single feature or chunk of a codebase. These features/chunks can have tests added for expected behavior. This is easily followed by bug fixes &amp; refactors to stop these errors from being thrown in production.</p><h4>Large Surface Area</h4><p>One last thing that may warrant test coverage is a function or component that has many uses or has a large surface area. If something were to break in these functions/components there will be many issues downstream. This may also be a sign that abstraction is needed. It may be time to break logic down into smaller, more testable chunks.</p><h3>Slightly Specific Examples</h3><p>Alright, this is all a bit abstract, I think a few examples would help. Within a React/Redux eCommerce app I’d start with the following:</p><ol><li>Test Redux Actions for “Checkout Process” (Enzyme)</li><li>Test User Flow for “Checkout Process” (Cypress)</li><li>Test Redux Actions for “Cart Actions — Product Add/Remove” (Enzyme)</li><li>Test User Flow for “Cart Actions — Product Add/Remove” (Cypress)</li><li>Test Redux Actions for “User Login” (Enzyme) [has a different priority if this is required for purchase, this assumes guest checkout is possible]</li><li>Test User Flow for “User Login” (Cypress)</li></ol><p>This would prevent the most direct customer facing issues that would impact them from making a purchase. Any new tests can be added using the above criteria.</p><h3>Is This All Really Worth It?</h3><p>Sometimes it may seem like adding and maintaining these tests is a lot of work and is all for nothing. You still see bugs come through in production regardless of tests, there’s always issues to fix. It may seem like your tests don’t even work at times. I know it can also be exhausting having to fight for testing time in some organizations, it can seem like a lost cause in many cases.</p><p>Let me explain a recent situation that <em>really</em> sold me on automated testing. I was already a staunch advocate, I didn’t think it was possible to sell me the concept any further.</p><h3>Why Is Everything On Fire? 🔥</h3><p>Seemingly out of nowhere we started to see a lot of new high occurrence errors. We recently pushed out an update that we suspected had a chance to cause issues due to the amount of areas this new feature touched. Multiple P1 and P2 issues were being filed daily, we were playing whack-a-mole trying to keep up and fix them.</p><h3>Where Did My Weekends Go?</h3><p>This went on for 2 weeks and didn’t seem like it was slowing down any time soon. I unfortunately was on-call during these 2 weeks, I suffered the loss of many nights and my weekends. I was also unable to accomplish any normal day-to-day work as all of my time was taken up dealing with priority issues within our production environment.</p><p>Why was this happening? We have tons of automated test coverage that runs automatically within TravisCI, they were all passing, <em>or so we though</em>t…</p><h3>Tests Weren’t Even Running</h3><p>During a checkup of our CI environment (we’ve been making many improvements to our testing pipeline) it was discovered that due to a misconfiguration, Travis was only running 20% of our tests but was returning that they were all passing. Once re-configured it was revealed that many of our tests had been failing this entire time.</p><h3>Back To Normal</h3><p><em>Wow</em>. After this was discovered, we were able to quickly fix issues based on failing tests. Our error rates seemed to drop off completely, we were back to normal rates. No more massive influx of P1/P2 issues, no more lost nights and weekends.</p><p>This 2 week period while terrible to experience, validated to everyone the benefits of testing. Especially to those outside of the development team. The same people that were complaining about features not being released quickly were now demanding that testing be extended at the cost of frequent feature releases. Testing is now a concrete standard within the organization.</p><h3>What We Learned</h3><p>The issue we experienced also showed the importance of doing regular maintenance and checkups of your testing / CI system. It also shows the importance of doing local testing for large features rather than depending on automated testing systems. Things can always go wrong, it doesn’t hurt to double check before major deployments.</p><h3>Feedback? Words of encouragement? 🎉</h3><p>I’m always looking to improve my articles, if you would like to leave feedback (I would love it if you did!) you can find the Google Form <a href="https://goo.gl/forms/HPH8ujAm60Jd8Qq13">here</a>. It’s very short, I promise!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8b7db0a127c7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/we-all-need-automated-tests-rant-js-8b7db0a127c7">We All Need Automated Tests (Rant.js)</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Rant.js — undefined vs null]]></title>
            <link>https://medium.com/dailyjs/rant-js-undefined-vs-null-7f90f203063b?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f90f203063b</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Thu, 18 Oct 2018 09:01:02 GMT</pubDate>
            <atom:updated>2018-10-25T12:37:14.749Z</atom:updated>
            <content:encoded><![CDATA[<h3>JavaScript — Undefined vs. Null (Rant.js)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0uKJzCv2fgnwEafhXG1nvw.png" /></figure><blockquote>Welcome to Rant.js, a new series I’m starting based on short, mostly opinions, on anything that interests me in the JavaScript world. These articles, while not entirely factual, may provide an interesting look into the day-to-day thought process of a JavaScript developer. These rants mostly stem from discussions with peers on Slack or GitHub.</blockquote><h3>Why Does This Even Matter?</h3><p>Do we really need to talk about this, does it even matter? They’re both falsy values that behave extremely similarly, isn’t this a little pedantic? <em>I don’t think so.</em></p><h3>Declared Vs. Undeclared</h3><p>To start, let’s take a moment to talk about declared vs undeclared values as they’re a little tricky within JavaScript. To make things confusing, undefined can be both a declared and an undeclared value. In the following example we show 2 variables with undefined values, one is declared and the other is undeclared. The undeclared variable will throw a ReferenceError stating that the variable is not defined, the other simply returns undefined .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/181ee8ba4531d00b3c72ca65307d5d50/href">https://medium.com/media/181ee8ba4531d00b3c72ca65307d5d50/href</a></iframe><p>While the undeclared variable throws an error, it still holds the same type as the declared variable. See the following example using the above variables:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51e830d43b2835507d91dbc33ee93901/href">https://medium.com/media/51e830d43b2835507d91dbc33ee93901/href</a></iframe><h3>Well That’s Confusing…</h3><p>Is the type situation better for a null value, is that why I prefer it? Not exactly, it has its own issues to deal with… <em>great! </em>Consider the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/109066310a8f3d05b95495f9bfb5e189/href">https://medium.com/media/109066310a8f3d05b95495f9bfb5e189/href</a></iframe><p>That’s odd, when you check the type of null it returns that it is an object. Does this mean null is an object and is not one of JavaScript’s <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures">six built-in primitives types</a>? Wrong! It’s a primitive type and is not an object, this is a near <a href="http://2ality.com/2013/10/typeof-null.html">20 year old bug</a> with JavaScript that most likely won’t ever be fixed due to the harm it would cause to programs that have adjusted for it.</p><h3>How Do We Type Check For Null?</h3><p>The situation above makes things a little tricky when you want to explicitly check for a null type. Thankfully since null isn’t really an object, it’s the only “object” that is a falsy value, empty objects are truthy. We can use this check along with a type check to check for a its type:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8012c8765a97462a3cfa622ec04e442b/href">https://medium.com/media/8012c8765a97462a3cfa622ec04e442b/href</a></iframe><p>While a little more to write, this is the only way we can explicitly check for a declared empty value. As shown before, the undefined type can be returned on declared and undeclared values. If you’re concerned about having to write the extra conditional, create a simple null checking function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/13b06e549300d4fb78f3d3ff96276d5a/href">https://medium.com/media/13b06e549300d4fb78f3d3ff96276d5a/href</a></iframe><h3>Isn’t This An Opinion Piece?</h3><p>Yes, but I felt that a little information was needed to drive the point home, back to the original topic.</p><h3>JavaScript Already Decided For Us</h3><p>We have 2 different types that can represent empty values, which should we choose? JS already chose that for us, most issues are caused due to an object or array that evaluates to undefined . This ends up throwing an error when attempting to access object properties or calling object/array methods. See below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a4d94bb8f576428cb2e90dabc909e587/href">https://medium.com/media/a4d94bb8f576428cb2e90dabc909e587/href</a></iframe><p>With every added level of depth, the likelihood that an error could be thrown is increased. This is because an error will be thrown if any parent property in the chain evaluates to a non-object.</p><h3>Intentionally Empty</h3><p>As the above scenario happens fairly frequently, we need a way to differentiate empty values caused by errors and empty values that are intentionally empty. This is where null comes into play.</p><p>Consider the following example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b16f97396f4909dce100966d7aaefa3/href">https://medium.com/media/2b16f97396f4909dce100966d7aaefa3/href</a></iframe><p>This may look innocent and easy to remember, but sometimes objects are imported within multiple files. On top of this, they may be passed through multiple functions. Original values may not be immediately obvious, especially to someone unfamiliar with your codebase (freelancers &amp; consultants).</p><p>It would be easy to do the following and assume something broke during data processing rather than the value was intentionally assigned as undefined:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/869b9a7c7785b4c3ac8499d0268584b7/href">https://medium.com/media/869b9a7c7785b4c3ac8499d0268584b7/href</a></iframe><p>If I saw the following output, I would assume this empty null value was intentional and would not waste my time investigating a possible issue:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b6bf618e6987e15a327e28cca7482fb6/href">https://medium.com/media/b6bf618e6987e15a327e28cca7482fb6/href</a></iframe><h3>A Better Alternative</h3><p>When possible<em> (see Polymorphic Values)</em>, it is better to represent empty values as an empty version of its expected type. This prevents array, object, and string methods from throwing an error when they’re presented with a null or undefined value.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2a3d17511bbff5a43abfa4ecdaee816e/href">https://medium.com/media/2a3d17511bbff5a43abfa4ecdaee816e/href</a></iframe><p>Compared to assigning the same variables with null values:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6175f4a42bf9de7679c96633fa5d18d4/href">https://medium.com/media/6175f4a42bf9de7679c96633fa5d18d4/href</a></iframe><h3>Polymorphic Values</h3><p>The above technique works great in most situations, but sometimes we don’t have that much fine grained control of how our values are assigned. For example, a variable’s value could be set directly from the response from an API. This API could return different types depending on parameters passed or other situations outside of the developer’s control. In this situation, it’s best to set an initial value of null and null check downstream.</p><h3>EXTRA: Safely Access Deep Object Properties</h3><p>A common method to safely access object properties is to use a ternary. This unfortunately becomes a little cumbersome when accessing properties that are multiple levels deep.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72bdc3e8570063390172376046f7a2bb/href">https://medium.com/media/72bdc3e8570063390172376046f7a2bb/href</a></iframe><p>We can define a function to safely access object properties. If any property in the chain can not properly evaluate, it will return null. This first argument is the object we’re attempting to access properties on. The second argument is a string of props, split by periods. We’ll first define an isUndefined method to match our isNull method from earlier.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4cae4d77c994870acb48d722f3a64acb/href">https://medium.com/media/4cae4d77c994870acb48d722f3a64acb/href</a></iframe><p>Now we define our get function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a65a575d4c9ec3f4e119741a043e3291/href">https://medium.com/media/a65a575d4c9ec3f4e119741a043e3291/href</a></iframe><p>The above example canSafelyAccessNestedProperty can be converted using our get function like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bea2ee15dfc231ff5011b9447c4a4721/href">https://medium.com/media/bea2ee15dfc231ff5011b9447c4a4721/href</a></iframe><p>We can save a getter to a specific object by only supplying the first argument. This is useful when you need to access an object multiple times with different property chains.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e1b475cf47ef29fcfa75b0df00d1079/href">https://medium.com/media/4e1b475cf47ef29fcfa75b0df00d1079/href</a></iframe><h3>Feedback? Words of encouragement? 🎉</h3><p>I’m glad you made it, hopefully you learned a few patterns or pieces of information to use in the future. I’m always looking to improve my articles, if you would like to leave feedback (I would love it if you did!) you can find the Google Form <a href="https://goo.gl/forms/HPH8ujAm60Jd8Qq13">here</a>. It’s very short, I promise!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f90f203063b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/rant-js-undefined-vs-null-7f90f203063b">Rant.js — undefined vs null</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functional JS with ES6 — Church Booleans, Conditionals, and Operators]]></title>
            <link>https://medium.com/dailyjs/functional-js-with-es6-church-booleans-conditionals-and-operators-c0461a9e0a66?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/c0461a9e0a66</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Sat, 13 Oct 2018 09:01:02 GMT</pubDate>
            <atom:updated>2018-10-26T13:18:25.252Z</atom:updated>
            <content:encoded><![CDATA[<h3>Functional JS with ES6 — Booleans, Conditionals, and Operators</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fN615FSFd3xcanqVQcZL9Q.png" /></figure><blockquote>I’ll start this article with a warning, I mention the use of Lambda Calculus, but do not let that scare you! I promise, no knowledge of λ-Calculus is required to understand or use the main concepts in this article! Phew!</blockquote><p>In my last <a href="https://medium.com/dailyjs/functional-js-with-es6-recursive-patterns-b7d0813ef9e3">Functional JS article</a>, we went over recursive patterns that allow you to operate/iterate over array values. This time, we’re going to get a bit more abstract to hopefully explain some of the bare fundamentals of functional programming.</p><h3>A moment on Currying</h3><p>You’ll notice that we never define a function with more than 1 argument. We return a new function for every additional argument needed. This is a technique used very commonly in functional programming that has its roots in Lambda Calculus. This has multiple benefits, but is used mainly to simplify partial application of functions.</p><h3><strong>What are we doing?</strong></h3><p>Glad you asked! We’re going to re-create: true, false, ||(or), &amp;&amp;(and), !(not), ==(equals), and !=(not equals). As a bonus, we’re going to use some magical Lambda Calculus to make our functions much more efficient!</p><h3>Let’s get started!</h3><p>The first functions we need to define are our booleans: true and false. How we define these functions makes more sense once they’re used with the conditional function we soon define. The way we represent booleans as functions is a concept known as Church Booleans.</p><h3>TRUE (true):</h3><p>No, I’m not trying to yell, I’m using this naming convention because true in all lowercase is a reserved word and can’t be used to name our function. This function takes two arguments and will always return the first argument. In comparison, the FALSE function does the same, but always returns the second argument. These functions are also known as selectFirst and selectSecond in other languages.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3f5347e1e1818f064a208eaeebd88fee/href">https://medium.com/media/3f5347e1e1818f064a208eaeebd88fee/href</a></iframe><h3>FALSE (false):</h3><p>Almost identical to the TRUE function, but returns the second argument rather than the first.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/88b88b559e3b846dd3bc82d38791045a/href">https://medium.com/media/88b88b559e3b846dd3bc82d38791045a/href</a></iframe><h3>Conditional (cond):</h3><p>The next function we need to define is our function that will handle conditional logic. Normally we’d use an if/else statement or a ternary to do this, but we want everything to be a function. It’s functions all the way down, this comes in handy later on.</p><p><strong>If you break down a conditional in JavaScript, you end up with 3 parts:</strong></p><ol><li>Conditional Statement</li><li>Expression when true</li><li>Expression when false</li></ol><p>To mimic this, we will create a function that takes 3 arguments and returns the conditional function with both expressions applied. If you notice, I place the conditional last rather than first. This is a functional programming convention that helps when creating partially applied functions.</p><p>If you remember how we defined our booleans, they are both functions that when called with 2 arguments, return one of these arguments. This is exactly how our conditional function is going to work. The conditional function (TRUE or FALSE) passed in is called with 2 arguments, our expressions when true and false.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a164ace1b82df4ec777becdc5ea02603/href">https://medium.com/media/a164ace1b82df4ec777becdc5ea02603/href</a></iframe><h3>Not (!)</h3><p>This is the first logical operator we will define. It takes a single argument: x. If x is TRUE it returns FALSE, if x is FALSE it returns TRUE. We make use of the previously defined cond function to handle the conditional logic. Within the conditional: if x is TRUE we return FALSE otherwise we return TRUE .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/de9d05f389ef65f5fdc169d324fdec84/href">https://medium.com/media/de9d05f389ef65f5fdc169d324fdec84/href</a></iframe><h3>Or (||)</h3><p>This is the first logical operator we will define. It takes 2 arguments: x and y. If either x or y is TRUE, we will return TRUE, otherwise we return FALSE. Again, we use the cond function to handle this. Within the conditional: if x is TRUE we return TRUE otherwise we return y .</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e02f103dbe410fbeedc146f8ec0ca196/href">https://medium.com/media/e02f103dbe410fbeedc146f8ec0ca196/href</a></iframe><h3>And (&amp;&amp;)</h3><p>This function takes 2 arguments: x and y. Both x and y must be TRUE for this to return TRUE, otherwise it will return FALSE. We make use of the cond function again, not much is different. Within the conditional: if x is TRUE we return y otherwise we return FALSE.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bc7a19062bcc0d1433ae937f4a47a361/href">https://medium.com/media/bc7a19062bcc0d1433ae937f4a47a361/href</a></iframe><h3>Equal (==)</h3><p>This function takes 2 arguments: x and y. Both x and y must be the same value for this to return TRUE otherwise it returns FALSE. We need to use the cond function and the not function to handle this one! Within the conditional: if x is TRUE we return y otherwise we return not(y)to only return TRUE when y is also FALSE.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c4c77370f6c5f0eea9d26746b7eb9e22/href">https://medium.com/media/c4c77370f6c5f0eea9d26746b7eb9e22/href</a></iframe><h3>Not Equal (!=)</h3><p>This function takes 2 arguments: x and y. x and y must not be the same value for this to return TRUE, otherwise FALSE is returned.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fc0d284700241bc9ae878d2ffac30c4/href">https://medium.com/media/9fc0d284700241bc9ae878d2ffac30c4/href</a></iframe><h3>EXTRA: <strong>β (Beta) Reductions</strong> through λ-Calculus</h3><blockquote>Disclaimer: do not feel the need to understand this at all as it is not a requirement to understand and use functional concepts. However, I think it’s an interesting topic to learn about and helps demonstrate the power of functional programming through math. I will gloss over topics as I am not the best resource to learn λ-Calculus, it is not the focus of this article, just an added bonus. These optimizations are handled automatically with most statically compiled functional languages.</blockquote><h4>And (&amp;&amp;) Reduction</h4><p>In the following example, we use β reduction to remove the use of our cond function entirely from our and function. Our and function is now composed entirely with booleans and is mathematically equivalent!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b884450a24b19b8b14f99b4b02a80c4f/href">https://medium.com/media/b884450a24b19b8b14f99b4b02a80c4f/href</a></iframe><h4>Or (||) Reduction</h4><p>Again, we use reduction to remove the use of our cond function entirely from our or function. Like before, our or function is now composed entirely with booleans and is mathematically equivalent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6fc6ad3b999bf788af105de1c349851b/href">https://medium.com/media/6fc6ad3b999bf788af105de1c349851b/href</a></iframe><h4>Equal (==) Reduction</h4><p>Just like before, we use reduction to remove the use of our cond function entirely from our equal function. Our equal function is now composed entirely with booleans and is mathematically equivalent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e529d8edbee9996d3131522013117acd/href">https://medium.com/media/e529d8edbee9996d3131522013117acd/href</a></iframe><h4>Not Equal(!=) Reduction</h4><p>For the last time, we remove the use of our cond function entirely from our notEqual function. Our notEqual function is now composed entirely with booleans and is mathematically equivalent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e19e281adb4cf596eff700e60184e7c7/href">https://medium.com/media/e19e281adb4cf596eff700e60184e7c7/href</a></iframe><h3>Wrapping Up</h3><p>I hope this helped demonstrate the power and flexibility of functions. This logic can be shared and re-written in any language that has first-class functions!</p><p>The functions and concepts used in the article will be expanded in a later article where we use Church numerals to represent natural numbers as pure functions. We will even implement arithmetic and comparison operators for these numbers! 🚀</p><h3>Feedback? Words of encouragement? 🎉</h3><p>I’m glad you made it, hopefully you learned a few patterns or pieces of information to use in the future. I’m always looking to improve my articles, if you would like to leave feedback (I would love it if you did!) you can find the Google Form <a href="https://goo.gl/forms/HPH8ujAm60Jd8Qq13">here</a>. It’s very short, I promise!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c0461a9e0a66" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/functional-js-with-es6-church-booleans-conditionals-and-operators-c0461a9e0a66">Functional JS with ES6 — Church Booleans, Conditionals, and Operators</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[React — Composing Higher-Order Components (HOCs)]]></title>
            <link>https://medium.com/dailyjs/react-composing-higher-order-components-hocs-3a5288e78f55?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/3a5288e78f55</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[functional-programming]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Mon, 11 Dec 2017 15:24:17 GMT</pubDate>
            <atom:updated>2018-10-21T21:28:52.311Z</atom:updated>
            <content:encoded><![CDATA[<h3>React — Composing Higher-Order Components (HOCs)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*k_AUYG--aM1kFVWIsWeadA.png" /></figure><h3>Intro</h3><p>To put it simply, a higher-order component is a function, that takes a component and returns a new component. I like to think of them as parameterized components. Many times I find myself creating several components with very similar logic, with only 1 or 2 changes. Once I find a use case like this, it’s very simple to abstract the shared logic, and put the logic that changes into parameters.</p><p>You can read more about HOCs <a href="https://reactjs.org/docs/higher-order-components.html">here</a>, in the official React docs. Since components are just functions and HOCs are just functions that return other functions, we can use functional concepts to chain them using utilities methods such as compose, which is provided by many libraries <em>(it&#39;s included in Redux!)</em>.</p><p>I’m going to talk about a few common use cases, then go over their implementation, and a few ways they can be composed together to make even more powerful HOCs!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4877k4Hq9dPdtmvg9hnGFA.jpeg" /></figure><h3>Follow Along (Example App)</h3><p>You can following along with a simple React app I create to demonstrate the following concepts and use cases. It can be used in browser or cloned from the repository.</p><blockquote><strong>Live sandbox:</strong> <a href="https://codesandbox.io/s/github/caseymorrisus/composing-hocs">codesandbox.io/s/github/caseymorrisus/composing-hocs</a></blockquote><blockquote><strong>GitHub repo:</strong> <a href="https://github.com/caseymorrisus/composing-hocs">github.com/caseymorrisus/composing-hocs</a></blockquote><h3>Use Cases</h3><p>Now that we know a little about higher-order components, we can go over a few common use cases.</p><h3>Loader</h3><p>It’s common to pull data from an API and display a “spinner” or message until the data has been fetched. You may have a standard Loading component which has a prop message for displaying a loading message specific to your current application.</p><h3>Error</h3><p>Almost identical to the Loader HOC, but instead displays an Error component and expects an error prop. Can be supplied a custom Error component.</p><h3>Default</h3><p>It’s good practice to display some type of message if data was successfully fetched, but none of the given type were returned from the data source. For example, instead of display a blank list of posts, if no posts have been created, you could have a DefaultPost component which could prompt the user to create a post.</p><h3>Data</h3><p>Container components frequently fetch data and pass that data as a prop to a child component. Generally the only thing that changes between these container components is the API endpoint. We can also use HOCs to easily pass in mock data for development purposes.</p><h3>MockData</h3><p>Used for development purposes to quickly prototype components before an API endpoint has been created or implemented within your React project. Data and a delay (to mock async data fetching) are supplied to the HOC. Data is passed down to the wrapped component.</p><h3>Logger</h3><p>Easily log all prop changes (on render) for a given component, helpful for quick debugging purposes.</p><h3>Props</h3><p>Sometimes you need to inject props into a component, this is especially useful when using a branch HOC and only want a single branch to receive specific props.</p><h3>Timeouts</h3><p>Adding a timeout that controls state within React will throw an error if a component unmounts before the function supplied to the timeout has fired. We can create an HOC that handles and shares functionality to easily add and clear timeouts.</p><h3>Container</h3><p>It’s fairly common to have a container component, which fetches data, and renders a list of components using that data. We can combine this with other HOCs such as: hasloader ,hasError, andhasDefault to extend functionality further.</p><h3>List</h3><p>As stated above, it’s fairly common to take an array of data and render a component for each item in the array. This HOC will also spread the current data item as props into the supplied component.</p><h3>HOCs</h3><p>Now that we’ve gone over a few use cases, lets look at some simple implementations before we get into any complex chaining.</p><h3>hasProps</h3><p>Takes a single parameter, injectedProps and returns a wrapped component with the supplied injectedProps . Useful when you need to inject props to a single component within the branch hoc, as the props supplied are injected into both passing and failing components.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a3a9881d1b0be22e1b73af235edc6ce4/href">https://medium.com/media/a3a9881d1b0be22e1b73af235edc6ce4/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5b12be0b8033d22f7d47e1173e0815b/href">https://medium.com/media/b5b12be0b8033d22f7d47e1173e0815b/href</a></iframe><p>Use with branch HOC:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31575ab26ffe4cdc664e26ec51da3b2c/href">https://medium.com/media/31575ab26ffe4cdc664e26ec51da3b2c/href</a></iframe><h3>hasLogger</h3><p>Takes a single parameter, prefix which prefixes the logged message, defaults to an empty string. Logs props to the console on every render of the WrappedComponent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a7d18ebacefb07cd951739fed2e1fbe1/href">https://medium.com/media/a7d18ebacefb07cd951739fed2e1fbe1/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39c87c31398fd7232faa46782718902b/href">https://medium.com/media/39c87c31398fd7232faa46782718902b/href</a></iframe><h3>branch</h3><p>Takes 3 parameters: test, ComponentOnPass, and ComponentOnFail and returns either of the supplied components depending on the result of the test function. If the test function returns true, ComponentOnPass will be returned, if false, ComponentOnFail will be returned. Immediately returns a new component unlike other HOCs in this post.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/552ba25e3af5c6a8845461081d43a269/href">https://medium.com/media/552ba25e3af5c6a8845461081d43a269/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a2c5e303056f63f8916aafbf511977e0/href">https://medium.com/media/a2c5e303056f63f8916aafbf511977e0/href</a></iframe><h3>hasMockData</h3><p>Takes 2 parameters: mockData and delay and returns a wrapped component with a new data prop. mockData is injected into the data prop after the delay has passed. Can set delay to 0 or leave blank to have no delay. Uses the hasTimeouts HOC to extends functionality to include addTimeout and clearTimeout props.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c588689a8dde7aa9f4a8076791c5e13a/href">https://medium.com/media/c588689a8dde7aa9f4a8076791c5e13a/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86b4dcd0f7fea5b83ea5235fa505e232/href">https://medium.com/media/86b4dcd0f7fea5b83ea5235fa505e232/href</a></iframe><h3>hasLoader</h3><p>This higher-order component has no parameters. It returns a wrapped component with new loading and loadingMessage props expected. It conditionally renders the supplied component if loading is false. Otherwise, it renders the Loading component with the loadingMessage supplied.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5c577eadb8b88518ecbfbbd35080a36/href">https://medium.com/media/b5c577eadb8b88518ecbfbbd35080a36/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65f39b167cdd34a3cecd27189b742181/href">https://medium.com/media/65f39b167cdd34a3cecd27189b742181/href</a></iframe><h3>hasError</h3><p>Takes 1 parameter: ErrorComponent which has a default value of the Error component. Can supply a custom ErrorComponent if desired rather than using the default value. It returns a wrapped component with a new expected prop: hasError. It conditionally renders the supplied component if hasError is false. Otherwise, it renders the ErrorComponent.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4494be91411a4d75e20aed392e58f4ce/href">https://medium.com/media/4494be91411a4d75e20aed392e58f4ce/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/966206230e706f7bc1f397af62db7815/href">https://medium.com/media/966206230e706f7bc1f397af62db7815/href</a></iframe><p>Uses a default Error component if none is supplied:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97cabb13c16070a42bb54e4f2a94c7d8/href">https://medium.com/media/97cabb13c16070a42bb54e4f2a94c7d8/href</a></iframe><h3>hasDefault</h3><p>Takes 1 parameter: Default which expects to be a React component. It returns a wrapped component with a new expected prop: useDefault. It conditionally renders the supplied component if useDefault is false. Otherwise, it renders the Default component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/76f1405e47710b39f84e030910fbf091/href">https://medium.com/media/76f1405e47710b39f84e030910fbf091/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/063baea7077cc0361f8a8da54e926206/href">https://medium.com/media/063baea7077cc0361f8a8da54e926206/href</a></iframe><h3>hasData</h3><p>Expects a single parameter object which expects properties of url, params, and loadingMessage. url dictates which endpoint is hit, params injects URL parameters for the request, and loadingMessage allows you to customize the loading message within the Loader component. Uses <a href="https://github.com/axios/axios">Axios</a> to get data from an API endpoint. Axios can easily be replaced with your fetch implementation of choice. Injects data, hasError, error, useDefault, loading, and loadingMessage as props to be used in the wrapped component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03685fd2fb2c9cbf168f50f9b43c5102/href">https://medium.com/media/03685fd2fb2c9cbf168f50f9b43c5102/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1c9360199e615a072f368633dd9cd6b7/href">https://medium.com/media/1c9360199e615a072f368633dd9cd6b7/href</a></iframe><h3>hasTimeouts</h3><p>Expects no parameters and returns a wrapped component with injected props addTimeout and clearTimeouts. These props allow you to easily create timeouts and be able to clear them when a component unmounts. If you have an active timeout and unmount a component, it will throw an error.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6e3664a5b7b88e55b8671318af82471/href">https://medium.com/media/a6e3664a5b7b88e55b8671318af82471/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/25a7a6f8e72661af12af106cf62be295/href">https://medium.com/media/25a7a6f8e72661af12af106cf62be295/href</a></iframe><h3>isList</h3><p>Expects a single parameter type which dictates the className used in the wrapped div for the list. This HOC expects the list component to have a data prop which contains an array of items. All properties of individual items are injected as props to the individual wrapped component.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/181aade82cccd01138a0585993c838bc/href">https://medium.com/media/181aade82cccd01138a0585993c838bc/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57215c86a92f417f40a1491b89105aa4/href">https://medium.com/media/57215c86a92f417f40a1491b89105aa4/href</a></iframe><h3>Composition Using Higher-Order Components</h3><p>The true power of HOCs comes when you start to chain (compose) them. You can chain as many of them as you like. If you have a component which needs an error and default state, simply compose it like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/69efc93d83bf9ed41ccbe5b72af1d565/href">https://medium.com/media/69efc93d83bf9ed41ccbe5b72af1d565/href</a></iframe><p>The returned component can now be controlled to show an error or a default using its props. In the case of the above example, if useDefault and hasError are both true, hasError and the ErrorComponent would take precedent since it is first within the compose function.</p><p>Do you want to add logging to the above component and also supply it with mockData? Easy! Simply change the above to:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/957e4e8f37e244e177d6210171c31970/href">https://medium.com/media/957e4e8f37e244e177d6210171c31970/href</a></iframe><p>HOCs can easily be added and composed to extend the functionality of any component!</p><h3>isContainer</h3><p>Expects a single parameter object with props data, Error, and DefaultComponent. data is expected to be the param object expected by the hasData HOC. Error expects a custom error component, but if left blank will display the default error component. DefaultComponent expects a component to display in the container list if no items were fetched from the API.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/973a6b0a8d94b65a3759ae5aa4072581/href">https://medium.com/media/973a6b0a8d94b65a3759ae5aa4072581/href</a></iframe><p>Use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46e7ec965e044d31c45f4960d97f20a3/href">https://medium.com/media/46e7ec965e044d31c45f4960d97f20a3/href</a></iframe><h3>Quick Note About Debugging</h3><p>For debugging purposes, we are making our code a bit more verbose. Using the hasProps HOC as an example, we could further simplify it like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/443a102a8d432f8533a00f08618cb96d/href">https://medium.com/media/443a102a8d432f8533a00f08618cb96d/href</a></iframe><p>The problem with this is that when viewed in the React Developer Tools, the component comes back as Unknown as it was never given a name. Using the original example provided for the hasProp HOC would display a HasProps component within the React Developer Tools.</p><h3>Using Redux Compose</h3><p>As mentioned above, Redux supplies a compose method that can be imported and used like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ecdbd2853eb4e70a460b8962681cc5b/href">https://medium.com/media/3ecdbd2853eb4e70a460b8962681cc5b/href</a></iframe><p>You can read more about their implementation over at their documentation <a href="https://github.com/reactjs/redux/blob/master/docs/api/compose.md">here</a>. The compose function isn’t very special in itself, it can be imported from many libraries.</p><h3>Example App</h3><h3>Home Page</h3><p>The provided example app shows use of all concepts above. The home page includes components that have been extended with HOCs and also includes buttons to control the props that control the HOCs used to extend them. To put it simply, you’re able to click a button and switch which component renders based on props. This page includes a MockPostContainer which extends the Post component with a logger, mock data, loader, error, default, and list.</p><h3>Posts, Todos, Comments, and Users Pages</h3><p>All of these pages consume real API endpoints from <a href="https://jsonplaceholder.typicode.com/">https://jsonplaceholder.typicode.com/</a> . They each extend a single component (Post, Todo, Comment, User) with the isContainer and isList HOCs.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4877k4Hq9dPdtmvg9hnGFA.jpeg" /></figure><h3>Wrapping Up</h3><p>I hope the above patterns / components help you create some new higher-order components of your own. These are the most common use cases for me, I’m sure you probably have a few common use cases not listed above. I’d love to hear how higher-order components have helped you simplify your code!</p><p><strong>If you appreciated the article and it helped in any way, don’t hesitate to share and clap!</strong> 👏</p><h3>Feedback? Words of encouragement? 🎉</h3><p>I’m glad you made it! I’m always looking to improve my articles, if you would like to leave feedback (I would love it if you did!) you can find the Google Form <a href="https://goo.gl/forms/HPH8ujAm60Jd8Qq13">here</a>. It’s very short, I promise!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3a5288e78f55" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/react-composing-higher-order-components-hocs-3a5288e78f55">React — Composing Higher-Order Components (HOCs)</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Functional JS with ES6 — Recursive Patterns]]></title>
            <link>https://medium.com/dailyjs/functional-js-with-es6-recursive-patterns-b7d0813ef9e3?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/b7d0813ef9e3</guid>
            <category><![CDATA[patterns]]></category>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[es6]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[utilities]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Fri, 29 Sep 2017 18:18:35 GMT</pubDate>
            <atom:updated>2018-10-21T21:29:31.129Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OU3sUKEGZoWzMAbvIEDUeg.png" /></figure><h3>What’s the deal here?</h3><p>Functional programming has been on the rise and is a topic that is very exciting to me. It allows me to write terse, declarative code that is easy to test and reason about. What is functional programming? I’ll defer that answer to someone with more knowledge on the subject, <a href="https://medium.com/@_ericelliott">Eric Elliot</a>:</p><blockquote><strong>Functional programming</strong> (often abbreviated FP) is the process of building software by composing <strong>pure functions</strong>, avoiding <strong>shared state,</strong> <strong>mutable data</strong>,and <strong>side-effects</strong>. Functional programming is <strong>declarative</strong> rather than <strong>imperative</strong>, and application state flows through pure functions. Contrast with object oriented programming, where application state is usually shared and colocated with methods in objects.</blockquote><p>ES6 brings many features that allow us to easily write pure functions, rest/spread being one of the most powerful. Using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters">rest params</a>, we’re able to “loop without loops” with <a href="https://en.wikipedia.org/wiki/Recursion_(computer_science)">recursion</a>. In this article, we’re going to rewrite many commonly used JavaScript methods/functions that allow for functional patterns.</p><h3>Preface</h3><p>The following functions are for demonstration and learning purposes. Many functions below are tail-recursive and should be optimized further. Fixing tail-recursion is not the subject of this article. ES6 brings tail-call optimization, but must be used in conjunction with &#39;use strict&#39; .</p><h3>Head</h3><p>Return the first item in an array. Is useful when you need to separate the first item from the rest of the array items. To do this, we make use of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment">destructuring assignment</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fa9e1b667b8a93822c10d9e6665aa860/href">https://medium.com/media/fa9e1b667b8a93822c10d9e6665aa860/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/edbf49a35d8efdfd285b3d1003601535/href">https://medium.com/media/edbf49a35d8efdfd285b3d1003601535/href</a></iframe><h3>Tail</h3><p>Return all but the first item in an array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27b247be45a8007c0a88ae5601a71216/href">https://medium.com/media/27b247be45a8007c0a88ae5601a71216/href</a></iframe><p>Which is essentially the same as writing:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a206303f1d78392569bd6d50308681af/href">https://medium.com/media/a206303f1d78392569bd6d50308681af/href</a></iframe><p>Since we don’t need to use x in the returned output, we can drop it, but keep the comma to get the rest of the items in the array.</p><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5c39e93684f5537e638ba3a29eca12d8/href">https://medium.com/media/5c39e93684f5537e638ba3a29eca12d8/href</a></iframe><h3>Def</h3><p>Return if argument supplied is defined.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/040ba009414529ca1ef51f9034d0a50e/href">https://medium.com/media/040ba009414529ca1ef51f9034d0a50e/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae78a0cc63dd95323716100e8f656548/href">https://medium.com/media/ae78a0cc63dd95323716100e8f656548/href</a></iframe><h3>Undef</h3><p>Return if argument supplied is undefined.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/274fc2d1201c4511aede669576698bfd/href">https://medium.com/media/274fc2d1201c4511aede669576698bfd/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b774d356ad9bebfb345fad46dc28e312/href">https://medium.com/media/b774d356ad9bebfb345fad46dc28e312/href</a></iframe><h3>Copy</h3><p>Returns a copy of an array without using Array.slice(). Makes use of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_operator">spread</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a05220d00d6188b3a4332d1a5d6aecc2/href">https://medium.com/media/a05220d00d6188b3a4332d1a5d6aecc2/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8846a8397c54c0f31fe95317772cf7ff/href">https://medium.com/media/8846a8397c54c0f31fe95317772cf7ff/href</a></iframe><h3>Length</h3><p>Return the length of an array. This is a very simple form of looping through an array with recursion, even though the values of the array don’t matter in this case (increments up starting at 1 for every item in array). We include the <strong>len</strong> param to avoid <a href="https://en.wikipedia.org/wiki/Tail_call">tail recursion</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cbcc6a569a86a25144747cc513c3ceaf/href">https://medium.com/media/cbcc6a569a86a25144747cc513c3ceaf/href</a></iframe><p>If we don’t care about tail recursion, we can write it as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3769da1c2cc453922ddf794a95be0ef/href">https://medium.com/media/f3769da1c2cc453922ddf794a95be0ef/href</a></iframe><p>This would add a stack frame for each item in the array, whereas the version that avoids tail recursion, replaces a single stack frame. If the array passed in is large enough, it will throw “Maximum call stack size exceeded”.</p><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d07eeaaf32ad1c4b5f9e23d8a28a5c86/href">https://medium.com/media/d07eeaaf32ad1c4b5f9e23d8a28a5c86/href</a></iframe><h3>Reverse</h3><p>Return a reversed array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ffec0e19c8e0cc4387e7d4331b0c9bfb/href">https://medium.com/media/ffec0e19c8e0cc4387e7d4331b0c9bfb/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6a9dc253debc1042561db9bcd3d8df0a/href">https://medium.com/media/6a9dc253debc1042561db9bcd3d8df0a/href</a></iframe><p>Array.reverse() is okay, but it modifies the value in place which is a side-effect. Consider the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4736b20279c5c2a1ce1d351e530ad4a6/href">https://medium.com/media/4736b20279c5c2a1ce1d351e530ad4a6/href</a></iframe><h3>First</h3><p>Returns a new array that contains the first n items of the given array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bfb46d9fbdc8e371228ce3b862765612/href">https://medium.com/media/bfb46d9fbdc8e371228ce3b862765612/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fda461c7f0aef9f1db5595d6037b26c9/href">https://medium.com/media/fda461c7f0aef9f1db5595d6037b26c9/href</a></iframe><h3>Last</h3><p>Returns a new array that contains the last n items of the given array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6166e81381accf14d9440e388010eb78/href">https://medium.com/media/6166e81381accf14d9440e388010eb78/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61124a493635a40a66b0a65a902e688e/href">https://medium.com/media/61124a493635a40a66b0a65a902e688e/href</a></iframe><h3>Slice</h3><p>Returns a new array with value inserted at given index.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e87dbeb028dbf96389b259911e925ae1/href">https://medium.com/media/e87dbeb028dbf96389b259911e925ae1/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04be304900e1931cbd8668915d9bed12/href">https://medium.com/media/04be304900e1931cbd8668915d9bed12/href</a></iframe><h3>isArray</h3><p>Returns if the value supplied is an array. Allows us to write Array.isArray() in a more functional manner.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/562f54242767a558c30609114dadf412/href">https://medium.com/media/562f54242767a558c30609114dadf412/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f5f174a86158c2499c20b700b5582b6/href">https://medium.com/media/2f5f174a86158c2499c20b700b5582b6/href</a></iframe><h3>Flatten</h3><p>Combines nested arrays into a single array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/faede8fd13fac6f010b86b6fd66d99b7/href">https://medium.com/media/faede8fd13fac6f010b86b6fd66d99b7/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/730007ad319a7984c5f4c8db2b7d0881/href">https://medium.com/media/730007ad319a7984c5f4c8db2b7d0881/href</a></iframe><h3>Swap</h3><p>Return a new array with 2 items swapped based on their index.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d083f6ce7e4b68f979b1582143d3920/href">https://medium.com/media/2d083f6ce7e4b68f979b1582143d3920/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/65ab8dbe0a8d274cca3b8501155c9d2b/href">https://medium.com/media/65ab8dbe0a8d274cca3b8501155c9d2b/href</a></iframe><h3>Map</h3><p>From MDN: “…creates a new array with the results of calling a provided function on every element in this array.”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9bf1cd09604134cfa21c6eda9f5cd3bc/href">https://medium.com/media/9bf1cd09604134cfa21c6eda9f5cd3bc/href</a></iframe><p>Which can be simplified as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/57d0119d0fec95fc14871eb9eddb9747/href">https://medium.com/media/57d0119d0fec95fc14871eb9eddb9747/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/744553a44d0d4bfce22c3cf837c7895a/href">https://medium.com/media/744553a44d0d4bfce22c3cf837c7895a/href</a></iframe><h3>Filter</h3><p>From MDN: “…creates a new array with all elements that pass the test implemented by the provided function.”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/671df81580ffe14a5588854d4d567261/href">https://medium.com/media/671df81580ffe14a5588854d4d567261/href</a></iframe><p>Which can be simplified as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a461c3391feaa1177a8445b0ae15ca7/href">https://medium.com/media/5a461c3391feaa1177a8445b0ae15ca7/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b2b2ecd9a5654534380a2543fd0e8882/href">https://medium.com/media/b2b2ecd9a5654534380a2543fd0e8882/href</a></iframe><h3>Reject</h3><p>The opposite of filter, returns an array that does not pass the filter function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/61231ee883ed67abd605cef23f2cdcb2/href">https://medium.com/media/61231ee883ed67abd605cef23f2cdcb2/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5eb30b951fc93915b4a0ca5e5b3be67c/href">https://medium.com/media/5eb30b951fc93915b4a0ca5e5b3be67c/href</a></iframe><h3>Partition</h3><p>Splits an array into two arrays. One whose items pass a filter function and one whose items fail.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a03bfa390236b909a38c2cb281bb6093/href">https://medium.com/media/a03bfa390236b909a38c2cb281bb6093/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91617720adc5cb40e6ce344365284f85/href">https://medium.com/media/91617720adc5cb40e6ce344365284f85/href</a></iframe><h3>Reduce</h3><p>From MDN: “…applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.”</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3ba7ec6ca4a873dd914133ed784e04a7/href">https://medium.com/media/3ba7ec6ca4a873dd914133ed784e04a7/href</a></iframe><p>Which can be simplified as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5f51649e4559abbd9591b2181b17093d/href">https://medium.com/media/5f51649e4559abbd9591b2181b17093d/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04939c45a9c062d19de977f9f905aefc/href">https://medium.com/media/04939c45a9c062d19de977f9f905aefc/href</a></iframe><h3>ReduceRight</h3><p>Similar to reduce, but applies the function from right-to-left.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8c0ab13746e0d37fc1bd564c3d1376a9/href">https://medium.com/media/8c0ab13746e0d37fc1bd564c3d1376a9/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/89b053a211612a1dc6d7724caa18f5b0/href">https://medium.com/media/89b053a211612a1dc6d7724caa18f5b0/href</a></iframe><h3>Partial</h3><p>Partially apply a function by filling in any number of its arguments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad2ff9adaf169d8bc2f2211b4a39d62d/href">https://medium.com/media/ad2ff9adaf169d8bc2f2211b4a39d62d/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d5ec4334e1a6c9d6b3dfa611f1856b0f/href">https://medium.com/media/d5ec4334e1a6c9d6b3dfa611f1856b0f/href</a></iframe><h3>SpreadArg</h3><p>Convert function that takes an array to one that takes multiple arguments. This is useful when partially applying.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8ca64acfd2db78cfbb7b0c00b5f3da73/href">https://medium.com/media/8ca64acfd2db78cfbb7b0c00b5f3da73/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f608b13000c6b285cccc1ecb2d86fb63/href">https://medium.com/media/f608b13000c6b285cccc1ecb2d86fb63/href</a></iframe><p>If you only want to define a single function you can write it as:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3382d33cac341170601d03732557575/href">https://medium.com/media/b3382d33cac341170601d03732557575/href</a></iframe><p>In the above, you need to remember to spread the array you are sending into the function recursively, since you are spreading the argument.</p><h3>ReverseArgs</h3><p>Reverse function argument order.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91a2b782a208713a579ba9a2120bfa48/href">https://medium.com/media/91a2b782a208713a579ba9a2120bfa48/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ac7227bb135e4b1424da916fa68b10d8/href">https://medium.com/media/ac7227bb135e4b1424da916fa68b10d8/href</a></iframe><p>Reversing arguments can be useful when partially applying arguments. Sometimes you want to partially apply arguments at the end of the list, not those at the beginning. Reversing the arguments allows us to do that.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0c0b7b7b9b06c23fe4c6c4ac1e283833/href">https://medium.com/media/0c0b7b7b9b06c23fe4c6c4ac1e283833/href</a></iframe><h3>Pluck</h3><p>Extract property value from array. Useful when combined with the <strong>map</strong> function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a8a6a52d2bef63267e3df6cf3388c22e/href">https://medium.com/media/a8a6a52d2bef63267e3df6cf3388c22e/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/23af5a17000107015479cd7c165a1582/href">https://medium.com/media/23af5a17000107015479cd7c165a1582/href</a></iframe><h3>Flow</h3><p>Each function consumes the return value of the function that came before.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e5c7dcd51930a3c27af48dfeb182c67f/href">https://medium.com/media/e5c7dcd51930a3c27af48dfeb182c67f/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3534f90e42ea58a6a6339a73dc9811a5/href">https://medium.com/media/3534f90e42ea58a6a6339a73dc9811a5/href</a></iframe><h3>Compose</h3><p>The same as flow, but arguments are applied in the reverse order. Compose matches up more naturally with how functions are written. Using the same data as defined for the <strong>flow</strong> function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5e13972791f21bc78d095e48f068453b/href">https://medium.com/media/5e13972791f21bc78d095e48f068453b/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ef6960fd15dd650cbb6966d6182e3394/href">https://medium.com/media/ef6960fd15dd650cbb6966d6182e3394/href</a></iframe><h3>Min</h3><p>Return the smallest number in an array. Returns <strong>Infinity</strong> if array supplied is empty.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e15d55727c2531ebd7e9b08ed1440e65/href">https://medium.com/media/e15d55727c2531ebd7e9b08ed1440e65/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/53589c7e995d763b0a7fc925139dda03/href">https://medium.com/media/53589c7e995d763b0a7fc925139dda03/href</a></iframe><h3>Max</h3><p>Return the largest number in an array. Returns <strong>-Infinity</strong> if array supplied is empty.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5484d342bcd2a2340a7b93c46c7e3acc/href">https://medium.com/media/5484d342bcd2a2340a7b93c46c7e3acc/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59cbd1f48701634d0d27027974b2cee3/href">https://medium.com/media/59cbd1f48701634d0d27027974b2cee3/href</a></iframe><h3>Factorial</h3><p>Returns the factorial of a number. Uses an accumulator to allow replacing of stack frames to allow larger factorials to be returned.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/40607d4f1d56fae0bf29d48e7f267008/href">https://medium.com/media/40607d4f1d56fae0bf29d48e7f267008/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f48517bf8afa63fd4da6c479621291bc/href">https://medium.com/media/f48517bf8afa63fd4da6c479621291bc/href</a></iframe><h3>Fibonacci</h3><p>Returns the Fibonacci number at the given place.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/574ae7cd22af05d3836ee64a9009b099/href">https://medium.com/media/574ae7cd22af05d3836ee64a9009b099/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4e38b546513c353c0c3673193b49fda8/href">https://medium.com/media/4e38b546513c353c0c3673193b49fda8/href</a></iframe><h3>Quicksort</h3><p>Sort an array from smallest to largest. This is done by re-ordering the array so that it contains two sub-arrays, one with smaller values, the other with larger values. The above steps are recursively applied to each sub-array until there are no arrays left, which is flatten to return a sorted array.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee9f3b2824f84d3cca6bd28bc9b35307/href">https://medium.com/media/ee9f3b2824f84d3cca6bd28bc9b35307/href</a></iframe><p>This can also be implemented using <strong>partition</strong>, but requires variable assignment.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f7508b1dba404839aacb8b4219fc6f0/href">https://medium.com/media/6f7508b1dba404839aacb8b4219fc6f0/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/903f808f23b70ee21fe8c86843a8cdf6/href">https://medium.com/media/903f808f23b70ee21fe8c86843a8cdf6/href</a></iframe><h3>Everything as a Reduction</h3><p>Many of the functions above can be converted into reductions, which should increase performance in most, if not all cases. This also shows the flexibility of the <strong>reduce</strong> function.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/94f554b0e54e396b6bff4fe980c27de4/href">https://medium.com/media/94f554b0e54e396b6bff4fe980c27de4/href</a></iframe><p>Example usage:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f9837718a4f1b12e70977701c19b852a/href">https://medium.com/media/f9837718a4f1b12e70977701c19b852a/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*4877k4Hq9dPdtmvg9hnGFA.jpeg" /></figure><h3>Wrapping Up</h3><p>I hope this article helps shed insight on some of the patterns made available with JavaScript and ES6. Many problems that can be solved with iteration/loops, can also be solved functionally through recursion. I hope this article was also able to show you the flexibility of the reduce function.</p><h3>Feedback? Words of encouragement? 🎉</h3><p>I’m glad you made it! I’m always looking to improve my articles, if you would like to leave feedback (I would love it if you did!) you can find the Google Form <a href="https://goo.gl/forms/HPH8ujAm60Jd8Qq13">here</a>. It’s very short, I promise!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b7d0813ef9e3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/dailyjs/functional-js-with-es6-recursive-patterns-b7d0813ef9e3">Functional JS with ES6 — Recursive Patterns</a> was originally published in <a href="https://medium.com/dailyjs">DailyJS</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Creating Reusable Components with Vue.js : Button Component]]></title>
            <link>https://codeburst.io/creating-reusable-components-with-vue-js-button-component-503167facfde?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/503167facfde</guid>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[components]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[front-end-development]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Thu, 06 Apr 2017 10:44:24 GMT</pubDate>
            <atom:updated>2017-11-16T18:37:27.729Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*n4wMeTDSXpRHCVt-hEGRJg.png" /></figure><blockquote>This article is the beginning of a series titled “Creating Reusable Components with Vue.js”. Each article will guide you through creating a specific UI component and will teach you many Vue essentials along the way. Each article will build upon the last, getting more complex the further we go. Application structure and other opinions will be shared as well.</blockquote><h3>Update — April 13, 2017</h3><p><em>Changed the application structure and WebPack alias setup a bit. The old setup was causing a circular dependency issue with WebPack which would cause nested components (that are both within the definition file) to fail. This type of definition file is still relevant in the context of creating a plugin, but it is used a bit differently. We won’t be worrying about creating a plugin for now.</em></p><h3>Whoa , slow down</h3><p>Isn’t there a <strong><em>native</em></strong> button element already, why would you need a custom component for that?</p><ol><li>Shared functionality</li><li>Shared styles</li></ol><p>Both of these will help the maintainability of our application. If a feature needs to be added, updated, or deleted, it only needs to be fixed in a single place. <em>Cha-ching!</em></p><h3>Preface</h3><p>This article is aimed at people who have a basic understanding of Vue and NPM already. The main focus of this article is JavaScript and how it interacts with the DOM. This means that CSS might not be explained. I cannot guarantee that anything in this article will work in future releases. I cannot guarantee that any of my personal practices are <em>best</em> practices. I am not responsible for any long tangents that seemingly have no point. Phew, now lets get to the good stuff.</p><h3>Boilerplate</h3><p>I have created a boilerplate base project to use. Setting up a project is not the subject for this article. This article assumes you have a very basic understanding of the single file component syntax. You can read up on single file components <a href="https://vuejs.org/v2/guide/single-file-components.html">here</a>.</p><p>Download the following repository locally:</p><p><a href="https://github.com/caseymorrisus/vue-button-tutorial">https://github.com/caseymorrisus/vue-button-tutorial</a></p><p>Once you have the project above cloned/checked out locally, head to the project root and use npm to install the project dependencies:</p><pre>npm install</pre><h3>Tutorial</h3><p>Now that everything is installed we should make sure everything works. From the root of your project, run:</p><pre>npm run dev</pre><p>This should start a development server at localhost:8080 which can be opened in your browser of choice. I prefer Google Chrome as it has a great extension, <a href="https://github.com/vuejs/vue-devtools">Vue.js devtools</a>. If you open src/App.vue you will see that it currently only has a header component loaded in, that&#39;s no fun! Let&#39;s start off by defining a simple template for our Button component. Open src/components/Button/Button.vue and make the following changes:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2df9e2d1963b53af4968d0ef4918cb5/href">https://medium.com/media/e2df9e2d1963b53af4968d0ef4918cb5/href</a></iframe><p>Cool, pretty easy so far. Lets prepare the component to be imported and make sure the component loads without error before explaining it further. To do that, we need to talk a bit about application structure.</p><h3>Application Structure</h3><p>By default to import a component, you need to reference its URL relative to the current file. With a small application, this doesn’t seem like an issue, but can quickly get annoying in larger applications with more complex directory structures. To get around this issue, I setup a WebPack alias that points to our src/components directory. This allows us to ignore the relative location of the directory we are importing. I also go ahead and add an index.js file to each of our component directories to shorten our imports.</p><p>This would allows us to import components like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2b64ffbf8dbbd05f21b5e25c7176bf4/href">https://medium.com/media/e2b64ffbf8dbbd05f21b5e25c7176bf4/href</a></iframe><p>The WebPack alias I use to let me use Components is located in webpack.config.js and looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/82fd8b6828af2575a8e265a63f4e94f4/href">https://medium.com/media/82fd8b6828af2575a8e265a63f4e94f4/href</a></iframe><p>You may be wondering why I give each component its own directory. I do this to give me a place to store files that are related to a component. These can be images used in a component, or child components that will only be used by a specific component. (E.g. Slider component has Slide components).</p><h3>Button Content</h3><p>The Button component still needs to be added to our view. Open App.vue and import it like so:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e1f82d53a081dcfc051aaea0598f5685/href">https://medium.com/media/e1f82d53a081dcfc051aaea0598f5685/href</a></iframe><p>The component still needs to be <a href="https://vuejs.org/v2/guide/components.html#Local-Registration">locally registered</a> before it can be used. Alter the components object for the App component to include the Button component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/cad7fad63b551714f1c5e9eca1af5b3f/href">https://medium.com/media/cad7fad63b551714f1c5e9eca1af5b3f/href</a></iframe><p>We give our Button component a custom tag name of v-button so that it does not conflict with reserved HTML elements, as I did with the Header component as well. This is not necessary if the component name is not a reserved HTML element.</p><p>Finally, we can add the Button component to our template:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1fda4031e7392d4b9bf1e8d112c5cab9/href">https://medium.com/media/1fda4031e7392d4b9bf1e8d112c5cab9/href</a></iframe><p>Doing this should add a button element to the page with text that says “Button”. The text content is coming from the fallback content defined within the Button component. Fallback content is defined by putting content between the &lt;slot&gt; tags for a component. More can be read about slots and fallback content <a href="https://vuejs.org/v2/guide/components.html#Single-Slot">here</a>.</p><p>Let’s add our own custom content to the Button component:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/199ebe7788aa97f9b504ecb410f16233/href">https://medium.com/media/199ebe7788aa97f9b504ecb410f16233/href</a></iframe><p>This should change the content of the button from “Button” to “Custom”. You are not limited to strings/text content. Any content inserted in-between component tags will be inserted into the slot. For example, you could use an image icon rather than text like so (don’t actually add this):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/977d5339dc858f05704569bab025a138/href">https://medium.com/media/977d5339dc858f05704569bab025a138/href</a></iframe><h3>Adding Interactivity/Events with Props</h3><p>Alright, so far our Button component is pretty boring. At this point it is nothing more than a bloated implementation of a native HTML button. To make the Button component useful, we need to add the option to pass in a click event from the parent component. This is done through the use of <a href="https://vuejs.org/v2/guide/components.html#Props">props</a>.</p><p>Open Button.vue , so far we&#39;ve only altered the template. To allow our parent component to send a method to our Button component, we need to define an onClick prop:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/44f31327b77a1ff8f87430637a82abb6/href">https://medium.com/media/44f31327b77a1ff8f87430637a82abb6/href</a></iframe><p>In the above, we are giving our Button component a prop of onClick. We&#39;re telling Vue to validate the value passed in by using the type property and passing it the value of Function. This will cause Vue to throw a useful error in your development environment if a value of a different type is given. We are also telling Vue that this prop should always exist using required as a button component without a click event is fairly useless. You can read more about prop validation and defaults <a href="https://vuejs.org/v2/guide/components.html#Prop-Validation">here</a>.</p><h3>Bind click event to the DOM</h3><p>Our component still doesn’t react to user interaction, we need to bind the onClick method that is passed in as a prop. To do this, we will use v-on (<a href="https://vuejs.org/v2/api/#v-on">more info</a>) to bind the event to the desired DOM element. We will use the shorthand @click which is the same as typing v-bind:click which can be read about <a href="https://vuejs.org/v2/guide/syntax.html#v-on-Shorthand">here</a>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/017e16f70e1214339140410210b85ef3/href">https://medium.com/media/017e16f70e1214339140410210b85ef3/href</a></iframe><p>If you try to click the button at this point nothing will happen. This is because we haven’t passed in a value for the onClick prop.</p><h3>Passing a method from parent to child component</h3><p>Open the App component located at src/App.vue and create a consoleClick method that logs a string to the console (<a href="https://vuejs.org/v2/guide/events.html#Method-Event-Handlers">more info on methods</a>):</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/334261ffbd49735dafa0d4a5cc30764c/href">https://medium.com/media/334261ffbd49735dafa0d4a5cc30764c/href</a></iframe><p>With the consoleClick method created within our App component, it is now ready to be passed in as a value for the onClick prop for the Button component. This can be done with the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e3fbf427b4e619b76289b83e2b7d7905/href">https://medium.com/media/e3fbf427b4e619b76289b83e2b7d7905/href</a></iframe><p>The key here is :onClick which is the equivalent to v-bind:onClick (<a href="https://vuejs.org/v2/guide/syntax.html#v-bind-Shorthand">shorthand</a>) which allows us to bind a JavaScript expression to our onClick prop. In this case we are passing in a function (and checking using validation!), but it can just as easily be a number, string, array, boolean, or object.</p><p>If you click on the button and look at your console, you should see the string Button clicked has been logged. Awesome, we&#39;ve successfully passed a method from a parent component down to a child component and deferred calling that method until the child component has been clicked.</p><h3>Multiple instances of a component</h3><p>Components aren’t much fun if they’re only used once, let’s create another instance of our new Button component. Let’s also pass in a different method to the new Button instance. First, let’s add the new method to our App component, alertClick, which will create an alert when the Button is clicked.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d5e442a939b23c56444fe514578580a/href">https://medium.com/media/8d5e442a939b23c56444fe514578580a/href</a></iframe><p>Add another Button component directly after the first, giving it new slot content. Let’s also alter the slot content of the original button to match its behavior.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c9731fbd8dff07e2aab53bee41b35b8b/href">https://medium.com/media/c9731fbd8dff07e2aab53bee41b35b8b/href</a></iframe><p>Both buttons should work, the first logging a message to your development console, the second, alerting the same message. Easy!</p><h3>Free Functionality</h3><p>Since we used a native button element as the root element of our Button component, we get some free functionality. If we had used a div as a root element, the following would not work. Using a native button element also has the added benefit of being <a href="https://en.wikipedia.org/wiki/Semantic_HTML">semantically correct</a> (<a href="https://davidwalsh.name/html5-buttons">more info</a>). Edit the template for the App component to pass in a disabled prop to one of the buttons.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/15262385d72cfdd4e9b300775659380d/href">https://medium.com/media/15262385d72cfdd4e9b300775659380d/href</a></iframe><p>We can use shorthand syntax when passing a true value for a prop that expects a boolean. Passing disabled to our component is the same as passing :disabled=&quot;true&quot;. Notice we are using the v-bind: shorthand syntax to send in the value of true. Without it, Vue will think you are sending a string with the value of true aka &quot;true&quot;. This is fine until you try to send in disabled=&quot;false&quot; which will disable the button, because a string with the value of false is not a <a href="https://developer.mozilla.org/en-US/docs/Glossary/Falsy">falsy</a> value. It will return true.</p><h3>Wrapping Up</h3><p>We now have ourselves a nice, reusable Button component that could be extended further if we wanted to. It may seem like it took a lot of work to get here, but we covered a lot of important topics that you can build upon in the future. Look for more articles in the future, I plan on continuing the series to the point where we build a small UI framework together.</p><p><em>If you found anything useful in this article, it would mean a ton to me if you clicked the 💚 and shared with friends!</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=503167facfde" width="1" height="1" alt=""><hr><p><a href="https://codeburst.io/creating-reusable-components-with-vue-js-button-component-503167facfde">Creating Reusable Components with Vue.js : Button Component</a> was originally published in <a href="https://codeburst.io/">codeburst</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a Slack Live Blog using Node.js, Express, RethinkDB, Socket.io, & Handlebars]]></title>
            <link>https://medium.com/@caseymorrisus/create-a-slack-live-blog-using-node-js-express-rethinkdb-socket-io-handlebars-fcbf6e601fa5?source=rss-c194ff39a976------2</link>
            <guid isPermaLink="false">https://medium.com/p/fcbf6e601fa5</guid>
            <category><![CDATA[slack]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[Casey Morris]]></dc:creator>
            <pubDate>Wed, 24 Feb 2016 15:19:07 GMT</pubDate>
            <atom:updated>2016-02-26T14:48:34.123Z</atom:updated>
            <content:encoded><![CDATA[<h4>Delivering content broadcasted by Slack in real-time</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*wHjyoQu162w_LhvQ0JXFhg.gif" /><figcaption>GIF taken from working example</figcaption></figure><h3>Why?</h3><p>I’m a developer at a digital design and technology studio called <a href="http://phdl.co/">PHDL</a>. One of our awesome clients is an experiential marketing firm based in San Francisco called <a href="http://www.wearemanifold.com/">Manifold</a>. Like many companies, Slack has become instrumental to Manifold’s daily workflow, communication and culture; so much that they came to us with an idea: is it possible to allow anyone on their team to publish content from a Slack channel onto a section of their website in real time? We had previously built a carousel that updated for them in the “contact us” section of their website, but to update it required admin access and WordPress familiarity — any funny inspirations to put on the page usually died on the vine if the site admin wasn’t around, and so the idea was born. We would use Slack to allow anyone at Manifold to publish content to this page on their WordPress site.</p><p>After doing some initial research, I didn’t like similar implementations that already existed. I found them to be inflexible, and so I set out to create an easy and flexible solution that can fit into any system or framework. If you can open a socket connection and receive data via an API, this setup should work for you. Resources for: <a href="https://slack.com/">Slack</a>, <a href="https://nodejs.org/">Node.js</a>, <a href="http://expressjs.com/">Express</a>, <a href="https://www.rethinkdb.com/">RethinkDB</a>, <a href="http://socket.io/">Socket.io</a>, <a href="http://handlebarsjs.com/">Handlebars</a></p><h3>The Flow</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*HuzUeDv-0MZjQgEfwGL7Gw.png" /></figure><h3>The Flow Expanded</h3><ul><li>User sends message to specific Slack channel (e.g. #blog)</li><li>Slack bot watches the #blog channel and receives the message</li><li>Slack bot creates a RethinkDB entry based on message data</li><li>Node server is connected to RethinkDB with a real-time change feed active (watches for changes, emits event and data)</li><li>RethinkDB notifies Node server that data has changed and sends the data</li><li>Node server emits ‘new post’ event with new data over Socket.io</li><li>Node server serves Front End at “/”</li><li>Client has an active Socket.io connection waiting for the Node server to emit events with data</li><li>Client receives socket events and data, then updates the DOM accordingly</li></ul><h3>Three Distinct Parts</h3><p>As you can see above, the application is split into three main parts:</p><h4>1. Slack Bot</h4><p>The Slack bot is a Node.js app that listens to a specific Slack channel (e.g #blog) and saves all data to a database using RethinkDB. It can be switched on and off by typing <strong>!on</strong> and <strong>!off</strong> commands in the chat.</p><h4>2. Node Server</h4><p>The Node server is an Express server that is connected to RethinkDB with a real-time change feed. When data is changed in the database, RethinkDB sends the data to the Node server through the change feed. When the Node server receives this data, it emits a socket event that the Client is connected to and waiting for. For added flexibility, the Node server also has an API endpoint to GET post data. This allows you to receive the posts without the need to connect to the database directly. This is useful for Front-End applications that cannot call a database.</p><h4>3. Client</h4><p>The Client is the end user. In this case, the Client is a website being served by Express at <a href="http://localhost:5000.">http://localhost:5000.</a> When a user loads the website, a new Socket.io connection is established between the Client and the Node Server. Events and data are passed from the Node Server to the client in real-time. Past posts (prior to the user’s Socket.io connection) are rendered on load using Handlebars.</p><p>Now that we know a bit about the process, we can start coding.</p><h3>Setup</h3><p>Start off by cloning my sample repository using git:</p><pre>git clone <a href="https://github.com/caseymorrisus/Slack-Live-Blog.git">https://github.com/caseymorrisus/Slack-Live-Blog.git</a></pre><p>You should see a file named <strong>server.js</strong> which contains the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/500f9c90008590b2714f10d2a1ee30ae/href">https://medium.com/media/500f9c90008590b2714f10d2a1ee30ae/href</a></iframe><p>Don’t worry about all of the dependencies for now, we will go over each part as we implement it.</p><h3>Express Config</h3><p>We will start off by adding the following Express configuration to the end of <strong>server.js:</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2f116e325b19ad5feafe8141f7b9721f/href">https://medium.com/media/2f116e325b19ad5feafe8141f7b9721f/href</a></iframe><h4>Explanation</h4><p>First, we tell Express to use Handlebars as its template engine.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a1a54af141cb3bf1ccda3abcf5f3a17e/href">https://medium.com/media/a1a54af141cb3bf1ccda3abcf5f3a17e/href</a></iframe><p>We then set up the middleware and tell Express to serve static content from publicDir which was set to <strong>/public</strong></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec5f04de0c17f5cffb7e9df3804401fc/href">https://medium.com/media/ec5f04de0c17f5cffb7e9df3804401fc/href</a></iframe><p>After setting up the middleware, we can start the server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8a30ea309c0bea2874a46a8fd74c4812/href">https://medium.com/media/8a30ea309c0bea2874a46a8fd74c4812/href</a></iframe><h3>Socket.io Server Config</h3><p>Configuring the server to use Socket.io is fairly simple. All we are doing is opening a socket connection and logging to the server when a user connects and disconnects. This connection will be used later to emit events to the client.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e7d5cd61d9b08e3cba3b7012b6b913f0/href">https://medium.com/media/e7d5cd61d9b08e3cba3b7012b6b913f0/href</a></iframe><h3>Define Routes</h3><p>Next to add into <strong>server.js</strong> are the two routes that will be served. The first route can be viewed at <a href="http://localhost:5000.">http://localhost:5000.</a> It contains the following:</p><h3>Route: “/”</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a50e104ed82d4f434eae880ea788dd9a/href">https://medium.com/media/a50e104ed82d4f434eae880ea788dd9a/href</a></iframe><h3>Explanation</h3><p>I won’t go into too much detail for the sake of brevity, however, I’ll continue to explain the most crucial pieces.</p><p>Below is how we set up a new route within Express. The following responds to GET requests at the route of “/”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/460ddeb8b889a676d297b7e75ac6a4bc/href">https://medium.com/media/460ddeb8b889a676d297b7e75ac6a4bc/href</a></iframe><p>Connect to RethinkDB with the host “<strong>localhos</strong>t” and the port “<strong>28015</strong>”. If you are hosting your database on a different server, replace localhost with the IP of that server.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8cf28db069ac7b172e220dfe9a38bc5a/href">https://medium.com/media/8cf28db069ac7b172e220dfe9a38bc5a/href</a></iframe><p>Get the five most recent entries in the “<strong>posts</strong>” table.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c8a4b24fbed3ba93f9ac6545f57fceb0/href">https://medium.com/media/c8a4b24fbed3ba93f9ac6545f57fceb0/href</a></iframe><p>Finally, we render the “<strong>home</strong>” view (home.handlebars) when the route is connected to. Send JSON object with 5 most recent entries to be rendered.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeec9b8222ca28ff9acfc1b80ccdc03f/href">https://medium.com/media/aeec9b8222ca28ff9acfc1b80ccdc03f/href</a></iframe><h3>Route: “/posts”</h3><p>Finally, we create a route which responds with a JSON object at <a href="http://localhost:5000/posts"><strong>http://localhost:5000/posts</strong></a>. This object contains the twenty-five most recent entries in the “<strong>posts</strong>” table within RethinkDB. While this functionality is not used for this specific implementation, it is a nice feature to have. Having this API allows the posts to be pulled safely from the database. This is useful for Front-End applications that don’t have the ability to connect to a database. Anything that can call an API now has access to your data!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fbbe9fcedd158bf45a6b54d32f799cae/href">https://medium.com/media/fbbe9fcedd158bf45a6b54d32f799cae/href</a></iframe><h3>RethinkDB Real-Time Change Feed</h3><p>Finally, the last thing we add to <strong>server.js</strong> is the code needed to set up a real-time change feed for RethinkDB:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7aa1389e9092240efc2d6f74ca02c27e/href">https://medium.com/media/7aa1389e9092240efc2d6f74ca02c27e/href</a></iframe><p>To explain further, the code below is for the change feed. We tell RethinkDB that we want to watch for changes in the <strong>‘posts’ </strong>table. Whenever an entry is changed, it is sent to the Node server via the change feed. When the Node server receives changed data from RethinkDB, it sends that data to the Front-End (client) through an open socket connection and event <strong>‘new post’</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/43b4e230e977a87003dd0622c25f4880/href">https://medium.com/media/43b4e230e977a87003dd0622c25f4880/href</a></iframe><h3>The View</h3><p>On the Front-End, we will be using Handlebars as our template engine. The data we are sending from Express is handled within <strong>views/home.handlebars. </strong>This file is inserted into <strong>views/layouts/main.handlebars</strong> where it says <strong>{{{body}}}. </strong>Anything you want to know about Handlebars can be found within their <a href="http://handlebarsjs.com/">documentation</a>.</p><h3>Client Socket.io Configuration</h3><p>Open <strong>public/js/production.js </strong>and you will find the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f05d400043b1cd7714f246d09eccb78b/href">https://medium.com/media/f05d400043b1cd7714f246d09eccb78b/href</a></iframe><p>The majority of this file deals with DOM manipulations and animations within jQuery. That is not the focus of the article so I will continue to the important parts.</p><p>In the entire file, these two lines are the most important:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7c13e84526c353876ee3b853b0e42ef0/href">https://medium.com/media/7c13e84526c353876ee3b853b0e42ef0/href</a></iframe><p>First, we open a socket connection with our Node server. Second, we wait for the Node server to emit a <strong>‘new post’ </strong>event. Third, the Node server emits an event when the database has changed. A JSON object is passed along to the client through the <strong>‘new post’ </strong>event. How you handle this data is up to you. In this case, I’m simply appending them to the DOM and keeping a maximum of five posts at any one time.</p><h3>I thought you said something about a bot?</h3><p>Start off by opening <strong>bot.coffee, </strong>as you can probably guess, we’ll be using CoffeeScript from now on. Once you open up the file, you should see a list of dependencies.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/00ed0cd9ecdf3cdd1bb11b441c7e724e/href">https://medium.com/media/00ed0cd9ecdf3cdd1bb11b441c7e724e/href</a></iframe><p>Each code snippet from here on should be appended to <strong>bot.coffee. </strong>Next, we configure Slack. You will need to visit the URL in the snippet to generate a bot and Slack Token.</p><h3>Slack Bot Configuration</h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8e25c11ec49d7677a362d88f8db457ca/href">https://medium.com/media/8e25c11ec49d7677a362d88f8db457ca/href</a></iframe><p>Next, we setup a simple object to store the bot’s options. For now, we are only using one option but this can be extended later.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/054195ea03dc5354e7bb17546bfc77ae/href">https://medium.com/media/054195ea03dc5354e7bb17546bfc77ae/href</a></iframe><p>Now, we will create a few helper functions to be used at a later time. Everything below is explained within the comments. If you want to use a different channel name, change <strong>‘blog’ </strong>to the name of your desired channel.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d2cc817aedee344319c2286f1a1763e/href">https://medium.com/media/8d2cc817aedee344319c2286f1a1763e/href</a></iframe><p>Due to the significance of indentation within CoffeeScript, I’ve included the rest of the file in a single gist. Again, everything is explained in the code’s comments.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3b109f243f39348b53e7779ff3f2c7bd/href">https://medium.com/media/3b109f243f39348b53e7779ff3f2c7bd/href</a></iframe><p>If you’d like to see the entire <strong>bot.coffee</strong> file you can find it<a href="https://gist.github.com/caseymorrisus/7a05b2b7139f680300ff"> here.</a></p><h3>Starting The Server</h3><p>We now have everything we need. You can start the Node server and bot in one command by entering <strong>npm start </strong>in the command line at document root. The page served at <a href="http://localhost.com/">http://localhost.com</a> will update in realtime when you post to Slack.</p><h3>Wrapping Up</h3><p>As you can see, it’s fairly easy to get something simple set up. The bot is looking a bit bare, but you can always add more features as they are needed. Some ideas for features and commands include:</p><ul><li>Admin permissions (only admins can use the bot)</li><li>Update posts using the bot</li><li>Delete posts using the bot</li><li>Command to list most recent posts in Slack</li></ul><p>Be warned, the code in this article was written as a prototype and proof of concept. It has since been refined, expanded upon, and changed for a production environment. If you use this in production, it is at your own risk. You can find examples in each resources documentation explaining what to do when deploying to production.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fcbf6e601fa5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>