<?xml version="1.0" encoding="utf-8" ?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Notes by Welling Guzmán</title><atom:link href="https://wellingguzman.com/feed.xml" rel="self" type="application/rss+xml"></atom:link><link>https://wellingguzman.com</link><description>Notes about code and among other things.</description><lastBuildDate>Fri, 10 Feb 2023 22:59:50 +0000</lastBuildDate><language>en-US</language><item><title>Lerp: Linear Interpolation Function</title><description><![CDATA[I have found the lerp function to be really useful when creating animation to make things transition smoothly.
function lerp(start, end, t) {
  return (1 - t) * start + t * end;
}
Many demo can be created from this, but for now here&#39;s a example how to transition between 2 colors in Canvas.

See it on CodePen
Full demo code:
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');

function lerp(start, end, t) {
  return (1 - t) * start + t * end;
}

function lerpColor(from, to, t)
{
    var color = {r:0, g: 0, b: 0};

    color.r = lerp(from.r, to.r, t);
    color.g = lerp(from.g, to.g, t);
    color.b = lerp(from.b, to.b, t);

    return color;
}

var now, last, t;
var duration = 2000;
var current = 0;
var colorA = {
  r: 0,
  g: 250,
  b: 250
};
var colorB = {
  r: 255,
  g: 0,
  b: 255,
};

function draw() {
  context.clearRect(0, 0, canvas.width, canvas.height);
  
  now = performance.now();
  var dt = now - last;

  current += dt;
  last = now;
  t = current/duration;
  
  if (current>duration) {
    t = 0;
    current = 0;
    var c = colorA;
    colorA = colorB;
    colorB = c;
  }
  
  var CC = lerpColor(colorA, colorB, current/duration);

  context.fillStyle = `rgba(${CC.r}, ${CC.g}, ${CC.b}, 1.0)`;
  context.fillRect(0, 0, canvas.width, canvas.height);
  
  context.restore();
  
  window.requestAnimationFrame(draw);
}

