How to use VIM as an Hex Editor

I want to display the hex format of a compiled binary from an helloworld.c program using vim.

The source code of the helloworld.c:

#include < stdio.h >
int main()
{
   // printf() displays the string inside quotation
   printf("Hello, World!");
   return 0;
}

So after you generate the binary helloworld using gcc:

gcc helloworld.c -o helloworld

You’ve to edit the executable using vim:

vim helloworld

The last step is to use the xxd command to transform the result to the hex presentation by doing :%!xxd.

Use ctrl + w to delete a word with MySQL Cli

By default, and while you’re using mysql CLI the combination ctrl + w delete the enter line, so if you type a long query and you want to delete the word before the cursor the combination ctrl + w will not work and you have to use the backspace instead, and the only configuration to solve this problem is by creating the file ~/.editrc if it doesn’t exist and add this line of code:

bind "^W" ed-delete-prev-word

How to access the MySQL CLI With MAMP

First, you’ve to start MAMP or MAMP PRO, and you open your terminal and type:

/Applications/MAMP/Library/bin/mysql -uroot -p

Enter the password, by default the password, is root:

Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 254
Server version: 5.6.35 MySQL Community Server (GPL)

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

If you want to use the command mysql by default without typing the entire command line you can edit one of these dot files ~./bashrc if you’re using bash or ~/.zshrc in case of using zsh and add this alias:

alias mysql=/Applications/MAMP/Library/bin/mysql -uroot -proot

After editing the file using Vim or Nano save the dot file, and run the source to validate the modification:

If you use zsh:

source ~/.zshrc

If you use bash:

source ~/.bashrc

Now you can run the command mysql from the terminal without typing any extra word.

Disallow prettier to format certain files

I’m using prettier with an auto format when I save any file, however, I want to disallow prettier completely on certain files, to do so I create a .prettierignore file on the root of my project and I add the file I want to ignore.

Pass optional function parameter in Typescript

You can use undefined for the non defined parameter:


function addNumbers(a: string, b: string, message: string) {
   return `${message} ${a + b}`;
}

const result = addNumbers(10, 17, undefined)
// result = 27

Because I set the message parameter to undefined the result will be in this case27.
You can also use the question mark close to the parametermessage?:string without using the undefined value when you use the function.


function addNumbers(a: string, b: string, message?: string) {
   return `${message} ${a + b}`;
}

const result = addNumbers(10, 17)
// result = 27

Use bcrypt to campare passwords using nodejs

Storing your password as a plain text is fast, however it is not secure, this why you have to make it complicated for hackers to get important information by using hashing, there is a couple of hashing functions like md5, we’ll not use md5 which it is not designed for passwords plus it’s cryptographically broken when the attacker can generate a list of common password and their corresponding hashes, then comparing the hashes to the site has stored.

You’ll use bcrypt which it is more designed for passwords, bcrypt use a salt to make a hash output unique even if your users use the same password, and this is a simple use case of bcrypt for a user who wanna update his password.

const bcrypt = require("bcryptjs");

// Generate Salt
const salt = bcrypt.genSaltSync(10);

// Plain Text Passwords
const currentpPassword = "abc123";
const oldPassword = "abc123";
const newPassword = "nWd6yCyj";

// Generate the Current User Password Hash
// by combining the salt and the password
const currentPasswordHash = bcrypt.hashSync(currentpPassword, salt);

// Compare the Old Password set by the user
// to the Current Password Hash
if (!bcrypt.compareSync(oldPassword, currentPasswordHash)) {
  console.log("The Current Password is Wrong");
}

// The new password should not be similar
// to the old password
if (bcrypt.compareSync(newPassword, currentPasswordHash)) {
  console.log(
    "The new password is similar to the new password, please choose a different one",
  );
}

Solve the Cannot read property hash of undefined error

After I update the next.js from the version 6.1.2 to the newest version at the time 7.0 I run yarn build to do the compilation and this is what I had as an errors:

[hardsource:efb84745] Could not freeze ./lib/queries.js: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/signin.js + 1 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/create-account.js + 2 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/activate-account.js + 1 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/reset-password.js + 1 modules: Cannot read property 'hash' of undefined
[hardsource:efb84745] Could not freeze ./pages/reset-password-confirmation.js + 1 modules: Cannot read property 'hash' of undefined

To solve this problem I remove the hard-source folder from /node_modules/.cache/hard-source/ and I rerun yarn build again.

yarn build
yarn run v1.9.4
$ next build
[hardsource:8c7e1fbc] Using 7 MB of disk space.
[hardsource:8c7e1fbc] Tracking node dependencies with: package-lock.json, yarn.lock.
[hardsource:8c7e1fbc] Reading from cache 8c7e1fbc...
[hardsource:efb84745] Using 7 MB of disk space.
[11:57:22 AM] Compiling client
[hardsource:efb84745] Tracking node dependencies with: package-lock.json, yarn.lock.
[hardsource:efb84745] Reading from cache efb84745...
[11:57:49 AM] Compiling server
[11:57:50 AM] Compiled client in 28s
[11:57:50 AM] Compiled server in 662ms
✨  Done in 34.11s.

Capitalise the first letter of each word using javascript

As an Example you’ve a full name john doe that you want it to be converted to John Doe when each first letter of the full name has to be in uppercase, to achieve that you’ll create a function that analyse a string entry and use regular expression to convert first letter to the uppercase form:

const capitalize = str => {
  const lower = String(str).toLowerCase();
  return lower.replace(/(^| )(\w)/g, function(x) {
    return x.toUpperCase();
  });
};

And this is the output of using the function capitalize("john doe"):

John Doe

Use the ES6 import on node

If you decide to use the ES6 import instead of require, this is a simple presentation on how to achieve that using babel:

– First Install the core dependencies:

npm install babel-register babel-preset-env --save-dev

– Create a starter.js file that contain how your code has to be transpiled plus the file that contains your imports:

require("babel-register")({
  presets: ["env"],
});

// Import the rest of our application.
module.exports = require("./test.js");

And to now your can use import on your test.js file instead of require:

import validator from "validator";

const email = "yep";

if (!validator.isEmail(email)) {
  console.log("the email is not valid");
}

To test the output:

node starters.js