<?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: CarlyRaeJepsenStan</title>
    <description>The latest articles on DEV Community by CarlyRaeJepsenStan (@carlyraejepsenstan).</description>
    <link>https://dev.to/carlyraejepsenstan</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%2F473556%2Fa406b097-1b61-4370-9e6a-9cfed13ebcbc.png</url>
      <title>DEV Community: CarlyRaeJepsenStan</title>
      <link>https://dev.to/carlyraejepsenstan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/carlyraejepsenstan"/>
    <language>en</language>
    <item>
      <title>Converting Hex to Text - the Bash Way</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Mon, 14 Mar 2022 22:27:06 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/converting-hex-to-text-the-bash-way-1483</link>
      <guid>https://dev.to/carlyraejepsenstan/converting-hex-to-text-the-bash-way-1483</guid>
      <description>&lt;p&gt;Let's start by pretending someone gave us some pure plain-text bytes, like this:&lt;br&gt;
&lt;code&gt;68656c6c6f20776f726c64&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;What are we going to do to find the hidden message?&lt;/p&gt;

&lt;p&gt;First, we know that hexadecimal bytes are always 2 characters - you can verify this by using &lt;code&gt;man ascii&lt;/code&gt; in your terminal and checking out the tables it shows. Next, we know that you can convert a hex byte into ASCII (or text) by using &lt;code&gt;echo&lt;/code&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo -e "\x61"

# a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What I thought we could do is split the hex string into an array of two-character pieces, and then use a loop to print them like above.  So deciphering our hex string is actually pretty straightforwards!&lt;/p&gt;

&lt;p&gt;You can use the command &lt;code&gt;fold&lt;/code&gt; to first split up the string:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; ~
  ▲ echo $(echo 68656c6c6f20776f726c64 | fold -w2))
68 65 6c 6c 6f 20 77 6f 72 6c 64
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's a cool fact: by default, anything with spaces is automatically split into a loopable array by bash!&lt;/p&gt;

&lt;p&gt;In case you forgot, you use arrays in bash using a rather odd notation: &lt;code&gt;${arrayname[location]}&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;So if we call our previous output &lt;code&gt;spaced&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for i in ${spaced[@]}; 
 do echo -n -e "\x$i";
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy!  Thanks for reading my short tutorial, and have a great day.  You never know when this might come in useful...&lt;/p&gt;

</description>
      <category>bash</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Decoding Binary - 3 Different Ways </title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Sun, 20 Feb 2022 02:40:25 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/decoding-binary-3-different-ways-6i1</link>
      <guid>https://dev.to/carlyraejepsenstan/decoding-binary-3-different-ways-6i1</guid>
      <description>&lt;p&gt;Hey everyone!  I recently saw this tweet:&lt;/p&gt;


&lt;blockquote class="ltag__twitter-tweet"&gt;

  &lt;div class="ltag__twitter-tweet__main"&gt;
    &lt;div class="ltag__twitter-tweet__header"&gt;
      &lt;img class="ltag__twitter-tweet__profile-image" src="https://res.cloudinary.com/practicaldev/image/fetch/s--euQ-vVgv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://pbs.twimg.com/profile_images/1436967784274092032/BxvksM62_normal.jpg" alt="anildash profile image"&gt;
      &lt;div class="ltag__twitter-tweet__full-name"&gt;
        anildash
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__username"&gt;
        &lt;a class="mentioned-user" href="https://dev.to/anildash"&gt;@anildash&lt;/a&gt;
      &lt;/div&gt;
      &lt;div class="ltag__twitter-tweet__twitter-logo"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ir1kO05j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-f95605061196010f91e64806688390eb1a4dbc9e913682e043eb8b1e06ca484f.svg" alt="twitter logo"&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__body"&gt;
      01110010 01100101 01101101 01100101 01101101 01100010 01100101 01110010 00100000 01101100 01100101 01100001 01110010 01101110 01101001 01101110 01100111 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00111111 &lt;a href="https://t.co/lF6HruWh5H"&gt;twitter.com/kelseyhightowe…&lt;/a&gt;
    &lt;/div&gt;
    &lt;div class="ltag__twitter-tweet__date"&gt;
      19:50 PM - 09 Feb 2022
    &lt;/div&gt;

      &lt;div class="ltag__twitter-tweet__quote"&gt;
        &lt;div class="ltag__twitter-tweet__quote__header"&gt;
          &lt;span class="ltag__twitter-tweet__quote__header__name"&gt;
            Kelsey Hightower
          &lt;/span&gt;
          @kelseyhightower
        &lt;/div&gt;
        Remember when we all learned Perl. https://t.co/rFLs2jqIMu
      &lt;/div&gt;

    &lt;div class="ltag__twitter-tweet__actions"&gt;
      &lt;a href="https://twitter.com/intent/tweet?in_reply_to=1491499682618654722" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fFnoeFxk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-reply-action-238fe0a37991706a6880ed13941c3efd6b371e4aefe288fe8e0db85250708bc4.svg" alt="Twitter reply action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/retweet?tweet_id=1491499682618654722" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--k6dcrOn8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-retweet-action-632c83532a4e7de573c5c08dbb090ee18b348b13e2793175fea914827bc42046.svg" alt="Twitter retweet action"&gt;
      &lt;/a&gt;
      &lt;a href="https://twitter.com/intent/like?tweet_id=1491499682618654722" class="ltag__twitter-tweet__actions__button"&gt;
        &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SRQc9lOp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev.to/assets/twitter-like-action-1ea89f4b87c7d37465b0eb78d51fcb7fe6c03a089805d7ea014ba71365be5171.svg" alt="Twitter like action"&gt;
      &lt;/a&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/blockquote&gt;


&lt;p&gt;Obviously as a programmer, this is what everyone thinks I do - talk about stuff in 1s and 0s.  Sadly, I didn't know what this said, so it's time to fix that.&lt;/p&gt;

&lt;p&gt;First, I tried figuring out how to convert binary to text in Rust.  After a short stint of searching on the combined intelligence of the entire world, I discovered the following useful commands: &lt;a href="https://doc.rust-lang.org/std/primitive.u8.html#method.from_str_radix"&gt;&lt;code&gt;u8::from_str_radix&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://doc.rust-lang.org/std/str/fn.from_utf8.html"&gt;&lt;code&gt;String::from_utf8&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now using these commands isn't super intuitive - in the docs for &lt;code&gt;u8::from_str_radix&lt;/code&gt;, this is true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_str_radix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick trip through &lt;code&gt;man ascii&lt;/code&gt; in my terminal revealed "A" to be 41 in base 16, 65 in base 10, and 101 in base 8.  But no 10!  Ignoring this oddity (maybe u8 tables aren't in &lt;code&gt;man ascii&lt;/code&gt;?), you can then chain the output from &lt;code&gt;from_str_radix&lt;/code&gt; into &lt;code&gt;from::utf8&lt;/code&gt; to get out a human-readable character.&lt;/p&gt;

&lt;p&gt;So, after some more interrogation of Stack Overflow and the friendly and intelligent Rust community, I finally got this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;    &lt;span class="nd"&gt;assert_eq!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nn"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_str_radix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="s"&gt;"01000001"&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="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()]),&lt;/span&gt;
        &lt;span class="nf"&gt;Ok&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"A"&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;So what's important here?  We first make sure the &lt;a href="https://en.wikipedia.org/wiki/Radix"&gt;radix&lt;/a&gt; (base) of our input is 2, and then because &lt;code&gt;String::from_&amp;lt;any byte type&amp;gt;&lt;/code&gt; only accepts vectors, we use the vector macro &lt;code&gt;vec!&lt;/code&gt; to ensure our input is a vector, and then finally feed it into &lt;code&gt;String::from&lt;/code&gt; to get out something readable.&lt;/p&gt;

&lt;p&gt;And because &lt;code&gt;from_str_radix&lt;/code&gt; puts out a result, and we're sure that our input is going to be valid, we can safely unwrap it to use the result as our byte. &lt;/p&gt;

&lt;p&gt;Great!  The hard part is done - all I need to do now is to loop through the tweet's content, feed the words into my script here, and then collect together the resulting bytes and join them together.  I won't give a full explanation, but in short &lt;code&gt;map&lt;/code&gt; performs a function on every element of an iterator and stores the result to be re-assembled into another array.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"01110010 01100101 01101101 01100101 01101101 01100010 01100101 01110010 00100000 01101100 01100101 01100001 01110010 01101110 01101001 01101110 01100111 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00111111"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;
        &lt;span class="nf"&gt;.split_whitespace&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
        &lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="nf"&gt;binary_to_ascii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="py"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="nf"&gt;.concat&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;pub&lt;/span&gt; &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;binary_to_ascii&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;input&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nn"&gt;String&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_utf8&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;vec!&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nn"&gt;u8&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;from_str_radix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;input&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="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()])&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&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;Output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Standard Error
   Compiling playground v0.0.1 (/playground)
    Finished dev [unoptimized + debuginfo] target(s) in 1.24s
     Running `target/debug/playground`
Standard Output
"remember learning binary?"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty cool, huh?  I never learned binary so...&lt;/p&gt;

&lt;p&gt;In any case, now it's time to switch gears and try doing it in the terminal!  Befitting a true hacker aesthetic, I decided I'll convert binary into text using only native shell commands - no Python or anything like that.  &lt;/p&gt;

&lt;p&gt;Since we don't have nice things like &lt;code&gt;from_radix&lt;/code&gt; and so on, we'll have to convert our base 2 numbers into text like this:&lt;br&gt;
Binary -&amp;gt; Hexadecimal&lt;br&gt;
Hexadecimal -&amp;gt; Text&lt;/p&gt;

