diceline-chartmagnifierquestion-marktwitter-whiteTwitter_Logo_Blue

Today I Learned

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;
});

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.

How to validate your password with regex

Example password validation regex

  • rules below can be concatenated
^(?=.*?[^a-zA-ZÄÖÜäöüß0-9])(?=.*?[0-9])(?=.*?[a-zäöüß])(?=.*?[A-ZÄÖÜ])(?!.*\d{2,}).{8,}$

Special character matching

  • Matches (operator is ?=) any string that has at least a special character e.g.: sadsds@asdasd
    (?=.*?[^a-zA-ZÄÖÜäöüß0-9])

Number matching

  • Matches (operator is ?=) any string that has at least a number: e.g.: s1adsdsasdasd
    (?=.*?[0-9])

Small letter matching

  • Matches (operator is ?=) any string that has at least a small letter: e.g.: SADSa
    (?=.*?[a-zäöüß])

Big letter matching

  • Matches (operator is ?=) any string that has at least a big letter: e.g.: SADSa
    (?=.*?[A-ZÄÖÜ])

Consecutive numbers matching

  • Doesn't match (operator is ?!) strings that have consecutive numbers in them: e.g.: asdasd42dada
    (?!.*\d{2,})

Sequential numbers matching (cannot be used at the same time with previous rule)

  • Doesn't match (operator is ?!) strings that have sequential numbers in them: e.g.: 12asdasd42dada
  • It will allow numbers that are separated by other letters e.g.: adasd1asd2asd3
  • It will allow consecutive numbers e.g.: ahadADS22dhsg44
    (?!.*((12)|(23)|(34)|(45)|(56)|(67)|(78)|(90)|(01)))

Length of string matching (should be placed last)

  • This will match any string that is less than 8 characters
    .{8,}

You can write better jQuery. A DRY approach to multiple configurations

From this
$('.multiSelectOne').multiselect({
    buttonClass: 'btn btn-default',
    buttonWidth: '100%',
    ...
});
$('.multiSelectTwo').multiselect({
    buttonClass: 'btn btn-default',
    buttonWidth: '100%',
    nonSelectedText: '---',
    ...
});
To This
<select class="js-multiselect" 
    data-include-select-all-option="true" 
    data-all-selected-text="All">
    <option></option>
</select>
$('.js-multi-select).each(function () {
    let options = {};

    Object.entries($(this).data())
        .map(property => options[property[0]] = property[1]);

  $(this).multiselect(options);

}

And never go back for adjustments in the script file ever again.