<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Seth Lachman</title>
    <description>The latest articles on DEV Community by Seth Lachman (@stlachman).</description>
    <link>https://dev.to/stlachman</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F207595%2F10cc5d56-1fe0-43b0-a2c8-a97d527b79bd.jpeg</url>
      <title>DEV Community: Seth Lachman</title>
      <link>https://dev.to/stlachman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/stlachman"/>
    <language>en</language>
    <item>
      <title>Chunk Function (Lodash)</title>
      <dc:creator>Seth Lachman</dc:creator>
      <pubDate>Sat, 29 Feb 2020 20:21:24 +0000</pubDate>
      <link>https://dev.to/stlachman/chunk-function-lodash-3i1p</link>
      <guid>https://dev.to/stlachman/chunk-function-lodash-3i1p</guid>
      <description>&lt;p&gt;(This post first appeared on my personal &lt;a href="https://www.sethlachman.com/chunk-array"&gt;blog&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Recently, I decided it would be a good idea to recreate some of the functions used in the popular JavaScript utility library &lt;a href="https://lodash.com/"&gt;Lodash&lt;/a&gt; to test my understanding of JavaScript and also practice writing unit tests in Jest.&lt;br&gt;
For anyone unfamiliar with Lodash, it is a library that allows you to drop in various helper functions for performing common transformations.&lt;br&gt;
In most instances, it is better to use a library like Lodash over rolling your own implementations for these methods because the functions included&lt;br&gt;
in these libraries are extremely performant and have been tested in many different browsers and use cases. However, for educational purposes, I think it is a good idea to solve these problems since a library as popular as Lodash wouldn't be created to solve these problems otherwise.&lt;/p&gt;

&lt;p&gt;I decided to start with the array methods and work my way down the list. Here is the description of the &lt;a href="https://lodash.com/docs/4.17.15#chunk"&gt;chunk&lt;/a&gt; utility function from the documentation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Creates an array of elements split into groups the length of size. If array can't be split evenly, the final chunk will be the remaining elements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's restate the problem to be sure that we understand it. The chunk function will return an array of elements or 'groups'; these groups will be arrays and each group will contain as many elements as determined by the size argument passed into the function. In the event of leftover elements, the leftover elements will be placed into the final array. The documentation also states that if a size value is not provided, a default value of 1 will be applied for the size. Below is an example of what we wish to accomplish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;chunk&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;a&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;b&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;c&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;d&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// =&amp;gt; [['a', 'b'], ['c', 'd']]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We know that we will need an array to store the groups, so let's create a function expression chunk and declare a variable &lt;code&gt;chunkedValues&lt;/code&gt; and assign it to an empty array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;chunkedValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Notice the use of the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Default_parameters"&gt;default parameter&lt;/a&gt;.&lt;br&gt;
Next, let's think about how we might group values before pushing them into an array. If we loop through the given array, and instead of incrementing by one each time, we incremented by the size argument, we would effectively start at the beginning of each group. Considering the example above, if we loop through the array and increment each time by 2 (the size), on the first loop we would start at the virst value, on the second loop, the index would point to value 'c', which is the start of the next and final group. To collect the groups, we can use the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice"&gt;slice method&lt;/a&gt; which returns a copy of the array&lt;br&gt;
from the index given up to an end index (the end index is not included in the array). In other words, at the first item in the array (index 0), we would slice from 0 to 2 (0 + 2), which would return the array &lt;code&gt;['a', 'b']&lt;/code&gt; since the slice doesn't include index 2 in the final result. Below is the final code for reference.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;chunkedValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;chunkedValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;slice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;chunkedValues&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the slice method isn't clicking at the moment, let's consider what the slice method is doing in this function by recreating it. By using the slice method, we are (in effect) creating a new array within the initial&lt;br&gt;
for loop at each iteration (denoted by group in the code below) and then using an inner for loop to collect and push each of the values for this particular group. When the inner for loop terminates, we push group array into the &lt;code&gt;chunkedValues&lt;/code&gt; array. I've provided some example code below to illustrate this concept:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;chunk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;chunkedValues&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
  &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;group&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;
    &lt;span class="c1"&gt;// prevents the loop from adding undefined values in the group array&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nx"&gt;chunkedValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;group&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;chunkedValues&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;One thing to notice in the implementation above is the length variable. When using the slice method, if the end value provided to the method is greater than the length of the array, the method only takes the values up to and including the last item in the array. The logic in the length variable handles this case since it checks if the index plus the size argument is greater than the length of the array. If it is greater than the length of the array, then we assign it the value of the length of the array, otherwise we assign it to the index + the size argument.&lt;/p&gt;

&lt;p&gt;I hope this article encourages you to look into Lodash and implement some of the functions yourself (and remember, this is only one implementation of this function, and certainly not the best or most performant solution). If you want to take a look at my github repo with the test cases, you can check it out &lt;a href="https://github.com/stlachman/lodash-functions"&gt;here&lt;/a&gt;. If you have any comments, questions, or suggestions, please let me know!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>Form Validation</title>
      <dc:creator>Seth Lachman</dc:creator>
      <pubDate>Tue, 25 Feb 2020 16:22:38 +0000</pubDate>
      <link>https://dev.to/stlachman/form-validation-22ml</link>
      <guid>https://dev.to/stlachman/form-validation-22ml</guid>
      <description>&lt;p&gt;(This post first appeared on my personal &lt;a href="https://www.sethlachman.com/form-validation"&gt;blog&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Forms facilitate communication and transactions between users around the globe, from a contact form on a blog to the transfer of money&lt;br&gt;
through your online bank or your favorite e-commerce platform, forms are a necessary part of every user's experience of the web.&lt;br&gt;
It should go without saying then that a great, accessible form can make a world of difference in providing a pleasant user experience. I'm sure most people have experienced the frustration around trying to fill out a form with obscure error messages that prevent you from completing your task. As developers, it is our responsibility to provide an accessible experience that allows a user to complete their task without any friction.&lt;/p&gt;

&lt;p&gt;If you are using a JavaScript framework to build and manage forms, you are probably aware that many libraries exist within the JavaScript ecosystem to help solve some of the problems associated with forms (some notable examples include: &lt;a href="https://jaredpalmer.com/formik"&gt;formik&lt;/a&gt; and &lt;a href="https://react-hook-form.com/"&gt;react hook forms&lt;/a&gt;). However, you might not always need a JavaScript framework to solve this problem (especially if your form isn't very complex), so it is important to understand the best practices around creating a form.&lt;/p&gt;

&lt;p&gt;Let's begin this journey with an essential part of every form: validation. The simplest way to add validation to an input field is by adding the &lt;code&gt;required&lt;/code&gt; attribute to the field. If a user attempts to submit a form without filling out a required field, a message provided by the browser will notify the user to fill out the field. If the input doesn't have a value, the &lt;code&gt;:invalid&lt;/code&gt; psuedo-class will be assigned to that input which can then be styled. Click on the submit button in the example below to see the default error message applied by the browser.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/slach/embed/zYGNgEx?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;One of the things I was surprised to learn about when researching form validation is the number of attributes provided by HTML to aid in the validation process. A prime example of such an attribute is the pattern attribute. The pattern attribute allows you to define a regular expression to validate the given input against. So for example, if you create a form with a beverage field and you wanted to restrict the answer to coffee, you could write the following regex pattern &lt;code&gt;coffee&lt;/code&gt; to prevent the user from submitting the form unless that regex pattern is satisfied.&lt;/p&gt;

&lt;p&gt;&lt;iframe height="600" src="https://codepen.io/slach/embed/eYNgqQN?height=600&amp;amp;default-tab=result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Although the pattern above would mostly serve the purpose of frustrating a user, it is not too difficult to think of a better use case for the pattern attribute,for example validating a phone number. Another helpful tip I picked up in my research into the pattern attribute: if the input type is email,then that field is validated against an email address pattern without any extra configuration! Don't believe me? Go back to the first codepen example, enter a value that isn't an email address and report back on the results. Other validation attributes exist, such as &lt;code&gt;minlength&lt;/code&gt; and &lt;code&gt;maxlength&lt;/code&gt; (useful for specifying a minimum length for a password) but I will leave it as an exercise for the reader to explore some of these other validation attributes.&lt;/p&gt;

&lt;p&gt;A great resource for discovering other validation attributes is MDN, specifically this article on &lt;a href="https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation"&gt;Client-side form validation&lt;/a&gt;. Let me know if you have any questions or wish to point out anything that I failed to mention in this brief overview. In my next post, I will look into adding some more custom error messages using JavaScript.&lt;/p&gt;

</description>
      <category>html</category>
      <category>codenewbie</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