&lt;p&gt;So, how do we change bases in the terminal?  We can use the built-in command &lt;a href="https://linux.die.net/man/1/bc"&gt;&lt;code&gt;bc&lt;/code&gt;&lt;/a&gt; (&lt;strong&gt;b&lt;/strong&gt;asic &lt;strong&gt;c&lt;/strong&gt;alculator) and the corresponding commands &lt;code&gt;obase&lt;/code&gt; (&lt;strong&gt;o&lt;/strong&gt;utput &lt;strong&gt;b&lt;/strong&gt;ase) and &lt;code&gt;ibase&lt;/code&gt; (&lt;strong&gt;i&lt;/strong&gt;nput &lt;strong&gt;b&lt;/strong&gt;ase) like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;me@my-UbuntuBook:~&lt;span class="nv"&gt;$ &lt;/span&gt;bc
bc 1.07.1
Copyright 1991-1994, 1997, 1998, 2000, 2004, 2006, 2008, 2012-2017 Free Software Foundation, Inc.
This is free software with ABSOLUTELY NO WARRANTY.
For details &lt;span class="nb"&gt;type&lt;/span&gt; &lt;span class="sb"&gt;`&lt;/span&gt;warranty&lt;span class="s1"&gt;'. 
obase=16;ibase=2;01110010
72 # HERE! 
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have &lt;code&gt;72&lt;/code&gt;, which maps to a corresponding character's hex code, we can convert it into a character using a reverse hexdump!  While tools like &lt;code&gt;od&lt;/code&gt; and &lt;code&gt;hexdump&lt;/code&gt; can convert characters into hexadecimal codes, only &lt;code&gt;xxd&lt;/code&gt; provides a way to reverse it via the &lt;code&gt;-r&lt;/code&gt; flag.  For example, if we have a file only with &lt;code&gt;72&lt;/code&gt; inside, and then reverse xxd it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;me@my-UbuntuBook:~$ cat has_seventy_two_inside
72
me@my-MacBookUbuntuBook:~$ xxd -r -p has_seventy_two_inside
r
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;-p&lt;/code&gt; flag means "plain", and outputs the result without line numbers and all that.  For some reason if I don't have that flag, the output is blank so I don't know why?  If you have any ideas, drop a comment! &lt;/p&gt;

&lt;p&gt;Cool huh?  But - we can't get arbitrary input into a running &lt;code&gt;bc&lt;/code&gt;, and it's going to be a huge pain to have to type everything in, and then make files to &lt;code&gt;xxd -r&lt;/code&gt; on.  So let me introduce you to &lt;a href="https://www.geeksforgeeks.org/piping-in-unix-or-linux/"&gt;piping&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Piping using the pipe character &lt;code&gt;|&lt;/code&gt; lets us move output from one command into another, or have a command take input from a previous one.  For example, we could do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;me@my-UbuntuBook:~&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"1+2"&lt;/span&gt; | bc
3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Cool!  So we can chain all our aforementioned commands together like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"obase=16; ibase=2; &lt;/span&gt;&lt;span class="nv"&gt;$BYTES_HERE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | bc | xxd &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Elegant, no?  And because &lt;code&gt;bash&lt;/code&gt; automatically turns strings into iterators split by string, I can skip splitting the string and just go straight to looping:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"01110010 01100101 01101101 01100101 01101101 01100010 01100101 01110010 00100000 01101100 01100101 01100001 01110010 01101110 01101001 01101110 01100111 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00111111"&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;i &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nv"&gt;$a&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
 &lt;span class="k"&gt;do &lt;/span&gt;&lt;span class="nb"&gt;echo&lt;/span&gt; &lt;span class="s2"&gt;"obase=16; ibase=2; &lt;/span&gt;&lt;span class="nv"&gt;$i&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; | bc | xxd &lt;span class="nt"&gt;-r&lt;/span&gt; &lt;span class="nt"&gt;-p&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;done&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(sorry for the bad variable names)&lt;/p&gt;

&lt;p&gt;Yay!  That took quite a while to solve, but gives a nice satisfactory result.&lt;/p&gt;

&lt;p&gt;And finally, everyone's favorite language - JavaScript.  Not to brag or anything, but I golfed (one-lined) this problem in 2 minutes:&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;a&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;01110010 01100101 01101101 01100101 01101101 01100010 01100101 01110010 00100000 01101100 01100101 01100001 01110010 01101110 01101001 01101110 01100111 00100000 01100010 01101001 01101110 01100001 01110010 01111001 00111111&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;

&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt; &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fromCharCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&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="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Easy peezy lemon squeezy. &lt;/p&gt;

&lt;p&gt;So how does this work?  The &lt;code&gt;.split()&lt;/code&gt; method on a string divides the string into an array by chopping it up at each argument passed into &lt;code&gt;split&lt;/code&gt;.  In this case, I passed a single space so the string of bytes got split up into an array of bytes.  Next, just like in the Rust solution, I mapped a function that consumes binary information, converts it into a character code of some sort, and then converts the character code into a human-readable letter.  More specifically, &lt;code&gt;parseInt&lt;/code&gt; accepts two argument: a string and then a radix (in that order), and converts it into base 10.  &lt;code&gt;String.fromCharCode&lt;/code&gt; is essentially a reverse decimal dump; it accepts base-10 numbers, and outputs their corresponding character.  And finally, because we output an array of letters, to put all the letters back together into a sentence, we use &lt;code&gt;.join&lt;/code&gt; on an array with no separator so everything just gets mashed together.  And with that, we get the same result.&lt;/p&gt;

&lt;p&gt;Hopefully this helped you get a role as a master hacker who can read and decode binary in 2022's Most Awesome Upcoming Hacker Action Movie or at least impress your non-programmer parents, so if you learned something, click all the reactions on the side, and even if you didn't, do it anyway!&lt;/p&gt;

&lt;p&gt;Thanks for reading, and see you next time!  &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>rust</category>
      <category>bash</category>
      <category>binary</category>
    </item>
    <item>
      <title>Do We Live In the Matrix?</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Mon, 17 Jan 2022 22:26:02 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/do-we-live-in-the-matrix-21h6</link>
      <guid>https://dev.to/carlyraejepsenstan/do-we-live-in-the-matrix-21h6</guid>
      <description>&lt;p&gt;Allow me to introduce you to the beautiful world of 3D rendering, by posing a simple question - do we live in the "real" world? &lt;/p&gt;

&lt;p&gt;Whether or not we experience true meaning in our daily experiences has been the topic of countless metaphysical or philosophical discussions.  But, I don't really care about that, neither do I know a lot about it - but what I do know, is how we can rebuild the world around us on the computer.  &lt;/p&gt;

&lt;p&gt;Let's begin with a simple assumption: the world we live in is truly, deeply, (rather madly) a giant computer simulation.  &lt;/p&gt;

&lt;h1&gt;
  
  
  Part 1: You're Smooth!
&lt;/h1&gt;

&lt;p&gt;Behold, the humble circle:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mCv6VwJz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqxq6d7weh7h0zvc0vs7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mCv6VwJz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqxq6d7weh7h0zvc0vs7.png" alt="circle lol" width="254" height="224"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This shape with infinite sides, at least as you perceive on your device, is not truly a circle.  If you were to zoom in - go ahead, I'll wait - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IrLxrJcv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oex97hzd3bb2itedrm4c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IrLxrJcv--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oex97hzd3bb2itedrm4c.png" alt="zoomed in circle" width="256" height="324"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You'll see that this "circle" is actually a set of squares.  This conversion of curved things, etc into a bunch of pixels is what's called "rasterization."  Early in the study of making realistic 3D scenes on a computer, everything, and I mean EVERYTHING was governed by raster engines.  3D renderers on browsers like &lt;a href="https://threejs.org"&gt;THREE.js&lt;/a&gt; are built on top of WebGL, which is a &lt;a href="https://webgl2fundamentals.org/webgl/lessons/webgl-fundamentals.html"&gt;rasterizer&lt;/a&gt; capable only of drawing 2d stuff.  Making our 2D drawings look 3D is a whole other article!  &lt;/p&gt;

&lt;p&gt;But for now, look closely at anything around you that isn't on your computer, such as, say, your arm.  Look REALLY close!  Are you seeing pixels yet?  Grab a magnifying glass or a pocket microscope and take a good look at anything.  If everything around you is dissolving into squares, close Minecraft.  Otherwise, keep reading!&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 2 - But Not Too Smooth
&lt;/h1&gt;

&lt;p&gt;Just because you can zoom in forever and see smoothness doesn't immediately prove that what you're seeing is super real - it's possible that your surroundings are just super high resolution.  For example, to see the pixels on a 16k monitor, you would &lt;em&gt;probably&lt;/em&gt; need a microscope or magnifying glass to identify the pixels.  &lt;/p&gt;

&lt;p&gt;So, take a look at this: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lb-GPDvs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rru89pqq4zhg9i28u0y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lb-GPDvs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9rru89pqq4zhg9i28u0y.png" alt="donut" width="648" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you know where this is from, congratulations!  Click all the reactions in the side, if you haven't already.  In any case...&lt;br&gt;
Ah, what beauteous smoothness!  So smooth, like if you bit into it, your teeth would slide right off its plastic exterior.  There is nothing in this world so perfectly and uniformly smooth like this, even plastic items, or Teflon.  &lt;/p&gt;

&lt;p&gt;However, imperfections are easy to make - things like &lt;a href="https://en.wikipedia.org/wiki/Perlin_noise"&gt;Perlin noise&lt;/a&gt; are the bread and butter of CGI.  In fact: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NMyjJoRG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r0htvkiiub6p7p6b2pc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NMyjJoRG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2r0htvkiiub6p7p6b2pc.png" alt="final donut" width="806" height="626"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This donut has been given a plush, extremely unhealthy texture thanks some magic involving noise textures.  &lt;/p&gt;

&lt;p&gt;So these fresh donuts near you may very well be high resolution rendered images.  So that's why its time to do some mirror gazing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 3: Oh Looking Glass...
&lt;/h1&gt;

&lt;p&gt;Do you have a mirror nearby?  Anything shiny and reflective, like a phone screen?  If you do, you'll notice that not only do reflective surfaces show you your beauteous visage, they also throw back a little bit of light.  However, with a rasterizer, mirrors are only capable of showing an accurate reflection - they can't show all the light bouncing everywhere. &lt;/p&gt;

&lt;p&gt;Now you're probably thinking, a ha!  We live in true reality, because our mirrors look pretty and glowy.  Not quite, because ray tracing exists.&lt;/p&gt;

&lt;p&gt;You may have heard a fair amount about ray tracing, with game companies, tech writers, and everyone in between tripping over themselves to proclaim a new wave of &lt;del&gt;things for anonymous bloggers to pretend to know a lot about&lt;/del&gt; hyperrealism.  &lt;/p&gt;

&lt;p&gt;What makes ray tracers so awesome?  We can try comparing these two images:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Zq9a-50e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ums1j7jdaf8a1aaqyqzn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Zq9a-50e--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ums1j7jdaf8a1aaqyqzn.png" alt="CYCLES" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HK-tKwZu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1m8f2r22mmogai3unwm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HK-tKwZu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1m8f2r22mmogai3unwm.png" alt="EEVEE" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The lower one was made using Blender's rasterizer (EEVEE) while the other was made with Cycles, Blender's ray tracer.  As you can tell, ray tracing makes much better reflections and overall realism - the shadows are more defined in the right places with ray tracing, and the reflection is much less warped.&lt;/p&gt;

&lt;p&gt;However, ray tracing is not a panacaea - it has its own limitations, which I'll cover later.  &lt;/p&gt;

&lt;p&gt;But for now, you can rest assured that ray tracing can provide physically accurate shadows, refractions, and reflections.  Now it's time to take a plunge and figure out how to break ray tracing.&lt;/p&gt;

&lt;h1&gt;
  
  
  Part 4: Splish Splash, What a Flash!
&lt;/h1&gt;

&lt;p&gt;Water is the kryptonite of computers.  And no, I'm not just talking about when you accidentally tipped a glass over your keyboard.  &lt;/p&gt;

&lt;p&gt;Caustics are what reclusive nerds call indirect lighting, and are responsible for some of the most beautiful things you can witness:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HSC6c9WR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbp4l6ms5qirdvj7mm0z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HSC6c9WR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xbp4l6ms5qirdvj7mm0z.png" alt="Image description" width="880" height="1173"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nSU24rdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpavr2kvp2ngh83lsglf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nSU24rdo--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fpavr2kvp2ngh83lsglf.png" alt="Image description" width="880" height="859"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Note the rainbows dispersed by the diamond - I'll cover this in just a sec). &lt;/p&gt;

&lt;p&gt;Because they look so awesome, caustics are also a &lt;a href="https://www.chaos.com/blog/what-are-caustics-and-how-to-render-them-the-right-way"&gt;huge pain to render accurately&lt;/a&gt;.  Think about it this way - for indirect lighting to be accurate, you need a LOT of photons.  A 1-amp LED projects 6E18 photons every second, while a 1 terabyte hard drive can only hold 1E12 bytes, so to even come close to having caustics not look like this:&lt;/p&gt;

&lt;p&gt;You'd need to fill your entire hard drive, and then some.  And even if we give each photon vector only 12 bounces max and assign each bounce, say, 15 operations, a 12 GHz (12E6 operations/second) processor would take a very, very long time to make a pretty diamond picture.  PAIN!  &lt;/p&gt;

&lt;p&gt;And if you've taken a physics class recently, you'll remember that when white light is diffracted, it splits into a rainbow, explaining the diamond's fire a few paragraphs up.  But, because physically accurate ray tracers treat light as a vector and not a set of waves, rainbows &lt;a href="https://www.reddit.com/r/blender/comments/rotlkb/caustics_luxcorerender_vs_blender/"&gt;don't show up!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;While there's some &lt;a href="https://blender.stackexchange.com/questions/40226/how-to-construct-a-diamond-or-other-gemstone-material-in-cycles"&gt;level of faking&lt;/a&gt; you can do to make rainbows inside of diffractive materials, these again aren't physically accurate.  So that's why we need &lt;em&gt;spectrally accurate&lt;/em&gt; ray tracers.  These treat light as a set of waves, and the math behind them is pretty difficult, but they do exist but aren't real time. &lt;/p&gt;

&lt;p&gt;And rendering water is only half the problem.  If you've ever tried Blender's fluid physics simulations, you need to "bake" (compute) the fluids moving around (I'm not sure if a real time fluid simulator exists).  For a low resolution simulation with one or two vertices in the way, baking takes a few seconds, but the water flow ends up looking weird:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--G1EnTdpU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47qgnyeczodm8p5zrx0o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--G1EnTdpU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/47qgnyeczodm8p5zrx0o.png" alt="blender water" width="466" height="516"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For a more realistic, higher resolution sim, baking can go up to twenty minutes!  When you accidentally shatter a glass of water of the ground, the world doesn't freeze as the direction of the water is computed.  Also, this doesn't just apply to water - fluids, like water and smoke are also a difficult to realistically render in a short amount of itme. &lt;/p&gt;

&lt;p&gt;Hopefully in this article, I've convinced you that the world you live in isn't CGI.  If you just skipped everything I wrote (I don't blame you!) here's a tl;dr: fluids take time to render.  And even if I haven't, you can probably go work for Pixar or explain why old movies look fake.  Thanks for reading, leave a comment if you think I said something wrong, and see you in my next article! &lt;/p&gt;

</description>
      <category>3d</category>
      <category>design</category>
      <category>writing</category>
      <category>webgl</category>
    </item>
    <item>
      <title>Regular expressions in Rust</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Fri, 18 Jun 2021 03:20:59 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/regular-expressions-in-rust-5c1c</link>
      <guid>https://dev.to/carlyraejepsenstan/regular-expressions-in-rust-5c1c</guid>
      <description>&lt;p&gt;In many popular languages such as Javascript or Python, using regular expressions is simple.  Well.. &lt;em&gt;mostly&lt;/em&gt; simple. &lt;/p&gt;

&lt;p&gt;For example, let's say I have this inscrutable JS string: &lt;br&gt;
&lt;code&gt;let a = "123bbasfsdf23asd2021-06-17";&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;For reasons known only to me, I desire to find numbers that are at most 3 digits long - specifically, I want to find the number "23."  &lt;/p&gt;

&lt;p&gt;The solution?  A regular expression.  In JavaScript syntax, &lt;code&gt;\d&lt;/code&gt; is a number, and putting &lt;code&gt;{1,3}&lt;/code&gt; after the digit marker means we search for any group of 1 to 3 adjacent digits&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;let&lt;/span&gt; &lt;span class="nx"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="se"&gt;\d{1,3}&lt;/span&gt;&lt;span class="sr"&gt;/gi&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123bbasfsdf23asd2021-06-17&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;match&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;regex&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="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;123&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="s2"&gt;23&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="s2"&gt;202&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="s2"&gt;1&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="s2"&gt;06&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="s2"&gt;17&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was easy!  Now let's try to reproduce this in Rust.  As Rust is a lower-level language focused on much different things than Javascript, the process is much different.&lt;/p&gt;

&lt;p&gt;First, we have to use the &lt;a href="https://docs.rs/regex"&gt;&lt;code&gt;regex&lt;/code&gt;&lt;/a&gt; crate, which thankfully uses similar expression syntax to JavaScript.  So to recreate our previous example, we first have to import the crate by both putting it into &lt;code&gt;Cargo.toml&lt;/code&gt; under &lt;code&gt;[dependencies]&lt;/code&gt; and by beginning our code with a &lt;code&gt;use&lt;/code&gt; statement.  So our beginning code is fairly simple:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;regex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;r"\d{3}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&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;Some of this might not make sense:&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://doc.rust-lang.org/reference/tokens.html#raw-string-literals"&gt;&lt;code&gt;r&lt;/code&gt;&lt;/a&gt; in front of our regular expression makes it so we don't have to spam backslashes to make our strings valid.  Especially useful for backslash-heavy regexes! &lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.unwrap()&lt;/code&gt; comes from &lt;a href="https://doc.rust-lang.org/stable/book/ch06-01-defining-an-enum.html?highlight=option#the-option-enum-and-its-advantages-over-null-values"&gt;&lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt;&lt;/a&gt; which basically replaces &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; with &lt;code&gt;None&lt;/code&gt;.  I imagine these regexes are Options because there are cases (such as in user input) where the regex can indeed be empty.  But, because we are hardcoding the value of the regular expression, we can safely call &lt;code&gt;unwrap&lt;/code&gt;, which exposes the string value of the regular expression and allows us to do stuff with it without having to do pattern matching or whatever.  &lt;/p&gt;

&lt;p&gt;Having written these simple lines, our problems begin.  In the &lt;a href="https://docs.rs/regex/1.5.4/regex/struct.Regex.html"&gt;documentation for Regex&lt;/a&gt;, there's no &lt;code&gt;match&lt;/code&gt; function.  There are &lt;code&gt;find&lt;/code&gt; and &lt;code&gt;capture&lt;/code&gt;, but each of these only return the first leftmost match - insufficient for my aim of finding the second match.  &lt;/p&gt;

&lt;p&gt;After lots of thinking and help from the Rust community, I finally decided on &lt;code&gt;find_iter&lt;/code&gt;, which returns an &lt;code&gt;Iterator&lt;/code&gt; composed of all the matches.  Does it work?  Note that I used the regex directly, skipping assigning it to a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;cap&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="nn"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;r"\d{1,3}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.find_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:#?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cap&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Match&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Match&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Match&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;Match&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;end&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="c"&gt;// --snip &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So yes, this works.  But it's also practically useless - trying to log &lt;code&gt;cap.start&lt;/code&gt; or &lt;code&gt;cap.end&lt;/code&gt; throws an error about "private fields."  After even more thinking, scrolling through documentation and conversing with friendly Discord members, I finally found the &lt;a href="https://docs.rs/regex/1.5.4/regex/struct.Match.html#method.as_str"&gt;&lt;code&gt;as_str&lt;/code&gt;&lt;/a&gt; method.  &lt;/p&gt;

&lt;p&gt;Side note, putting &lt;code&gt;:?&lt;/code&gt; in the curly braces of &lt;code&gt;println&lt;/code&gt; lets you log stuff that empty braces can not format, while &lt;code&gt;:#?&lt;/code&gt; prettyprints it.  &lt;a href="https://doc.rust-lang.org/std/fmt/"&gt;Read more about it&lt;/a&gt; - its cool!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nn"&gt;regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="n"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"123bbasfsdf23asd2021-06-17"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;cap&lt;/span&gt; &lt;span class="n"&gt;in&lt;/span&gt; &lt;span class="nn"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;r"\d{1,3}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.find_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:#?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cap&lt;/span&gt;&lt;span class="nf"&gt;.as_str&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="s"&gt;"123"&lt;/span&gt;
&lt;span class="s"&gt;"23"&lt;/span&gt;
&lt;span class="s"&gt;"202"&lt;/span&gt;
&lt;span class="s"&gt;"1"&lt;/span&gt;
&lt;span class="s"&gt;"06"&lt;/span&gt;
&lt;span class="s"&gt;"17"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Yay!!  Now I have my clean, useable outputs.  But how do I turn this into an array?  &lt;/p&gt;

&lt;p&gt;In Rust, an &lt;a href="https://doc.rust-lang.org/rust-by-example/trait/iter.html"&gt;&lt;code&gt;Iterator&lt;/code&gt;&lt;/a&gt; is essentially a fancy array that makes looping and recursive functions easier.  As vectors (as arrays are named in Rust) can be easily turned into iterators through &lt;code&gt;into_iter&lt;/code&gt;, iterators can be turned back into vectors through &lt;code&gt;collect&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;However, just running &lt;code&gt;Regex::new(r"\d{1,3}").unwrap().find_iter(a).collect()&lt;/code&gt; doesn't work - not only do we have to write some type annotations, we get an error that we can't collapse a match iterator into a clean string vector.  &lt;/p&gt;

&lt;p&gt;The solution?  Use the incredible &lt;code&gt;map&lt;/code&gt; function (which every developer should know about!) and apply &lt;code&gt;as_str&lt;/code&gt; to every item of the iterator.  Slap on a type annotation (as Rust requires) and some random borrowing, and voila:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;my_captures&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Vec&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&amp;amp;&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Regex&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;r"\d{1,3}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.unwrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.find_iter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.map&lt;/span&gt;&lt;span class="p"&gt;(|&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="nf"&gt;.as_str&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;&lt;span class="nf"&gt;.collect&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{:?}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_captures&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
     &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;"123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"23"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"202"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"06"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"17"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great!  Now use bracket notation: &lt;code&gt;my_captures[1]&lt;/code&gt;, and you're done!  Try it yourself &lt;a href="https://play.rust-lang.org/?version=stable&amp;amp;mode=debug&amp;amp;edition=2018&amp;amp;gist=2691c4ff74771c9b2d0d749cdeba3d3c"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Hopefully this article was helpful to you - I spent over an hour banging my head against documentation and Discord to solve this.  Thanks for reading, and good luck! &lt;/p&gt;

</description>
      <category>rust</category>
      <category>regex</category>
      <category>guide</category>
    </item>
    <item>
      <title>A one-click cookie deleter</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Fri, 23 Oct 2020 00:08:33 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/a-one-click-cookie-deleter-3mfa</link>
      <guid>https://dev.to/carlyraejepsenstan/a-one-click-cookie-deleter-3mfa</guid>
      <description>&lt;p&gt;Are you sick of clearing your cookies and getting logged out of literally everything? Use this code snippet instead!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;javascript:(()=&amp;gt;{document.cookie.split(";").map(o=&amp;gt;o.split("=")).map(o=&amp;gt;o[0]).forEach(o=&amp;gt;document.cookie =`${o}=;max-age=-100`);})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Visit a random site and add it as a bookmark.&lt;/li&gt;
&lt;li&gt;Control-click, right-click, tap with two fingers, whatever... click "edit" and paste the above code into the "Address" box.
2a. NOTE! Some browsers automatically strip the &lt;code&gt;javascript:&lt;/code&gt; part from the beginning.  Check to make sure that the bookmarklet address starts with &lt;code&gt;javascript:&lt;/code&gt;.  If it doesn't, this won't work.&lt;/li&gt;
&lt;li&gt;Visit some site (like Glitch) and click on the bookmarklet.  Poof!  No more cookies.
(You might get logged out).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;How does this work?  Here's the un-minified, readable version:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;javascript: (() =&amp;gt; {        
document.cookie.split(";")
  .map(o =&amp;gt; o.split("="))
  .map(o =&amp;gt; o[0])
  .forEach(o =&amp;gt; document.cookie = `${o}=;max-age=-100`);
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does each part work?&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;javascript:&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;This tells the browser that the link is a javascript script.  Otherwise, you'll perform a search for "(()=&amp;gt;{....."&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;(() =&amp;gt; {})()&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;These are very cool functions - they're &lt;strong&gt;A&lt;/strong&gt;nonymous &lt;strong&gt;S&lt;/strong&gt;elf-&lt;strong&gt;I&lt;/strong&gt;nvoking &lt;strong&gt;F&lt;/strong&gt;unctions, or ASIFs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;() =&amp;gt; {}&lt;/code&gt;
The above is the most concise form of a function possible, using ES6 arrow syntax.
Before ES6, functions were:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const functionName = function(parameters) {
//do something...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With arrow functions, they became:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const functionName = (parameters) =&amp;gt; {
//do something...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note the A in ASIF - "anonymous".  Just like people with no names, functions with no names are anonymous functions.   So, this is an anonymous arrow expression.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;()&lt;/code&gt; (around function)&lt;br&gt;
The set of parentheses around the anonymous arrow function is called a &lt;a href="https://tensor-programming.com/anonymous-functions-in-javascript/"&gt;"closure"&lt;/a&gt;.  As you can see in the article, the point of the closure is to avoid naming conflicts.  This actually served me well - on sites like Google and Twitter, there were already variables called "a" and "b".  😂&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;()&lt;/code&gt; (end of function)&lt;br&gt;
How does a function call its friends?  With parentheses!&lt;br&gt;
Ok, not a funny joke.  You should remember how to use functions with names:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const functionName = (parameters) =&amp;gt; {
//do something...
}

functionName(params)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What happens if the function doesn't have a name and doesn't have parameters?  That's right, it's just:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simplicity is beautiful.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;document.cookie.split(";");&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;If you go the console on some random site and run &lt;code&gt;document.cookie&lt;/code&gt;, you'll see cookie syntax looks like this:&lt;br&gt;
&lt;code&gt;"key=value; key2=value2; ..."&lt;/code&gt;. &lt;br&gt;
By using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split"&gt;&lt;code&gt;String#split&lt;/code&gt;&lt;/a&gt; method, we return an array of all the individual keys and values, like &lt;code&gt;["key=value", "key2=value"]&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  &lt;code&gt;.map(o =&amp;gt; o.split("="))&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Using the &lt;a href="https://www.w3schools.com/jsref/jsref_map.asp"&gt;&lt;code&gt;.map&lt;/code&gt;&lt;/a&gt; array iterator method, we go through and split apart each key and value pair into two different values.&lt;br&gt;
This line then produces an array of arrays:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
 ["key", 
   "value"
 ], 
 ["key2", 
  "value"
 ]
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  &lt;code&gt;.map(o =&amp;gt; o[0])&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Now, we need to get the cookie keys.  (Trust me, there's an end to this!).  Note that the key is the first position of each array element - we simply need to iterate through and grab that key, and then make a new array.  Now &lt;code&gt;c&lt;/code&gt; looks like &lt;code&gt;["key", "key2"]&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;code&gt;1.forEach(o =&amp;gt; document.cookie =&lt;/code&gt;${o}=;max-age=-100&lt;code&gt;);&lt;/code&gt;`
&lt;/h2&gt;