// start
last = performance.now();
window.requestAnimationFrame(draw);]]></description><content:encoded><![CDATA[<p>I have found the lerp function to be really useful when creating animation to make things transition smoothly.</p>
<pre><code><span class="token phrase">function lerp(start, end, t) {
  return (1 - t) <span class="token inline"><span class="token punctuation">*</span><span class="token bold"> start + t </span><span class="token punctuation">*</span></span> end;
}</span>
</code></pre><p>Many demo can be created from this, but for now here&#39;s a example how to transition between 2 colors in Canvas.</p>
<p><img src="/images/lerp-color.gif" alt="Transition between 2 colors in Canvas"></p>
<p><a href="https://codepen.io/wellingguzman/pen/poZMNxM">See it on CodePen</a></p>
<p>Full demo code:</p>
<pre><code class="language-js"><span class="token keyword">const</span> canvas <span class="token operator">=</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'canvas'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> context <span class="token operator">=</span> canvas<span class="token punctuation">.</span><span class="token function">getContext</span><span class="token punctuation">(</span><span class="token string">'2d'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">lerp</span><span class="token punctuation">(</span><span class="token parameter">start<span class="token punctuation">,</span> end<span class="token punctuation">,</span> t</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token number">1</span> <span class="token operator">-</span> t<span class="token punctuation">)</span> <span class="token operator">*</span> start <span class="token operator">+</span> t <span class="token operator">*</span> end<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">lerpColor</span><span class="token punctuation">(</span><span class="token parameter">from<span class="token punctuation">,</span> to<span class="token punctuation">,</span> t</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">var</span> color <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token literal-property property">r</span><span class="token operator">:</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token literal-property property">g</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

    color<span class="token punctuation">.</span>r <span class="token operator">=</span> <span class="token function">lerp</span><span class="token punctuation">(</span>from<span class="token punctuation">.</span>r<span class="token punctuation">,</span> to<span class="token punctuation">.</span>r<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
    color<span class="token punctuation">.</span>g <span class="token operator">=</span> <span class="token function">lerp</span><span class="token punctuation">(</span>from<span class="token punctuation">.</span>g<span class="token punctuation">,</span> to<span class="token punctuation">.</span>g<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>
    color<span class="token punctuation">.</span>b <span class="token operator">=</span> <span class="token function">lerp</span><span class="token punctuation">(</span>from<span class="token punctuation">.</span>b<span class="token punctuation">,</span> to<span class="token punctuation">.</span>b<span class="token punctuation">,</span> t<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> color<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">var</span> now<span class="token punctuation">,</span> last<span class="token punctuation">,</span> t<span class="token punctuation">;</span>
<span class="token keyword">var</span> duration <span class="token operator">=</span> <span class="token number">2000</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> current <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> colorA <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">r</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
  <span class="token literal-property property">g</span><span class="token operator">:</span> <span class="token number">250</span><span class="token punctuation">,</span>
  <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">250</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
<span class="token keyword">var</span> colorB <span class="token operator">=</span> <span class="token punctuation">{</span>
  <span class="token literal-property property">r</span><span class="token operator">:</span> <span class="token number">255</span><span class="token punctuation">,</span>
  <span class="token literal-property property">g</span><span class="token operator">:</span> <span class="token number">0</span><span class="token punctuation">,</span>
  <span class="token literal-property property">b</span><span class="token operator">:</span> <span class="token number">255</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">draw</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  context<span class="token punctuation">.</span><span class="token function">clearRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
  
  now <span class="token operator">=</span> performance<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token keyword">var</span> dt <span class="token operator">=</span> now <span class="token operator">-</span> last<span class="token punctuation">;</span>

  current <span class="token operator">+=</span> dt<span class="token punctuation">;</span>
  last <span class="token operator">=</span> now<span class="token punctuation">;</span>
  t <span class="token operator">=</span> current<span class="token operator">/</span>duration<span class="token punctuation">;</span>
  
  <span class="token keyword">if</span> <span class="token punctuation">(</span>current<span class="token operator">></span>duration<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    t <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    current <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">var</span> c <span class="token operator">=</span> colorA<span class="token punctuation">;</span>
    colorA <span class="token operator">=</span> colorB<span class="token punctuation">;</span>
    colorB <span class="token operator">=</span> c<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
  
  <span class="token keyword">var</span> <span class="token constant">CC</span> <span class="token operator">=</span> <span class="token function">lerpColor</span><span class="token punctuation">(</span>colorA<span class="token punctuation">,</span> colorB<span class="token punctuation">,</span> current<span class="token operator">/</span>duration<span class="token punctuation">)</span><span class="token punctuation">;</span>

  context<span class="token punctuation">.</span>fillStyle <span class="token operator">=</span> <span class="token template-string"><span class="token template-punctuation string">`</span><span class="token string">rgba(</span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">CC</span><span class="token punctuation">.</span>r<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">CC</span><span class="token punctuation">.</span>g<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, </span><span class="token interpolation"><span class="token interpolation-punctuation punctuation">${</span><span class="token constant">CC</span><span class="token punctuation">.</span>b<span class="token interpolation-punctuation punctuation">}</span></span><span class="token string">, 1.0)</span><span class="token template-punctuation string">`</span></span><span class="token punctuation">;</span>
  context<span class="token punctuation">.</span><span class="token function">fillRect</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>width<span class="token punctuation">,</span> canvas<span class="token punctuation">.</span>height<span class="token punctuation">)</span><span class="token punctuation">;</span>
  
  context<span class="token punctuation">.</span><span class="token function">restore</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  
  window<span class="token punctuation">.</span><span class="token function">requestAnimationFrame</span><span class="token punctuation">(</span>draw<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// start</span>
last <span class="token operator">=</span> performance<span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
window<span class="token punctuation">.</span><span class="token function">requestAnimationFrame</span><span class="token punctuation">(</span>draw<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>]]></content:encoded><pubDate>Fri, 10 Feb 2023 22:59:50 +0000</pubDate><link>https://wellingguzman.com/notes/lerp-linear-interpolation-function</link></item><item><title>Get PHP config values</title><description><![CDATA[There is a PHP built-in function named ini_get that allow us to get the value of a configuration option. The value returned by the function present the value during runtime rather than the value defined in php.ini file. The value of a configuration option set on files can be overwritten, as mentioned before, by various means, such as web server. Some example are:

By using PHP_VALUE on .htaccess or &lt;VirtualHost&gt; directive on Apache
By using fastcgi_param PHP_VALUE on NGINX
By code during execution, as example, using ini_set

To get the configuration value defined in php.ini, there&#39;s another built-in function called get_cfg_var, that actually do this. This function will ignore any values set by a webserver or during runtime and return the value set on the configuration file.

This function will not return configuration information set when the PHP was compiled, or read from an Apache configuration file.
PHP get_cfg_var Manual

It can be possible that PHP is not loading values from the configuration file (php.ini), well in that case no value will be returned from get_cfg_var.

To check whether the system is using a configuration file, try retrieving the value of the cfg_file_path configuration setting. If this is available, a configuration file is being used.
PHP get_cfg_var Manual

One thing to keep in mind is that defining a configuration option using php -d, even when php didn&#39;t load the values from php.ini this value will be available through get_cfg_var.
function get_config($key)
{
    if (get_cfg_var('cfg_file_path')) {
        return get_cfg_var($key);
    }

    return ini_get($key);
}
In the example above the function&#39;s goal is to return the value from php.ini, otherwise is going to fallback to retrieve the value present during runtime by using ini_get.]]></description><content:encoded><![CDATA[<p>There is a PHP built-in function named <a href="http://php.net/manual/en/function.ini-get.php"><code>ini_get</code></a> that allow us to get the value of a configuration option. The value returned by the function present the value during runtime rather than the value defined in <code>php.ini</code> file. The value of a configuration option set on files can be overwritten, as mentioned before, by various means, such as web server. Some example are:</p>
<ul>
<li>By using <code>PHP_VALUE</code> on <code>.htaccess</code> or <code>&lt;VirtualHost&gt;</code> directive on Apache</li>
<li>By using <code>fastcgi_param PHP_VALUE</code> on NGINX</li>
<li>By code during execution, as example, using <a href="http://php.net/manual/en/function.ini-get.php"><code>ini_set</code></a></li>
</ul>
<p>To get the configuration value defined in <code>php.ini</code>, there&#39;s another built-in function called <a href="http://php.net/manual/en/function.get-cfg-var.php"><code>get_cfg_var</code></a>, that actually do this. This function will ignore any values set by a webserver or during runtime and return the value set on the configuration file.</p>
<blockquote>
<p>This function will not return configuration information set when the PHP was compiled, or read from an Apache configuration file.
<cite><a href="http://php.net/manual/en/function.get-cfg-var.php">PHP get_cfg_var Manual</a></cite></p>
</blockquote>
<p>It can be possible that PHP is not loading values from the configuration file (<code>php.ini</code>), well in that case no value will be returned from <code>get_cfg_var</code>.</p>
<blockquote>
<p>To check whether the system is using a <a href="http://php.net/manual/en/configuration.file.php">configuration file</a>, try retrieving the value of the cfg_file_path configuration setting. If this is available, a configuration file is being used.
<cite><a href="http://php.net/manual/en/function.get-cfg-var.php">PHP get_cfg_var Manual</a></cite></p>
</blockquote>
<p>One thing to keep in mind is that defining a configuration option using <code>php -d</code>, even when php didn&#39;t load the values from <code>php.ini</code> this value will be available through <code>get_cfg_var</code>.</p>
<pre><code class="language-php"><span class="token keyword">function</span> <span class="token function-definition function">get_config</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">get_cfg_var</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cfg_file_path'</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">return</span> <span class="token function">get_cfg_var</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token function">ini_get</span><span class="token punctuation">(</span><span class="token variable">$key</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><p>In the example above the function&#39;s goal is to return the value from <code>php.ini</code>, otherwise is going to fallback to retrieve the value present during runtime by using <code>ini_get</code>.</p>
]]></content:encoded><pubDate>Sun, 10 Feb 2019 20:08:04 +0000</pubDate><link>https://wellingguzman.com/notes/get-php-config-values</link></item><item><title>Generating checksum hashes in node.js</title><description><![CDATA[Creating a checksum from a huge file can impact the memory consumption if it&#39;s not done correctly. One solution is to use Hash.update method to hash the data by pieces.
To create a checksum of a file we need to read its whole content and hash it. Reading the whole content of a big file could result in an undesired error due to not enough memory, because the content loaded into memory.
To overcome this we can use Hash.update method from the crypto module. It allows us to incrementally hash a string by appending new data, which makes it the perfect option to hash big files with low memory consumption.
We can read the file by chunk and incrementally update the hash by adding chunk to the hash object.
Below there&#39;s a snippet on how to generate a md5 checksum using Hash.update.
const crypto = require('crypto');
const fs = require('fs');

function getChecksum(path) {
  return new Promise(function (resolve, reject) {
    // crypto.createHash('sha1');
    // crypto.createHash('sha256');
    const hash = crypto.createHash('md5');
    const input = fs.createReadStream(path);

    input.on('error', reject);

    input.on('data', function (chunk) {
      hash.update(chunk);
    });

    input.on('close', function () {
      resolve(hash.digest('hex'));
    });
  });
}

// Usage
// node ./file.js path/to/file
getChecksum(process.argv[2])
  .then(console.log)
  .catch(console.error);

The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform.

Example of some algorithms you can use are: md5, sha1, sha256, and sha512.
References

crypto.createHash(algorithm[, options])
fs.createReadStream]]></description><content:encoded><![CDATA[<p>Creating a checksum from a huge file can impact the memory consumption if it&#39;s not done correctly. One solution is to use <code>Hash.update</code> method to hash the data by pieces.</p>
<p>To create a checksum of a file we need to read its whole content and hash it. Reading the whole content of a big file could result in an undesired error due to not enough memory, because the content loaded into memory.</p>
<p>To overcome this we can use <code>Hash.update</code> method from the <code>crypto</code> module. It allows us to incrementally hash a string by appending new data, which makes it the perfect option to hash big files with low memory consumption.</p>
<p>We can read the file by chunk and incrementally update the hash by adding chunk to the hash object.</p>
<p>Below there&#39;s a snippet on how to generate a md5 checksum using <code>Hash.update</code>.</p>
<pre><code class="language-js"><span class="token keyword">const</span> crypto <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'crypto'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">const</span> fs <span class="token operator">=</span> <span class="token function">require</span><span class="token punctuation">(</span><span class="token string">'fs'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">function</span> <span class="token function">getChecksum</span><span class="token punctuation">(</span><span class="token parameter">path</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// crypto.createHash('sha1');</span>
    <span class="token comment">// crypto.createHash('sha256');</span>
    <span class="token keyword">const</span> hash <span class="token operator">=</span> crypto<span class="token punctuation">.</span><span class="token function">createHash</span><span class="token punctuation">(</span><span class="token string">'md5'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">const</span> input <span class="token operator">=</span> fs<span class="token punctuation">.</span><span class="token function">createReadStream</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>

    input<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'error'</span><span class="token punctuation">,</span> reject<span class="token punctuation">)</span><span class="token punctuation">;</span>

    input<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'data'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">chunk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      hash<span class="token punctuation">.</span><span class="token function">update</span><span class="token punctuation">(</span>chunk<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    input<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'close'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span>hash<span class="token punctuation">.</span><span class="token function">digest</span><span class="token punctuation">(</span><span class="token string">'hex'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// Usage</span>
<span class="token comment">// node ./file.js path/to/file</span>
<span class="token function">getChecksum</span><span class="token punctuation">(</span>process<span class="token punctuation">.</span>argv<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>console<span class="token punctuation">.</span>log<span class="token punctuation">)</span>
  <span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span>console<span class="token punctuation">.</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><blockquote>
<p>The algorithm is dependent on the available algorithms supported by the version of OpenSSL on the platform.</p>
</blockquote>
<p>Example of some algorithms you can use are: md5, sha1, sha256, and sha512.</p>
<h3>References</h3>
<ul>
<li><a href="https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options"><code>crypto.createHash(algorithm[, options])</code></a></li>
<li><a href="https://nodejs.org/api/fs.html#fs_fs_createreadstream_path_options"><code>fs.createReadStream</code></a></li>
</ul>
]]></content:encoded><pubDate>Sat, 09 Feb 2019 21:25:21 +0000</pubDate><link>https://wellingguzman.com/notes/node-checksum</link></item><item><title>Node pipe input</title><description><![CDATA[If you want to pipe the output of one program to a node script, you can use process.stdin.
echo "name" | ./hello.js
The example above should output &quot;Hello &quot; + any output of the first script.
const stdin = process.stdin;
let data = '';

stdin.setEncoding('utf8');

stdin.on('data', function (chunk) {
  data += chunk;
});

stdin.on('end', function () {
  console.log("Hello " + data);
});

stdin.on('error', console.error);
Using the input stream (stdin), we read the input data that was sent by the first script.
This can be rewrite to use promises, so it looks like this:
getInput().then(sayHello).catch(console.error);
Complete example below:
function sayHello(name) {
  console.log("Hello " + name);
}

function getInput() {
  return new Promise(function (resolve, reject) {
    const stdin = process.stdin;
    let data = '';

    stdin.setEncoding('utf8');
    stdin.on('data', function (chunk) {
      data += chunk;
    });

    stdin.on('end', function () {
      resolve(data);
    });

    stdin.on('error', reject);
  });
}

getInput().then(sayHello).catch(console.error);]]></description><content:encoded><![CDATA[<p>If you want to pipe the output of one program to a node script, you can use <code>process.stdin</code>.</p>
<pre><code class="language-shell"><span class="token builtin class-name">echo</span> <span class="token string">"name"</span> <span class="token operator">|</span> ./hello.js
</code></pre><p>The example above should output &quot;Hello &quot; + any output of the first script.</p>
<pre><code class="language-js"><span class="token keyword">const</span> stdin <span class="token operator">=</span> process<span class="token punctuation">.</span>stdin<span class="token punctuation">;</span>
<span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>

stdin<span class="token punctuation">.</span><span class="token function">setEncoding</span><span class="token punctuation">(</span><span class="token string">'utf8'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'data'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">chunk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  data <span class="token operator">+=</span> chunk<span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'end'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello "</span> <span class="token operator">+</span> data<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'error'</span><span class="token punctuation">,</span> console<span class="token punctuation">.</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>Using the input stream (<code>stdin</code>), we read the input data that was sent by the first script.</p>
<p>This can be rewrite to use promises, so it looks like this:</p>
<pre><code class="language-js"><span class="token function">getInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>sayHello<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span>console<span class="token punctuation">.</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>Complete example below:</p>
<pre><code class="language-js"><span class="token keyword">function</span> <span class="token function">sayHello</span><span class="token punctuation">(</span><span class="token parameter">name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span><span class="token string">"Hello "</span> <span class="token operator">+</span> name<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">function</span> <span class="token function">getInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Promise</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">resolve<span class="token punctuation">,</span> reject</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> stdin <span class="token operator">=</span> process<span class="token punctuation">.</span>stdin<span class="token punctuation">;</span>
    <span class="token keyword">let</span> data <span class="token operator">=</span> <span class="token string">''</span><span class="token punctuation">;</span>

    stdin<span class="token punctuation">.</span><span class="token function">setEncoding</span><span class="token punctuation">(</span><span class="token string">'utf8'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'data'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token parameter">chunk</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      data <span class="token operator">+=</span> chunk<span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'end'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token function">resolve</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    stdin<span class="token punctuation">.</span><span class="token function">on</span><span class="token punctuation">(</span><span class="token string">'error'</span><span class="token punctuation">,</span> reject<span class="token punctuation">)</span><span class="token punctuation">;</span>
  <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token function">getInput</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">then</span><span class="token punctuation">(</span>sayHello<span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">catch</span><span class="token punctuation">(</span>console<span class="token punctuation">.</span>error<span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>]]></content:encoded><pubDate>Fri, 08 Feb 2019 23:38:12 +0000</pubDate><link>https://wellingguzman.com/notes/node-pipe-input</link></item><item><title>Zend DB Nested Conditions</title><description><![CDATA[If you want to nest multiple conditions using Zend DB, there&#39;s two methods made for this, nest() and unnest(), it begins and ends the nesting. Alternative you can use nest and unnest as property.
We are going to create the query below using Zend DB.
SELECT `id`, `name`, `language`, `country`
FROM `customers`
WHERE `country` = 'jp'
    AND (`language` = 'en' OR `language` = 'ja')
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\Where;

$sql = new Sql($adapter);
$select = $sql->select();
$select->columns([
  'id',
  'name',
  'language',
  'country'
]);
$select->from('customers');

$where = new Where();
$where->equalTo('country', 'jp');

// Open nesting
$whereNest = $where->nest();
$whereNest->equalTo('language', 'en');
$whereNest->or;
$whereNest->equalTo('language', 'ja');

// Close nesting
$whereNest->unnest();
$select->where($where);

$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();]]></description><content:encoded><![CDATA[<p>If you want to nest multiple conditions using Zend DB, there&#39;s two methods made for this, <code>nest()</code> and <code>unnest()</code>, it begins and ends the nesting. Alternative you can use <code>nest</code> and <code>unnest</code> as property.</p>
<p>We are going to create the query below using Zend DB.</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>language<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>country<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>customers<span class="token punctuation">`</span></span>
<span class="token keyword">WHERE</span> <span class="token identifier"><span class="token punctuation">`</span>country<span class="token punctuation">`</span></span> <span class="token operator">=</span> <span class="token string">'jp'</span>
    <span class="token operator">AND</span> <span class="token punctuation">(</span><span class="token identifier"><span class="token punctuation">`</span>language<span class="token punctuation">`</span></span> <span class="token operator">=</span> <span class="token string">'en'</span> <span class="token operator">OR</span> <span class="token identifier"><span class="token punctuation">`</span>language<span class="token punctuation">`</span></span> <span class="token operator">=</span> <span class="token string">'ja'</span><span class="token punctuation">)</span>
</code></pre><pre><code class="language-php"><span class="token keyword">use</span> <span class="token package">Zend<span class="token punctuation">\</span>Db<span class="token punctuation">\</span>Sql<span class="token punctuation">\</span>Sql</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Zend<span class="token punctuation">\</span>Db<span class="token punctuation">\</span>Sql<span class="token punctuation">\</span>Where</span><span class="token punctuation">;</span>

<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Sql</span><span class="token punctuation">(</span><span class="token variable">$adapter</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span> <span class="token operator">=</span> <span class="token variable">$sql</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span><span class="token operator">-></span><span class="token function">columns</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
  <span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span>
  <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span>
  <span class="token string single-quoted-string">'language'</span><span class="token punctuation">,</span>
  <span class="token string single-quoted-string">'country'</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'customers'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$where</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Where</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$where</span><span class="token operator">-></span><span class="token function">equalTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'country'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'jp'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// Open nesting</span>
<span class="token variable">$whereNest</span> <span class="token operator">=</span> <span class="token variable">$where</span><span class="token operator">-></span><span class="token function">nest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$whereNest</span><span class="token operator">-></span><span class="token function">equalTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'language'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'en'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$whereNest</span><span class="token operator">-></span><span class="token property">or</span><span class="token punctuation">;</span>
<span class="token variable">$whereNest</span><span class="token operator">-></span><span class="token function">equalTo</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'language'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'ja'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// Close nesting</span>
<span class="token variable">$whereNest</span><span class="token operator">-></span><span class="token function">unnest</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token variable">$where</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$statement</span> <span class="token operator">=</span> <span class="token variable">$sql</span><span class="token operator">-></span><span class="token function">prepareStatementForSqlObject</span><span class="token punctuation">(</span><span class="token variable">$select</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$statement</span><span class="token operator">-></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>]]></content:encoded><pubDate>Thu, 07 Feb 2019 21:09:25 +0000</pubDate><link>https://wellingguzman.com/notes/zend-db-nested-conditions</link></item><item><title>PHP DateTime createFromFormat c fails</title><description><![CDATA[PHP has a datetime format character for ISO 8601 named c, that outputs a datetime string like this: 2019-02-06T23:19:33-01:00.
There&#39;s a problem with this character when you use it with DateTime::createFromFormat – it fails.
// Returns false
$datetime = DateTime::createFromFormat('c', '2019-02-06T23:19:33-01:00');
The character can be used with date function, and it works.
$date = date('c');
It also works when it&#39;s used with DateTime() class.
$datetime = (new \DateTime())->format('c');
I couldn&#39;t find the reason behind this, but I found a workaround. From the datetime string we can use the date and time character formats, what&#39;s missing it&#39;s what format character to use for the timezone offset.
Why I am trying to do here? I want to confirm the input datetime is of an c format.
After looking at the date function format list, there&#39;s a format character to determine the offset time.

P Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00

Instead of using c, we are going to use the following format: Y-m-d\TH:i:sP.
$datetime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2019-02-06T23:19:33-01:00');]]></description><content:encoded><![CDATA[<p>PHP has a datetime format character for ISO 8601 named <code>c</code>, that outputs a datetime string like this: <code>2019-02-06T23:19:33-01:00</code>.</p>
<p>There&#39;s a problem with this character when you use it with <code>DateTime::createFromFormat</code> – it fails.</p>
<pre><code class="language-php"><span class="token comment">// Returns false</span>
<span class="token variable">$datetime</span> <span class="token operator">=</span> <span class="token class-name static-context">DateTime</span><span class="token operator">::</span><span class="token function">createFromFormat</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'c'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'2019-02-06T23:19:33-01:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>The character can be used with <code>date</code> function, and it works.</p>
<pre><code class="language-php"><span class="token variable">$date</span> <span class="token operator">=</span> <span class="token function">date</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>It also works when it&#39;s used with <code>DateTime()</code> class.</p>
<pre><code class="language-php"><span class="token variable">$datetime</span> <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>DateTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">format</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'c'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>I couldn&#39;t find the reason behind this, but I found a workaround. From the datetime string we can use the date and time character formats, what&#39;s missing it&#39;s what format character to use for the timezone offset.</p>
<p>Why I am trying to do here? I want to confirm the input datetime is of an <code>c</code> format.</p>
<p>After looking at the <a href="http://php.net/manual/en/function.date.php">date function format list</a>, there&#39;s a format character to determine the offset time.</p>
<blockquote>
<p><code>P</code> Difference to Greenwich time (GMT) with colon between hours and minutes (added in PHP 5.1.3) Example: +02:00</p>
</blockquote>
<p>Instead of using <code>c</code>, we are going to use the following format: <code>Y-m-d\TH:i:sP</code>.</p>
<pre><code class="language-php"><span class="token variable">$datetime</span> <span class="token operator">=</span> <span class="token class-name static-context">DateTime</span><span class="token operator">::</span><span class="token function">createFromFormat</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Y-m-d\TH:i:sP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'2019-02-06T23:19:33-01:00'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>]]></content:encoded><pubDate>Wed, 06 Feb 2019 20:17:02 +0000</pubDate><link>https://wellingguzman.com/notes/php-datetime-createfromformat-c</link></item><item><title>Webpack without .babelrc</title><description><![CDATA[Following my previous note: Hello Preact Modules.
We can avoid the .babelrc file by adding these configuration to the webpack.config.js file.
.babelrc file:
{
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": [
    ["@babel/plugin-transform-react-jsx", {
      "pragma": "h"
    }],
  ]
}
Below there&#39;s the new webpack.config.js file.
module.exports = {
  entry: './app.js',
  output: {
    path: __dirname,
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        test: /\.js$/,
-       loader: 'babel-loader',
+       use: {
+         loader: 'babel-loader',
+         options: {
+           presets: [
+             '@babel/preset-env'
+           ],
+           plugins: [
+             ['@babel/plugin-transform-react-jsx', {
+               'pragma': 'h'
+             }],
+           ]
+         }
        }
      }
    ]
  }
}
Now we can delete .babelrc, and everything should work the same.]]></description><content:encoded><![CDATA[<p>Following my previous note: <a href="/notes/hello-preact-modules">Hello Preact Modules</a>.</p>
<p>We can avoid the <code>.babelrc</code> file by adding these configuration to the <code>webpack.config.js</code> file.</p>
<p><code>.babelrc</code> file:</p>
<pre><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"presets"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token string">"@babel/preset-env"</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token property">"plugins"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token string">"@babel/plugin-transform-react-jsx"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      <span class="token property">"pragma"</span><span class="token operator">:</span> <span class="token string">"h"</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre><p>Below there&#39;s the new <code>webpack.config.js</code> file.</p>
<pre><code class="language-diff">module.exports = {
<span class="token unchanged"><span class="token prefix unchanged"> </span><span class="token line"> entry: './app.js',
</span><span class="token prefix unchanged"> </span><span class="token line"> output: {
</span><span class="token prefix unchanged"> </span><span class="token line">   path: __dirname,
</span><span class="token prefix unchanged"> </span><span class="token line">   filename: 'bundle.js'
</span><span class="token prefix unchanged"> </span><span class="token line"> },
</span><span class="token prefix unchanged"> </span><span class="token line"> module: {
</span><span class="token prefix unchanged"> </span><span class="token line">   rules: [
</span><span class="token prefix unchanged"> </span><span class="token line">     {
</span><span class="token prefix unchanged"> </span><span class="token line">       test: /\.js$/,
</span></span><span class="token deleted-sign deleted"><span class="token prefix deleted">-</span><span class="token line">       loader: 'babel-loader',
</span></span><span class="token inserted-sign inserted"><span class="token prefix inserted">+</span><span class="token line">       use: {
</span><span class="token prefix inserted">+</span><span class="token line">         loader: 'babel-loader',
</span><span class="token prefix inserted">+</span><span class="token line">         options: {
</span><span class="token prefix inserted">+</span><span class="token line">           presets: [
</span><span class="token prefix inserted">+</span><span class="token line">             '@babel/preset-env'
</span><span class="token prefix inserted">+</span><span class="token line">           ],
</span><span class="token prefix inserted">+</span><span class="token line">           plugins: [
</span><span class="token prefix inserted">+</span><span class="token line">             ['@babel/plugin-transform-react-jsx', {
</span><span class="token prefix inserted">+</span><span class="token line">               'pragma': 'h'
</span><span class="token prefix inserted">+</span><span class="token line">             }],
</span><span class="token prefix inserted">+</span><span class="token line">           ]
</span><span class="token prefix inserted">+</span><span class="token line">         }
</span></span><span class="token unchanged"><span class="token prefix unchanged"> </span><span class="token line">       }
</span><span class="token prefix unchanged"> </span><span class="token line">     }
</span><span class="token prefix unchanged"> </span><span class="token line">   ]
</span><span class="token prefix unchanged"> </span><span class="token line"> }
</span></span>}
</code></pre><p>Now we can delete <code>.babelrc</code>, and everything should work the same.</p>
]]></content:encoded><pubDate>Tue, 05 Feb 2019 23:30:21 +0000</pubDate><link>https://wellingguzman.com/notes/webpack-without-babelrc</link></item><item><title>Hello Preact Modules</title><description><![CDATA[In a previous note I created a Preact example with minimal configuration. In that example I added Preact using script tag instead of using ES6 modules. To make this work I need to install webpack and babel-loader.
Webpack allow us to convert the preact package into a preact module. While in the other hand babel-loader is a webpack loader that allow us to convert ES6 code into ES5.
Following the previous example, I am going to use the same HTML file, but removing the Preact script tag, so it looks like the code below:
&lt;!doctype html>
&lt;html>
  &lt;head>
    &lt;title>Hello Preact&lt;/title>
  &lt;/head>
  &lt;body>
    &lt;div id="root">&lt;/div>
    &lt;script src="bundle.js">&lt;/script>
  &lt;/body>
&lt;/html>
Now I&#39;m replacing the following line from app.js to use import.
From:
const { h, render, Component } = window.preact;
To:
import { h, render, Component } from 'preact';
Webpack &amp; Babel Loader
I need to install webpack, webpack-cli, and babel-loader using npm.
npm install --save-dev babel-loader webpack webpack-cli
I am going to install webpack-cli to execute webpack from the command line.
Webpack Configuration
I need to create a new file for the webpack configuration named webpack.config.js. This file will tells webpack what&#39;s the input and output file, and to use the babel-loader to convert the ES6 features.
In the previous example this was done using the babel command, now webpack will take care of all this.
module.exports = {
    entry: './app.js',
    output: {
        path: __dirname,
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
              test: /\.js$/,
                loader: 'babel-loader'
            }
        ]
    }
}
Webpack is ready to be used to convert app.js into bundle.js, allowing us to use import as well.
Build Script
We are now replacing the build script from package.json to use webpack instead of babel.
From:
"scripts": {
  "build": "babel app.js -o bundle.js",
  "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
},
To:
"scripts": {
  "build": "webpack",
  "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
},
Run the build command, and access index.html to see the result.
$ npm run build
See final code here: WellingGuzman/hello-preact.]]></description><content:encoded><![CDATA[<p>In a <a href="/notes/hello-preact">previous note</a> I created a Preact example with minimal configuration. In that example I added Preact using script tag instead of using ES6 modules. To make this work I need to install <code>webpack</code> and <code>babel-loader</code>.</p>
<p>Webpack allow us to convert the <code>preact</code> package into a <code>preact</code> module. While in the other hand <code>babel-loader</code> is a webpack loader that allow us to convert ES6 code into ES5.</p>
<p>Following the previous example, I am going to use the same HTML file, but removing the Preact script tag, so it looks like the code below:</p>
<pre><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">doctype</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Hello Preact<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bundle.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span>
</code></pre><p>Now I&#39;m replacing the following line from <code>app.js</code> to use <code>import</code>.</p>
<p>From:</p>
<pre><code class="language-js"><span class="token keyword">const</span> <span class="token punctuation">{</span> h<span class="token punctuation">,</span> render<span class="token punctuation">,</span> Component <span class="token punctuation">}</span> <span class="token operator">=</span> window<span class="token punctuation">.</span>preact<span class="token punctuation">;</span>
</code></pre><p>To:</p>
<pre><code class="language-js"><span class="token keyword">import</span> <span class="token punctuation">{</span> h<span class="token punctuation">,</span> render<span class="token punctuation">,</span> Component <span class="token punctuation">}</span> <span class="token keyword">from</span> <span class="token string">'preact'</span><span class="token punctuation">;</span>
</code></pre><h2>Webpack &amp; Babel Loader</h2>
<p>I need to install <code>webpack</code>, <code>webpack-cli</code>, and <code>babel-loader</code> using <code>npm</code>.</p>
<pre><code class="language-shell"><span class="token function">npm</span> <span class="token function">install</span> --save-dev babel-loader webpack webpack-cli
</code></pre><p>I am going to install <code>webpack-cli</code> to execute webpack from the command line.</p>
<h2>Webpack Configuration</h2>
<p>I need to create a new file for the webpack configuration named <code>webpack.config.js</code>. This file will tells webpack what&#39;s the input and output file, and to use the <code>babel-loader</code> to convert the ES6 features.</p>
<p>In the previous example this was done using the <code>babel</code> command, now <code>webpack</code> will take care of all this.</p>
<pre><code class="language-js">module<span class="token punctuation">.</span>exports <span class="token operator">=</span> <span class="token punctuation">{</span>
    <span class="token literal-property property">entry</span><span class="token operator">:</span> <span class="token string">'./app.js'</span><span class="token punctuation">,</span>
    <span class="token literal-property property">output</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">path</span><span class="token operator">:</span> __dirname<span class="token punctuation">,</span>
        <span class="token literal-property property">filename</span><span class="token operator">:</span> <span class="token string">'bundle.js'</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token literal-property property">module</span><span class="token operator">:</span> <span class="token punctuation">{</span>
        <span class="token literal-property property">rules</span><span class="token operator">:</span> <span class="token punctuation">[</span>
            <span class="token punctuation">{</span>
              <span class="token literal-property property">test</span><span class="token operator">:</span> <span class="token regex"><span class="token regex-delimiter">/</span><span class="token regex-source language-regex">\.js$</span><span class="token regex-delimiter">/</span></span><span class="token punctuation">,</span>
                <span class="token literal-property property">loader</span><span class="token operator">:</span> <span class="token string">'babel-loader'</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">]</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><p>Webpack is ready to be used to convert <code>app.js</code> into <code>bundle.js</code>, allowing us to use <code>import</code> as well.</p>
<h2>Build Script</h2>
<p>We are now replacing the <code>build</code> script from <code>package.json</code> to use <code>webpack</code> instead of <code>babel</code>.</p>
<p>From:</p>
<pre><code class="language-json"><span class="token property">"scripts"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  <span class="token property">"build"</span><span class="token operator">:</span> <span class="token string">"babel app.js -o bundle.js"</span><span class="token punctuation">,</span>
  <span class="token property">"test"</span><span class="token operator">:</span> <span class="token string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre><p>To:</p>
<pre><code class="language-json"><span class="token property">"scripts"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  <span class="token property">"build"</span><span class="token operator">:</span> <span class="token string">"webpack"</span><span class="token punctuation">,</span>
  <span class="token property">"test"</span><span class="token operator">:</span> <span class="token string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre><p>Run the build command, and access <code>index.html</code> to see the result.</p>
<pre><code class="language-shell">$ <span class="token function">npm</span> run build
</code></pre><p>See final code here: <a href="https://github.com/WellingGuzman/hello-preact/tree/webpack">WellingGuzman/hello-preact</a>.</p>
]]></content:encoded><pubDate>Mon, 04 Feb 2019 22:58:35 +0000</pubDate><link>https://wellingguzman.com/notes/hello-preact-modules</link></item><item><title>Hello Preact</title><description><![CDATA[I have worked on projects that uses Preact/React before, but those projects already have the workflow already configured and everything is already done for you using Webpack, babel, and a list of other tools that help this process of building all the js/jsx files to bundle js file.
Preact describes itself as an React lightweight alternative with the same API.

Fast 3kB alternative to React with the same modern API.

I have sit it down – well, lay down – to try create a &quot;hello world&quot; using Preact, with the minimal tooling possible to understand how this all modern front-end development framework works. As most tutotials, and guides I have found they all assumed you are familiar with all these tools, I didn&#39;t find much straight up guide for curious newbies that want to know how this all glue together.
At this point of time, your browser probably cannot understand JSX or some of the new ES6/2015 features, there&#39;s where Babel comes into action.
Setup Project
First thing first, let&#39;s create a new directoty and create a package.json file.
$ mkdir hello-preact
$ cd hello-preact
$ npm init -y
After executing npm init -y, a file called package.json should have been created.
Add HTML Page
&lt;!doctype html>
&lt;html>
  &lt;head>
    &lt;title>Hello Preact&lt;/title>
  &lt;/head>
  &lt;body>
    &lt;div id="root">&lt;/div>
    &lt;script src="https://cdn.jsdelivr.net/npm/preact/dist/preact.min.js">&lt;/script>
    &lt;script src="bundle.js">&lt;/script>
  &lt;/body>
&lt;/html>
Babel is going to transpile our app file into a single bundle.js file.
Add Preact Component
Create a new simple component:
const { h, render, Component } = window.preact;

class HelloPreact extends Component {
  render() {
    return &lt;div>Hello Preact&lt;/div>
  }
}

render(&lt;HelloPreact />, document.getElementById('root'));
And save it in a file named app.js, or any name you prefer.
The window.preact reference comes from the script tag inside the HTML file.
What is Babel?
In Babel own description:

Use next generation JavaScript, today

It is a tool that allows you to convert javascript features not yet implemented, into something actual javascript engine understands.
Install Babel
We are going to use @babel/cli package to transpile app.js into bundle.js.
There&#39;s different way you can use Babel, see instructions in Babel installation page.
$ npm install --save-dev @babel/core
$ npm install --save-dev @babel/cli

# Or both together
# npm install --save-dev @babel/core @babel/cli
@babel/cli allows us to execute @babel/core from the terminal.
Add Babel Plugins
Now we&#39;ve installed Babel, but we need make it useful by installing some plugins. What we want is to convert the modern JS code from app.js into something our browser understands.
@babel/preset-env helps us convert any code from ES2015 and newer standard.
Let&#39;s install and enable this preset. Preset is a collection of plugins.
$ npm install --save-dev @babel/preset-env
All modern/future features can be converted, but JSX are not.
Let&#39;s install the React JSX transform plugin. React and Preact have the same API, which means we can use the React plugin with a minor change.
$ npm install --save-dev @babel/plugin-transform-react-jsx
The preset and plugin are installed, but we need to tell @babel/cli that we want to use it.
Create a file named .babelrc, it will holds the configurations for @babel/cli.
$ touch .babelrc
To enable the preset and plugin we need to add it to .babelrc file.
{
  "presets": [
    "@babel/preset-env"
  ],
  "plugins": [
    ["@babel/plugin-transform-react-jsx", {
      "pragma": "h"
    }],
  ]
}
By default @babel/plugin-transform-react-jsx because is a React specific plugin, it will translate &lt;&gt;&lt;/&gt; from JSX into React.createElement, instead of Preact h function, we must substitute this by changing the pragma option to h.
Pragma option reference
Transform JavaScript
Add a new command to package.json inside the scripts property to execute the babel command.
"scripts": {
  "build": "babel app.js -o bundle.js",
  "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
},
Run the build command, and access index.html to see the result.
$ npm run build
See final code here: WellingGuzman/hello-preact.
See also:

Hello Preact Modules]]></description><content:encoded><![CDATA[<p>I have worked on projects that uses Preact/React before, but those projects already have the workflow already configured and everything is already done for you using Webpack, babel, and a list of other tools that help this process of building all the js/jsx files to bundle js file.</p>
<p>Preact describes itself as an React lightweight alternative with the same API.</p>
<blockquote>
<p>Fast 3kB alternative to React with the same modern API.</p>
</blockquote>
<p>I have sit it down – well, lay down – to try create a &quot;hello world&quot; using Preact, with the minimal tooling possible to understand how this all modern front-end development framework works. As most tutotials, and guides I have found they all assumed you are familiar with all these tools, I didn&#39;t find much straight up guide for curious newbies that want to know how this all glue together.</p>
<p>At this point of time, your browser probably cannot understand JSX or some of the new ES6/2015 features, there&#39;s where Babel comes into action.</p>
<h2>Setup Project</h2>
<p>First thing first, let&#39;s create a new directoty and create a <code>package.json</code> file.</p>
<pre><code class="language-shell">$ <span class="token function">mkdir</span> hello-preact
$ <span class="token builtin class-name">cd</span> hello-preact
$ <span class="token function">npm</span> init <span class="token parameter variable">-y</span>
</code></pre><p>After executing <code>npm init -y</code>, a file called <code>package.json</code> should have been created.</p>
<h2>Add HTML Page</h2>
<pre><code class="language-html"><span class="token doctype"><span class="token punctuation">&lt;!</span><span class="token doctype-tag">doctype</span> <span class="token name">html</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>html</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>head</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>title</span><span class="token punctuation">></span></span>Hello Preact<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>title</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>head</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>body</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>div</span> <span class="token attr-name">id</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>root<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>div</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>https://cdn.jsdelivr.net/npm/preact/dist/preact.min.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
    <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;</span>script</span> <span class="token attr-name">src</span><span class="token attr-value"><span class="token punctuation attr-equals">=</span><span class="token punctuation">"</span>bundle.js<span class="token punctuation">"</span></span><span class="token punctuation">></span></span><span class="token script"></span><span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>script</span><span class="token punctuation">></span></span>
  <span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>body</span><span class="token punctuation">></span></span>
<span class="token tag"><span class="token tag"><span class="token punctuation">&lt;/</span>html</span><span class="token punctuation">></span></span>
</code></pre><p>Babel is going to transpile our app file into a single <code>bundle.js</code> file.</p>
<h2>Add Preact Component</h2>
<p>Create a new simple component:</p>
<pre><code class="language-js"><span class="token keyword">const</span> <span class="token punctuation">{</span> h<span class="token punctuation">,</span> render<span class="token punctuation">,</span> Component <span class="token punctuation">}</span> <span class="token operator">=</span> window<span class="token punctuation">.</span>preact<span class="token punctuation">;</span>

<span class="token keyword">class</span> <span class="token class-name">HelloPreact</span> <span class="token keyword">extends</span> <span class="token class-name">Component</span> <span class="token punctuation">{</span>
  <span class="token function">render</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token operator">&lt;</span>div<span class="token operator">></span>Hello Preact<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">></span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token function">render</span><span class="token punctuation">(</span><span class="token operator">&lt;</span>HelloPreact <span class="token operator">/</span><span class="token operator">></span><span class="token punctuation">,</span> document<span class="token punctuation">.</span><span class="token function">getElementById</span><span class="token punctuation">(</span><span class="token string">'root'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>And save it in a file named <code>app.js</code>, or any name you prefer.</p>
<p>The <code>window.preact</code> reference comes from the script tag inside the HTML file.</p>
<h2>What is Babel?</h2>
<p>In Babel own description:</p>
<blockquote>
<p>Use next generation JavaScript, today</p>
</blockquote>
<p>It is a tool that allows you to convert javascript features not yet implemented, into something actual javascript engine understands.</p>
<h2>Install Babel</h2>
<p>We are going to use <code>@babel/cli</code> package to transpile <code>app.js</code> into <code>bundle.js</code>.</p>
<p>There&#39;s different way you can use Babel, <a href="https://babeljs.io/setup#installation">see instructions</a> in Babel installation page.</p>
<pre><code class="language-shell">$ <span class="token function">npm</span> <span class="token function">install</span> --save-dev @babel/core
$ <span class="token function">npm</span> <span class="token function">install</span> --save-dev @babel/cli

<span class="token comment"># Or both together</span>
<span class="token comment"># npm install --save-dev @babel/core @babel/cli</span>
</code></pre><p><code>@babel/cli</code> allows us to execute <code>@babel/core</code> from the terminal.</p>
<h2>Add Babel Plugins</h2>
<p>Now we&#39;ve installed Babel, but we need make it useful by installing some plugins. What we want is to convert the modern JS code from <code>app.js</code> into something our browser understands.</p>
<p><code>@babel/preset-env</code> helps us convert any code from ES2015 and newer standard.</p>
<p>Let&#39;s install and enable this preset. Preset is a collection of plugins.</p>
<pre><code class="language-shell">$ <span class="token function">npm</span> <span class="token function">install</span> --save-dev @babel/preset-env
</code></pre><p>All modern/future features can be converted, but JSX are not.</p>
<p>Let&#39;s install the React JSX transform plugin. React and Preact have the same API, which means we can use the React plugin with a minor change.</p>
<pre><code class="language-shell">$ <span class="token function">npm</span> <span class="token function">install</span> --save-dev @babel/plugin-transform-react-jsx
</code></pre><p>The preset and plugin are installed, but we need to tell <code>@babel/cli</code> that we want to use it.</p>
<p>Create a file named <code>.babelrc</code>, it will holds the configurations for <code>@babel/cli</code>.</p>
<pre><code class="language-shell">$ <span class="token function">touch</span> .babelrc
</code></pre><p>To enable the preset and plugin we need to add it to <code>.babelrc</code> file.</p>
<pre><code class="language-json"><span class="token punctuation">{</span>
  <span class="token property">"presets"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token string">"@babel/preset-env"</span>
  <span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token property">"plugins"</span><span class="token operator">:</span> <span class="token punctuation">[</span>
    <span class="token punctuation">[</span><span class="token string">"@babel/plugin-transform-react-jsx"</span><span class="token punctuation">,</span> <span class="token punctuation">{</span>
      <span class="token property">"pragma"</span><span class="token operator">:</span> <span class="token string">"h"</span>
    <span class="token punctuation">}</span><span class="token punctuation">]</span><span class="token punctuation">,</span>
  <span class="token punctuation">]</span>
<span class="token punctuation">}</span>
</code></pre><p>By default <code>@babel/plugin-transform-react-jsx</code> because is a React specific plugin, it will translate <code>&lt;&gt;&lt;/&gt;</code> from JSX into <code>React.createElement</code>, instead of Preact <code>h</code> function, we must substitute this by changing the <code>pragma</code> option to <code>h</code>.</p>
<p><a href="https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragma">Pragma option reference</a></p>
<h2>Transform JavaScript</h2>
<p>Add a new command to <code>package.json</code> inside the <code>scripts</code> property to execute the babel command.</p>
<pre><code class="language-json"><span class="token property">"scripts"</span><span class="token operator">:</span> <span class="token punctuation">{</span>
  <span class="token property">"build"</span><span class="token operator">:</span> <span class="token string">"babel app.js -o bundle.js"</span><span class="token punctuation">,</span>
  <span class="token property">"test"</span><span class="token operator">:</span> <span class="token string">"echo \"Error: no test specified\" &amp;&amp; exit 1"</span>
<span class="token punctuation">}</span><span class="token punctuation">,</span>
</code></pre><p>Run the build command, and access <code>index.html</code> to see the result.</p>
<pre><code class="language-shell">$ <span class="token function">npm</span> run build
</code></pre><p>See final code here: <a href="https://github.com/WellingGuzman/hello-preact">WellingGuzman/hello-preact</a>.</p>
<h3>See also:</h3>
<ul>
<li><a href="/notes/hello-preact-modules">Hello Preact Modules</a></li>
</ul>
]]></content:encoded><pubDate>Sun, 03 Feb 2019 18:13:35 +0000</pubDate><link>https://wellingguzman.com/notes/hello-preact</link></item><item><title>Change timezone in Ubuntu</title><description><![CDATA[To show the system date, time, and other related information, use the command timedatectl.
With this command you can do more than just change the timezone, such as change the date and time.
To change the timezone you should use timedatectl set-timezone &lt;timezone&gt;. If you don&#39;t know the names of the timezone, use timedatectl list-timezones to list all timezones available.
Replace &lt;timezone&gt; with any of the value listed by the timedatectl list-timezones command.
timedatectl set-timezone America/Vancouver
Run timedatectl again to confirm the changes.]]></description><content:encoded><![CDATA[<p>To show the system date, time, and other related information, use the command <code>timedatectl</code>.</p>
<p>With this command you can do more than just change the timezone, such as change the date and time.</p>
<p>To change the timezone you should use <code>timedatectl set-timezone &lt;timezone&gt;</code>. If you don&#39;t know the names of the timezone, use <code>timedatectl list-timezones</code> to list all timezones available.</p>
<p>Replace <code>&lt;timezone&gt;</code> with any of the value listed by the <code>timedatectl list-timezones</code> command.</p>
<pre><code><span class="token phrase">timedatectl set-timezone America/Vancouver</span>
</code></pre><p>Run <code>timedatectl</code> again to confirm the changes.</p>
]]></content:encoded><pubDate>Sat, 02 Feb 2019 21:40:19 +0000</pubDate><link>https://wellingguzman.com/notes/change-timezone-ubuntu</link></item><item><title>PHP: Zero equals to any string</title><description><![CDATA[In PHP any string is equals to 0, except when it starts with a number that&#39;s not zero.
// Equals
$result = 0 == '';
$result = 0 == '0';
$result = 0 == 'string';
$result = 0 == '0string';

// Not Equals
$result = 0 == '1string';
When there&#39;s an integer in one of the operands, PHP converts the other to an integer. If it starts with a number, all the subsequent numbers will be returned when casting the string value to an integer. 1string will result in 1, and 123string will result in 123.
I spent sometime trying to figure out why my code wasn&#39;t working. This could be avoided by using the identical operator ===, or casting the integer value to string, rather the equal operator ==.]]></description><content:encoded><![CDATA[<p>In PHP any string is equals to <code>0</code>, except when it starts with a number that&#39;s not zero.</p>
<pre><code class="language-php"><span class="token comment">// Equals</span>
<span class="token variable">$result</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">;</span>
<span class="token variable">$result</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'0'</span><span class="token punctuation">;</span>
<span class="token variable">$result</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'string'</span><span class="token punctuation">;</span>
<span class="token variable">$result</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'0string'</span><span class="token punctuation">;</span>

<span class="token comment">// Not Equals</span>
<span class="token variable">$result</span> <span class="token operator">=</span> <span class="token number">0</span> <span class="token operator">==</span> <span class="token string single-quoted-string">'1string'</span><span class="token punctuation">;</span>
</code></pre><p>When there&#39;s an integer in one of the operands, PHP converts the other to an integer. If it starts with a number, all the subsequent numbers will be returned when casting the string value to an integer. <code>1string</code> will result in <code>1</code>, and <code>123string</code> will result in <code>123</code>.</p>
<p>I spent sometime trying to figure out why my code wasn&#39;t working. This could be avoided by using the identical operator <code>===</code>, or casting the integer value to string, rather the equal operator <code>==</code>.</p>
]]></content:encoded><pubDate>Fri, 01 Feb 2019 21:22:23 +0000</pubDate><link>https://wellingguzman.com/notes/php-zero-equals-any-string</link></item><item><title>Pagination with MySQL</title><description><![CDATA[Having a large dataset and only needing to fetch a specific number of rows, it is the reason LIMIT clause exists. It allows to restrict the number of rows in a result returned by a SQL query statement.
Pagination refers to the process of dividing a large dataset into smaller parts.
The ability to send data to the user faster by fetching a whole dataset by small pieces at a time is one of the benefits of using pagination.
How it works
Pagination works by defining the maximum number of rows in the results per request and what page is being requested.
The table below represents the items on a table named users, that is going to be used an example.
+----+----------+
| id | Name     |
+----+----------+
| 1  | John     |
| 2  | Jane     |
| 3  | Peter    |
| 4  | Joseph   |
| 5  | Mary     |
| 6  | Jack     |
| 7  | Ann      |
| 8  | Bill     |
| 9  | Sam      |
| 10 | Rose     |
| 11 | Juan     |
+----+----------+
For this example the maximum number of rows will be 2, which means on every request we are going to get at most 2 rows.
The table has 11 rows, and we are limiting the result by 2 rows per request, resulting in a 6 pages of 2 items. The number of pages are determined by dividing the number of rows (11) by the number of rows per page (2), and making sure the result is rounded to the next integer number.
Total pages = CEIL(Total number of rows / Limit number of rows)
MySQL doesn&#39;t have a PAGE clause, but it has a OFFSET clause, which allow to move the position from where to start counting up to the LIMIT number.
The value of OFFSET is done by multiplying the LIMIT clause value by the page number your are looking for minus 1.
OFFSET = LIMIT * (PAGE - 1)
In the table above there is 11 users and to get the first 2 users we use the following query:
PAGE = 1
LIMIT = 2
OFFSET = (PAGE-1) * LIMIT
OFFSET = (1-1) * 2
OFFSET = 0 * 2
OFFSET = 0
The offset initial value is 0, and not 1, that&#39;s why we subtract 1 from the page number.
SELECT `id`, `name`
FROM `users`
LIMIT 2
OFFSET 0
The previous query will produce the following result which represents the page 1 of the pagination:
+----+----------+
| id | Name     |
+----+----------+
| 1  | John     |
| 2  | Jane     |
+----+----------+
MySQL has a different way to use offset, without using the OFFSET clause.
SELECT `id`, `name`
FROM `users`
LIMIT 0,2
The first parameter is the offset and the second parameter is the rows count.
To get the second page, or in other word the next two rows, we must calculate again the OFFSET or increase by one the previous value.
PAGE = 2
LIMIT = 2
OFFSET = (PAGE-1) * LIMIT
OFFSET = (2-1) * 2
OFFSET = 1 * 2
OFFSET = 2
SELECT `id`, `name`
FROM `users`
LIMIT 2
OFFSET 2
Below can be seen the result of the previous query:
+----+----------+
| id | Name     |
+----+----------+
| 3  | Peter    |
| 4  | Joseph   |
+----+----------+
The query translate to skip the first 2 items and get the next 2 rows.
So getting the third page we use the following OFFSET of 4, to skip the first 4 items.
PAGE = 3
LIMIT = 2
OFFSET = (PAGE-1) * LIMIT
OFFSET = (3-1) * 2
OFFSET = 2 * 2
OFFSET = 4
SELECT `id`, `name`
FROM `users`
LIMIT 2 OFFSET 4
+----+----------+
| id | Name     |
+----+----------+
| 5  | Mary     |
| 6  | Jack     |
+----+----------+
OFFSET and ORDER BY
Using OFFSET and ORDER BY together could make the pagination non-functional returning in rows random orders, and unexpected rows on each page.

If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.
MySQL documentation

The most common situation is that if you are sorting by a column that doesn&#39;t have an index, MySQL Server cannot determine a proper order of the rows.
One way to solve this is by adding an index to the column or columns. Although this may not be as optimal if you don&#39;t want or need to add indexes to multiple columns only for this purpose.

If it is important to ensure the same row order with and without LIMIT, include additional columns in the ORDER BY clause to make the order deterministic.
MySQL documentation

What this means is there&#39;s another way to solve this is by adding to the ORDER BY clause an unique column, for example a primary key column.
SELECT `id`, `name`
FROM `users`
LIMIT 2
OFFSET 2
ORDER BY `name`, `id`
Instead of:
SELECT `id`, `name`
FROM `users`
LIMIT 2
OFFSET 2
ORDER BY `name`
This way you can make sure that MySQL sorts the rows by an unique column before finding the LIMIT number of rows.]]></description><content:encoded><![CDATA[<p>Having a large dataset and only needing to fetch a specific number of rows, it is the reason <code>LIMIT</code> clause exists. It allows to restrict the number of rows in a result returned by a SQL query statement.</p>
<p>Pagination refers to the process of dividing a large dataset into smaller parts.</p>
<p>The ability to send data to the user faster by fetching a whole dataset by small pieces at a time is one of the benefits of using pagination.</p>
<h2>How it works</h2>
<p>Pagination works by defining the maximum number of rows in the results per request and what page is being requested.</p>
<p>The table below represents the items on a table named <code>users</code>, that is going to be used an example.</p>
<pre><code><span class="token phrase"><span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token table"><span class="token punctuation">|</span> id <span class="token punctuation">|</span> Name     <span class="token punctuation">|</span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token punctuation">|</span> 1  <span class="token punctuation">|</span> John     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 2  <span class="token punctuation">|</span> Jane     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 3  <span class="token punctuation">|</span> Peter    <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 4  <span class="token punctuation">|</span> Joseph   <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 5  <span class="token punctuation">|</span> Mary     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 6  <span class="token punctuation">|</span> Jack     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 7  <span class="token punctuation">|</span> Ann      <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 8  <span class="token punctuation">|</span> Bill     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 9  <span class="token punctuation">|</span> Sam      <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 10 <span class="token punctuation">|</span> Rose     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 11 <span class="token punctuation">|</span> Juan     <span class="token punctuation">|</span></span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+</span>
</code></pre><p>For this example the maximum number of rows will be <code>2</code>, which means on every request we are going to get at most 2 rows.</p>
<p>The table has 11 rows, and we are limiting the result by 2 rows per request, resulting in a 6 pages of 2 items. The number of pages are determined by dividing the number of rows (<code>11</code>) by the number of rows per page (<code>2</code>), and making sure the result is rounded to the next integer number.</p>
<pre><code><span class="token phrase">Total pages = <span class="token acronym">CEIL<span class="token punctuation">(</span><span class="token comment">Total number of rows / Limit number of rows</span><span class="token punctuation">)</span></span></span>
</code></pre><p>MySQL doesn&#39;t have a <code>PAGE</code> clause, but it has a <code>OFFSET</code> clause, which allow to move the position from where to start counting up to the <code>LIMIT</code> number.</p>
<p>The value of <code>OFFSET</code> is done by multiplying the <code>LIMIT</code> clause value by the page number your are looking for minus 1.</p>
<pre><code><span class="token phrase">OFFSET = LIMIT * (PAGE - 1)</span>
</code></pre><p>In the table above there is 11 users and to get the first 2 users we use the following query:</p>
<pre><code><span class="token phrase">PAGE = 1
LIMIT = 2
OFFSET = (PAGE-1) * LIMIT
OFFSET = (1-1) * 2
OFFSET = 0 * 2
OFFSET = 0</span>
</code></pre><p>The offset initial value is <code>0</code>, and not <code>1</code>, that&#39;s why we subtract 1 from the page number.</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>users<span class="token punctuation">`</span></span>
<span class="token keyword">LIMIT</span> <span class="token number">2</span>
<span class="token keyword">OFFSET</span> <span class="token number">0</span>
</code></pre><p>The previous query will produce the following result which represents the page 1 of the pagination:</p>
<pre><code><span class="token phrase"><span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token table"><span class="token punctuation">|</span> id <span class="token punctuation">|</span> Name     <span class="token punctuation">|</span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token punctuation">|</span> 1  <span class="token punctuation">|</span> John     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 2  <span class="token punctuation">|</span> Jane     <span class="token punctuation">|</span></span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+</span>
</code></pre><p>MySQL has a different way to use offset, without using the <code>OFFSET</code> clause.</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>users<span class="token punctuation">`</span></span>
<span class="token keyword">LIMIT</span> <span class="token number">0</span><span class="token punctuation">,</span><span class="token number">2</span>
</code></pre><p>The first parameter is the offset and the second parameter is the rows count.</p>
<p>To get the second page, or in other word the next two rows, we must calculate again the <code>OFFSET</code> or increase by one the previous value.</p>
<pre><code><span class="token phrase">PAGE = 2
LIMIT = 2
OFFSET = (PAGE-1) * LIMIT
OFFSET = (2-1) * 2
OFFSET = 1 * 2
OFFSET = 2</span>
</code></pre><pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>users<span class="token punctuation">`</span></span>
<span class="token keyword">LIMIT</span> <span class="token number">2</span>
<span class="token keyword">OFFSET</span> <span class="token number">2</span>
</code></pre><p>Below can be seen the result of the previous query:</p>
<pre><code><span class="token phrase"><span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token table"><span class="token punctuation">|</span> id <span class="token punctuation">|</span> Name     <span class="token punctuation">|</span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token punctuation">|</span> 3  <span class="token punctuation">|</span> Peter    <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 4  <span class="token punctuation">|</span> Joseph   <span class="token punctuation">|</span></span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+</span>
</code></pre><p>The query translate to skip the first 2 items and get the next 2 rows.</p>
<p>So getting the third page we use the following <code>OFFSET</code> of 4, to skip the first 4 items.</p>
<pre><code><span class="token phrase">PAGE = 3
LIMIT = 2
OFFSET = (PAGE-1) * LIMIT
OFFSET = (3-1) * 2
OFFSET = 2 * 2
OFFSET = 4</span>
</code></pre><pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>users<span class="token punctuation">`</span></span>
<span class="token keyword">LIMIT</span> <span class="token number">2</span> <span class="token keyword">OFFSET</span> <span class="token number">4</span>
</code></pre><pre><code><span class="token phrase"><span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token table"><span class="token punctuation">|</span> id <span class="token punctuation">|</span> Name     <span class="token punctuation">|</span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+
<span class="token punctuation">|</span> 5  <span class="token punctuation">|</span> Mary     <span class="token punctuation">|</span>
<span class="token punctuation">|</span> 6  <span class="token punctuation">|</span> Jack     <span class="token punctuation">|</span></span>
<span class="token inline"><span class="token punctuation">+</span><span class="token inserted"><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-</span><span class="token punctuation">+</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span><span class="token inline"><span class="token punctuation">-</span><span class="token deleted">-</span><span class="token punctuation">-</span></span>-+</span>
</code></pre><h2>OFFSET and ORDER BY</h2>
<p>Using <code>OFFSET</code> and <code>ORDER BY</code> together could make the pagination non-functional returning in rows random orders, and unexpected rows on each page.</p>
<blockquote>
<p>If multiple rows have identical values in the ORDER BY columns, the server is free to return those rows in any order, and may do so differently depending on the overall execution plan. In other words, the sort order of those rows is nondeterministic with respect to the nonordered columns.
<cite><a href="https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html">MySQL documentation</a></cite></p>
</blockquote>
<p>The most common situation is that if you are sorting by a column that doesn&#39;t have an index, MySQL Server cannot determine a proper order of the rows.</p>
<p>One way to solve this is by adding an index to the column or columns. Although this may not be as optimal if you don&#39;t want or need to add indexes to multiple columns only for this purpose.</p>
<blockquote>
<p>If it is important to ensure the same row order with and without LIMIT, include additional columns in the ORDER BY clause to make the order deterministic.
<cite><a href="https://dev.mysql.com/doc/refman/5.7/en/limit-optimization.html">MySQL documentation</a></cite></p>
</blockquote>
<p>What this means is there&#39;s another way to solve this is by adding to the <code>ORDER BY</code> clause an unique column, for example a primary key column.</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>users<span class="token punctuation">`</span></span>
<span class="token keyword">LIMIT</span> <span class="token number">2</span>
<span class="token keyword">OFFSET</span> <span class="token number">2</span>
<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span>
</code></pre><p>Instead of:</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> <span class="token identifier"><span class="token punctuation">`</span>id<span class="token punctuation">`</span></span><span class="token punctuation">,</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
<span class="token keyword">FROM</span> <span class="token identifier"><span class="token punctuation">`</span>users<span class="token punctuation">`</span></span>
<span class="token keyword">LIMIT</span> <span class="token number">2</span>
<span class="token keyword">OFFSET</span> <span class="token number">2</span>
<span class="token keyword">ORDER</span> <span class="token keyword">BY</span> <span class="token identifier"><span class="token punctuation">`</span>name<span class="token punctuation">`</span></span>
</code></pre><p>This way you can make sure that MySQL sorts the rows by an unique column before finding the <code>LIMIT</code> number of rows.</p>
]]></content:encoded><pubDate>Fri, 07 Sep 2018 08:20:01 +0000</pubDate><link>https://wellingguzman.com/notes/pagination-with-mysql</link></item><item><title>Upgrading nodejs/npm on Ubuntu 14.04</title><description><![CDATA[I was updating my site and everything was working correctly on my local machine, but as soon as it was deployed, the new code crashed the http server.
I noticed that the server has an outdated version of nodejs. Running a 0.x nodejs version while everything was created under 8.x.
After trying to update to a new version can be tricky as there&#39;s a tons of way documented on how to install it and easier on windows and mac systems.
How to install or update nodejs can be found on the package manager section of nodejs&#39;s downloads page.
This method also works for any Debian and ubuntu based distributions.
Update Source List
First, You would need to update your system package source list. Depending on the version you want to upgrade, there&#39;s different script that will try to update your source list.
NOTE: Be carefully, these are bash scripts and can execute dangerous code, if you are a little bit skeptical you can see the content first before you use it or you can read the manual installation
# Node.js v4
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -

# Node.js v5
curl -sL https://deb.nodesource.com/setup_5.x | sudo -E bash -

# Node.js v6
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -

# Node.js v7
curl -sL https://deb.nodesource.com/setup_7.x | sudo -E bash -

# Node.js v8
curl -sL https://deb.nodesource.com/setup_8.x | sudo -E bash -

# Node.js v9
curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -

# Node.js v10
curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
Install package
After the source list has been updated, the next step is installing the new nodejs version.
sudo apt-get install -y nodejs
Confirm
The last step will be to confirm the version installed are correct, or pointing to the right path as multiple nodejs can be installed in the same system.
Try running:
$ node -v
$ which node
$ npm -v
$ which npm
In conclusion you may want to have node and npm point to the right path. For example, you can look into /usr/bin or /usr/local/bin to make sure which node and which npm points to the right version.]]></description><content:encoded><![CDATA[<p>I was updating my site and everything was working correctly on my local machine, but as soon as it was deployed, the new code crashed the http server.</p>
<p>I noticed that the server has an outdated version of nodejs. Running a <code>0.x</code> nodejs version while everything was created under <code>8.x</code>.</p>
<p>After trying to update to a new version can be tricky as there&#39;s a tons of way documented on how to install it and easier on windows and mac systems.</p>
<p>How to install or update nodejs can be found on the <a href="https://nodejs.org/en/download/package-manager/">package manager</a> section of nodejs&#39;s downloads page.</p>
<p>This method also works for any Debian and ubuntu based distributions.</p>
<h2>Update Source List</h2>
<p>First, You would need to update your system package source list. Depending on the version you want to upgrade, there&#39;s different script that will try to update your source list.</p>
<p><strong>NOTE</strong>: Be carefully, these are bash scripts and can execute dangerous code, if you are a little bit skeptical you can see the content first before you use it or you can read the <a href="https://github.com/nodesource/distributions#debmanual">manual installation</a></p>
<pre><code class="language-shell"><span class="token comment"># Node.js v4</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_4.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -

<span class="token comment"># Node.js v5</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_5.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -

<span class="token comment"># Node.js v6</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_6.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -

<span class="token comment"># Node.js v7</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_7.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -

<span class="token comment"># Node.js v8</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_8.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -

<span class="token comment"># Node.js v9</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_9.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -

<span class="token comment"># Node.js v10</span>
<span class="token function">curl</span> <span class="token parameter variable">-sL</span> https://deb.nodesource.com/setup_10.x <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token parameter variable">-E</span> <span class="token function">bash</span> -
</code></pre><h2>Install package</h2>
<p>After the source list has been updated, the next step is installing the new nodejs version.</p>
<pre><code class="language-shell"><span class="token function">sudo</span> <span class="token function">apt-get</span> <span class="token function">install</span> <span class="token parameter variable">-y</span> nodejs
</code></pre><h2>Confirm</h2>
<p>The last step will be to confirm the version installed are correct, or pointing to the right path as multiple nodejs can be installed in the same system.</p>
<p>Try running:</p>
<pre><code class="language-shell">$ <span class="token function">node</span> <span class="token parameter variable">-v</span>
$ <span class="token function">which</span> <span class="token function">node</span>
$ <span class="token function">npm</span> <span class="token parameter variable">-v</span>
$ <span class="token function">which</span> <span class="token function">npm</span>
</code></pre><p>In conclusion you may want to have <code>node</code> and <code>npm</code> point to the right path. For example, you can look into <code>/usr/bin</code> or <code>/usr/local/bin</code> to make sure <code>which node</code> and <code>which npm</code> points to the right version.</p>
]]></content:encoded><pubDate>Thu, 23 Aug 2018 10:53:13 +0000</pubDate><link>https://wellingguzman.com/notes/upgrading-nodejs-npm-on-ubuntu-14-04</link></item><item><title>Manipulating Pixels Using Canvas</title><description><![CDATA[Modern browsers support playing video via the &lt;video&gt; element. Most browsers also have access to webcams via the MediaDevices.getUserMedia() API. But even with those two things combined, we can’t really access and manipulate those pixels directly.
Fortunately, browsers have a Canvas API that allows us to draw graphics using JavaScript. We can actually draw images to the &lt;canvas&gt; from the video itself, which gives us the ability to manipulate and play with those pixels.
Everything you learn here about how to manipulate pixels will give you a foundation to work with images and videos of any kind or any source, not just canvas.
Read full article.]]></description><content:encoded><![CDATA[<p>Modern browsers support playing video via the <code>&lt;video&gt;</code> element. Most browsers also have access to webcams via the <a href="https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia">MediaDevices.getUserMedia()</a> API. But even with those two things combined, we can’t really access and manipulate those pixels directly.</p>
<p>Fortunately, browsers have a <a href="https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API">Canvas API</a> that allows us to draw graphics using JavaScript. We can actually draw images to the <code>&lt;canvas&gt;</code> from the video itself, which gives us the ability to manipulate and play with those pixels.</p>
<p>Everything you learn here about how to manipulate pixels will give you a foundation to work with images and videos of any kind or any source, not just canvas.</p>
<p><a href="https://css-tricks.com/manipulating-pixels-using-canvas">Read full article</a>.</p>
]]></content:encoded><pubDate>Sat, 21 Jul 2018 13:55:13 +0000</pubDate><link>https://wellingguzman.com/notes/manipulating-pixels-using-canvas</link></item><item><title>#100DaysOfEnglish</title><description><![CDATA[Along side with #100DaysOfCode I have decided I should practice my English for the next 100 days.
I learned english almost by myself. I was taught english at school, but the same thing for a decade, nothing useful at the long run. about 5 years ago I put myself into a intensive english course for grammar and allow myself to speak with people in english, since then I have not talk much.
I would say I am good at listening and reading, I read and listen english every day, but I write from time to time, and speak not very often (almost zero time).
My goals is to get used to speak and write english more often. The way I want to accomplish this is by writing every day or record myself talking or both.
I hope by the end of the 100 days I&#39;ve improved my skill of communicate.
This will count as the first day.
Follow my progress on Twitter.]]></description><content:encoded><![CDATA[<p>Along side with <a href="/notes/hashtag-100-days-of-code">#100DaysOfCode</a> I have decided I should practice my English for the next 100 days.</p>
<p>I learned english almost by myself. I was taught english at school, but the same thing for a decade, nothing useful at the long run. about 5 years ago I put myself into a intensive english course for grammar and allow myself to speak with people in english, since then I have not talk much.</p>
<p>I would say I am good at listening and reading, I read and listen english every day, but I write from time to time, and speak not very often (almost zero time).</p>
<p>My goals is to get used to speak and write english more often. The way I want to accomplish this is by writing every day or record myself talking or both.</p>
<p>I hope by the end of the 100 days I&#39;ve improved my skill of communicate.</p>
<p><em>This will count as the first day.</em></p>
<p>Follow my progress on <a href="https://twitter.com/WellingGuzman">Twitter</a>.</p>
]]></content:encoded><pubDate>Thu, 10 May 2018 10:55:03 +0000</pubDate><link>https://wellingguzman.com/notes/hashtag-100-days-of-english</link></item><item><title>#100DaysOfCode</title><description><![CDATA[I found old codes I wrote more than a decade ago and realized how much I have learned so far, still there is a lot more to learn.
I feel bad when I have to search for a simple task, such as, how substring minus start position works? I have used it a lot of times and still forget how it works.
I used to create a lot of things, even useless app because I like to make things. Lately it has been different as I was believing I should act more &quot;Pro&quot; and just build things that matter and useful to at least a group of people.
I realized I should go back to my roots and create things just for fun. I don&#39;t have to worry about whether or not is perfect or useful just make things works and learn something new.
I have been thinking on challenging myself on coding a project each week or code something everyday for a n period of time, but I did not find any strong motivation until today.
Today I came across a tweet and a video that looked like it was directly written to me, these put the cherry on top and I decided to make the #100DaysOfCode challenge.
The rules are basically:

Code minimum an hour every day for the next 100 days.
Publish your progress

The goals will be create around 20 mini projects in the next 100 days.
Follow my progress on Twitter and GitHub.]]></description><content:encoded><![CDATA[<p>I found old codes I wrote more than a decade ago and realized how much I have learned so far, still there is a lot more to learn.</p>
<p>I feel bad when I have to search for a simple task, such as, how substring minus start position works? I have used it a lot of times and still forget how it works.</p>
<p>I used to create a lot of things, even useless app because I like to make things. Lately it has been different as I was believing I should act more &quot;Pro&quot; and just build things that matter and useful to at least a group of people.</p>
<p>I realized I should go back to my roots and create things just for fun. I don&#39;t have to worry about whether or not is perfect or useful just make things works and learn something new.</p>
<p>I have been thinking on challenging myself on coding a project each week or code something everyday for a n period of time, but I did not find any strong motivation until today.</p>
<p>Today I came across a <a href="https://twitter.com/wilto/status/994216304503590912">tweet</a> and a <a href="https://www.youtube.com/watch?v=c0bsKc4tiuY">video</a> that looked like it was directly written to me, these put the cherry on top and I decided to make the <strong>#100DaysOfCode</strong> challenge.</p>
<p>The rules are basically:</p>
<ul>
<li>Code minimum an hour every day for the next 100 days.</li>
<li>Publish your progress</li>
</ul>
<p>The goals will be create around 20 mini projects in the next 100 days.</p>
<p>Follow my progress on <a href="https://twitter.com/WellingGuzman">Twitter</a> and <a href="https://github.com/WellingGuzman/100DaysOfCoding">GitHub</a>.</p>
]]></content:encoded><pubDate>Thu, 10 May 2018 10:33:22 +0000</pubDate><link>https://wellingguzman.com/notes/hashtag-100-days-of-code</link></item><item><title>Guzzle HTTP: Upgrade mocking from version 5 to 6</title><description><![CDATA[Testing a http response with guzzle 5 was done using the Subscriber/Mock class, but on version 6, this class doesn&#39;t exists.
The way this mock response works is by attaching fake response objects to the http client, and on every request it will pick the result from the queue instead of making a real request to the server.
Guzzle HTTP 5
Let&#39;s take a look how mocking was done on version 5 in the example below:
// Guzzle http client
$client = new \GuzzleHttp\Client([
    'base_url' => 'http://localhost'
]);

