diceline-chartmagnifiermouse-upquestion-marktwitter-whiteTwitter_Logo_Blue

Today I Learned

vue-cli-service environment variables

While you can find detailed information in the official docs, I dare to spoil the fun for you:

1. All app variables are pulled in if they are prefixed with VUE_APP_.

Example: VUE_APP_API_URL.

You can now access the variables in your application like this: process.env.APP_URL.

2. All e2e variables are fetched if they are prefixed withCYPRESS_.

Example: CYPRESS_APP_URL.

You can now access the variables in your tests like this: Cypress.env('APP_URL').

Loop through a list of VueJS components

Ever wondered how you can loop through a list of VueJS components?

Let's say you have an array of components and you want to render them dynamically:

data() {
    return {
        components: ['ComponentOne', 'ComponentTwo', 'ComponentThree']       
    }
}

It's simple. We just need to use vue's dynamic component.

###Let's see how:

<ul>
    <li v-for="component, index in components" :key="index">
        <component :is="component"></component>
    </li>
</ul>

Just make sure to register all the used components where you need them.

How to use chmod to change permissions

For a long time I've been afraid of using the shell command chmod. That because I didn't understand how it works and I feared breaking things. Today though, I learned how easy it is actually. No, not to break things, but to use the chmod command.

A Unix system allows for multiple users with different access rights, which can be changed in the shell by using this command.

The permissions are for reading, writing and executing (rwx) and are expressed for all three types of users a file or folder has. It will come in the form of rwxrwxrwx where the first batch of three characters (rwx), belong to the owner, the second to the group, and the third to all the other users (owner/group/other).

Sure, (rwx rwx rwx) would mean that everybody has all the access rights to that particular file or folder. And we all know that's not a good idea. So, most often you'll see something like rw-r--r-- which means that only the owner has the right to read and write, while everybody else can only read that particular file.

Now onto chmod, which can be used with either a symbolic or a numeric notation for the access settings. You've probably seen somebody else type chmod 644 file_name on your computer, leaving you wondering about what that means.

That's the numeric notation.

If you think about the access rights as a series of bits, the whole thing would look like this:

rwx rwx rwx = 111 111 111
rw- r-- r-- = 110 100 100
rw- r-x --- = 110 101 000

That translates into

rwx rwx rwx = chmod 777
rw- r-- r-- = chmod 644
rw- r-x --- = chmod 650

because in binary notation:

100 = 4
101 = 5
110 = 6
111 = 7

And now you've got a simple rule on how to construct your chmod command depending on what permissions you need to set.

The rwxr--r-- access rights means chmod 744, rw-r----- is chmod 640, and so on.

Simple enough, right?

Write to file with JavaScript and Node.js

Sometimes, as a programmer, you might need to write something to a file on the local file system. And that can be a piece of cake, case in which you'll use your favorite text editor to do it manually, but other times, it needs to be something complex and dynamic and you want your JavaScript to do it for you.

Here's how:

We'll first require the fs module from Node.js into our JS file.

const fs = require('fs');

Then we'll grab our content:

const content = 'Hello World!';

And finally we'll use the writeFile function from the fs module to populate our file. We need to provide the name of our file, the data or content to populate it with, the options (optional - for encoding, modes or flags) and a callback (also optional - to show the error message should one arise).

fs.writeFile(filename, data, [options], [callback]);

Here I've given it the a+ flag - "Open file for reading and appending. The file is created if it does not exist.". The encoding defaults to 'utf8' if none is given.

fs.writeFile('file.txt', content, { flag: 'a+' }, (err) => {
  if (err) throw err;
});

Make LiveView template variables readable

If you ever seen this in the wild:

{:ok, assign(socket,
      %{my_variable: @my_variable
        my_other_variable: nil.
        yet_another_variable: nil 
      })}

Remember that applying elixir's pipe operator makes everything better:

def mount(_session, socket) do
    socket =
      socket
      |> assign(:my_variable, @my_variable)
      |> assign(:my_other_variable, nil)
      |> assign(:yet_another_variable, nil)
    {:ok, socket}
end

Currying in JavaScript

No, this has nothing to do with those delicious Indian curry dishes you might be thinking about, but with a functional technique used in JavaScript.

It seems confusing at first, but currying is simple. If you've never heard of it before, it's because this concept isn't native to JS.

Basically, it allows you to transform a function like f(a, b, c) into something like f(a)(b)(c). What that means is that you can split up a function with multiple arguments into a sequence of functions with one argument each.

Let's take a basic example.

const newUser = function(name, age, skill) {
  return {
    name,
    age,
    skill
  }
}
	
newUser("John", 27, "JS")

Now to the curry part:

const newUser = function(name) {
  return function(age) {
    return function(skill) {
      return {
        name,
        age,
        skill
      }
    }
  }
}
	
newUser("John")(27)("JS")

Add in some arrow functions and voila:

const newUser = 
  name => 
    age => 
      skill =>
      {
        name,
        age,
        skill
      }

The purpose of all this you might ask?

Think about situations when you don't have the complete data available in the beginning and you still need your function to gradually pass through your app and receive its arguments step by step as more and more data is arriving, until you add the final argument and receive the output.

Cheatsheet

As a developer, you frequently find yourself in the position when you forgot a certain syntax or you just want to see some quick code snippet to implement certain functionality. No need to google it now, you don't even need to leave your favorite terminal, welcome cheat.sh.

Usage:

curl cheat.sh/programming_language/query_string
curl cheat.sh/python/random+list+elements