&lt;p&gt;Unlike the &lt;code&gt;.map&lt;/code&gt; iterators which make new arrays, &lt;a href="https://www.w3schools.com/jsref/jsref_forEach.asp"&gt;&lt;code&gt;Array#forEach&lt;/code&gt;&lt;/a&gt; basically provides a compressed &lt;code&gt;for...&lt;/code&gt; loop - it does something for each element.  We're taking each array of the element (which is the key), setting it to nothing, and then making the &lt;code&gt;max-age&lt;/code&gt; negative so the cookie expires immediately.&lt;/p&gt;

&lt;p&gt;Thanks for reading!  If you made it all the way to the end, a pat on the back and a free mask for you 🤿, and if you found any mistakes or have any thoughts, don't be afraid to comment!&lt;/p&gt;

&lt;p&gt;Credits: &lt;a href="https://twitter.com/jcurtiswood"&gt;Josh Wood&lt;/a&gt; for the tips on Twitter, and &lt;a href="https://javascript-minifier.com"&gt;Javascript Minifier&lt;/a&gt; for cleaning up my code. &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>cookie</category>
      <category>storage</category>
    </item>
    <item>
      <title>How Do You Make That? - Javascript Random Function</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Fri, 02 Oct 2020 20:56:34 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/how-do-you-make-that-javascript-random-function-10n4</link>
      <guid>https://dev.to/carlyraejepsenstan/how-do-you-make-that-javascript-random-function-10n4</guid>
      <description>&lt;p&gt;For quite a while now, if I wanted to generate a random number between two other numbers, I'd use the functions written in the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random" rel="noopener noreferrer"&gt;&lt;code&gt;Math.random() MDN docs&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But I never really took the time to understand them.  How do they work?  I'll explain that, in this article.&lt;/p&gt;