// Create Mock
$mock = new \GuzzleHttp\Subscriber\Mock();

// Attach mocking subscriber to the client
$client->getEmitter()->attach($mock);

// Add response to a queue
$mockPath = '/path/to/raw/response.txt';
$mockContent = file_get_contents($mockPath);
$mock->addResponse($mockContent);
The content of /path/to/raw/response.txt is a raw http response.
HTTP/1.1 200 OK
Date: Wed, 15 Jun 2016 17:02:51 GMT
Server: nginx
Content-Length: 86
Content-Type: application/json; charset=utf-8

{"id":1,"active":1,"title":"Article 1","body":"Content","tags":"tags,tugs","author":1}
The next request the client makes it will pick the first response on the queue as the result.
You can add more responses to the queue, and make sure you the queue is not empty before you send a new request.
$mockPath = '/path/to/raw/http/response/file.txt';
$mockContent = file_get_contents($mockPath);
$mock->addResponse($mockContent);
Guzzle HTTP 6
On version 6 they removed the Mock class and introduce a new MockHandler class. docs.
We now need to create a http client and attach the MockHandler object as the client handler
// Guzzle http client
$handler = new \GuzzleHttp\Handler\MockHandler();
$client = new \GuzzleHttp\Client(['handler' => $handler]);
There is not way to access the handler, so you have to keep a reference somewhere.
Now all the response needs to be added to the mock handler using the append method.
// Add response to a queue
$mockPath = '/path/to/raw/http/response/file.txt';
$mockContent = file_get_contents($mockPath);
// Convert the raw http response into a Response Object
$response = \GuzzleHttp\Psr7\parse_response($mockContent);
$handler->append($response);
Same as previous version each request pull the first response from the queue on each request.]]></description><content:encoded><![CDATA[<p>Testing a http response with guzzle 5 was done using the <code>Subscriber/Mock</code> class, but on version 6, this class doesn&#39;t exists.</p>
<p>The way this mock response works is by attaching fake response objects to the http client, and on every request it will pick the result from the queue instead of making a real request to the server.</p>
<h2>Guzzle HTTP 5</h2>
<p>Let&#39;s take a look how mocking was done on version 5 in the example below:</p>
<pre><code class="language-php"><span class="token comment">// Guzzle http client</span>
<span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>GuzzleHttp<span class="token punctuation">\</span>Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span>
    <span class="token string single-quoted-string">'base_url'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'http://localhost'</span>
<span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// Create Mock</span>
<span class="token variable">$mock</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>GuzzleHttp<span class="token punctuation">\</span>Subscriber<span class="token punctuation">\</span>Mock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// Attach mocking subscriber to the client</span>
<span class="token variable">$client</span><span class="token operator">-></span><span class="token function">getEmitter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">attach</span><span class="token punctuation">(</span><span class="token variable">$mock</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// Add response to a queue</span>
<span class="token variable">$mockPath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/path/to/raw/response.txt'</span><span class="token punctuation">;</span>
<span class="token variable">$mockContent</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token variable">$mockPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$mock</span><span class="token operator">-></span><span class="token function">addResponse</span><span class="token punctuation">(</span><span class="token variable">$mockContent</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>The content of <code>/path/to/raw/response.txt</code> is a raw http response.</p>
<pre><code class="language-http"><span class="token response-status"><span class="token http-version property">HTTP/1.1</span> <span class="token status-code number">200</span> <span class="token reason-phrase string">OK</span></span>
<span class="token header"><span class="token header-name keyword">Date</span><span class="token punctuation">:</span> <span class="token header-value">Wed, 15 Jun 2016 17:02:51 GMT</span></span>
<span class="token header"><span class="token header-name keyword">Server</span><span class="token punctuation">:</span> <span class="token header-value">nginx</span></span>
<span class="token header"><span class="token header-name keyword">Content-Length</span><span class="token punctuation">:</span> <span class="token header-value">86</span></span>
<span class="token header"><span class="token header-name keyword">Content-Type</span><span class="token punctuation">:</span> <span class="token header-value">application/json; charset=utf-8</span></span>

{"id":1,"active":1,"title":"Article 1","body":"Content","tags":"tags,tugs","author":1}
</code></pre><p>The next request the client makes it will pick the first response on the queue as the result.</p>
<p>You can add more responses to the queue, and make sure you the queue is not empty before you send a new request.</p>
<pre><code class="language-php"><span class="token variable">$mockPath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/path/to/raw/http/response/file.txt'</span><span class="token punctuation">;</span>
<span class="token variable">$mockContent</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token variable">$mockPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$mock</span><span class="token operator">-></span><span class="token function">addResponse</span><span class="token punctuation">(</span><span class="token variable">$mockContent</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><h2>Guzzle HTTP 6</h2>
<p>On version 6 they removed the <code>Mock</code> class and introduce a new <code>MockHandler</code> class. <a href="http://docs.guzzlephp.org/en/stable/testing.html#mock-handler">docs</a>.</p>
<p>We now need to create a http client and attach the <code>MockHandler</code> object as the client handler</p>
<pre><code class="language-php"><span class="token comment">// Guzzle http client</span>
<span class="token variable">$handler</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>GuzzleHttp<span class="token punctuation">\</span>Handler<span class="token punctuation">\</span>MockHandler</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$client</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified"><span class="token punctuation">\</span>GuzzleHttp<span class="token punctuation">\</span>Client</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'handler'</span> <span class="token operator">=></span> <span class="token variable">$handler</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>There is not way to access the handler, so you have to keep a reference somewhere.</p>
<p>Now all the response needs to be added to the mock handler using the <code>append</code> method.</p>
<pre><code class="language-php"><span class="token comment">// Add response to a queue</span>
<span class="token variable">$mockPath</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/path/to/raw/http/response/file.txt'</span><span class="token punctuation">;</span>
<span class="token variable">$mockContent</span> <span class="token operator">=</span> <span class="token function">file_get_contents</span><span class="token punctuation">(</span><span class="token variable">$mockPath</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token comment">// Convert the raw http response into a Response Object</span>
<span class="token variable">$response</span> <span class="token operator">=</span> <span class="token function"><span class="token punctuation">\</span>GuzzleHttp<span class="token punctuation">\</span>Psr7<span class="token punctuation">\</span>parse_response</span><span class="token punctuation">(</span><span class="token variable">$mockContent</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$handler</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token variable">$response</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><p>Same as previous version each request pull the first response from the queue on each request.</p>
]]></content:encoded><pubDate>Sat, 31 Mar 2018 19:08:19 +0000</pubDate><link>https://wellingguzman.com/notes/guzzle-http-upgrade-mocking-from-version-5-to-6</link></item><item><title>Cocoa: Set the nib name on a view controller</title><description><![CDATA[When you create NSViewController and makes it the owner of a view nib, the controller and the nib has to have the same name otherwise the controller will fails loading the nib.
From NSViewController source file:

On 10.10 and higher, a nil nibName can be used, and NSViewController will automatically attempt to load a view with the same class name.

It is important to notice that even this comment states that will attempt to load a view with the same class name, it seems to be attempting the file name instead.
NSViewController -loadView comment:

Prior to 10.10, -loadView would not have well defined behavior if [self nibName] returned nil. On 10.10 and later, if nibName is nil, NSViewController will automatically try to load a nib with the same name as the classname. This allows a convenience of doing [[MyViewController alloc] init] (which has a nil nibName) and having it automatically load a nib with the name &quot;MyViewController&quot;.

Having a controller with the name MyViewController and a view with the name MyView wouldn&#39;t work because the controller will attempt to load a nib with the name MyViewController. Naming a view MyViewController doesn&#39;t make sense because it is not a controller.
What should we do here? use the method -nibName to set the default nib name.
In your controller implementation file add the following method:
- (NSNibName)nibName
{
    return @"MyView";
}

NSNibName is an alias for NSString.

Now the controller will attempt to load MyView nib instead of a MyViewController.]]></description><content:encoded><![CDATA[<p>When you create <code>NSViewController</code> and makes it the owner of a view nib, the controller and the nib has to have the same name otherwise the controller will fails loading the nib.</p>
<p>From <code>NSViewController</code> source file:</p>
<blockquote>
<p>On 10.10 and higher, a nil nibName can be used, and NSViewController will automatically attempt to load a view with the same class name.</p>
</blockquote>
<p>It is important to notice that even this comment states that will attempt to load a view with the same class name, it seems to be attempting the file name instead.</p>
<p><code>NSViewController</code> <code>-loadView</code> comment:</p>
<blockquote>
<p>Prior to 10.10, -loadView would not have well defined behavior if [self nibName] returned nil. On 10.10 and later, if nibName is nil, NSViewController will automatically try to load a nib with the same name as the classname. This allows a convenience of doing [[MyViewController alloc] init] (which has a nil nibName) and having it automatically load a nib with the name &quot;MyViewController&quot;.</p>
</blockquote>
<p>Having a controller with the name <code>MyViewController</code> and a view with the name <code>MyView</code> wouldn&#39;t work because the controller will attempt to load a nib with the name <code>MyViewController</code>. Naming a view <code>MyViewController</code> doesn&#39;t make sense because it is not a controller.</p>
<p>What should we do here? use the method <code>-nibName</code> to set the default nib name.</p>
<p>In your controller implementation file add the following method:</p>
<pre><code class="language-objective-c"><span class="token operator">-</span> <span class="token punctuation">(</span>NSNibName<span class="token punctuation">)</span>nibName
<span class="token punctuation">{</span>
    <span class="token keyword">return</span> <span class="token string">@"MyView"</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><blockquote>
<p><code>NSNibName</code> is an alias for <code>NSString</code>.</p>
</blockquote>
<p>Now the controller will attempt to load <code>MyView</code> nib instead of a <code>MyViewController</code>.</p>
]]></content:encoded><pubDate>Sat, 31 Mar 2018 01:06:32 +0000</pubDate><link>https://wellingguzman.com/notes/cocoa-set-the-nib-name-on-a-view-controller</link></item><item><title>Github: Re-authentication on Mac OSX</title><description><![CDATA[Trying to interact with github, pushing mainly as it requires write permission from the user to perform such task, I was getting a 403 error, which means I am not authorized to push to that repository.
remote: Permission to directus/directus.git denied to WellingGuzman.
fatal: unable to access 'https://github.com/directus/directus.git/': The requested URL returned error: 403
For some reason this started to happen after I installed Github Desktop Application.
I don&#39;t know the reason why it got invalid or corrupted, but I did find a way to re-authenticate myself.
On Mac OSX Git uses the Keychain Access to store credentials information, you can either update or remove the credentials from the keychain.
Removing this information the next time you try to push it will ask you to enter your username and password
$ git push origin master
Username for 'https://github.com': wellingguzman
Password for 'https://wellingguzman@github.com':
remote: Invalid username or password.
Terminal
git credential-osxkeychain erase
Application

Using the method of your preferences, Finder search, manually search the application directory or CMD (⌘) + Spacebar look for &quot;Keychain Access&quot;.
Search for &quot;Github.com&quot;
Find and edit/remove the one that says to be &quot;Internet password&quot; kind, to make sure this is the one, open this entry and on the access control tab should says credentials-osxkeychain.
After you are sure about this, edit or delete to get back your git access control.

Hope it helps, hope I can remember this next time.]]></description><content:encoded><![CDATA[<p>Trying to interact with github, pushing mainly as it requires write permission from the user to perform such task, I was getting a 403 error, which means I am not authorized to push to that repository.</p>
<pre><code class="language-shell">remote: Permission to directus/directus.git denied to WellingGuzman.
fatal: unable to access <span class="token string">'https://github.com/directus/directus.git/'</span><span class="token builtin class-name">:</span> The requested URL returned error: <span class="token number">403</span>
</code></pre><p>For some reason this started to happen after I installed Github Desktop Application.</p>
<p>I don&#39;t know the reason why it got invalid or corrupted, but I did find a way to re-authenticate myself.</p>
<p>On Mac OSX Git uses the Keychain Access to store credentials information, you can either update or remove the credentials from the keychain.</p>
<p>Removing this information the next time you try to push it will ask you to enter your username and password</p>
<pre><code class="language-shell">$ <span class="token function">git</span> push origin master
Username <span class="token keyword">for</span> <span class="token string">'https://github.com'</span><span class="token builtin class-name">:</span> wellingguzman
Password <span class="token keyword">for</span> <span class="token string">'https://wellingguzman@github.com'</span><span class="token builtin class-name">:</span>
remote: Invalid username or password.
</code></pre><h2>Terminal</h2>
<pre><code class="language-shell"><span class="token function">git</span> credential-osxkeychain erase
</code></pre><h2>Application</h2>
<ol>
<li>Using the method of your preferences, Finder search, manually search the application directory or <code>CMD (⌘) + Spacebar</code> look for &quot;Keychain Access&quot;.</li>
<li>Search for &quot;Github.com&quot;</li>
<li>Find and edit/remove the one that says to be &quot;Internet password&quot; kind, to make sure this is the one, open this entry and on the access control tab should says credentials-osxkeychain.</li>
<li>After you are sure about this, edit or delete to get back your git access control.</li>
</ol>
<p>Hope it helps, hope I can remember this next time.</p>
]]></content:encoded><pubDate>Sat, 10 Mar 2018 16:25:06 +0000</pubDate><link>https://wellingguzman.com/notes/github-reauthentication-on-mac-osx</link></item><item><title>MySQL fetch table name with original case</title><description><![CDATA[MySQL provides a database with metadata and information about the server, such as list of all the tables in a database and columns data type.
Fetching a table information can be done with the following query:
SELECT TABLE_NAME, ENGINE, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES
WHERE
  TABLE_SCHEMA = "mydatabase"
  AND TABLE_NAME = "Products"
Result:
+------------+--------+-----------------+
| TABLE_NAME | ENGINE | TABLE_COLLATION |
+------------+--------+-----------------+
| products   | InnoDB | utf8_general_ci |
+------------+--------+-----------------+
This result is what we expected, the name, the engine and the collation. The problem comes when the table have uppercase letter, as the result always seems to be in lowercase.
I can&#39;t tell if this is a configuration issue or a mysql bug.
Making a the condition for table name twice solves the issue.
SELECT TABLE_NAME, ENGINE, TABLE_COLLATION
FROM INFORMATION_SCHEMA.TABLES
WHERE
  TABLE_SCHEMA = "mydatabase"
  AND (
    TABLE_NAME = "Products"
    OR TABLE_NAME = "Products"
  )
The query above will result with the table name in the original case it was created.
+------------+--------+-----------------+
| TABLE_NAME | ENGINE | TABLE_COLLATION |
+------------+--------+-----------------+
| Products   | InnoDB | utf8_general_ci |
+------------+--------+-----------------+]]></description><content:encoded><![CDATA[<p>MySQL provides a database with metadata and information about the server, such as list of all the tables in a database and columns data type.</p>
<p>Fetching a table information can be done with the following query:</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> TABLE_NAME<span class="token punctuation">,</span> <span class="token keyword">ENGINE</span><span class="token punctuation">,</span> TABLE_COLLATION
<span class="token keyword">FROM</span> INFORMATION_SCHEMA<span class="token punctuation">.</span><span class="token keyword">TABLES</span>
<span class="token keyword">WHERE</span>
  TABLE_SCHEMA <span class="token operator">=</span> <span class="token string">"mydatabase"</span>
  <span class="token operator">AND</span> TABLE_NAME <span class="token operator">=</span> <span class="token string">"Products"</span>
</code></pre><p>Result:</p>
<pre><code class="language-text">+------------+--------+-----------------+
| TABLE_NAME | ENGINE | TABLE_COLLATION |
+------------+--------+-----------------+
| products   | InnoDB | utf8_general_ci |
+------------+--------+-----------------+
</code></pre><p>This result is what we expected, the name, the engine and the collation. The problem comes when the table have uppercase letter, as the result always seems to be in lowercase.</p>
<p>I can&#39;t tell if this is a configuration issue or a mysql bug.</p>
<p>Making a the condition for table name twice solves the issue.</p>
<pre><code class="language-sql"><span class="token keyword">SELECT</span> TABLE_NAME<span class="token punctuation">,</span> <span class="token keyword">ENGINE</span><span class="token punctuation">,</span> TABLE_COLLATION
<span class="token keyword">FROM</span> INFORMATION_SCHEMA<span class="token punctuation">.</span><span class="token keyword">TABLES</span>
<span class="token keyword">WHERE</span>
  TABLE_SCHEMA <span class="token operator">=</span> <span class="token string">"mydatabase"</span>
  <span class="token operator">AND</span> <span class="token punctuation">(</span>
    TABLE_NAME <span class="token operator">=</span> <span class="token string">"Products"</span>
    <span class="token operator">OR</span> TABLE_NAME <span class="token operator">=</span> <span class="token string">"Products"</span>
  <span class="token punctuation">)</span>
</code></pre><p>The query above will result with the table name in the original case it was created.</p>
<pre><code class="language-text">+------------+--------+-----------------+
| TABLE_NAME | ENGINE | TABLE_COLLATION |
+------------+--------+-----------------+
| Products   | InnoDB | utf8_general_ci |
+------------+--------+-----------------+
</code></pre>]]></content:encoded><pubDate>Wed, 14 Feb 2018 20:22:34 +0000</pubDate><link>https://wellingguzman.com/notes/mysql-fetch-table-name-with-original-case</link></item><item><title>MySQL string columns key length</title><description><![CDATA[After switching the default charset from utf8 to utf8mb4 to support emojis on Directus, we started to receive errors that the key was too long. One my wondering how changing the charset affect the key length. Below can be see examples of the errors:
#1071 - Specified key was too long; max key length is 767 bytes
#1071 - Specified key was too long; max key length is 1000 bytes
#1071 - Specified key was too long; max key length is 3072 bytes
It can be any of previous errors depending on what is the storage engine of the table. MySIAM, InnoDb or InnoDb with innodb_large_prefix enabled have differents key length limitation.
TL;DR
The difference between utf8 and utf8mb4 charset is the bytes requires to store each characters. utf8 requires 3 bytes, while utf8mb4 requires 4 bytes. This means using utf8mb4 charset in a table with innodb engine with innodb_large_prefix disabled, at most 191 characters in a string column must be used.
191 characters × 4 bytes = 764 bytes which is less than the maximum length of 767 bytes allowed when innodb_large_prefix is disabled. Since MySQL 5.7 innodb_large_prefix is enabled by default allowing up to 3072 bytes.
String Storage
String storage size vary depends on whether the column is fixed-length or variable-length. It also depends on the charset, it takes more bytes to storage a japanese character than an ASCII/Latin letter.
As an example, CHAR is a fixed-length while VARCHAR and TEXT are variable-length.
All fixed-length data types uses all the bytes they were declared. For example CHAR(16), no matter what its value is, it&#39;s right padded with spaces to fill up to the specific length. On the other hand VARCHAR only uses 1 byte + the content size.
VARCHAR requires a prefix value of 1 byte to store the length of the string if the size is less than 256, otherwise it will uses 2 bytes.
One tip is not to use CHAR if you are not going to use all the characters almost all the time, because the size can pile up with empty strings column.
Character Set
The UTF8 character set uses a maximum of 3 bytes per character and only contains Basic Multilingual Plane (BMP) characters, which is the home of 65,536 characters (16 bits) from U+0000 to U+FFFF.
The UTF8mb4 character set uses a maximum of 4 bytes per character including all of BMP characters and Supplementary Multilingual Plane (SMP) which include another possibility of 65,536 new characters from U+10000 to U+1FFFF.
Emojis (Unicode characters)
UTF8 can support emojis, but not all of them. All of the new emojis are part of the SMP, so in order to support both basic and supplementary multilingual plane UTF8mb4 must be used.
The sparkle emoji (✨ U+2728) value is between U+0000 and U+FFFF then it can be used on utf8 charset, but the Woman Health Worker (👩 U+1F469) value which is not between U+0000 and U+FFFF, must use the utf8mb4 charset that range between U+10000 and U+1FFFF.
Index length
Now after using utf8mb4 all the characters use 4 bytes instead of 3, so all columns that has more than 191 characters now uses more than 767 bytes, because 192 x 4 bytes is 768 bytes.
Keep in mind the 768 bytes limit is only when using innodb engine and innodb_large_prefix is disabled. Since MySQL 5.7 innodb_large_prefix is enabled by default allowing up to 3072 bytes. MySIAM has a maximum length of 1000 bytes.