&lt;h1&gt;
  
  
  1. &lt;code&gt;Math.random()&lt;/code&gt;, &lt;code&gt;Math.floor()&lt;/code&gt;, and &lt;code&gt;Math.ceil()&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;code&gt;Math.random()&lt;/code&gt; is simple!  It outputs a random decimal number between 0 to 1.&lt;br&gt;&lt;br&gt;
&lt;code&gt;Math.floor()&lt;/code&gt; and &lt;code&gt;ceil()&lt;/code&gt; are a little different - they always round down and up, respectively.   For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var firstnum = 1.6
Math.floor(firstnum)
&amp;gt;&amp;gt;&amp;gt; 1
var secondnum = 1.2
Math.ceil(secondnum)
&amp;gt;&amp;gt;&amp;gt; 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  2. Random Decimals
&lt;/h1&gt;

&lt;p&gt;If you try using &lt;code&gt;Math.random&lt;/code&gt; on sites like &lt;a href="//jsconsole.com"&gt;jsconsole&lt;/a&gt;, you'll see it always puts out decimals.&lt;br&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsb9zu3yisvr7pttio4li.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fsb9zu3yisvr7pttio4li.png" alt="Screen Shot 2020-10-02 at 1.34.21 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So if whatever you're doing is ok with decimals, you can use a function like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRandomArbitrary(min, max) {
  return Math.random() * (max - min) + min;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How does it work?&lt;br&gt;&lt;br&gt;
Well first, we need the &lt;code&gt;return&lt;/code&gt; statement - otherwise the function doesn't do anything.&lt;br&gt;
Next, you have &lt;code&gt;Math.random() * (max - min)&lt;/code&gt;.  This multiplies the difference between your two numbers by the decimal less than 1 that &lt;code&gt;Math.random()&lt;/code&gt; produces.  For example, if your range is 1 to 10, than the difference is nine - multiply this by a number less than 1, and it becomes less than nine.  &lt;/p&gt;

&lt;p&gt;To ensure that you don't get a number out of the range (say, 9 * 0.0007 [which would produce 0.0063 which is less than 1!]), it is then added to the minimum number (in this case, 1).&lt;/p&gt;
&lt;h1&gt;
  
  
  3. Random Integers
&lt;/h1&gt;

&lt;p&gt;Ah, the function that I have copy and pasted the most.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRandomInt(min, max) {
  min = Math.ceil(min);
  max = Math.floor(max);
  return Math.floor(Math.random() * (max - min) + min); //The maximum is exclusive and the minimum is inclusive
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one's a little more complicated - it returns random whole numbers, or integers.  The helpful people at MDN have added a comment saying "The maximum is exclusive and the minimum is inclusive".  How does this work?  Why use Math.ceil and floor?&lt;/p&gt;

&lt;p&gt;Imagine for a moment that you are using the function, and put in 1.2 as the minimum, and 10.6 for the maximum.  What kind of numbers would you expect to come out of this function?  Any number from 2 to 10.  If you were to simply use &lt;code&gt;Math.round()&lt;/code&gt; on the minimum and maximum, your bounds would become 1 to 11 - which are too big! &lt;/p&gt;

&lt;p&gt;Lastly, we again use &lt;code&gt;Math.floor&lt;/code&gt; instead of &lt;code&gt;Math.round&lt;/code&gt; or &lt;code&gt;ceil&lt;/code&gt;- now that the bounds are from 2 to 10, using Math.round or ceil could result in a 2 being unfairly excluded from the results, or the number 11 being returned. &lt;/p&gt;

&lt;p&gt;Helpful?  Interesting?  Too simple?  Well, it's for beginners 😉.  Leave a comment! &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>beginners</category>
      <category>math</category>
    </item>
    <item>
      <title>How Do You Make That? - CSS Razer Animation</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Tue, 29 Sep 2020 05:24:47 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/how-do-you-make-that-css-razer-animation-4opo</link>
      <guid>https://dev.to/carlyraejepsenstan/how-do-you-make-that-css-razer-animation-4opo</guid>
      <description>&lt;p&gt;Part 1 of "How Do You Make That? - Breaking Down Web Animations".&lt;/p&gt;

&lt;p&gt;For this article, we'll be looking at this cool codepen I discovered recently:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/Huhtamaki/embed/GPzwPY?height=600&amp;amp;default-tab=css,result&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;It's.  So.  Cool!  So, I have to make it myself.  &lt;/p&gt;

&lt;p&gt;A quick look at the code reveals some cool things.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;There's only one element - a div!&lt;/li&gt;
&lt;li&gt;The shadow/glowing effect seems to be coming from two pseudo elements.&lt;/li&gt;
&lt;li&gt;It's very complicated...&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So, let's start breaking it down!&lt;/p&gt;

&lt;h1&gt;
  
  
  1 - Pseudoelements Galore
&lt;/h1&gt;

&lt;p&gt;Let's jump about halfway through the code to something I haven't worked with a lot - the pseudoelements &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt;.  The developer of this codepen accomplishes this amazing animation with only one element using these selectors.  How?  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;:after:&lt;/code&gt; and &lt;code&gt;:before&lt;/code&gt; can add elements to a page without explicitly declaring them with HTML tags.  &lt;a href="https://www.w3schools.com/cssref/tryit.asp?filename=trycss_sel_after"&gt;This W3Schools example&lt;/a&gt; is a good way to understand them.  &lt;/p&gt;

&lt;p&gt;These pseudoelements - so named because they exist but aren't true HTML elements (they can't be selected with DOM functions and such) - don't just have to be text - they can also be styles.  For example, we can make a pseudoelement that's a little bigger than the original, and put it behind.&lt;br&gt;&lt;br&gt;
&lt;strong&gt;IMPORTANT!&lt;/strong&gt; When using z-index, you have to set position to absolute.  So, we get this beauty: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--nL-D7Pia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uxrcs6bhxmikr7e52fvw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--nL-D7Pia--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/uxrcs6bhxmikr7e52fvw.png" alt="Screen Shot 2020-09-28 at 9.40.01 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It looks... sorta alright.  How do we get that sexy border around it?  The answer is in these two lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;left: -2px;
top: -2px;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Yup.  It makes sense if you look at a graphic: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5dbNUn8D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u6ioy4l5ezigftfnw5m4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5dbNUn8D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/u6ioy4l5ezigftfnw5m4.png" alt="Screen Shot 2020-09-28 at 9.42.41 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So -2px left makes it like: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--TCocm-8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e6av6cy1qzolh3ywruyw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--TCocm-8x--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/e6av6cy1qzolh3ywruyw.png" alt="Screen Shot 2020-09-28 at 9.43.17 PM"&gt;&lt;/a&gt;&lt;br&gt;
and -2px top makes it like:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rtaQi7Xa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a0n9o4fn6qqjmvkiy7ut.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rtaQi7Xa--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/a0n9o4fn6qqjmvkiy7ut.png" alt="Screen Shot 2020-09-28 at 9.43.46 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome!  We got the gradient down.  Now if we make the pseudoelement a gradient...&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--YP6yVjt---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9h155y6rzb5k5b2obg7v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--YP6yVjt---/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9h155y6rzb5k5b2obg7v.png" alt="Screen Shot 2020-09-28 at 9.45.24 PM"&gt;&lt;/a&gt;&lt;br&gt;
Beautiful.  But wait, the codepen has both &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt;, but we only have one.  Why is that?    On to the next part, where I'll explain... &lt;/p&gt;
&lt;h1&gt;
  
  
  2 - &lt;code&gt;calc&lt;/code&gt; + &lt;code&gt;filter&lt;/code&gt; = 🤯
&lt;/h1&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;width: calc(100% + 4px);
height: calc(100% + 4px);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;🤯🤯🤯&lt;br&gt;
How did I not know about this?  It's so cool.  Basically what this code does is takes the previous width and height (100% is inherited from the parent element) and then adds 4px.  I thought this functionality was only for postprocessors like Sass! &lt;br&gt;
One way we could improve this code is to set the 4px as a variable (like &lt;code&gt;--border-width-gradient&lt;/code&gt;) and then &lt;code&gt;calc(--border-width-gradient / 2)&lt;/code&gt; for the offset.  Pretty cool, huh? &lt;/p&gt;

&lt;p&gt;Now, on to blur.&lt;br&gt;
Using our previous square div, I guessed I could just write &lt;code&gt;filter: blur(50px);&lt;/code&gt;&lt;br&gt;
WHOA.&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QnP2-aPq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4l3x8gk3v2matxvr5ceg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QnP2-aPq--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/4l3x8gk3v2matxvr5ceg.png" alt="Screen Shot 2020-09-28 at 9.53.33 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It even works for gradients! &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xKF7cLpF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5segcsgu600swxuax3va.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xKF7cLpF--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5segcsgu600swxuax3va.png" alt="Screen Shot 2020-09-28 at 9.54.15 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Awesome!  Now, I'll explain why we need both &lt;code&gt;:before&lt;/code&gt;: and &lt;code&gt;:after&lt;/code&gt;.  If I blur the only pseudoelement we have right now, it becomes like:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QgzdNDvf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o64k6ll9bcuzmjwptjgq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QgzdNDvf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/o64k6ll9bcuzmjwptjgq.png" alt="Screen Shot 2020-09-28 at 9.55.57 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Not good.  Where's my border?  This is easily solved by making both &lt;code&gt;:before&lt;/code&gt; and &lt;code&gt;:after&lt;/code&gt; the same style, and then blurring one (before or after).  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IOj6l04j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2jaqb2r25p83qykeq329.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IOj6l04j--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/2jaqb2r25p83qykeq329.png" alt="Screen Shot 2020-09-28 at 9.57.26 PM"&gt;&lt;/a&gt;&lt;br&gt;
I hardcoded some margins for the main div, just so I could see the entire shadow.  And there!  We're almost done.  &lt;/p&gt;
&lt;h1&gt;
  
  
  3 - Gradients and Animations
&lt;/h1&gt;


&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@keyframes steam {
    0% {
        background-position: 0 0;
    }
    50% {
        background-position: 400% 0;
    }
    100% {
        background-position: 0 0;
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;This is the cool part of the code - it's moving around the gradient.  If you remember your background-positions:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--tFy0ULfI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/njua4g0ubj4tir5f107c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--tFy0ULfI--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/njua4g0ubj4tir5f107c.png" alt="Screen Shot 2020-09-28 at 10.01.09 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, it seems like the gradient is being shifted back and forth - because the gradient is like 10 colors, there's plenty of space to move it around with.  Because the &lt;code&gt;:after, :before&lt;/code&gt; selector has the animation applied to it, the shadow and the gradient will move in sync.  Now all we need to do is darken the background (to make it look like its glowing) and put together everything we've learned:&lt;/p&gt;

&lt;p&gt;Also note that the original div has a very slight gradient so it looks a little curved outwards - I'll just copy that 😅&lt;/p&gt;

&lt;p&gt;And, here's the final result!&lt;br&gt;
&lt;/p&gt;
&lt;div class="glitch-embed-wrap"&gt;
  &lt;iframe src="https://glitch.com/embed/#!/embed/razer-animation-example?path=index.html" alt="razer-animation-example on glitch"&gt;&lt;/iframe&gt;
&lt;/div&gt;



</description>
      <category>css</category>
      <category>webdev</category>
      <category>design</category>
      <category>animation</category>
    </item>
    <item>
      <title>Building a Colors API Through Web Scraping, pt. 1
</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Sat, 26 Sep 2020 18:44:58 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/building-a-colors-api-through-web-scraping-pt-1-39m6</link>
      <guid>https://dev.to/carlyraejepsenstan/building-a-colors-api-through-web-scraping-pt-1-39m6</guid>
      <description>&lt;p&gt;For this weekend's first backend project, I thought I'd make a useful API.  From one of my &lt;a href="https://dev.to/carlyraejepsenstan/reverse-engineering-neumorphism-io-5af4"&gt;previous articles&lt;/a&gt;, you can see that my simple site accepts hex codes - what if it could accept color names like &lt;code&gt;darkorchid&lt;/code&gt; or &lt;code&gt;peru&lt;/code&gt;?  &lt;/p&gt;

&lt;p&gt;My immediate thought was a colors API!  I did searching, but no luck.  What I did find, though, was this URl:&lt;br&gt;
&lt;code&gt;http://www.html-color-names.com/darkorchid.php&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Oooh 👀.  Do you see that?  There's &lt;code&gt;darkorchid&lt;/code&gt; in the URL name!  I fiddled with the URL, and it turns out &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;http://www.html-color-names.com/powderblue.php&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;http://www.html-color-names.com/thistle.php&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;http://www.html-color-names.com/lightseagreen.php&lt;/code&gt;
were all valid URLs!
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, I wanted a site that could get both color names AND hex codes.  Turns out, I was asking for too much - my favorite site &lt;a href="//colorhexa.com"&gt;colorhexa&lt;/a&gt; has hex codes in its url, like &lt;code&gt;https://www.colorhexa.com/8acaff&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Great!  Looks like I can make my own API now.  Here's my basic structure:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If input is a hex code, send to colorhexa&lt;/li&gt;
&lt;li&gt;If input is a name, send to html-color-names&lt;/li&gt;
&lt;li&gt;Scrape sites for the color info (hex code, rgb, etc)&lt;/li&gt;
&lt;li&gt;Return in an object, like
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  input: input, 
  hex: hexcode, rgb: 
  rgb(r,g,b), 
  name: color name (if exists)
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I have some previous experience with web scraping - I used tools like &lt;a href="https://github.com/axios/axios"&gt;axios&lt;/a&gt; and &lt;a href="https://github.com/cheeriojs/cheerio"&gt;cheerio&lt;/a&gt; before. &lt;br&gt;
Axios downloads the entire HTML page for you to manipulate, and cheerio is like jQuery for the backend - it allows you to manipulate and get elements with CSS selectors like &lt;code&gt;.class&lt;/code&gt; and &lt;code&gt;#id&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;Anyway, before we start building the Node app, let's check out the HTML code behind these two sites.&lt;/p&gt;

&lt;p&gt;On html-color-names, I ran into an issue: &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--BeeXwYdO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ll0yrp6zmfvpi1p7ympq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--BeeXwYdO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ll0yrp6zmfvpi1p7ympq.png" alt="Screen Shot 2020-09-25 at 12.40.15 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hmmm.... I assumed that the RGB, hex code and name would have different IDs.&lt;br&gt;&lt;br&gt;
Anyways, I wrote a few lines to make sure the only elements with &lt;code&gt;class="color-data light"&lt;/code&gt; were the ones with the color info. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--pv2HAvBh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3se26ra5uwq9htanqrak.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--pv2HAvBh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/3se26ra5uwq9htanqrak.png" alt="Screen Shot 2020-09-25 at 12.42.48 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whew 😅.  I'll work this into my API later.&lt;/p&gt;

&lt;p&gt;Next is colorhexa.  As it turns out, their site organization is much more complex than html-color-names:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sB9yiad_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bhbi1in6pijy80cu14qq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sB9yiad_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bhbi1in6pijy80cu14qq.png" alt="Screen Shot 2020-09-25 at 12.48.03 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A quick read seems to show that all the numbers are inside of &lt;code&gt;class="value"&lt;/code&gt; elements...&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--LpWNSlDg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cgm3f0g1brswrqckmeyk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--LpWNSlDg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cgm3f0g1brswrqckmeyk.png" alt="Screen Shot 2020-09-25 at 12.48.34 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anyways, one good thing is that the colorhexa data pattern is consistent -  &lt;code&gt;arr[1]&lt;/code&gt; is always the RGB value.  &lt;/p&gt;

&lt;p&gt;Now, on to the backend coding.  We can use &lt;code&gt;axios&lt;/code&gt; to fetch the site, and then use &lt;code&gt;cheerio&lt;/code&gt; to parse the HTML and get what we want.&lt;/p&gt;

&lt;p&gt;So first, we're going to do something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//import our packages
const axios = require("axios")
const cheerio = require("cheerio")

//load html-color-names
axios.get("http://www.html-color-names.com/darkorchid.php")
    .then(html =&amp;gt; {
        console.log(html)
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can't start parsing out HTML yet - this is the axios output:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mGGWd6lx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ca6aovyjhvy0a4ihpgqm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mGGWd6lx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/ca6aovyjhvy0a4ihpgqm.png" alt="Screen Shot 2020-09-25 at 1.34.57 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Whoaaa, what?  I expected a plain-text response, but its actually an object!  We need to look for the HTML part, which is actually &lt;code&gt;html.data&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;axios.get("http://www.html-color-names.com/darkorchid.php")
    .then(html =&amp;gt; {
        console.log(html.data)
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d6Q98siu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mzpo8grkfi04y5qdw8kl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d6Q98siu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/mzpo8grkfi04y5qdw8kl.png" alt="Screen Shot 2020-09-25 at 1.36.29 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Cool!  Now we get HTML plain text.  Now, we're going to look at this through our cheerio-tinted lenses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//import our packages
const axios = require("axios")
const cheerio = require("cheerio")
const data = []

//load html-color-names
axios.get("http://www.html-color-names.com/darkorchid.php")
    .then(html =&amp;gt; {
      //console.log(html.data)
    const $ = cheerio.load(html.data)
    var arr = $(".color-data")
    console.log(arr)
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;However, I found an error - &lt;code&gt;arr&lt;/code&gt; returns a large object.  I tried using &lt;code&gt;arr.text()&lt;/code&gt;, but it returned a string.  &lt;code&gt;arr.toArray()&lt;/code&gt; results in an array.  What to do?&lt;/p&gt;

&lt;p&gt;After that I tried:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.text()&lt;/code&gt;: returns the names, but with tons of whitespace.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.data()&lt;/code&gt;: returns the text of only the first element.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://teamtreehouse.com/community/returning-%20an-array-from-a-class-in-jquery"&gt;this link&lt;/a&gt; - I rewrote it as an &lt;a href="https://developer.mozilla.org/en-%20US/docs/Web/JavaScript/Reference/Functions/Arrow_functions"&gt;arrow 
function&lt;/a&gt;, but it just returned &lt;code&gt;["","",""]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Finally, I tried&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;arr.each(function(i, elem) {
  a[i] = $(this).text();
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whoa!  It worked!  I got this as the input:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  '\n\t\tDarkOrchid\n\t\t',
  '\n\t\t#9932cc\n\t\t',
  '\n\t\t\trgb(153, 50, 204)\n\t\t'
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Whoa!  It worked!  It seems like arrow functions don't work in Cheerio.  And because we're smart developers who don't copy and paste, let's figure out how it works:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$(this)&lt;/code&gt; -&amp;gt; The element being passed into the loop&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;i&lt;/code&gt; -&amp;gt; Every time &lt;code&gt;each&lt;/code&gt; runs, &lt;code&gt;i&lt;/code&gt; is increased by 1.  Basically, it's a compressed &lt;code&gt;for&lt;/code&gt; loop.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;elem&lt;/code&gt; -&amp;gt; a useless parameter - I deleted it and it still works.  BUT: you can also write &lt;code&gt;$(elem)&lt;/code&gt; instead of &lt;code&gt;$(this)&lt;/code&gt;.  It seems to work the same way, so I'll use it that way in the future.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Getting info from colorhexa is going to be pretty similar; I selected &lt;code&gt;.value&lt;/code&gt;, iterated over it, and then got out a result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[
  'd8bfd8',
  '216, 191, 216',
  '84.7, 74.9, 84.7',
  '0, 12, 0, 15',
  '300°, 24.3, 79.8',
  '300°, 11.6, 84.7',
  'cccccc',
  '80.078, 13.217, -9.237',
  '59.342, 56.819, 72.803',
  '0.314, 0.301, 56.819',
  '80.078, 16.125, 325.053',
  '80.078, 12.716, -16.458',
  '75.378, 8.614, -4.499',
  '11011000, 10111111, 11011000'
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The second position &lt;code&gt;[1]&lt;/code&gt; is the RGB value.  &lt;/p&gt;

&lt;p&gt;Yay!  Looks like I can get the values now.  &lt;/p&gt;

&lt;p&gt;Ok, now we need to parse our results.  As you remember from the html-color-names array, each string in the array is like &lt;br&gt;
&lt;code&gt;'\n\t\tDarkOrchid\n\t\t'&lt;/code&gt;&lt;br&gt;
What I'm going to do (and what would be smart) would be to write a quick regex matching &lt;code&gt;\n&lt;/code&gt; or &lt;code&gt;\t&lt;/code&gt;, replace them with spaces, and then use &lt;code&gt;.trim()&lt;/code&gt; to strip the spaces.&lt;br&gt;&lt;br&gt;
But, because each string has exactly six characters behind and in front (&lt;code&gt;\n\t\t&lt;/code&gt;), I could also slice &lt;code&gt;(6, str.length() - 6&lt;/code&gt;).  That would be the fast and hacky way to do it.  &lt;/p&gt;

&lt;p&gt;But I have all weekend!  I'll write a regex.  At first, I tried writing &lt;code&gt;\\n&lt;/code&gt; and &lt;code&gt;\\t&lt;/code&gt;, but, as it turns out &lt;code&gt;\n&lt;/code&gt; and &lt;code&gt;\t&lt;/code&gt; are legal regex statements - so my code became like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var regex = /(\n|\t){3}/
//match any three sets of \n or \t
str.replace(regex, "").trim()
//replace the regex positions with spaces, and then rinse them out
&amp;gt;&amp;gt;&amp;gt; "DarkOrchid"
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Anyway, I did end up spending close to an hour debugging this, but my code was much cleaner and simpler.  &lt;/p&gt;

&lt;p&gt;As for colorhexa, I really only wanted the RGB, so:&lt;br&gt;
&lt;code&gt;a[1]&lt;/code&gt;.&lt;br&gt;
Yes, that's it.  The RGB values are in the second slot in the array, and arrays start from 0, so its 1.  Perfect!  We can get the color info from the URLs.&lt;br&gt;&lt;br&gt;
Now, let's get on with the actual API part.&lt;/p&gt;
&lt;h1&gt;
  
  
  Serving the API
&lt;/h1&gt;

&lt;p&gt;First, you should be familiar with NodeJS and Express and stuff like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app.get("/" , (req, res) =&amp;gt; {
 res.send("Hello world!")
})
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above, the path is "/" - I'll be talking about paths a lot later on.&lt;/p&gt;

&lt;p&gt;So right now, my workflow looks like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the color&lt;/li&gt;
&lt;li&gt;If the color starts with "#", send it to colorhexa.&lt;/li&gt;
&lt;li&gt;If it doesn't, send it to html-color-names.&lt;/li&gt;
&lt;li&gt;Respond with an object with the desired information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point, I ran into another roadblock.  Variables declared inside of the &lt;code&gt;axios.get()&lt;/code&gt; functions can't be used outside - so I have to put both of them inside the same path. &lt;/p&gt;

&lt;p&gt;So, I came up with this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Get the input on the path /input/:color.&lt;/li&gt;
&lt;li&gt;If :color starts with "#" or matches my color regex from my &lt;a href="//dev.to/carlyraejepsenstan/reverse-engineering-neumorphism-io-5af4"&gt;earlier article&lt;/a&gt;, send to /hex/:color and remove the "#".&lt;/li&gt;
&lt;li&gt;If color is a string, send to /string/:color&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And for those who aren't familiar with the colons, using things like &lt;code&gt;:color&lt;/code&gt; allows you to get part of the path using &lt;code&gt;req.params.color&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Basically, I would pass the color parameter into the functions I wrote previously, and then send the results.&lt;/p&gt;

&lt;p&gt;That wraps up the first part of my API!  Check out the next article to see how I split up the code and set up the validation. &lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>javascript</category>
      <category>api</category>
      <category>webscrape</category>
    </item>
    <item>
      <title>Streamlining my Git workflow with bash</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Thu, 24 Sep 2020 21:22:25 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/streamlining-my-git-workflow-with-bash-11l8</link>
      <guid>https://dev.to/carlyraejepsenstan/streamlining-my-git-workflow-with-bash-11l8</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Think about your every-day github workflow.  You wrap up your code, make sure everything works fine, and then open your terminal.  You would then:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git pull&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git stage .&lt;/code&gt; or &lt;code&gt;git add .&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git commit&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;Type your commit message and press &lt;code&gt;:x!&lt;/code&gt; to exit&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;git push&lt;/code&gt; &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Or, if you use the &lt;code&gt;git&lt;/code&gt; GUI in an IDE, you would click a bunch of buttons to get your changes sent to your Github/Gitlab/Gitwhatever repo. &lt;/p&gt;

&lt;p&gt;Although I have &lt;code&gt;ohmyzsh&lt;/code&gt; installed and can run short commands for a lot of git actions (&lt;code&gt;gc&lt;/code&gt; instead of git commit, &lt;code&gt;gp&lt;/code&gt; instead of git push, etc), I still found the process of committing to Github very...inefficient.  Do I really have to write &lt;code&gt;:x!&lt;/code&gt; every time?  And moving the cursor around to click buttons is always slower than straight typing.  How could I make this faster and less error-prone?  &lt;/p&gt;

&lt;h2&gt;
  
  
  Enter bash
&lt;/h2&gt;

&lt;p&gt;I talked with some friends and learned about &lt;code&gt;bash&lt;/code&gt;.  &lt;code&gt;bash&lt;/code&gt; files (extension &lt;code&gt;.sh&lt;/code&gt;) combine a programming language with basic shell commands - like git commands!&lt;/p&gt;

&lt;p&gt;If I wanted to write a bash script to do my git commits for me, it would need to do the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;git pull&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;cd&lt;/code&gt; into my git repo&lt;/li&gt;
&lt;li&gt;Accept my input&lt;/li&gt;
&lt;li&gt;Stage the files and commit with my input as the message.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I did some quick searching, and found the &lt;a href="https://ss64.com/bash/read.html" rel="noopener noreferrer"&gt;man page for &lt;code&gt;read&lt;/code&gt;&lt;/a&gt;, and also found that you use variables my typing a $ in front of the variable name. &lt;/p&gt;

&lt;p&gt;Writing something like &lt;code&gt;read myvar&lt;/code&gt; means you can get user input, and you can use &lt;code&gt;myvar&lt;/code&gt; by writing &lt;code&gt;$myvar&lt;/code&gt;.  Now I had the beginnings of my script:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "Enter your commit message below:"
read myvar
echo "Your commit message is:"
echo $myvar
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also use optional flags to limit the character input - I used the flag &lt;code&gt;-n&lt;/code&gt; to set the character input to 1, just so I could make a cool &lt;code&gt;y/n&lt;/code&gt; thing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;echo "Would you like to commit? [y/n]"
read -n 1 -s input
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And, the &lt;code&gt;-s&lt;/code&gt; flag makes the read "silent," so it doesn't output the character you pressed. &lt;/p&gt;

&lt;p&gt;Cool!  Now, I thought I could work this into my script: display the commit message, and then confirm my intent to commit. So then I looked up how to write an if/else block.  &lt;/p&gt;

&lt;h4&gt;
  
  
  If/else blocks
&lt;/h4&gt;

&lt;p&gt;Ah, the if/else statement - the basis of beginner programmers and things like IFTTT.  In bash, an if/else block looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if [statement];
 then
  # do something
elif [statement];
 then # do something
else 
 # do something - no then! 
fi 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where I started finding errors - things like "Unexpected syntax error near &lt;code&gt;else&lt;/code&gt;".  Whaaat?&lt;/p&gt;

&lt;p&gt;So after extensive searching, I figured out this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myvar = "foo"
myvar="foo"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Is there a difference in the above script?  In python and javascript, there isn't - spaces don't matter. &lt;br&gt;
But.  In shell...&lt;br&gt;
&lt;strong&gt;spaces matter.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This was a huge source of frustration for me - it turns out, declaring a variable means you can't have any spaces, but comparing variables requires spaces.  That means&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if [ myvar = "foo" ];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is different from:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;if [ myvar="foo" ];&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;🤦‍♀️&lt;br&gt;
I spent 20 minutes figuring this out.  I hope by reading this article, you'll avoid the same!  &lt;/p&gt;

&lt;p&gt;Stupid mistakes aside, the if/else statement was pretty simple - if my input was &lt;code&gt;y&lt;/code&gt;, commit, if it was &lt;code&gt;n&lt;/code&gt; exit and say "You aborted the process," and if it wasn't either of them, exit and say "Your input wasn't y or n". &lt;/p&gt;

&lt;p&gt;Recalling our previous line of code &lt;code&gt;read -n 1 -s input&lt;/code&gt;, and remembering that we can use input as &lt;code&gt;$input&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F84vnozqg8z2vzbqi0au5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F84vnozqg8z2vzbqi0au5.png" alt="Screen Shot 2020-09-24 at 2.29.07 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And remember, the &lt;code&gt;thens&lt;/code&gt; are important!  I forgot the &lt;code&gt;then&lt;/code&gt; for the &lt;code&gt;elif&lt;/code&gt; statement, causing more headaches.&lt;/p&gt;

&lt;p&gt;Here's the final product:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#!/bin/bash
cd vanillanimate
git pull
echo "Enter your commit message below:"
read myvar
echo "Your commit message is:"
echo $myvar
echo "Would you like to commit? [y/n]"
read -n 1 -s input

if [ $input = "y" ];
 then 
  git stage .
  git commit -m "$myvar"
  git push
  exit 0
elif [ $input = "n" ];
 then
  echo "process abort exit code 0"
  exit 0
else
  echo "process abort exit code 1 - your input $input is not y or n"
  exit 1
fi
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I were to make this even better, I might make it so the script asks for a path to repo first, instead of having a hard-coded path.  But, it worked and only took about an hour.  And it worked!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5o40sp1zoful3l7rbyfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5o40sp1zoful3l7rbyfa.png" alt="Screen Shot 2020-09-24 at 2.21.38 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anyways, thanks for reading and remember that as a developer, you never stop learning.&lt;br&gt;
Thoughts?  Found a typo?  Leave a comment! &lt;/p&gt;

</description>
      <category>git</category>
      <category>bash</category>
      <category>shell</category>
    </item>
    <item>
      <title>(Partially) Reverse Engineering Neumorphism.io </title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Tue, 22 Sep 2020 22:42:53 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/reverse-engineering-neumorphism-io-5af4</link>
      <guid>https://dev.to/carlyraejepsenstan/reverse-engineering-neumorphism-io-5af4</guid>
      <description>&lt;p&gt;Recently, I've been using &lt;a href="//neumorphism.io"&gt;neumorphism.io&lt;/a&gt; a lot for my projects - I really like the soft and squishy feel buttons have.&lt;br&gt;&lt;br&gt;
However, as I pasted my colors into the site for the nth time, I started to wonder - how does it work?  Can I calculate these shadow colors myself?&lt;/p&gt;

&lt;p&gt;Of course, the first step was to plug in a bunch of random colors, grab the hex codes for the shadows, and then check out their RGB numbers.  I used &lt;a href="//colorhexa.com"&gt;colorhexa&lt;/a&gt; for this.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;R&lt;/th&gt;
&lt;th&gt;G&lt;/th&gt;
&lt;th&gt;B&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Thistle&lt;/td&gt;
&lt;td&gt;216&lt;/td&gt;
&lt;td&gt;191&lt;/td&gt;
&lt;td&gt;216&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thistle's pale shadow&lt;/td&gt;
&lt;td&gt;248&lt;/td&gt;
&lt;td&gt;220&lt;/td&gt;
&lt;td&gt;248&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thistle's dark shadow&lt;/td&gt;
&lt;td&gt;184&lt;/td&gt;
&lt;td&gt;162&lt;/td&gt;
&lt;td&gt;184&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Powderblue&lt;/td&gt;
&lt;td&gt;176&lt;/td&gt;
&lt;td&gt;224&lt;/td&gt;
&lt;td&gt;230&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Powderblue's pale shadow&lt;/td&gt;
&lt;td&gt;202&lt;/td&gt;
&lt;td&gt;255&lt;/td&gt;
&lt;td&gt;255&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Powderblue's dark shadow&lt;/td&gt;
&lt;td&gt;150&lt;/td&gt;
&lt;td&gt;190&lt;/td&gt;
&lt;td&gt;196&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Peru&lt;/td&gt;
&lt;td&gt;205&lt;/td&gt;
&lt;td&gt;133&lt;/td&gt;
&lt;td&gt;63&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Peru's pale shadow&lt;/td&gt;
&lt;td&gt;236&lt;/td&gt;
&lt;td&gt;153&lt;/td&gt;
&lt;td&gt;72&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Peru's dark shadow&lt;/td&gt;
&lt;td&gt;174&lt;/td&gt;
&lt;td&gt;113&lt;/td&gt;
&lt;td&gt;54&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Ok, I learned to code so I could avoid typing numbers all day....😭&lt;/p&gt;

&lt;p&gt;Anyway, now that we have the numbers, we can try seeing how they are mutated. &lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Name&lt;/th&gt;
&lt;th&gt;R change&lt;/th&gt;
&lt;th&gt;G change&lt;/th&gt;
&lt;th&gt;B change&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Thistle pale&lt;/td&gt;
&lt;td&gt;+32&lt;/td&gt;
&lt;td&gt;+29&lt;/td&gt;
&lt;td&gt;+32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thistle dark&lt;/td&gt;
&lt;td&gt;-32&lt;/td&gt;
&lt;td&gt;-29&lt;/td&gt;
&lt;td&gt;-32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Powderblue pale&lt;/td&gt;
&lt;td&gt;+26&lt;/td&gt;
&lt;td&gt;+31&lt;/td&gt;
&lt;td&gt;+25&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Powderblue dark&lt;/td&gt;
&lt;td&gt;-26&lt;/td&gt;
&lt;td&gt;-34&lt;/td&gt;
&lt;td&gt;-37&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;td&gt;-&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Note that for powderblue pale, the green and blue maxed out at 255, so we can assume that both numbers could be 32, or something larger.&lt;br&gt;
I didn't do any more math for Peru because I got sick of it.  However, what we do see is assuming the variable &lt;code&gt;x&lt;/code&gt; where &lt;code&gt;x&lt;/code&gt; is the amount R, G, and B are changed by, then the pale shadows are &lt;code&gt;r + x, g + x, b + x&lt;/code&gt; while the dark shadows are &lt;code&gt;r - x, g - x, b - x&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;code&gt;x&lt;/code&gt; also appears to range from 26 to 37 - with more colors, one could assume it can be from 20 to 30, or possibly even larger.&lt;/p&gt;

&lt;p&gt;At this point, I took a step back.  This project was supposed to be my quick frontend puzzle for today, not a long, code-intensive and complex like my in-progress Node.js app.  Just adding and subtracting, say, 25 from each rgb value would be fine enough.  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KtIBdOBu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0z1de7026edo3hihczah.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KtIBdOBu--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/0z1de7026edo3hihczah.png" alt="Screen Shot 2020-09-22 at 4.14.44 PM"&gt;&lt;/a&gt;&lt;br&gt;
Just for fun, I checked out the code running behind neumorphism.io.  There's no way I'm going to write anything with a similar function anytime soon.  So for now, I'm going to prototype something that just adds and subtracts 25 to RGB values provided by the user.&lt;br&gt;
(I'll address converting between hex and RGB farther down).&lt;/p&gt;

&lt;p&gt;Now the structure looks something like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;3 inputs - R, G, and B&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;onsubmit&lt;/code&gt;, compose R, G, and B into a legal CSS string 
using a template literal like &lt;code&gt;background: rgb(${r},${g}, 
${b})&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Subtract 25 from each number and set this to the positive 
shadow, and add 25 and set this as the negative shadow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And I was able to spin up a working demo in about a half-hour:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/carlyraejepsenstan/embed/qBZLVra?height=600&amp;amp;default-tab=js&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Pretty cool, huh?  It even looks pretty good!  I'll definitely use this for my own projects in the future.&lt;/p&gt;

&lt;p&gt;The next step: also consume hex codes.  But to compute the shadow colors, we need to convert them into HEX codes!  This is the part when learning how to read a RegEx comes in handy. &lt;/p&gt;

&lt;p&gt;I found this script from this &lt;a href="https://www.html-code-generator.com/javascript/color-converter-script"&gt;site&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function HEXtoRGB(hex) {
    var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
    hex = hex.replace(shorthandRegex, function (m, r, g, b) {
        return r + r + g + g + b + b;
    });
    var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
    return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
    } : null;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;But.....I don't really understand how it works.  And I dislike copying code.  And it looks scary.  So let's write break apart this function and make it ourselves! &lt;/p&gt;
&lt;h4&gt;
  
  
  Hex Color Structure:
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;8adaff&lt;/code&gt;  &amp;lt;- This is an example hex color.  There are two parts to a hex color:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The optional &lt;code&gt;#&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The two digit hexadecimal numbers composed from letters or 
numbers or both&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To match the optional hash, we can use &lt;br&gt;
&lt;code&gt;^#?&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;^&lt;/code&gt; marks the beginning of the string&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;#&lt;/code&gt; matches &lt;code&gt;#&lt;/code&gt; (duh)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;?&lt;/code&gt; means "optional" in RegEx-ese. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we need to split apart the remaining 6 characters and convert them from Base 16 to Base 10. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IS9q_1wf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xahc2ugr7uokqouwg0h9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IS9q_1wf--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/xahc2ugr7uokqouwg0h9.png" alt="Screen Shot 2020-09-22 at 6.29.14 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What we could do is validate the form like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search the form input for &lt;code&gt;#&lt;/code&gt; - if there is, slice the 
function and make it not have a &lt;code&gt;#&lt;/code&gt; in front.

&lt;ul&gt;
&lt;li&gt;Slice the new string at (0,2), (2,4), and (4,6)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;#&lt;/code&gt; is not present, start slicing. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But, a more elegant solution is to use RegExs, like the complex code snippet I found does. &lt;br&gt;
To match each hexadecimal number, we can do this:&lt;br&gt;
&lt;code&gt;a-f&lt;/code&gt; - hexadecimals only use letters from a to f.  The full hexadecimal number line is 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. &lt;br&gt;
&lt;code&gt;\d&lt;/code&gt; - matches any number&lt;br&gt;
&lt;code&gt;[]&lt;/code&gt; - a bracket will match only one character&lt;br&gt;
&lt;code&gt;{2}&lt;/code&gt; - will match twice - placed after the bracket, will match against any two character permutation of number or letter.&lt;br&gt;
&lt;code&gt;()&lt;/code&gt; - I learned about these today: they are "matching groups".  I'll explain their function later.&lt;/p&gt;

&lt;p&gt;Now, to match &lt;code&gt;#8adaff&lt;/code&gt;, we can use this RegEx:&lt;br&gt;
&lt;code&gt;^#?([a-f\d]{2}){3}&lt;/code&gt; &lt;br&gt;
Cool!  Now we can use the RegEx method &lt;code&gt;match()&lt;/code&gt; and mess with the numbers.  &lt;/p&gt;

&lt;p&gt;Or... can we?&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--DNNrZBjG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5c5ciea2gb5fg6dk7lor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--DNNrZBjG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/5c5ciea2gb5fg6dk7lor.png" alt="Screen Shot 2020-09-22 at 6.40.07 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is bad - it seems like we're only matching the whole string, and then the last two characters.  How do we fix it?&lt;/p&gt;

&lt;p&gt;The solution was actually to write the regex like &lt;br&gt;
 &lt;code&gt;^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})&lt;/code&gt; &lt;br&gt;
instead of using &lt;code&gt;{3}&lt;/code&gt;:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_rSa9sdx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cclg8qz7aa9s01zyqgd8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_rSa9sdx--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/cclg8qz7aa9s01zyqgd8.png" alt="Screen Shot 2020-09-22 at 6.42.13 PM"&gt;&lt;/a&gt;&lt;br&gt;
This is where the &lt;code&gt;()&lt;/code&gt; matching groups come in - by making three matching groups, the regex match method will spit out three two-character strings: our hexadecimal numbers. &lt;/p&gt;

&lt;p&gt;I found another error while putting together the final result - put &lt;code&gt;i&lt;/code&gt; at the end of the regex, like &lt;br&gt;
&lt;code&gt;/^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})/i&lt;/code&gt; &lt;br&gt;
so it matches case-insensitively. &lt;/p&gt;

&lt;p&gt;Perfect! Now we just have to parse the numbers into base 10, and we're good to go.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//assuming hex came from the form:
var num = hex.match(regex)
var r = parseInt(num[1], 16)
var g = parseInt(num[2], 16)
var b = parseInt(num[3], 16)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;And because we use parseInt, the values are automatically numbers - we don't need to worry about that anymore!  &lt;/p&gt;

&lt;p&gt;And if we want to output hex codes, turns out you can't use parseInt - &lt;a href="https://stackoverflow.com/questions/1337419/how-do-you-convert-numbers-between-different-bases-in-javascript"&gt;you have to use toString()&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Anyway, here's the final product, hosted on Glitch:&lt;/p&gt;


&lt;div class="glitch-embed-wrap"&gt;
  &lt;iframe src="https://glitch.com/embed/#!/embed/neumorphism-calculator?path=script.js" alt="neumorphism-calculator on glitch"&gt;&lt;/iframe&gt;
&lt;/div&gt;



&lt;p&gt;Note that this app has some issues - numbers greater than 255 are not filtered out, so they become three-character hexadecimal numbers.  If I were to optimize this, I would probably add a layer of validation changing variables greater than 255 or less than 0.&lt;br&gt;
Additionally, the blocks of declaring variables might be better optimized with arrays and methods like .map or .forEach.&lt;/p&gt;

&lt;p&gt;But for a project built in a few hours, this isn't too bad!  &lt;/p&gt;

&lt;p&gt;Thoughts? Ideas? Hate mail? Please leave a comment! &lt;/p&gt;

</description>
      <category>css</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Animating linear-gradient: a guide</title>
      <dc:creator>CarlyRaeJepsenStan</dc:creator>
      <pubDate>Tue, 22 Sep 2020 20:16:58 +0000</pubDate>
      <link>https://dev.to/carlyraejepsenstan/animating-linear-gradient-a-guide-2l04</link>
      <guid>https://dev.to/carlyraejepsenstan/animating-linear-gradient-a-guide-2l04</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  width: 100px;
  height: 100px;
  background-image: linear-gradient(-45deg, peru, papayawhip);
  transition: 0.5s;
}

div:hover{
  background-image: linear-gradient(-45deg, peru, peru);
  transition: 0.5s;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What's wrong with this code?  It should make a nice, clean, gradient animation, from a gradient to a solid fill.  It should work, right?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Wrong&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.w3schools.com/cssref/pr_background-image.asp"&gt;background-image is not animatable.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What's a developer to do?  I lay in my bed and considered the possibilities.  I could use Javascript DOM functions like &lt;code&gt;backgroundImage&lt;/code&gt; and template literals to change the color.  I could use a RGB to CMYK function, and call requestAnimationFrame.  Or, I could look it up.&lt;br&gt;&lt;br&gt;
I found &lt;a href="https://stackoverflow.com/questions/23441060/animating-linear-gradient-using-css"&gt;this.&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;What this StackOverflow thread suggests is basically increasing the &lt;code&gt;background-size&lt;/code&gt; to "200% 200%" (I assume double the size of the target window) and then translate it using a bunch of percentages and &lt;code&gt;background-position&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Cool!  After spending a while reading up on documentation, I figured out this:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--zJVCLJ0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/04qldu7htk1vohhcqs2d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--zJVCLJ0A--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/04qldu7htk1vohhcqs2d.png" alt="Screen Shot 2020-09-22 at 12.52.29 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Assuming &lt;code&gt;linear-gradient(145deg, red, blue)&lt;/code&gt;, of course.&lt;/p&gt;

&lt;p&gt;From the StackOverflow code and my newfound knowledge about background-position, I had my steps.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Write gradient &lt;/li&gt;
&lt;li&gt;Increase gradient size&lt;/li&gt;
&lt;li&gt;Center gradient (&lt;code&gt;50% 50%&lt;/code&gt;) so it looked about the same&lt;/li&gt;
&lt;li&gt;background-position is animatable, so translate either &lt;code&gt;100% 100%&lt;/code&gt; or &lt;code&gt;0% 0%&lt;/code&gt; to make it look more blue or more red, respectively&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yay!  Problem solved.  Here's a working demo:&lt;br&gt;
&lt;iframe height="600" src="https://codepen.io/carlyraejepsenstan/embed/bGpOWxa?height=600&amp;amp;default-tab=css&amp;amp;embed-version=2"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;But, as you can probably tell, it's not perfect - &lt;code&gt;100% 100%&lt;/code&gt; still has some red tinge to it, and on a blue background this div will definitely stand out.  Also, combining a 200% background size and 50% background position changes the gradient slightly - as you can see in this side by side comparison:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--5SfyOy_M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7zx4kdxvmubv8p96rsjy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--5SfyOy_M--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/7zx4kdxvmubv8p96rsjy.png" alt="Screen Shot 2020-09-22 at 1.01.43 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What's a perfection obsessed developer like me to do?  After excessive brainstorming, I figured out these things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;background-size&lt;/code&gt; is animatable&lt;/li&gt;
&lt;li&gt;Changing the gradient to &lt;code&gt;red, blue, blue&lt;/code&gt; makes a completely blue portion at one corner.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So my steps now looked like this:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start the div with a normal gradient
*On hover, do these things:

&lt;ul&gt;
&lt;li&gt;change background-size to 200%&lt;/li&gt;
&lt;li&gt;change background-position to 100% 100% (the &lt;code&gt;blue blue&lt;/code&gt; 
part of the gradient) &lt;/li&gt;
&lt;li&gt;use nicer colors than red and blue&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While messing with the code, I found that I didn't need to change the background-size on hover; leaving it 200% to begin with actually helped differentiate the div from the same-colored background.  I also added a quick box-shadow for a cool effect.&lt;/p&gt;

&lt;p&gt;And here it is!  A successfully animated linear-gradient, in pure CSS.&lt;/p&gt;

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

&lt;p&gt;Helpful? Confusing? Thoughts?  Please leave a comment! &lt;/p&gt;

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