Engine
Limit



InnodB with innodb_large_prefix disabled
768 bytes


MySAIM
1000 bytes


InnodB with innodb_large_prefix enabled
3072 bytes


Solutions
The solve this will depend on what we actually need it can be either removing the index, keep using utf8, add a length to the index key or reduce the length of the column.
Reduce length
For us removing the index wasn&#39;t a good option, neither keep using the utf8. Reducing the length was possible because the columns will probably never met the actual length which is 255 characters, reducing it to 191 was optimal and in no way impact the table.
Index length
If changing the length was not possible or desired option, changing the column index to only a chunk of n characters, is another possible option.
CREATE INDEX `index_name` ON `posts` (title(191));]]></description><content:encoded><![CDATA[<p>After switching the default charset from <code>utf8</code> to <code>utf8mb4</code> to support emojis on <a href="https://getdirectus.com">Directus</a>, we started to receive errors that the key was too long. One my wondering how changing the charset affect the key length. Below can be see examples of the errors:</p>
<pre><code><span class="token phrase">#1071 - Specified key was too long; max key length is 767 bytes
#1071 - Specified key was too long; max key length is 1000 bytes
#1071 - Specified key was too long; max key length is 3072 bytes</span>
</code></pre><p>It can be any of previous errors depending on what is the storage engine of the table. MySIAM, InnoDb or InnoDb with <code>innodb_large_prefix</code> enabled have differents key length limitation.</p>
<h2>TL;DR</h2>
<p>The difference between <code>utf8</code> and <code>utf8mb4</code> charset is the bytes requires to store each characters. <code>utf8</code> requires 3 bytes, while <code>utf8mb4</code> requires 4 bytes. This means using <code>utf8mb4</code> charset in a table with innodb engine with <code>innodb_large_prefix</code> disabled, at most 191 characters in a string column must be used.</p>
<p>191 characters × 4 bytes = 764 bytes which is less than the maximum length of 767 bytes allowed when <code>innodb_large_prefix</code> is disabled. Since MySQL 5.7 <code>innodb_large_prefix</code> is enabled by default allowing up to 3072 bytes.</p>
<h2>String Storage</h2>
<p>String storage size vary depends on whether the column is fixed-length or variable-length. It also depends on the charset, it takes more bytes to storage a japanese character than an ASCII/Latin letter.</p>
<p>As an example, <code>CHAR</code> is a fixed-length while <code>VARCHAR</code> and <code>TEXT</code> are variable-length.</p>
<p>All fixed-length data types uses all the bytes they were declared. For example <code>CHAR(16)</code>, no matter what its value is, it&#39;s right padded with spaces to fill up to the specific length. On the other hand <code>VARCHAR</code> only uses 1 byte + the content size.</p>
<p><code>VARCHAR</code> requires a prefix value of 1 byte to store the length of the string if the size is less than 256, otherwise it will uses 2 bytes.</p>
<p>One tip is not to use CHAR if you are not going to use all the characters almost all the time, because the size can pile up with empty strings column.</p>
<h2>Character Set</h2>
<p>The <code>UTF8</code> character set uses a maximum of 3 bytes per character and only contains Basic Multilingual Plane (BMP) characters, which is the home of 65,536 characters (16 bits) from <code>U+0000</code> to <code>U+FFFF</code>.</p>
<p>The <code>UTF8mb4</code> character set uses a maximum of 4 bytes per character including all of BMP characters and Supplementary Multilingual Plane (SMP) which include another possibility of 65,536 new characters from <code>U+10000</code> to <code>U+1FFFF</code>.</p>
<h2>Emojis (Unicode characters)</h2>
<p><code>UTF8</code> can support emojis, but not all of them. All of the new emojis are part of the SMP, so in order to support both basic and supplementary multilingual plane <code>UTF8mb4</code> must be used.</p>
<p>The sparkle emoji (✨ <code>U+2728</code>) value is between <code>U+0000</code> and <code>U+FFFF</code> then it can be used on <code>utf8</code> charset, but the Woman Health Worker (👩 <code>U+1F469</code>) value which is not between <code>U+0000</code> and <code>U+FFFF</code>, must use the <code>utf8mb4</code> charset that range between <code>U+10000</code> and <code>U+1FFFF</code>.</p>
<h2>Index length</h2>
<p>Now after using <code>utf8mb4</code> all the characters use 4 bytes instead of 3, so all columns that has more than 191 characters now uses more than 767 bytes, because 192 x 4 bytes is 768 bytes.</p>
<p>Keep in mind the 768 bytes limit is only when using innodb engine and <code>innodb_large_prefix</code> is disabled. Since MySQL 5.7 <code>innodb_large_prefix</code> is enabled by default allowing up to 3072 bytes. MySIAM has a maximum length of 1000 bytes.</p>
<table>
<thead>
<tr>
<th>Engine</th>
<th>Limit</th>
</tr>
</thead>
<tbody><tr>
<td>InnodB with <code>innodb_large_prefix</code> disabled</td>
<td>768 bytes</td>
</tr>
<tr>
<td>MySAIM</td>
<td>1000 bytes</td>
</tr>
<tr>
<td>InnodB with <code>innodb_large_prefix</code> enabled</td>
<td>3072 bytes</td>
</tr>
</tbody></table>
<h2>Solutions</h2>
<p>The solve this will depend on what we actually need it can be either removing the index, keep using <code>utf8</code>, add a length to the index key or reduce the length of the column.</p>
<h3>Reduce length</h3>
<p>For us removing the index wasn&#39;t a good option, neither keep using the <code>utf8</code>. Reducing the length was possible because the columns will probably never met the actual length which is 255 characters, reducing it to 191 was optimal and in no way impact the table.</p>
<h3>Index length</h3>
<p>If changing the length was not possible or desired option, changing the column index to only a chunk of n characters, is another possible option.</p>
<pre><code class="language-sql"><span class="token keyword">CREATE</span> <span class="token keyword">INDEX</span> <span class="token identifier"><span class="token punctuation">`</span>index_name<span class="token punctuation">`</span></span> <span class="token keyword">ON</span> <span class="token identifier"><span class="token punctuation">`</span>posts<span class="token punctuation">`</span></span> <span class="token punctuation">(</span>title<span class="token punctuation">(</span><span class="token number">191</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>]]></content:encoded><pubDate>Sun, 28 May 2017 20:06:14 +0000</pubDate><link>https://wellingguzman.com/notes/mysql-key-limit</link></item><item><title>Ubuntu missing package sources</title><description><![CDATA[While trying to install Directus in an ubuntu server I got an error from a composer dependency that php5-mcrypt is not installed.
php5-mcrypt : Depends: libmcrypt4 but it is not installable
Running apt-get install php5-mcrypt didn&#39;t work.
Running apt-get update first didn&#39;t work either.
After some time figuring out what was the problem, I end up noticing that the source.list file was missing.
The next question would be where do I find the official ubuntu repositories? Luckily I found this generator where you can select a ubuntu release and all the repositories you need and it generates a source.list file.
Copying and pasting the new generate content to source.list file, and then running apt-get update, will updates the ubuntu package repositories.
Now apt-get install php5-mcrypt works.]]></description><content:encoded><![CDATA[<p>While trying to install <a href="http://getdirectus.com">Directus</a> in an ubuntu server I got an error from a composer dependency that <code>php5-mcrypt</code> is not installed.</p>
<pre><code><span class="token phrase">php5-mcrypt : Depends: libmcrypt4 but it is not installable</span>
</code></pre><p>Running <code>apt-get install php5-mcrypt</code> didn&#39;t work.</p>
<p>Running <code>apt-get update</code> first didn&#39;t work either.</p>
<p>After some time figuring out what was the problem, I end up noticing that the <code>source.list</code> file was missing.</p>
<p>The next question would be where do I find the official ubuntu repositories? Luckily I found <a href="https://repogen.simplylinux.ch/">this generator</a> where you can select a ubuntu release and all the repositories you need and it generates a <code>source.list</code> file.</p>
<p>Copying and pasting the new generate content to <code>source.list</code> file, and then running <code>apt-get update</code>, will updates the ubuntu package repositories.</p>
<p>Now <code>apt-get install php5-mcrypt</code> works.</p>
]]></content:encoded><pubDate>Tue, 20 Sep 2016 04:22:34 +0000</pubDate><link>https://wellingguzman.com/notes/ubuntu-missing-package-sources</link></item><item><title>Zend DB select from a different database</title><description><![CDATA[Using Zend DB to select data from or use a table that doesn&#39;t belong to the adapter selected database, can be done by using TableIdentifier instead of a string as it shows below:
&lt;?php
use Zend\Db\Sql\Sql;
use Zend\Db\Sql\TableIdentifier;

$sql = new Sql($adapter);
$select = $sql->select();
$select->from(new TableIdentifier('table', 'database'));
$select->where(array('id' => 1));

$statement = $sql->prepareStatementForSqlObject($select);
$results = $statement->execute();]]></description><content:encoded><![CDATA[<p>Using Zend DB to select data from or use a table that doesn&#39;t belong to the adapter selected database, can be done by using <code>TableIdentifier</code> instead of a string as it shows below:</p>
<pre><code class="language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">use</span> <span class="token package">Zend<span class="token punctuation">\</span>Db<span class="token punctuation">\</span>Sql<span class="token punctuation">\</span>Sql</span><span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token package">Zend<span class="token punctuation">\</span>Db<span class="token punctuation">\</span>Sql<span class="token punctuation">\</span>TableIdentifier</span><span class="token punctuation">;</span>

<span class="token variable">$sql</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Sql</span><span class="token punctuation">(</span><span class="token variable">$adapter</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span> <span class="token operator">=</span> <span class="token variable">$sql</span><span class="token operator">-></span><span class="token function">select</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span><span class="token operator">-></span><span class="token function">from</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">TableIdentifier</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'database'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$select</span><span class="token operator">-></span><span class="token function">where</span><span class="token punctuation">(</span><span class="token keyword">array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token variable">$statement</span> <span class="token operator">=</span> <span class="token variable">$sql</span><span class="token operator">-></span><span class="token function">prepareStatementForSqlObject</span><span class="token punctuation">(</span><span class="token variable">$select</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token variable">$results</span> <span class="token operator">=</span> <span class="token variable">$statement</span><span class="token operator">-></span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
</code></pre>]]></content:encoded><pubDate>Mon, 09 May 2016 00:56:13 +0000</pubDate><link>https://wellingguzman.com/notes/zend-db-select-from-different-database</link></item><item><title>Run node app in background (Linux)</title><description><![CDATA[There are different tools to run a node.js script in the background. In my experience I have used nohup, Forever, and PM2.
nohup
Running a script in the background in linux can be done using nohup, using nohup we can run node application in the background.
$ nohup node /nodeapp/index.js &amp;
Kill Process
You can stop the process using the kill command as well:
First you need to know which process ID to kill, list all the process running node by running:
ps axl | grep node
The second column of your result is probably the PID, take that number and run the command below:
kill -9 [PID]
Forever
Forever is another solution for Node.js scripts.
Installation
$ npm install forever -g
Usage
$ forever start /nodeapp/index.js
$ forever restart /nodeapp/index.js
$ forever stop /nodeapp/index.js
$ forever list
PM2
Another tool I found is PM2, it has a lot of extras features that I have not used, except process management.
Installation
$ npm install pm2 -g
Usage
$ pm2 start /nodeapp/index.js
$ pm2 restart /nodeapp/index.js
$ pm2 reload /nodeapp/index.js
$ pm2 stop /nodeapp/index.js
$ pm2 delete /nodeapp/index.js
$ pm2 list
References

Forever
nohup(1)
PM2]]></description><content:encoded><![CDATA[<p>There are different tools to run a node.js script in the background. In my experience I have used <code>nohup</code>, <code>Forever</code>, and <code>PM2</code>.</p>
<h2>nohup</h2>
<p>Running a script in the background in linux can be done using <code>nohup</code>, using nohup we can run node application in the background.</p>
<pre><code class="language-shell">$ <span class="token function">nohup</span> <span class="token function">node</span> /nodeapp/index.js <span class="token operator">&amp;</span>
</code></pre><h3>Kill Process</h3>
<p>You can stop the process using the <code>kill</code> command as well:</p>
<p>First you need to know which process ID to kill, list all the process running node by running:</p>
<pre><code class="language-shell"><span class="token function">ps</span> axl <span class="token operator">|</span> <span class="token function">grep</span> <span class="token function">node</span>
</code></pre><p>The second column of your result is probably the PID, take that number and run the command below:</p>
<pre><code class="language-shell"><span class="token function">kill</span> <span class="token parameter variable">-9</span> <span class="token punctuation">[</span>PID<span class="token punctuation">]</span>
</code></pre><h2>Forever</h2>
<p><a href="https://github.com/foreverjs/forever">Forever</a> is another solution for Node.js scripts.</p>
<h3>Installation</h3>
<pre><code class="language-shell">$ <span class="token function">npm</span> <span class="token function">install</span> forever <span class="token parameter variable">-g</span>
</code></pre><h3>Usage</h3>
<pre><code class="language-shell">$ forever start /nodeapp/index.js
$ forever restart /nodeapp/index.js
$ forever stop /nodeapp/index.js
$ forever list
</code></pre><h2>PM2</h2>
<p>Another tool I found is <a href="https://github.com/Unitech/pm2">PM2</a>, it has a lot of extras features that I have not used, except process management.</p>
<h3>Installation</h3>
<pre><code><span class="token phrase">$ npm install pm2 -g</span>
</code></pre><h3>Usage</h3>
<pre><code class="language-shell">$ pm2 start /nodeapp/index.js
$ pm2 restart /nodeapp/index.js
$ pm2 reload /nodeapp/index.js
$ pm2 stop /nodeapp/index.js
$ pm2 delete /nodeapp/index.js
$ pm2 list
</code></pre><h2>References</h2>
<ul>
<li><a href="https://github.com/foreverjs/forever">Forever</a></li>
<li><a href="http://man7.org/linux/man-pages/man1/nohup.1.html">nohup(1)</a></li>
<li><a href="https://github.com/Unitech/pm2">PM2</a></li>
</ul>
]]></content:encoded><pubDate>Thu, 28 Apr 2016 07:01:59 +0000</pubDate><link>https://wellingguzman.com/notes/run-node-app-in-background-linux</link></item><item><title>PHP - call_user_func reference</title><description><![CDATA[PHP function call_user_func() does not pass parameter variable as reference. the code below won&#39;t work as expected.
From PHP documentation:

Note that the parameters for call_user_func() are not passed by reference.

&lt;?php

function increment(&amp;$var)
{
    $var++;
}

$a = 0;
call_user_func('increment', $a);
echo $a."\n"; // $a is equals to 0
In order to solve this problem call_user_func_array() must be used instead.
&lt;?php
function increment(&amp;$var)
{
    $var++;
}

$a = 0;

call_user_func_array('increment', array(&amp;$a));
echo $a."\n"; // $a is equals to 1]]></description><content:encoded><![CDATA[<p>PHP function <code>call_user_func()</code> does not pass parameter variable as reference. the code below won&#39;t work as expected.</p>
<p>From PHP documentation:</p>
<blockquote>
<p>Note that the parameters for <code>call_user_func()</code> are not passed by reference.</p>
</blockquote>
<pre><code class="language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>

<span class="token keyword">function</span> <span class="token function-definition function">increment</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token variable">$var</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token variable">$var</span><span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token variable">$a</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token function">call_user_func</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'increment'</span><span class="token punctuation">,</span> <span class="token variable">$a</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$a</span><span class="token operator">.</span><span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span> <span class="token comment">// $a is equals to 0</span></span>
</code></pre><p>In order to solve this problem <code>call_user_func_array()</code> must be used instead.</p>
<pre><code class="language-php"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span>
<span class="token keyword">function</span> <span class="token function-definition function">increment</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token variable">$var</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
    <span class="token variable">$var</span><span class="token operator">++</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token variable">$a</span> <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

<span class="token function">call_user_func_array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'increment'</span><span class="token punctuation">,</span> <span class="token keyword">array</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token variable">$a</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">echo</span> <span class="token variable">$a</span><span class="token operator">.</span><span class="token string double-quoted-string">"\n"</span><span class="token punctuation">;</span> <span class="token comment">// $a is equals to 1</span></span>
</code></pre>]]></content:encoded><pubDate>Fri, 01 Apr 2016 10:49:12 +0000</pubDate><link>https://wellingguzman.com/notes/php-call-user-func-reference</link></item><item><title>Disable nginx basic_auth for one location</title><description><![CDATA[If you protected your website with nginx basic_auth, and want to disable it for just one (or maybe some specific locations), you can use basic_auth off for that location and the authorization won&#39;t be required.
Example:
server {
  auth_basic "Restricted content";
  auth_basic_user_file /etc/nginx/.htpasswd;

  location /public/ {
    auth_basic off;
  }
}]]></description><content:encoded><![CDATA[<p>If you protected your website with nginx <code>basic_auth</code>, and want to disable it for just one (<em>or maybe some specific locations</em>), you can use <code>basic_auth off</code> for that location and the authorization won&#39;t be required.</p>
<p>Example:</p>
<pre><code class="language-bash">server <span class="token punctuation">{</span>
  auth_basic <span class="token string">"Restricted content"</span><span class="token punctuation">;</span>
  auth_basic_user_file /etc/nginx/.htpasswd<span class="token punctuation">;</span>

  location /public/ <span class="token punctuation">{</span>
    auth_basic off<span class="token punctuation">;</span>
  <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>]]></content:encoded><pubDate>Thu, 18 Feb 2016 02:19:21 +0000</pubDate><link>https://wellingguzman.com/notes/disable-nginx-basic-auth-for-one-location</link></item><item><title>sudo and redirect output</title><description><![CDATA[Concatenating two files in unix-like operative system can be done with a single line like these:
$ cat file1.txt file2.txt > newfile.txt
If permission is needed to create and write into new files in the specified path you must use sudo, so you do this:
$ sudo cat file1.txt file2.txt > newfile.txt
But that doesn&#39;t work because the output is handle by the shell and not sudo, and by that it means it won&#39;t let you create the new file returning something like this: -bash: newfile.txt: Permission denied. Note: This only happen when the current user doesn&#39;t have permission to create/write on the new file.
There&#39;s several solutions to this but a one-liner solution is to run a shell command inline:
sudo sh -c 'sudo cat file1.txt file2.txt > newfile.txt']]></description><content:encoded><![CDATA[<p>Concatenating two files in unix-like operative system can be done with a single line like these:</p>
<pre><code class="language-bash">$ <span class="token function">cat</span> file1.txt file2.txt <span class="token operator">></span> newfile.txt
</code></pre><p>If permission is needed to create and write into new files in the specified path you must use <code>sudo</code>, so you do this:</p>
<pre><code class="language-bash">$ <span class="token function">sudo</span> <span class="token function">cat</span> file1.txt file2.txt <span class="token operator">></span> newfile.txt
</code></pre><p>But that doesn&#39;t work because the output is handle by the shell and not sudo, and by that it means it won&#39;t let you create the new file returning something like this: <code>-bash: newfile.txt: Permission denied</code>. <strong>Note:</strong> This only happen when the current user doesn&#39;t have permission to create/write on the new file.</p>
<p>There&#39;s several solutions to this but a one-liner solution is to run a shell command inline:</p>
<pre><code class="language-bash"><span class="token function">sudo</span> <span class="token function">sh</span> <span class="token parameter variable">-c</span> <span class="token string">'sudo cat file1.txt file2.txt > newfile.txt'</span>
</code></pre>]]></content:encoded><pubDate>Tue, 16 Feb 2016 21:20:21 +0000</pubDate><link>https://wellingguzman.com/notes/sudo-and-redirect-output</link></item><item><title>&quot;JavaScript&quot; is as related to &quot;Java&quot; as ...</title><description><![CDATA[Some people mix JavaScript with Java, thinking they are the same thing or one are based on the other.
There is a comparison that start with &quot;Javascript is related to java as something is to some&quot; is a clever and funny comparison to me.
So this would be a list of this comparison phrases:

JavaScript is related to Java as Hamburger is to Ham.
JavaScript is related to Java as Hamster is to Ham.
JavaScript is related to Java as Rocket is to Rock.
JavaScript is related to Java as Carnival is to Car. From YDKJS Books
More to come...]]></description><content:encoded><![CDATA[<p>Some people mix <em>JavaScript</em> with <em>Java</em>, thinking they are the same thing or one are based on the other.</p>
<p>There is a comparison that start with &quot;Javascript is related to java as <em>something</em> is to <em>some</em>&quot; is a clever and funny comparison to me.</p>
<p>So this would be a list of this comparison phrases:</p>
<ul>
<li>JavaScript is related to Java as Hamburger is to Ham.</li>
<li>JavaScript is related to Java as Hamster is to Ham.</li>
<li>JavaScript is related to Java as Rocket is to Rock.</li>
<li>JavaScript is related to Java as Carnival is to Car. <em>From YDKJS Books</em></li>
<li>More to come...</li>
</ul>
]]></content:encoded><pubDate>Mon, 25 Jan 2016 23:58:02 +0000</pubDate><link>https://wellingguzman.com/notes/javascript-is-as-related-to-java-as</link></item><item><title>Writing on the moment</title><description><![CDATA[This is probably has been said a lot out there, but I wanted to tell you this again, if you like to express an idea, a experience or simply want to share your thoughts, write it down while you got the momentum going.
If you love something and want to say how much you love it, say it while you love it, and when you stop loving it, you can share it again and compare saying why you stop loving it.
If you want to share something today, and if you are like me, there&#39;s a high chance that tomorrow you won&#39;t feel that spark that make you feel you need it to share something.
I want to share so much, but then I end up thinking, who wants to read this anyway, who will find this anyway. Don&#39;t be that person.
Don&#39;t be me and share your thoughts, it doesn&#39;t matter that you think it&#39;s worthless, someone will find it worth it.
I&#39;m saying this not because I do it, because I don&#39;t, but because I&#39;ve been on the other side as a reader, reading comments about how they hate the post AND the author, while I&#39;m sitting here thinking why do they hate them, they are awesome and this content is great. The author probably is thinking is all true, because people who liked the content are not expressing their love to them and their content, but rather they just stare at how other people hate (or don&#39;t like) them.
Please, share your thoughts and let people know if you liked their content or not, but do not offend them, they can talk, if not, move along.
I&#39;ll take my own advice and do the same. Let&#39;s]]></description><content:encoded><![CDATA[<p>This is probably has been said a lot out there, but I wanted to tell you this again, if you like to express an idea, a experience or simply want to share your thoughts, write it down while you got the momentum going.</p>
<p>If you love something and want to say how much you love it, say it while you love it, and when you stop loving it, you can share it again and compare saying why you stop loving it.</p>
<p>If you want to share something today, and if you are like me, there&#39;s a high chance that tomorrow you won&#39;t feel that spark that make you feel you need it to share something.</p>
<p>I want to share so much, but then I end up thinking, who wants to read this anyway, who will find this anyway. Don&#39;t be that person.</p>
<p>Don&#39;t be me and share your thoughts, it doesn&#39;t matter that you think it&#39;s worthless, someone will find it worth it.</p>
<p>I&#39;m saying this not because I do it, because I don&#39;t, but because I&#39;ve been on the other side as a reader, reading comments about how they hate the post AND the author, while I&#39;m sitting here thinking why do they hate them, they are awesome and this content is great. The author probably is thinking is all true, because people who liked the content are not expressing their love to them and their content, but rather they just stare at how other people hate (or <em>don&#39;t like</em>) them.</p>
<p>Please, share your thoughts and let people know if you liked their content or not, but do not offend them, they can talk, if not, move along.</p>
<p>I&#39;ll take my own advice and do the same. Let&#39;s</p>
]]></content:encoded><pubDate>Sun, 29 Nov 2015 00:51:55 +0000</pubDate><link>https://wellingguzman.com/notes/writing-on-the-moment</link></item><item><title>Twenty years of PHP</title><description><![CDATA[Yesterday 9 days ago it was PHP 20th birthday. Twenty years ago Rasmus Lerdorf released PHP 1.0, but it wasn’t until 8 years later when I had my first encounter with this language.
In 2003, I found out that anyone were able to make websites, that it wasn’t something just for big company to show their product and information. So started digging and searching on Altavista and Yahoo (Remember those search engine?), “where” and “how” do I start making websites.
I found Geocities, it was a great service from Yahoo. but as far as I remember it was only HTML, CSS, images and glitter, then I found Lycos, here I could upload PHP code and MySQL databases, it was all about CuteFTP and PHPMyAdmin. I uploaded CMS, forums, blogs (I think it were called news back then, don’t recall), and thousands of PHP scripts for any purpose, such as PHPNuke and PHPBB.
I didn’t know programming at all, but I loved how easy it was, you didn’t need to know OOP, just open a text editor and write some basic c-style code instruction and you are all set, no framework, no nothing.
I really learned how to program in PHP (or build websites) by analyzing for hours a pagination script, and since then I’ve been using and learning more and more PHP.
I’ve built so many sites, for different type of clients, I buy my first PC by selling my first website, a well-configured and customized PHP-Nuke script.
Until last year I thought PHP was death, but I realized that I was around different people, because I was surprised how many people still using php, so it makes go back to PHP.
I don’t mind the people who hate PHP and PHP Developers, the most common thing question I get after I say I’m a PHP developer is: “why?”.

Thanks to PHP I got into Web Development, and I love it.
I would like to thanks: Rasmus Lerdorf, PHP core developers and the whole PHP community and web folks.]]></description><content:encoded><![CDATA[<p><del>Yesterday</del> 9 days ago it was PHP 20th birthday. Twenty years ago <strong>Rasmus Lerdorf</strong> released PHP 1.0, but it wasn’t until 8 years later when I had my first encounter with this language.</p>
<p>In 2003, I found out that anyone were able to make websites, that it wasn’t something just for big company to show their product and information. So started digging and searching on Altavista and Yahoo (Remember those search engine?), “where” and “how” do I start making websites.</p>
<p>I found Geocities, it was a great service from Yahoo. but as far as I remember it was only HTML, CSS, images and glitter, then I found Lycos, here I could upload PHP code and MySQL databases, it was all about CuteFTP and PHPMyAdmin. I uploaded CMS, forums, blogs (I think it were called news back then, don’t recall), and thousands of PHP scripts for any purpose, such as PHPNuke and PHPBB.</p>
<p>I didn’t know programming at all, but I loved how easy it was, you didn’t need to know OOP, just open a text editor and write some basic c-style code instruction and you are all set, no framework, no nothing.</p>
<p>I really learned how to program in PHP (or build websites) by analyzing for hours a pagination script, and since then I’ve been using and learning more and more PHP.</p>
<p>I’ve built so many sites, for different type of clients, I buy my first PC by selling my first website, a well-configured and customized PHP-Nuke script.</p>
<p>Until last year I thought PHP was death, but I realized that I was around different people, because I was surprised how many people still using php, so it makes go back to PHP.</p>
<p>I don’t mind the people who hate PHP and PHP Developers, the most common thing question I get after I say I’m a PHP developer is: “why?”.</p>
<p><img src="/images/breaking_the_ice_with_php_s.jpg" alt="PHP Comic"></p>
<p>Thanks to PHP I got into Web Development, and I love it.</p>
<p>I would like to thanks: Rasmus Lerdorf, PHP core developers and the whole PHP community and web folks.</p>
]]></content:encoded><pubDate>Wed, 17 Jun 2015 10:39:55 +0000</pubDate><link>https://wellingguzman.com/notes/twenty-years-of-php</link></item></channel></rss>