<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Code with Hugo</title><link>https://codewithhugo.com/</link><description>Recent content on Code with Hugo</description><generator>Hugo -- gohugo.io</generator><language>en-US</language><lastBuildDate>Mon, 18 Aug 2025 05:42:16 +0000</lastBuildDate><atom:link href="https://codewithhugo.com/index.xml" rel="self" type="application/rss+xml"/><item><title>Clear Module Cache/Force Import in Node.js with CommonJS and with ES Modules (ESM)</title><link>https://codewithhugo.com/nodejs-esm-cjs-clear-module-cache/</link><pubDate>Mon, 18 Aug 2025 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/nodejs-esm-cjs-clear-module-cache/</guid><description>&lt;p>Node.js supports two primary module systems: CommonJS and ES Modules (ESM). Both systems cache imported modules, ensuring that a module&amp;rsquo;s code runs only once, even if it&amp;rsquo;s imported multiple times.&lt;/p>
&lt;p>When writing tests it can be useful to bypass caching for example if the code being tested is executed at the module scope (outside of exported functions).&lt;/p>
&lt;p>While most test frameworks handle this for you eg. with &lt;code>jest.mock&lt;/code>/&lt;code>vi.mock&lt;/code>, when using a more barebones testing framework like &lt;code>node:test&lt;/code> (built-in Node.js test runner) or in rare exceptions in application code.&lt;/p></description></item><item><title>Taming Timezones: Replacing moment.parseZone with @date-fns/tz and Vanilla JS Date</title><link>https://codewithhugo.com/taming-timezones-moment-parsezone-to-date-fns-tz/</link><pubDate>Mon, 11 Aug 2025 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/taming-timezones-moment-parsezone-to-date-fns-tz/</guid><description>&lt;h1 id="taming-timezones-replacing-momentparsezone-with-date-fnstz-and-vanilla-js-date">Taming Timezones: Replacing moment.parseZone with @date-fns/tz and Vanilla JS Date&lt;/h1>
&lt;!-- Taming Timezones without moment.js -->
&lt;p>How we preserved timezone-aware parsing when migrating from moment.js to date-fns and JS Date.&lt;/p>
&lt;p>One of the last pieces of work I completed at my last workplace was to migrate a core system from &lt;code>moment&lt;/code> to &lt;code>date-fns&lt;/code>, vanilla Date manipulation &amp;amp; formatting.&lt;/p></description></item><item><title>TypeScript in the Trenches: Utility types and type manipulation patterns for application development</title><link>https://codewithhugo.com/typescript-types-in-the-trenches/</link><pubDate>Mon, 28 Jul 2025 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/typescript-types-in-the-trenches/</guid><description>&lt;p>7 advanced types and patterns that I&amp;rsquo;ve used as part of TypeScript application development.&lt;/p>
&lt;p>These patterns help unlock new levels of type safety and maintainability in TypeScript.&lt;/p></description></item><item><title>Configure Timezone for Jest/Node.js with the `TZ` env var</title><link>https://codewithhugo.com/jest-set-timezone-tz-env-var/</link><pubDate>Sat, 17 Aug 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-set-timezone-tz-env-var/</guid><description>&lt;p>In order to test code that involves date and time manipulation and parsing. It can be useful to set the timezone under which the Jest tests run.&lt;/p>
&lt;p>Thankfully Node.js supports the &lt;code>TZ&lt;/code> environment variable: &amp;ldquo;The TZ environment variable is used to specify the timezone configuration.&amp;rdquo; (&lt;a href="https://nodejs.org/docs/latest-v22.x/api/cli.html#tz">Node.js docs&lt;/a>).&lt;/p></description></item><item><title>Nock on Node 18/20/22 Fails to Intercept isomorphic-unfetch/fetch Request</title><link>https://codewithhugo.com/nock-node-18-20-22-fails-to-intercept-fetch/</link><pubDate>Sun, 11 Aug 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/nock-node-18-20-22-fails-to-intercept-fetch/</guid><description>&lt;p>On Node versions 18, 20 and 22, users may encounter an issue where Nock fails to intercept requests made via isomorphic-unfetch.&lt;/p>
&lt;p>What follows is a sample error you may see (timeout is reached because the request is not intercepted).&lt;/p>
&lt;pre tabindex="0">&lt;code>returns a valid response
thrown: &amp;#34;Exceeded timeout of 5000 ms for a test.
Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout.&amp;#34;
1 |
&amp;gt; 2 | it(&amp;#39;returns a valid response&amp;#39;, async () =&amp;gt; {
&lt;/code>&lt;/pre></description></item><item><title>`dotenv` not required: load `.env` and parse env vars natively in modern Node.js</title><link>https://codewithhugo.com/load-env-vars-files-nodejs-dotenv/</link><pubDate>Sun, 28 Jul 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/load-env-vars-files-nodejs-dotenv/</guid><description>&lt;p>&lt;a href="https://www.npmjs.com/package/dotenv">&lt;code>dotenv&lt;/code>&lt;/a> is a &lt;a href="https://www.npmjs.com/package/dotenv">39 million download a week&lt;/a> package allows Node.js apps to load environment variables from &lt;code>.env&lt;/code> files and other utilities around environment variables. It&amp;rsquo;s very useful for building &lt;a href="https://12factor.net/config">12-factor apps&lt;/a> for which a principle is to read configuration from environment variables instead of config or code files.&lt;/p>
&lt;p>As of Node 20.6 (and all Node 22.x versions), env files can be loaded via the &lt;code>--env-file&lt;/code> CLI argument.&lt;/p>
&lt;p>As of Node.js 20.12 (and all Node 22.x versions), env files can be loaded programmatically via &lt;code>process.loadEnvFile()&lt;/code> and env vars in a string can be parsed with &lt;code>util.parseEnv()&lt;/code>.&lt;/p>
&lt;p>Note that the Node 20 release line is the Active LTS until October 2024, Node 22 is &amp;ldquo;current&amp;rdquo; until October 2024 when it becomes the Active LTS.&lt;/p>
&lt;p>We&amp;rsquo;ll now detail how to use the native APIs to replace dotenv.&lt;/p>
&lt;p>You can skip to the code on GitHub at: &lt;a href="https://github.com/HugoDF/dotenv-not-required/">github.com/HugoDF/dotenv-not-required/&lt;/a>, see the &lt;code>package.json&lt;/code> and &lt;code>index.test.js&lt;/code>.&lt;/p></description></item><item><title>Mocking/stubbing the current Date in Jest tests</title><link>https://codewithhugo.com/mocking-the-current-date-in-jest-tests/</link><pubDate>Thu, 06 Jun 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/mocking-the-current-date-in-jest-tests/</guid><description>&lt;p>There are situations where &lt;code>new Date()&lt;/code> or &lt;code>Date.now&lt;/code> is used in application code. That code needs to be tested, and it’s always a struggle to remember how to mock/stub or spy on &lt;code>Date.now&lt;/code>/&lt;code>new Date&lt;/code> with Jest.&lt;/p>
&lt;p>This post goes through multiple approaches to mocking, stubbing and spying on the date constructor using Jest.&lt;/p>
&lt;p>There&amp;rsquo;s a full examples repository at &lt;a href="https://github.com/HugoDF/jest-handbook-examples/tree/master/src">github.com/HugoDF/jest-handbook-examples&lt;/a>.&lt;/p></description></item><item><title>Docsify Mermaid 10</title><link>https://codewithhugo.com/docsify-mermaid-10/</link><pubDate>Mon, 04 Mar 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/docsify-mermaid-10/</guid><description>&lt;p>Using &lt;a href="https://docsify.js.org">Docsify&lt;/a> and &lt;a href="https://mermaid.js.org/">Mermaid 10&lt;/a> can be tricky since Mermaid 10 renders asynchronously (&lt;code>mermaid.render&lt;/code> returns a Promise).&lt;/p>
&lt;p>This post shows how to work around the mismatch between Docsify&amp;rsquo;s &lt;code>markdown.renderer.code&lt;/code> and Mermaid&amp;rsquo;s &lt;code>render&lt;/code> function on Mermaid v9 and v10.&lt;/p></description></item><item><title>Mocking/stubbing the Date and timers (setTimeout) in Node.js tests with built-in `node:test` MockTimers</title><link>https://codewithhugo.com/node-test-mock-date-and-timers/</link><pubDate>Mon, 26 Feb 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-test-mock-date-and-timers/</guid><description>&lt;p>&lt;a href="https://nodejs.org/api/test.html#class-mocktimers">&lt;code>MockTimers&lt;/code>&lt;/a> is an &lt;strong>experimental&lt;/strong> class in the &lt;code>node:test&lt;/code> module which concerns itself with allowing mocking of timers (&lt;code>setTimeout&lt;/code>, &lt;code>setInterval&lt;/code>, &lt;code>setImmediate&lt;/code>) and the &lt;code>Date&lt;/code> built-ins.&lt;/p></description></item><item><title>Direnv setup for multiple git/GitHub, npm and AWS accounts/credentials</title><link>https://codewithhugo.com/direnv/</link><pubDate>Mon, 19 Feb 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/direnv/</guid><description>&lt;p>In this post, we&amp;rsquo;ll look at how to configure direnv to manage multiple git, GitHub, npm and AWS accounts and credentials on a single machine.&lt;/p></description></item><item><title>Test Native `fetch` in Node.js with Undici interception and mock utils</title><link>https://codewithhugo.com/node-test-native-fetch-intercept-undici/</link><pubDate>Mon, 12 Feb 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-test-native-fetch-intercept-undici/</guid><description>&lt;p>Node.js 18+ has a built-in &lt;code>fetch&lt;/code> available, where prior versions had to use libraries such a &lt;code>node-fetch&lt;/code>, &lt;code>axios&lt;/code>, &lt;code>got&lt;/code> or other to get such functionality.&lt;/p>
&lt;p>The &amp;ldquo;native fetch&amp;rdquo; was implemented in userland first as the &lt;a href="https://github.com/nodejs/undici">undici package&lt;/a>.&lt;/p>
&lt;p>This post goes through how to use &lt;code>undici&lt;/code>&amp;rsquo;s mock utilities (&lt;code>MockAgent&lt;/code>, &lt;code>MockPool&lt;/code>, &lt;code>setGlobalDispatcher&lt;/code>) to intercept &amp;ldquo;native &lt;code>fetch&lt;/code>&amp;rdquo; requests in Node.js 18+.&lt;/p></description></item><item><title>Node Test Runner: skip a test if an environment variable is missing/empty</title><link>https://codewithhugo.com/node-test-skip-test-env-variable/</link><pubDate>Mon, 05 Feb 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-test-skip-test-env-variable/</guid><description>&lt;p>The &lt;a href="https://nodejs.org/api/test.html#test-runner">&lt;code>node:test&lt;/code>&lt;/a> module is a built-in test runner and orchestrator in Node.js. It&amp;rsquo;s available as experimental since Node 18 but has &lt;a href="https://nodejs.org/api/test.html#test-runner">stabilised in Node 20&lt;/a> and some features are still experimental at the time of writing, in Node 21.&lt;/p>
&lt;p>&lt;code>node:test&lt;/code> supports test skipping throught multiple approaches but the two we&amp;rsquo;ll focus on here are the &amp;ldquo;programmatic skips&amp;rdquo;, where at runtime we can decide whether a test should be skipped or not.&lt;/p>
&lt;p>This can be useful for integration or end to end testing scenarios being driven via the Node.js test runner (using &lt;code>node:test&lt;/code>).&lt;/p></description></item><item><title>Lerna publish with no git commit, tag or push</title><link>https://codewithhugo.com/lerna-publish-no-git/</link><pubDate>Mon, 29 Jan 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/lerna-publish-no-git/</guid><description>&lt;p>To &lt;code>lerna publish&lt;/code> without a &lt;strong>git commit&lt;/strong> or attempting to &lt;strong>git push&lt;/strong>, use &lt;code>--no-push --no-git-tag-version&lt;/code>, like so:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">lerna publish --no-push --no-git-tag-version
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>For official documentation on these options see the &lt;a href="https://github.com/lerna/lerna/blob/4fcac71c782cf91a3624fded775faaf3ddb8d636/libs/commands/version/README.md">&lt;code>lerna version&lt;/code> docs&lt;/a>.&lt;/p>
&lt;p>To understand the specifics of &lt;code>--no-push&lt;/code>, &lt;code>--no-git-tag&lt;/code> and why these options are documented in &lt;code>lerna version&lt;/code>, read on.&lt;/p></description></item><item><title>What is Jest and why should I use it?</title><link>https://codewithhugo.com/what-is-jest/</link><pubDate>Mon, 22 Jan 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/what-is-jest/</guid><description>&lt;blockquote>
&lt;p>Jest is a delightful JavaScript Testing Framework with a focus on simplicity.&lt;/p>
&lt;p>&lt;a href="https://jestjs.io/">jestjs.io&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>Jest is a batteries-included framework. Due to its fully-featured nature and large surface area, we can compare it to test runners (Mocha), assertion libraries (Chai, ShouldJS, power-assert, expect.js), integrated test-runner + assertion libraries (QUnit, Protractor, Tape, AVA, Jasmine), stubbing libraries (Sinon), module interception libraries (Proxyquire), coverage tools (NYC, Istanbul).&lt;/p></description></item><item><title>Concurrent/Parallel HTTP Requests in Go</title><link>https://codewithhugo.com/parallel-http-request-golang/</link><pubDate>Mon, 15 Jan 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/parallel-http-request-golang/</guid><description>&lt;p>A common source of high latency in all types of applications (scripts, HTTP servers, CLI tools), is to run HTTP requests in sequence that could be run in parallel.&lt;/p>
&lt;p>We&amp;rsquo;ll be looking at how to achieve this in Go. Examples for concurrency in the Go ecosystem tend to be more abstract or low-level than a close to real-world example of integrating a third party HTTP API.&lt;/p></description></item><item><title>Markdown escape backticks in inline code and fenced code blocks</title><link>https://codewithhugo.com/markdown-escape-backticks/</link><pubDate>Mon, 08 Jan 2024 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/markdown-escape-backticks/</guid><description>&lt;p>When writing technical documentation, blog posts or conducting code reviews, it can be useful to render backticks in Markdown inline code, code blocks or fenced code blocks.&lt;/p>
&lt;p>Since backticks are part of the Markdown syntax for inline code and code fences, this post looks at how to render them without clashing with the syntax.&lt;/p></description></item><item><title>git diff by word</title><link>https://codewithhugo.com/git-diff-by-word/</link><pubDate>Mon, 14 Nov 2022 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/git-diff-by-word/</guid><description>&lt;p>The default diffing algorithm for &lt;code>git diff&lt;/code> is by lines. There are scenarios where it&amp;rsquo;s more interesting to diff by word, for example: long lines. In that case it&amp;rsquo;s useful to use &lt;code>--word-diff=color&lt;/code>.&lt;/p></description></item><item><title>git/GitHub CLI set editor to vim or VSCode</title><link>https://codewithhugo.com/git-gh-cli-editor-vim-vscode/</link><pubDate>Wed, 09 Nov 2022 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/git-gh-cli-editor-vim-vscode/</guid><description>&lt;p>The GitHub CLI (&lt;code>gh&lt;/code> binary), defaults to nano as its editor.&lt;/p>
&lt;p>Developers may want to set it to &lt;code>vim&lt;/code> or VSCode to keep it in line with the &lt;code>git&lt;/code> CLI&amp;rsquo;s behaviour.&lt;/p></description></item><item><title>Node.js Native Test Runner</title><link>https://codewithhugo.com/node-test-runner/</link><pubDate>Mon, 23 May 2022 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-test-runner/</guid><description>&lt;p>If anyone missed it, Node.js 18 includes a test runner/test definition module (&lt;code>node --test&lt;/code> and &lt;code>node:test&lt;/code> respectively).&lt;/p>
&lt;p>&lt;code>node:test&lt;/code> exports a &lt;code>test&lt;/code> function and you can run the Node CLI with a &lt;code>--test&lt;/code> flag which does some basic search/matching for test files.&lt;/p>
&lt;p>Full Documentation: &lt;a href="https://nodejs.org/api/test.html">nodejs.org/api/test.html&lt;/a>&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://nodejs.org/api/test.html">Documentation for &lt;code>node:test&lt;/code>&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://nodejs.org/api/test.html#running-tests-from-the-command-line">Documentation for &lt;code>node --test&lt;/code>&lt;/a>&lt;/li>
&lt;li>Curated set of examples: &lt;a href="https://github.com/HugoDF/node-test-runner-examples">github.com/HugoDF/node-test-runner-examples&lt;/a>.&lt;/li>
&lt;/ul></description></item><item><title>Tips for real-world Alpine.js</title><link>https://codewithhugo.com/alpine-tips/</link><pubDate>Wed, 09 Jun 2021 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpine-tips/</guid><description>&lt;p>Alpine Day 2021 Talk: tackling Alpine FAQs, common issues &amp;amp; web patterns with Alpine.js&lt;/p></description></item><item><title>Why doesn't React.useEffect run on React server-side renders (SSR)?</title><link>https://codewithhugo.com/react-useeffect-ssr/</link><pubDate>Mon, 01 Mar 2021 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/react-useeffect-ssr/</guid><description>&lt;p>Why doesn&amp;rsquo;t &lt;code>React.useEffect&lt;/code> run during a server-side render (SSR) for example when using it inside a Next.js application?&lt;/p>
&lt;p>The obvious spots in the docs for information like this are the &lt;a href="https://reactjs.org/docs/hooks-effect.html">React docs on &lt;code>useEffect&lt;/code>&lt;/a> and the &lt;a href="https://reactjs.org/docs/react-dom-server.html#rendertostring">React docs on string/static rendering&lt;/a> neither of which mention any particular behaviour of &lt;code>useEffect&lt;/code> during server-side rendering (SSR).&lt;/p></description></item><item><title>Cypress Scroll Position Assertions</title><link>https://codewithhugo.com/cypress-scroll-assertions/</link><pubDate>Tue, 17 Nov 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/cypress-scroll-assertions/</guid><description>&lt;p>This post goes through approaches to asserting on the scroll position.&lt;/p>
&lt;p>First we&amp;rsquo;ll see how to assert that we&amp;rsquo;re at the top of the page.&lt;/p>
&lt;p>Next we&amp;rsquo;ll look at 2 approaches to assert that we&amp;rsquo;ve scrolled to an element.&lt;/p>
&lt;p>The examples for this post are available at &lt;a href="https://github.com/HugoDF/cypress-scroll-position-assertions/">github.com/HugoDF/cypress-scroll-position-assertions/&lt;/a>.&lt;/p></description></item><item><title>What is the "?." JavaScript/TypeScript operator</title><link>https://codewithhugo.com/javascript-typescript-optional-chaining/</link><pubDate>Mon, 09 Nov 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/javascript-typescript-optional-chaining/</guid><description>&lt;p>In JavaScript and TypeScript, the &amp;ldquo;?.&amp;rdquo; operator is called optional chaining.&lt;/p>
&lt;p>It works in a similar fashion to the &lt;code>.&lt;/code> (chaining) operator, except that it short-circuits instead of causing an error when a value is &lt;code>null&lt;/code> or &lt;code>undefined&lt;/code>.&lt;/p>
&lt;p>That means &lt;code>const obj = {}; console.log(obj.hello?.world);&lt;/code> will log &lt;code>undefined&lt;/code> (the value of &lt;code>obj.hello&lt;/code>) instead of throwing a &lt;code>TypeError: obj.hello is undefined&lt;/code> that &lt;code>console.log(obj.hello.world);&lt;/code> would cause.&lt;/p></description></item><item><title>Convert images to data URLs at the command line (Mac OS)</title><link>https://codewithhugo.com/convert-image-data-url/</link><pubDate>Mon, 26 Oct 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/convert-image-data-url/</guid><description>&lt;p>Converting images to data URLs using the Mac OS command line is a fun demonstration of the command line&amp;rsquo;s capabilities.&lt;/p>
&lt;p>To convert an image file to a data URL, we can use the following shell function:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">&lt;span class="k">function&lt;/span> img-data&lt;span class="o">()&lt;/span> &lt;span class="o">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nv">TYPE&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="k">$(&lt;/span>file --mime-type -b &lt;span class="nv">$1&lt;/span>&lt;span class="k">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nv">ENC&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="k">$(&lt;/span>base64 &lt;span class="nv">$1&lt;/span>&lt;span class="k">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nb">echo&lt;/span> &lt;span class="s2">&amp;#34;data:&lt;/span>&lt;span class="nv">$TYPE&lt;/span>&lt;span class="s2">;base64,&lt;/span>&lt;span class="nv">$ENC&lt;/span>&lt;span class="s2">&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="o">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>What follows is a walkthrough of what data URLs are, why they&amp;rsquo;re useful and how to compose the &lt;code>file&lt;/code> and &lt;code>base64&lt;/code> command line utilities to build the &lt;code>img-data&lt;/code> shell function.&lt;/p></description></item><item><title>The Future of PHP: Is It a Dead Programming Language?</title><link>https://codewithhugo.com/future-of-php/</link><pubDate>Thu, 20 Aug 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/future-of-php/</guid><description>&lt;p>There&amp;rsquo;s an opinion that PHP is dead today, but is this true?&lt;/p>
&lt;p>Should we believe in this or only people who don&amp;rsquo;t like PHP are sharing this news? &lt;a href="https://litslink.com/blog/why-all-the-hate-for-php-and-why-to-use-it">Why do people hate PHP&lt;/a>? Let&amp;rsquo;s take a closer look at PHP and try to predict its future.&lt;/p></description></item><item><title>Tips For Students Choosing Their First Programming Language</title><link>https://codewithhugo.com/select-programming-language/</link><pubDate>Tue, 18 Aug 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/select-programming-language/</guid><description>&lt;p>More and more colleges offer their students the opportunity to try their hand in coding.&lt;/p>
&lt;p>With technologies continuing making massive inroads people&amp;rsquo;s our everyday lives, such professions as a computer programmer, web developer, software engineer, &lt;a href="https://sopa.tulane.edu/blog/do-you-have-what-it-takes-career-mobile-app-development">mobile app developer&lt;/a>, and others ranks among the best paying and on-demand. No wonder, so many students, even those who initially chose completely different majors, strive to become conversant with popular programming languages. Being able to write code and program is not only promising, it&amp;rsquo;s prestigious. But slews of your learners find themselves plagued by some concerns and doubts we&amp;rsquo;ll try to dispel in today&amp;rsquo;s article. What&amp;rsquo;s more, we&amp;rsquo;ll help you answer the hardest questions most beginners ask themselves when tackling programming and choose the programming language you&amp;rsquo;ll be most comfortable with.&lt;/p></description></item><item><title>Disable a HTML &lt;a> link/anchor tag</title><link>https://codewithhugo.com/disable-html-anchor/</link><pubDate>Mon, 17 Aug 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/disable-html-anchor/</guid><description>&lt;p>Here are 2 ways to disable a HTML &lt;code>&amp;lt;a&amp;gt;&lt;/code> link/anchor element using CSS or by using inline JavaScript.&lt;/p></description></item><item><title>An accessible Alpine.js menu toggle</title><link>https://codewithhugo.com/alpinejs-a11y-menu/</link><pubDate>Mon, 10 Aug 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-a11y-menu/</guid><description>The &amp;ldquo;Hello World&amp;rdquo; of JavaScript frameworks and libraries seems to have become the todo app. In the case of Alpine.js a todo app is almost too large to showcase Alpine&amp;rsquo;s core benefits and use case.
Another issue with a lot of JavaScript examples is that they forego accessibility. Therefore we won&amp;rsquo;t be building a todo app but an accessible navigation menu.
Our menu is as follows (read on for a breakdown of how it&amp;rsquo;s built).</description></item><item><title>Alpine.js in-depth: listen to JavaScript events using x-on</title><link>https://codewithhugo.com/alpinejs-x-on/</link><pubDate>Mon, 03 Aug 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-on/</guid><description>&lt;p>The core Alpine.js functionality beyond toggling visibility, displaying data in HTML &lt;code>textContent&lt;/code> and binding HTML attributes to data is listening for events using Alpine.js. Listening to events with &lt;code>x-on&lt;/code> is usually coupled with state updates, for example we can create the following &amp;ldquo;counter&amp;rdquo; using &lt;code>x-data&lt;/code>, &lt;code>x-on&lt;/code> and &lt;code>x-text&lt;/code>.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">div&lt;/span> &lt;span class="na">x-data&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;{ count: 0 }&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">button&lt;/span> &lt;span class="na">x-on:click&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;count--&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>Decrement&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">button&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">code&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>count: &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">code&lt;/span>&lt;span class="p">&amp;gt;&amp;lt;&lt;/span>&lt;span class="nt">code&lt;/span> &lt;span class="na">x-text&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;count&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&amp;lt;/&lt;/span>&lt;span class="nt">code&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">button&lt;/span> &lt;span class="na">x-on:click&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;count++&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>Increment&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">button&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">div&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Alpine.js in-depth: reactive HTML attribute binding with x-bind</title><link>https://codewithhugo.com/alpinejs-x-bind/</link><pubDate>Mon, 27 Jul 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-bind/</guid><description>&lt;p>We&amp;rsquo;ve seen how to print out values using &lt;code>x-text&lt;/code> and toggle visibility of elements based on them with &lt;code>x-show&lt;/code>. We&amp;rsquo;ll now see how to set the value of HTML attributes using &lt;code>x-bind&lt;/code>.&lt;/p>
&lt;p>In this example we&amp;rsquo;ll display and image based on data that&amp;rsquo;s in our Alpine.js initial state (as defined in &lt;code>x-data&lt;/code>), in this case &lt;code>caption&lt;/code>, &lt;code>src&lt;/code> and &lt;code>width&lt;/code>. Which, respectively, represent the images&amp;rsquo; caption, the URL from which it&amp;rsquo;s being served and its display width on the page.&lt;/p></description></item><item><title>Show/hide in Alpine.js with x-show</title><link>https://codewithhugo.com/alpinejs-x-show/</link><pubDate>Mon, 20 Jul 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-show/</guid><description>&lt;p>In Alpine.js the directive used to toggle visibility of an element is &lt;code>x-show&lt;/code>. Per the &lt;a href="https://github.com/alpinejs/alpine#x-show">Alpine.js README&lt;/a>, &amp;ldquo;&lt;code>x-show&lt;/code> toggles the &lt;code>display: none;&lt;/code> style on the element depending if the expression resolves to &lt;code>true&lt;/code> or &lt;code>false&lt;/code>&amp;rdquo;.&lt;/p></description></item><item><title>Github Pull Request Review: reset viewed files</title><link>https://codewithhugo.com/github-review-reset-viewed/</link><pubDate>Mon, 13 Jul 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/github-review-reset-viewed/</guid><description>&lt;p>GitHub Pull Request Review workflow is great.&lt;/p>
&lt;p>What I particularly like is the &amp;ldquo;file viewed&amp;rdquo; toggle, which mean that I&amp;rsquo;m not tempted to re-review the whole Pull Request when a single file has changed and I&amp;rsquo;ve already checked the rest of them.&lt;/p>
&lt;p>However there are instances where I want to re-review the PR as a whole instead of just the changes since my last review. For example when there have been a lot of small changes that add up.&lt;/p></description></item><item><title>Alpine.js In-Depth: x-data "state" &amp; x-text "echo"</title><link>https://codewithhugo.com/alpinejs-x-data-x-text/</link><pubDate>Tue, 07 Jul 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-data-x-text/</guid><description>&lt;p>The simplest Alpine.js application is the following HTML file. It loads Alpine.js version 2+ from the JSDelivr CDN using a script tag and has an Alpine.js component with &lt;code>x-data=&amp;quot;{ msg: 'Hello Alpine.js' }&amp;quot;&lt;/code> and a &lt;code>p&lt;/code> element with &lt;code>x-text=&amp;quot;msg&amp;quot;&lt;/code>.&lt;/p></description></item><item><title>Alpine.js `x-for` a number of iterations (n times)</title><link>https://codewithhugo.com/alpinejs-x-for-n-times/</link><pubDate>Mon, 29 Jun 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-for-n-times/</guid><description>&lt;p>Alpine.js falls back to JavaScript to allow you to iterate a set number of times.&lt;/p>
&lt;p>For example if I want to create 4 lines on a page I can use the following.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">div&lt;/span> &lt;span class="na">x-data&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">template&lt;/span> &lt;span class="na">x-for&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;_ in Array.from({ length: 4 })&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">hr&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">template&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">div&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>You can see all the examples at &lt;a href="https://alpinejs.codewithhugo.com/x-for-n-times/">Alpine.js Playground - x-for n times&lt;/a>.&lt;/p></description></item><item><title>Alpine.js `x-for` with objects: 4 ways to iterate/loop through JavaScript objects</title><link>https://codewithhugo.com/alpinejs-x-for-with-objects/</link><pubDate>Mon, 22 Jun 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-for-with-objects/</guid><description>&lt;p>One of the missing features of Alpine.js is the ability to iterate through objects with &lt;code>x-for&lt;/code>.&lt;/p>
&lt;p>Alpine.js is heavily inspired by Vue.js but it&amp;rsquo;s designed to be lean and rugged instead of incrementally adoptable. Therefore &lt;code>x-for&lt;/code> only supports arrays/iterables.&lt;/p>
&lt;p>The reason there&amp;rsquo;s no first-class support for iterating through objects with Alpine.js&amp;rsquo; &lt;code>x-for&lt;/code> is that converting a JavaScript Object to an Array is reasonably easy in modern JavaScript (ES6+) environments using &lt;code>Object.keys&lt;/code>, &lt;code>Object.values&lt;/code> or even &lt;code>Object.entries&lt;/code>. This is the purpose of this post.&lt;/p>
&lt;p>You can find all the examples at &lt;a href="https://alpinejs.codewithhugo.com/x-for-object/">Alpine.js Playground - x-for with object&lt;/a>.&lt;/p></description></item><item><title>How to get type-checking and generate TypeScript Typing declaration (types.d.ts) from JSDoc annotations</title><link>https://codewithhugo.com/jsdoc-typescript-typings-types-d-ts/</link><pubDate>Mon, 15 Jun 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jsdoc-typescript-typings-types-d-ts/</guid><description>How to achieve TypeScript-like behaviour in Vanilla JavaScript using JSDoc and @ts-check in VSCode: that&amp;rsquo;s the purpose of this post.
TypeScript is a JavaScript superset with types. It solves one of the big problems with JavaScript, which is &amp;ldquo;what parameters does this function expect?&amp;rdquo;
TypeScript is great for the JavaScript ecosystem, code written in TypeScript (or with a type declaration file) has better IDE tooling (autocomplete, intellisense) and static checks can be performed.</description></item><item><title>Sync Alpine.js x-data to localStorage/sessionStorage</title><link>https://codewithhugo.com/alpinejs-localstorage-sessionstorage/</link><pubDate>Mon, 08 Jun 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-localstorage-sessionstorage/</guid><description>&lt;p>Alpine.js is great for writing widgets. &lt;code>localStorage&lt;/code>/&lt;code>sessionStorage&lt;/code> are Web APIs that enable JavaScript application to store data beyond the life of the current JavaScript process.&lt;/p>
&lt;p>This is useful for example if we wanted to persist our todos when the user closes the tab and comes back to it later.&lt;/p>
&lt;p>If you want to skip to the examples, they&amp;rsquo;re in this &lt;a href="https://codepen.io/collection/nVyKNz">CodePen collection&lt;/a> or at the following:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://codepen.io/hugodf/pen/mdepQbL">Alpine.js sync to localStorage - simple example&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://codepen.io/hugodf/pen/mdepQRG">Alpine.js sync to localStorage - persistent todo app&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>How to Access Alpine.js Magic Properties from inline handlers and function/component methods</title><link>https://codewithhugo.com/alpinejs-magic-property-access/</link><pubDate>Mon, 01 Jun 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-magic-property-access/</guid><description>&lt;p>Alpine.js magic properties are crucial to leveraging its best features.&lt;/p>
&lt;p>When using Alpine.js in a &amp;ldquo;mainly markup&amp;rdquo; configuration (no script tags), the magic properties tend to be accessible seamlessly.&lt;/p>
&lt;p>Alpine.js magic properties are as follows:&lt;/p>
&lt;ul>
&lt;li>&lt;code>$el&lt;/code>: the element to which an Alpine.js component is bound (also called the root element)&lt;/li>
&lt;li>&lt;code>$refs&lt;/code>: references to DOM Nodes as defined in the markup for an Alpine.js component&lt;/li>
&lt;li>&lt;code>$event&lt;/code>: (only exists in templates) when adding an event handler, using &lt;code>$event&lt;/code> will pass the full event into the handler.&lt;/li>
&lt;li>&lt;code>$dispatch&lt;/code>: dispatch a custom event see &lt;a href="https://codewithhugo.com/alpinejs-component-communication-event-bus/">A guide to Alpine.js component communication&lt;/a>&lt;/li>
&lt;li>&lt;code>$nextTick&lt;/code>: delay running of a function (callback) until after Alpine.js has updated/rendered&lt;/li>
&lt;li>&lt;code>$watch&lt;/code>: run a function (callback) on every change to a reactive property.&lt;/li>
&lt;/ul></description></item><item><title>Access Alpine.js component data/state from outside its scope</title><link>https://codewithhugo.com/alpinejs-inspect-component-data-from-js/</link><pubDate>Mon, 25 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-inspect-component-data-from-js/</guid><description>&lt;p>&lt;strong>Warning&lt;/strong> this uses Alpine.js v2 internals, there isn&amp;rsquo;t currently a public-facing API to do this.&lt;/p>
&lt;p>As an active Alpine.js and Alpine.js Devtools contributor, I&amp;rsquo;ve had the pleasure of explaining how to access Alpine.js component data from outside the component and of using my suggestion as part of work I did on the community-maintained &lt;a href="https://github.com/Te7a-Houdini/alpinejs-devtools">Alpine.js Devtools&lt;/a>.&lt;/p></description></item><item><title>A guide to Alpine.js component communication</title><link>https://codewithhugo.com/alpinejs-component-communication-event-bus/</link><pubDate>Mon, 18 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-component-communication-event-bus/</guid><description>&lt;p>Learn how to share information between Alpine.js components with the &lt;code>$dispatch&lt;/code> magic property and the window/document as an event bus.&lt;/p>
&lt;p>This post will show how to trigger and listen to global/window/document events with Alpine.js in order to use it as an event bus to communicate between sibling components (which is the only type of component composition that Alpine.js supports)&lt;/p>
&lt;p>Alpine.js is a great lightweight option for declarative view rendering. One of the first non-documented use-cases Alpine adopters come across is how to communicate between different Alpine.js components on the page.&lt;/p>
&lt;p>For example a &amp;ldquo;flash&amp;rdquo; message (or &amp;ldquo;toast&amp;rdquo; in Material UI parlance) component but want to be able to trigger it from anywhere on the page (not just from inside its body). Other common occurences are triggering modals to open/close from outside of them or submitting a form from another part of the page (that&amp;rsquo;s a separate Alpine.js component).&lt;/p>
&lt;p>Alpine.js, unlike React or Vue, doesn&amp;rsquo;t provide any parent-child communication patterns or primitives. &amp;ldquo;Alpine components&amp;rdquo; don&amp;rsquo;t receive any context from parent components, each component has its own individual and independent scope which means nesting doesn&amp;rsquo;t work as you would expect coming from Vue/React or even JavaScript (JavaScript has lexical scoping, Alpine does not).&lt;/p>
&lt;p>That means that in Alpine.js there&amp;rsquo;s no parent-child component communication only &amp;ldquo;sibling&amp;rdquo; component communication. For sibling communication, the pattern often proposed in issues by Caleb (the creator of Alpine.js) and other contributors is to use the &lt;code>window&lt;/code> or &lt;code>document&lt;/code> as an event bus.&lt;/p></description></item><item><title>App Ideas: 17 Web Apps You Can Build To Level Up Your Coding Skills</title><link>https://codewithhugo.com/coding-web-app-ideas/</link><pubDate>Tue, 12 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/coding-web-app-ideas/</guid><description>&lt;p>Have you ever wanted to build something as a web developer? The answer is probably&amp;hellip;yes. A common problem web developers have is that they don&amp;rsquo;t know WHAT to build and HOW to build them. Well, you&amp;rsquo;ve come to the right place.&lt;/p>
&lt;p>This is a list of 17 web apps you can (and should) build to level up your coding skills and expand your coding knowledge.&lt;/p>
&lt;p>This isn&amp;rsquo;t just a simple list of projects you can build. Each of these projects are described in detail with links to tutorials so you can develop them from the ground up. If you want to become a solid front-end web developer, then stay tuned.&lt;/p></description></item><item><title>Alpine.js + jQuery/JavaScript Plugin Integration: a Select2 example</title><link>https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/</link><pubDate>Mon, 11 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-jquery-plugin-integration-select2/</guid><description>&lt;p>One of the jQuery ecosystem&amp;rsquo;s greatest strength is the wealth of drop-in plugins available.&lt;/p>
&lt;p>Alpine.js is a great way to phase out jQuery spaghetti code from current and future projects with its declarative nature and small bundle size.&lt;/p>
&lt;p>What Alpine doesn&amp;rsquo;t have (yet), is a thriving plugin ecosystem. However, it&amp;rsquo;s all &amp;ldquo;just JavaScript&amp;rdquo; and it&amp;rsquo;s completely possible to leverage jQuery plugins in Alpine.js components.&lt;/p>
&lt;p>There have been a slew of questions in the Alpine.js Issues about integrating 3rd party libraries and jQuery plugins. This post will go through the example of integrating &lt;a href="https://select2.org/">Select2 - The jQuery replacement for select boxes &lt;/a> although the principles and tools explained should work for other plugins and tools.&lt;/p>
&lt;p>This post is &amp;ldquo;choose your own adventure&amp;rdquo;, if you&amp;rsquo;re interested in a CodePen or copy-pasting the code, head straight to &lt;a href="#show-me-the-code">Show me the code&lt;/a>. If on the other hand you&amp;rsquo;re interested in the step by step breakdown of the integration, that follows the first code/demo section.&lt;/p></description></item><item><title>JavaScript remove duplicates, get unique/distinct values from Array with ES6+ Set and spread</title><link>https://codewithhugo.com/es6-dedupe-set/</link><pubDate>Sat, 09 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/es6-dedupe-set/</guid><description>&lt;blockquote>
&lt;p>With ES6+, more developers should be leveraging built-ins than are using lodash functions.&lt;/p>
&lt;/blockquote>
&lt;p>This post will go through how to remove duplicates ie. get distinct/unique values from an Array using ES6 Set. This gives you a one-line implementation of lodash/underscore&amp;rsquo;s &lt;code>uniq&lt;/code> function:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">dedupe&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">list&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">[...&lt;/span>&lt;span class="k">new&lt;/span> &lt;span class="nx">Set&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">list&lt;/span>&lt;span class="p">)];&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This rest of the post goes through the how and why this works. It explores &lt;a href="https://en.wikipedia.org/wiki/Set_(abstract_data_type)">Sets&lt;/a>, &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">iterables&lt;/a> and a bit of complexity theory with &lt;a href="https://en.wikipedia.org/wiki/Big_O_notation">Big-O notation&lt;/a>.&lt;/p></description></item><item><title>AVA: pass or fail a test if an environment variable is missing/empty</title><link>https://codewithhugo.com/ava-pass-fail-test-env-variable/</link><pubDate>Mon, 04 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/ava-pass-fail-test-env-variable/</guid><description>&lt;p>In a recent project I needed to fail/pass some AVA if a runtime environment variable was unset or empty. Here&amp;rsquo;s how I solved this issue.&lt;/p>
&lt;p>&lt;a href="https://github.com/avajs/ava">AVA&lt;/a> is a test runner for Node.js with a concise API, detailed error output, embrace of new language features and process isolation that let you write tests more effectively.&lt;/p>
&lt;p>I&amp;rsquo;m a big fan of AVA&amp;rsquo;s explicit nature and it plays nicely with my favourite formatting tool: &lt;a href="https://github.com/xojs/xo">xo&lt;/a>.&lt;/p>
&lt;p>I recently used AVA to drive both unit and end to end tests for a JavaScript SDK/API wrapper for the &lt;a href="https://api.buttondown.email/">Buttondown API&lt;/a>. &lt;a href="https://buttondown.email">Buttondown&lt;/a> is &amp;ldquo;a small, elegant tool for producing newsletters&amp;rdquo;.&lt;/p>
&lt;p>You can see the SDK/Node.js API Wrapper at &lt;a href="https://github.com/HugoDF/buttondown">github.com/HugoDF/buttondown&lt;/a>.&lt;/p>
&lt;p>In the End to End suite, I needed a way to skip the tests, but only if the environment didn&amp;rsquo;t have an environment variable needed to run them.&lt;/p></description></item><item><title>Use microbundle for a TypeScript npm module</title><link>https://codewithhugo.com/microbundle-typescript-npm-module/</link><pubDate>Sat, 02 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/microbundle-typescript-npm-module/</guid><description>&lt;p>For those looking to write a package and publish it to npm, TypeScript + microbundle is a low-friction way to build a high-quality library.&lt;/p>
&lt;p>I&amp;rsquo;ve created a GitHub repository template with microbundle, TypeScript, &lt;a href="https://avajs.dev">ava&lt;/a> and &lt;a href="https://github.com/xojs/xo#readme">xo&lt;/a>. You can find it at &lt;a href="https://github.com/HugoDF/microbundle-ts-pkg/">github.com/HugoDF/microbundle-ts-pkg/&lt;/a>&lt;/p></description></item><item><title>Add days to a Date in vanilla JavaScript</title><link>https://codewithhugo.com/add-date-days-js/</link><pubDate>Fri, 01 May 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/add-date-days-js/</guid><description>&lt;p>Despite the JavaScript &lt;code>Date&lt;/code> warts, it&amp;rsquo;s straightforward to add day(s) to a date in JavaScript.&lt;/p>
&lt;p>While it would be very easy to reach for moment.js or another date manipulation library (date-fns, luxon, dayjs) to do something as simple as adding days to a Date in JavaScript, writing a short helper function might just be easier.&lt;/p></description></item><item><title>Synchronize x-data and the URL in Alpine.js with the location/History APIs and $watch</title><link>https://codewithhugo.com/alpinejs-x-data-watch-url/</link><pubDate>Mon, 27 Apr 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-data-watch-url/</guid><description>&lt;p>Alpine.js doesn&amp;rsquo;t have a router as yet since it&amp;rsquo;s designed to deliver simple interactive experiences on top of server or statically rendered sites.&lt;/p>
&lt;p>For single page applications that make heavy use of the History API or require a router, one would be better served heading over to Vue, React or Angular since they come with well-supported routers and routing solutions (eg. react-router, vue-router, reach-router, Next.js, Nuxt). If bundle size is a big concern (which it shouldn&amp;rsquo;t be for a Single Page Application), some lighter weight alternatives to the main SPA players are Preact + htm as described in &lt;a href="https://preactjs.com/guide/v10/getting-started#alternatives-to-jsx">No build tools route &amp;gt; Alternatives to JSX - Preact Docs&lt;/a> or &lt;a href="https://riot.js.org/compiler/#in-browser-compilation">Riot.js with in-browser compilation&lt;/a>.&lt;/p>
&lt;p>Note that the majority of the content of this post is not Alpine.js-specific since we&amp;rsquo;ll be leveraging DOM/Web APIs heavily. What this post is, is a showcase of how to structure syncing operations between the browser URL and Alpine.js so that Alpine.js state is represented in the URL and the URL is reflected in Alpine.js state.&lt;/p></description></item><item><title>Integrating Alpine.js + Pre/Server-rendered content</title><link>https://codewithhugo.com/alpine-server-pre-render-x-remove/</link><pubDate>Mon, 20 Apr 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpine-server-pre-render-x-remove/</guid><description>&lt;p>&lt;a href="https://github.com/alpinejs/alpine">Alpine.js&lt;/a> is a great choice for adding some interactive feature to a server rendered, static or pre-rendered site.&lt;/p>
&lt;p>In the Alpine.js GitHub Issues, we&amp;rsquo;ve seen a lot of questions around how to deal with content that&amp;rsquo;s pre-rendered or server-rendered in Alpine.js in order to use Alpine.js as a progressive enhancement.&lt;/p>
&lt;p>From a poll I ran recently, 95.2% of Alpine.js users want to use it with a server-rendered page, static site generators or HTML pages.&lt;/p>
&lt;blockquote>
&lt;p>I (want to) use &lt;a href="https://twitter.com/Alpine_JS">@Alpine_JS&lt;/a> in projects that are:&lt;/p>
&lt;p>Server-rendered -&amp;gt; Rails/Laravel/Django/Express/WordPress/&lt;em>insert CMS&lt;/em>&lt;br>
Static site (with generator) -&amp;gt; Hugo/Hexo/Jekyll/11ty&lt;br>
HTML pages -&amp;gt; hand-written HTML&lt;br>
SPAs (single page apps) -&amp;gt; in React/Angular/Ember/Vue, or with a router&lt;/p>
&lt;p>Poll results (21 votes)&lt;/p>
&lt;ul>
&lt;li>Server rendered: 71.4%&lt;/li>
&lt;li>Static sites (with generator): 9.5%&lt;/li>
&lt;li>HTML pages: 14.3%&lt;/li>
&lt;li>SPAs: 4.8%&lt;/li>
&lt;/ul>
&lt;p>— Hugo (&lt;a href="https://twitter.com/hugo__df">@hugo__df&lt;/a>) &lt;a href="https://twitter.com/hugo__df/status/1243552559257837571?ref_src=twsrc%5Etfw">March 27, 2020&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>This post details solutions to handling these cases. You can find all the examples at &lt;a href="https://alpinejs.codewithhugo.com/pre-rendered-content/">alpinejs.codewithhugo.com/pre-rendered-content/&lt;/a>.&lt;/p></description></item><item><title>Integrating Alpine.js with Eleventy &amp; YAML files to create Alpine Playground's Collections</title><link>https://codewithhugo.com/eleventy-collections-yaml-alpinejs/</link><pubDate>Tue, 14 Apr 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/eleventy-collections-yaml-alpinejs/</guid><description>&lt;p>Adding collections to Alpine.js Playground, essentially bringing it in line with projects such as &lt;a href="https://github.com/alpinejs/awesome-alpine">awesome-alpine&lt;/a> and &lt;a href="https://alpinetoolbox.com">alpinetoolbox.com&lt;/a>.&lt;/p>
&lt;p>It came from the fact that I&amp;rsquo;m curating quite a bit of content for each newsletter and it makes sense that content featured on the newsletter should be accessible in an easy to scan manner.&lt;/p>
&lt;p>Here&amp;rsquo;s the rough mockup of what it should look like, ready for comparison with how it ended up looking on &lt;a href="https://alpinejs.codewithhugo.com/?type=components">Alpine.js Playground Collections&lt;/a>&lt;/p>
&lt;p>&lt;img src="./alpine-playground-collections-mockup.jpg" alt="Mockup of the feature on Excalidraw">&lt;/p>
&lt;p>While building this new feature, I came across a couple of interesting problems to which I&amp;rsquo;m documenting the solutions in the post.&lt;/p>
&lt;p>There were 2 big parts to the implementation: Eleventy data capture + injection and then implementing with Alpine.js.&lt;/p></description></item><item><title>How to migrate a bunch of HTML pages (Alpine.js Playground) to Eleventy</title><link>https://codewithhugo.com/migrate-html-alpinejs-to-eleventy/</link><pubDate>Wed, 08 Apr 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/migrate-html-alpinejs-to-eleventy/</guid><description>&lt;p>&lt;a href="https://alpinejs.codewithhugo.com">Alpine.js Playground&lt;/a> was recently migrated from custom build scripts &amp;amp; HTML pages to leverage &lt;a href="https://11ty.dev">Eleventy&lt;/a>.&lt;/p>
&lt;p>For context, &lt;a href="https://alpinejs.codewithhugo.com">Alpine.js Playground&amp;rsquo;s&lt;/a> custom build scripts + HTML files had the following pros and cons.&lt;/p>
&lt;p>Pros:&lt;/p>
&lt;ul>
&lt;li>simple&lt;/li>
&lt;li>very simple, everything is in the &lt;code>scripts&lt;/code> folder.&lt;/li>
&lt;li>no dependencies (except the scripts &lt;em>do&lt;/em> have dependencies)&lt;/li>
&lt;/ul>
&lt;p>Cons:&lt;/p>
&lt;ul>
&lt;li>hand-rolled, doesn&amp;rsquo;t leverage any tooling&lt;/li>
&lt;li>no markdown/data file support&lt;/li>
&lt;li>clunky editing the homepage HTML inside the JS file that generates it&lt;/li>
&lt;/ul></description></item><item><title>Practical Alpine.js: x-data data access &amp; data fetching/AJAX examples</title><link>https://codewithhugo.com/alpinejs-x-data-fetching/</link><pubDate>Mon, 09 Mar 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpinejs-x-data-fetching/</guid><description>&lt;p>&lt;a href="https://github.com/alpinejs/alpine">Alpine.js&lt;/a> &amp;ldquo;A rugged, minimal framework for composing JavaScript behavior in your markup.&amp;rdquo; is a great, lightweight replacement for interactive jQuery or Vanilla JS sprinkles due to its small size (~7kb min-gzipped) &amp;amp; the fact it can be included from a CDN with a &lt;code>script&lt;/code> tag.&lt;/p>
&lt;p>Caleb Porzio, Alpine.js&amp;rsquo; creator, likens it to &amp;ldquo;&lt;a href="https://tailwindcss.com/">Tailwind&lt;/a> for JavaScript&amp;rdquo;. One of the great things about &lt;a href="https://tailwindcss.com/">Tailwind&lt;/a> is that the examples provided are very much copy-pasteable since the logic lives in the HTML/markup instead of in a CSS file off somewhere else.&lt;/p>
&lt;p>A lot of Caleb&amp;rsquo;s Alpine.js examples in the README (no official documentation site at the time of writing), use &lt;em>pure&lt;/em> HTML markup, functionality is declared inline of the attributes that use it.&lt;/p>
&lt;p>This allows the Alpine.js examples to be very concise, but coming from a Vue.js/React background, I like to wrap my functionality in an &lt;code>x-data&lt;/code> function that&amp;rsquo;s not declared inline, it&amp;rsquo;s in a &lt;code>script&lt;/code> tag later on in the page. This also means that shared JavaScript/Alpine.js components can be extracted to a &lt;code>.js&lt;/code> file and included using &lt;code>&amp;lt;script src=&amp;quot;./path/to/fn.js&amp;quot;&amp;gt;&amp;lt;/script&amp;gt;&lt;/code>.&lt;/p>
&lt;p>This post is about how to access, fetch and set data in Alpine.js components using a setup where JavaScript is in script tags or a separate file, not inline in the markup.&lt;/p></description></item><item><title>Run Alpine.js inside of React</title><link>https://codewithhugo.com/alpine-js-react/</link><pubDate>Mon, 02 Mar 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/alpine-js-react/</guid><description>&lt;p>Alpine.js is well-suited for building widgets and easily creating client-side experiences for websites that are mainly server-side rendered or static.&lt;/p>
&lt;p>Good news: it &lt;strong>is&lt;/strong> possible (at the time of writing with Alpine.js 2.x) to integrate Alpine.js with React trivially.&lt;/p>
&lt;p>&lt;a href="https://github.com/alpinejs/alpine">Alpine.js&lt;/a> is &amp;ldquo;A rugged, minimal framework for composing JavaScript behavior in your markup.&amp;rdquo;. It can be included simply with a &lt;code>script&lt;/code> tag from a CDN and weights in under 5kb.&lt;/p>
&lt;p>Caleb Porzio, Alpine.js&amp;rsquo; creator, likens it to &amp;ldquo;&lt;a href="https://tailwindcss.com/">Tailwind&lt;/a> for JavaScript&amp;rdquo;.&lt;/p>
&lt;p>Now one of the great things about &lt;a href="https://tailwindcss.com/">Tailwind&lt;/a> is that the examples provided are very much copy-pasteable.&lt;/p>
&lt;p>If Alpine.js widgets ever pick up, integrating them in a React application might be useful.&lt;/p>
&lt;p>In this post we&amp;rsquo;ll see how to run Alpine.js inside of a React application.&lt;/p>
&lt;p>To see the full demo/code repo head to &lt;a href="https://github.com/HugoDF/react-alpinejs">github.com/HugoDF/react-alpinejs&lt;/a>.&lt;/p></description></item><item><title>Advantages and Disadvantages of Node.js</title><link>https://codewithhugo.com/node-pros-and-cons/</link><pubDate>Fri, 24 Jan 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-pros-and-cons/</guid><description>&lt;p>JavaScript is perhaps the most highly regarded of the different programming languages out there, and in terms of client-side programming languages, there are few tools that are peers to JavaScript. In terms of web development, it is clear that JavaScript has a very important role. That said, JavaScript is not just content to stay on one side. There are server-side programming languages as well, and JavaScript is now heading in this area. Thanks to Node.js, this shift has been facilitated with great ease and it is important to learn all about Node.js, how it works, and the advantages and disadvantages of it.&lt;/p>
&lt;p>The first question needing to be answered is what exactly Node.js is. The simplistic answer is Node.js functions as a Javascript run-time environment that allows JavaScript coding to be executed in a server-side environment. This is an open source platform as well which increases flexibility to get things done. Whether you &lt;a href="https://makeawebsitehub.com/free-node-js-hosting/">click here to find free Node.js hosting&lt;/a> or keep reading to learn more, what you’ll find is that Node.js does have many fans and it has detractors as well. Understand that Node.js is not a type of framework, it also does not have a relationship with .NET, PHP, or JAVA. This makes Node.js a different way to work with server-side programming.&lt;/p></description></item><item><title>5 Reasons Why You Should Learn Programming</title><link>https://codewithhugo.com/5-reasons-to-code/</link><pubDate>Fri, 24 Jan 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/5-reasons-to-code/</guid><description>&lt;p>When it comes to expanding what you do and being more valuable to potential employers, programming is a great way to get yourself greater value on the job market. Programming is a valuable skill. There are a lot of people who do a lot of things with computers, but at the same time, there are not as many people who are learning programming. The biggest reason is programming is not done by more people is because there is a degree of complexity to it. That being said, just because something may not be easy is no reason to dismiss the undertaking. When you learn programming, you are literally learning a new language – and that is a very valuable language skill.&lt;/p>
&lt;p>The reality is if you do anything on the computer, you are encountering programming and that makes it vitally important that you learn it because you will have a deeper understanding. In today’s world, there is a greater emphasis on the information technology aspect of careers, programming is vitally important. However, if you decide to ditch simple website builders and &lt;a href="https://makeawebsitehub.com/become-a-web-developer/">become a professional web developer&lt;/a> then you should take up programming. This is a vital aspect of how you can make yourself more marketable in this field. There are plenty of excellent ways to learn how to program. The key is to find yourself a program that works for your learning style, and once you do so, you will create an additional area of marketability for yourself.
Here are five reasons you should learn how to program.&lt;/p></description></item><item><title>Managing Your Videos With HTML and JavaScript</title><link>https://codewithhugo.com/html-js-video/</link><pubDate>Wed, 22 Jan 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/html-js-video/</guid><description>&lt;p>This article explains how to manage video content with HTML and
JavaScript. Video content has become a significant part of Internet use,
and it plays a large role in user experience. The ability to effectively
include video content in your sites can help you improve user
experience.&lt;/p>
&lt;p>In this article, you&amp;rsquo;ll learn some basic HTML and JavaScript tools for
working with video. You&amp;rsquo;ll also learn how to make some common
customizations to your videos to give your users the best experience
possible.&lt;/p></description></item><item><title>Catalina "Apple cannot check for malicious software" Command Line Fix</title><link>https://codewithhugo.com/catalina-permission-command-line-fix/</link><pubDate>Mon, 20 Jan 2020 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/catalina-permission-command-line-fix/</guid><description>&lt;p>Fix &amp;ldquo;X can&amp;rsquo;t be opened because Apple cannot check for malicious software&amp;rdquo; and &amp;ldquo;Developer or app can&amp;rsquo;t be verified&amp;rdquo; using the command line.&lt;/p>
&lt;p>Mac OS X Catalina has introduced a new host of permission issues. There are workarounds for applications that you&amp;rsquo;re installing to your &amp;ldquo;Applications&amp;rdquo; folder, namely right-click and use the &amp;ldquo;Open&amp;rdquo; -&amp;gt; &amp;ldquo;Open Anyway&amp;rdquo; trick.&lt;/p>
&lt;p>For developers among you, you will want to use the command line to fix this situation rather than clicking around the UI.&lt;/p>
&lt;p>For the non-developers among you, some applications will fail to open even if you give them the correct system &amp;amp; security settings.&lt;/p>
&lt;p>This post details how to do that using the command line (Terminal) and &lt;code>xattr -d com.apple.quarantine &amp;lt;path-to-program&amp;gt;&lt;/code>.&lt;/p></description></item><item><title>Fix "[Vue warn]: You are using the runtime-only build of Vue"</title><link>https://codewithhugo.com/vue-warn-runtime-only-build/</link><pubDate>Mon, 25 Nov 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/vue-warn-runtime-only-build/</guid><description>&lt;p>This post explains how to fix &amp;ldquo;[Vue warn]: You are using the runtime-only build of Vue where the template compiler is not available. Either pre-compile the templates into render functions, or use the compiler-included build.&amp;rdquo;.&lt;/p>
&lt;p>This error happens when using the Vue CLI and attempting to use a component that has its &lt;code>template&lt;/code> defined as a string.&lt;/p></description></item><item><title>Jest set, clear and reset mock/spy/stub implementation</title><link>https://codewithhugo.com/jest-stub-mock-spy-set-clear/</link><pubDate>Mon, 11 Nov 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-stub-mock-spy-set-clear/</guid><description>&lt;p>Between test runs we need mocked/spied on imports and functions to be reset so that assertions don&amp;rsquo;t fail due to stale calls (from a previous test).
This is a way to mitigate what little statefulness is in the system.&lt;/p>
&lt;p>In unit tests of complex systems, it&amp;rsquo;s not always possible to keep business logic in pure functions, where the only input are the parameters and the only output is the return value.
Systems are inherently side-effectful (things that are not parameters or output values). HTTP requests, database reads and writes are side-effects that are crucial to writing applications.&lt;/p>
&lt;p>This is why we want to be able to set and modify the implementation and return value of functions in Jest.&lt;/p>
&lt;p>This post goes through how to set, reset and clear mocks, stubs and spies in Jest using techniques such as the &lt;code>beforeEach&lt;/code> hook and methods such as &lt;code>jest.clearAllMocks&lt;/code> and &lt;code>jest.resetAllMocks&lt;/code>. We&amp;rsquo;ll also see how to update a mock or spy&amp;rsquo;s implementation with &lt;code>jest.fn().mockImplementation()&lt;/code>, as well as &lt;code>mockReturnValue&lt;/code> and &lt;code>mockResolvedValue&lt;/code>. This post is a reference to be able to discern when to use each of these.&lt;/p></description></item><item><title>Jest .fn() and .spyOn() spy/stub/mock assertion reference</title><link>https://codewithhugo.com/jest-fn-spyon-stub-mock/</link><pubDate>Tue, 05 Nov 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-fn-spyon-stub-mock/</guid><description>&lt;p>Where other JavaScript testing libraries would lean on a specific stub/spy library like &lt;a href="https://sinonjs.org/">Sinon - Standalone test spies, stubs and mocks for JavaScript. Works with any unit testing framework.&lt;/a>, Jest comes with stubs, mocks and spies out of the box.&lt;/p>
&lt;p>This post looks at how to instantiate stubs, mocks and spies as well as which assertions can be done over them.&lt;/p></description></item><item><title>Jest Array/Object partial match with objectContaining and arrayContaining</title><link>https://codewithhugo.com/jest-array-object-match-contain/</link><pubDate>Mon, 28 Oct 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-array-object-match-contain/</guid><description>&lt;p>It&amp;rsquo;s possible to do partial matches on Arrays and Objects in Jest using &lt;code>expect.objectContaining&lt;/code> and &lt;code>expect.arrayContaining&lt;/code>.&lt;/p>
&lt;p>&lt;code>expect&lt;/code> has some powerful matcher methods to do things like the above partial matches.&lt;/p>
&lt;p>Using Jest at an advanced level means using tools like these to write tests that are better isolated and less brittle (this is what I&amp;rsquo;m tryin to achieve with the &lt;a href="https://jesthandbook.com">Jest Handbook&lt;/a>).&lt;/p>
&lt;p>This post starts with an explanation to give context to partial matches followed by sample use-cases in a recipe/cookbook format.&lt;/p>
&lt;p>We finish off by mentioning further resources that cover this topic.&lt;/p></description></item><item><title>Jest explicitly or arbitrarily force fail() a test</title><link>https://codewithhugo.com/jest-force-explicitly-fail-test/</link><pubDate>Mon, 21 Oct 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-force-explicitly-fail-test/</guid><description>&lt;p>When testing code with Jest, it can sometimes be useful to fail a test arbitrarily.&lt;/p>
&lt;p>This post goes through a few scenarios where that might be useful and how to fail a Jest test explicitly/in a forced manner.&lt;/p>
&lt;p>It also presents more idiomatic Jest patterns that could be used interchangeably.&lt;/p></description></item><item><title>How to integrate Python/Ruby/PHP/shell script with Node.js using child_process.spawn or child_process.exec</title><link>https://codewithhugo.com/integrate-python-ruby-php-shell-with-node-js/</link><pubDate>Wed, 16 Oct 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/integrate-python-ruby-php-shell-with-node-js/</guid><description>&lt;p>There are occasions when running a Python/Ruby/PHP shell script from Node.js is necessary. This post looks at best practices around leveraging child_process.spawn and child_process.exec to encapsulate this call in Node.js/JavaScript.&lt;/p>
&lt;p>The goal here is to have an interoperability layer between Node.js and an outside shell. This is a quick workaround if some other part of your system isn’t developed in JavaScript.&lt;/p>
&lt;p>&lt;code>spawn&lt;/code> is used over &lt;code>exec&lt;/code> because we’re talking about passing data, and potentially large amounts of it. To understand the difference between &lt;code>child_process.spawn&lt;/code> and &lt;code>child_process.exec&lt;/code> (see “&lt;a href="https://www.hacksparrow.com/difference-between-spawn-and-exec-of-node-js-child_process.html">Difference between spawn and exec of Node.js child_process&lt;/a>”).&lt;/p>
&lt;p>We&amp;rsquo;ll use &lt;code>exec&lt;/code> to run arbitrary commands (eg. Pandoc generation), it&amp;rsquo;s ideal for small amounts of data (under 200k) using a Buffer interface and &lt;code>spawn&lt;/code> for larger amounts using a stream interface.&lt;/p>
&lt;p>&lt;code>spawn&lt;/code> has a more verbose syntax for some of the use-cases we’ll look at, but it’s more serviceable for integrating with Ruby/Python/PHP since we might get more data than a couple of lines of text.&lt;/p>
&lt;p>&lt;code>exec&lt;/code> is brilliant to integrate with system binaries (where we don&amp;rsquo;t care about the output).&lt;/p>
&lt;p>Full examples &lt;a href="https://github.com/HugoDF/node-run-python">github.com/HugoDF/node-run-python&lt;/a>.&lt;/p></description></item><item><title>Jest Full and Partial Mock/Spy of CommonJS and ES6 Module Imports</title><link>https://codewithhugo.com/jest-mock-spy-module-import/</link><pubDate>Tue, 15 Oct 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-mock-spy-module-import/</guid><description>&lt;blockquote>
&lt;p>JavaScript import/require module testing do&amp;rsquo;s and don&amp;rsquo;ts with Jest&lt;/p>
&lt;/blockquote>
&lt;p>The example repository is available at &lt;a href="https://github.com/HugoDF/mock-spy-module-import">github.com/HugoDF/mock-spy-module-import&lt;/a>.&lt;/p>
&lt;p>This post goes through how to achieve different types of module mocking scenarios with Jest.&lt;/p>
&lt;p>From simple &lt;a href="#intercepting-imports">Import interception&lt;/a>, to &lt;a href="#spying-stubbing-calls-to-internal-module-functions">how to approach stubbing out an internal function call&lt;/a> or &lt;a href="#spy-on-imports-or-mock-part-of-a-module-by-referencing-the-module">Mocking only part of a module (by spying&amp;hellip;)&lt;/a>.&lt;/p>
&lt;p>These are methods that work in more specific cases than what the Jest official documentation shows.&lt;/p>
&lt;p>This isn&amp;rsquo;t strictly a Jest testing guide, the same principles can be applied to any application/tests that need to mock CommonJS or ES Modules.&lt;/p></description></item><item><title>Importance of Blocking Web Crawlers and Bots From Your Website</title><link>https://codewithhugo.com/importance-crawler-bot-block/</link><pubDate>Fri, 11 Oct 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/importance-crawler-bot-block/</guid><description>&lt;p>The technical term, &amp;ldquo;crawling&amp;rdquo; means accessing websites automatically
and obtaining data. Web crawlers, spiders, or search engine bots
download and index web content from the Internet. Search engines, like
Google, use bots or web crawlers and apply search algorithm to gather
data so relevant links are provided in response to search queries. It
helps in generating a list of web pages or search engine
results.&lt;/p>
&lt;p>But, why is it recommend to block bots and web crawlers? Find out more
by reading below:&lt;/p></description></item><item><title>How to run, ignore or skip Jest tests, suites and files</title><link>https://codewithhugo.com/run-skip-single-jest-test/</link><pubDate>Mon, 07 Oct 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/run-skip-single-jest-test/</guid><description>&lt;p>When debugging or writing units tests with Jest it can be useful to specify a single file or test to run or to exclude a specific failing test for the Jest run.&lt;/p>
&lt;p>This post goes how to skip and exclude a single test, a whole Jest test suite and a whole Jest test file using the CLI or Jest built-ins.&lt;/p></description></item><item><title>Auto-publish future posts with JAMstack on Netlify and IFTTT/CircleCI 2.0 scheduled jobs</title><link>https://codewithhugo.com/scheduled-future-posts-jamstack/</link><pubDate>Sun, 29 Sep 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/scheduled-future-posts-jamstack/</guid><description>&lt;p>Static site generators like &lt;a href="http://gohugo.io">Hugo&lt;/a>, &lt;a href="https://jekyllrb.com/">Jekyll&lt;/a>, &lt;a href="https://hexo.io/">Hexo&lt;/a>, &lt;a href="https://www.gatsbyjs.org/">Gatsby&lt;/a> or other &lt;a href="https://jamstack.org/">JAMstack&lt;/a> alternative are a great option to run your blog on.&lt;/p>
&lt;p>One of the earliest and possibly most painful issues you will come across is “How do I schedule content to publish for later?”.&lt;/p>
&lt;p>In the traditional database-backed blog site, using WordPress or Ghost for instance. The posts are retrieved on access (at run-time in Computer Science-speak). To have a post that only starts appearing after a certain date is as simple as having a field with a “publishedAt” date, and some code that checks whether that field is before or after the current date.&lt;/p>
&lt;p>Enter the JAMstack. It doesn’t run using a classic PHP/Node.js/Python/Rails server-side component backed by a relational or non-relational database.&lt;/p>
&lt;blockquote>
&lt;p>JAMstack: Modern web development architecture based on client-side JavaScript, reusable APIs, and prebuilt Markup.&lt;/p>
&lt;p>See &lt;a href="https://jamstack.org/">jamstack.org&lt;/a> and &lt;a href="https://jamstack.wtf">jamstack.wtf&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>Instead the JAMstack does the bulk of the processing work at &lt;strong>build-time&lt;/strong> (as opposed to &lt;strong>runtime&lt;/strong>). This tends to have a bunch of benefits like improved cache ability, higher leverage of a CDN (Content Delivery Network) which all contribute to a snappier experience.&lt;/p>
&lt;p>The downside of this ahead-of-time computation of all the pages on the site is that there isn’t an obvious way to do “scheduled” posts. To make a post appear, you need to re-build the site after its “publishedAt” date so that it’s included in the build pages.&lt;/p>
&lt;p>This post goes through how to achieve this using modern tools. Namely CircleCI 2.0 or IFTTT and Netlify (which is a hosting platform specialised in JAMstack sites).&lt;/p>
&lt;p>&lt;a href="https://codewithhugo.com/">codewithhugo.com&lt;/a> used to run its daily builds using the CircleCI scheduled job approach but was updated to use IFTTT.&lt;/p></description></item><item><title>Detect ctrl/cmd/alt/shift + Click in Vanilla JavaScript, React and Vue.js</title><link>https://codewithhugo.com/detect-ctrl-click-js-react-vue/</link><pubDate>Mon, 16 Sep 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/detect-ctrl-click-js-react-vue/</guid><description>&lt;p>Detecting control-click, command-click, meta-click, alt-click and shift-click in JavaScript without listening to keydown events is straightforward.&lt;/p>
&lt;p>The naive approach is to bind aggressively to key events (&lt;code>keydown&lt;/code>, &lt;code>keypress&lt;/code>, &lt;code>keyup&lt;/code>) and to maintain state of what is currently pressed using JavaScript.&lt;/p>
&lt;p>That&amp;rsquo;s not necessary since MouseEvents have some keypress specific properties: &lt;code>metaKey&lt;/code>, &lt;code>ctrlKey&lt;/code>, &lt;code>altKey&lt;/code> and &lt;code>shiftKey&lt;/code>
(see the corresponding pages on MDN,
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/metaKey">metaKey&lt;/a>,
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/ctrlKey">ctrlKey&lt;/a>,
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/altKey">altKey&lt;/a> and
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/shiftKey">shiftKey&lt;/a>).&lt;/p></description></item><item><title>Record analytics events with a Zeit "micro" + SQLite3 Node.js application</title><link>https://codewithhugo.com/micro-analytics-events-sqlite/</link><pubDate>Mon, 09 Sep 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/micro-analytics-events-sqlite/</guid><description>&lt;blockquote>
&lt;p>&lt;a href="https://github.com/HugoDF/micro-analytics-events">micro-analytics-events&lt;/a> - A Node.js microservice to record analytics events to SQLite3 with Zeit&amp;rsquo;s &amp;ldquo;micro&amp;rdquo;&lt;/p>
&lt;/blockquote>
&lt;p>In order to finally switch off Google Analytics, I need to replace my goal conversion tracking somehow.&lt;/p>
&lt;p>I&amp;rsquo;ve got pageview data using &lt;a href="https://referral.simpleanalytics.com/hugo">Simple Analytics&lt;/a>, which also has an events system.&lt;/p>
&lt;p>In order to use events, you have to get cookie consent and those banners don&amp;rsquo;t look nice.&lt;/p>
&lt;p>Instead I built &lt;a href="https://github.com/HugoDF/micro-analytics-events">micro-analytics-events&lt;/a>. A Node.js microservice to record analytics events.&lt;/p>
&lt;p>It uses &lt;a href="https://github.com/zeit/micro">micro&lt;/a> because it&amp;rsquo;s lightweight and easy to get started with. It stores things with SQLite3 because I intend to host it for free on &lt;a href="https://glitch.com">Glitch&lt;/a>.&lt;/p>
&lt;p>It&amp;rsquo;s a very simple application, if you send a POST request with an &lt;code>event_type&lt;/code> it records it. If you send a GET request with optional &lt;code>start&lt;/code>, &lt;code>end&lt;/code> and &lt;code>event_type&lt;/code> query params, it returns &lt;code>{ &amp;quot;events&amp;quot;: [] }&lt;/code>, an object with an &lt;code>event&lt;/code> key that&amp;rsquo;s an array of &lt;code>{ event_type, date }&lt;/code>.&lt;/p></description></item><item><title>How to run Jest tests sequentially</title><link>https://codewithhugo.com/run-jest-sequentially/</link><pubDate>Mon, 02 Sep 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/run-jest-sequentially/</guid><description>&lt;p>By default Jest runs tests in parallel with a &amp;ldquo;a worker pool of child processes that run tests&amp;rdquo; (&lt;a href="https://jestjs.io/docs/en/cli.html#runinband">Jest CLI docs&lt;/a>).&lt;/p>
&lt;p>As per the Jest documentation, running tests in sequence (serially), can be helpful for debugging purposes and for test run performance reasons.&lt;/p></description></item><item><title>Detect if the current file/module is the entrypoint or import/require-d in Node.js</title><link>https://codewithhugo.com/node-module-entry-required/</link><pubDate>Mon, 26 Aug 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-module-entry-required/</guid><description>&lt;p>How does one identify whether a JavaScript file is being run directly (&lt;code>node file.js&lt;/code>) or it&amp;rsquo;s being import/require-d (&lt;code>require('./file')&lt;/code> in another JS file).&lt;/p>
&lt;p>In Python the pattern leverages the &lt;code>__main__&lt;/code> property in modules like so:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-py" data-lang="py">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="vm">__name__&lt;/span> &lt;span class="o">==&lt;/span> &lt;span class="s1">&amp;#39;__main__&amp;#39;&lt;/span>&lt;span class="p">:&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># do something&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1"># or not&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">pass&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The equivalent code in Node.js/JavaScript would be:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="k">if&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="nx">require&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">main&lt;/span> &lt;span class="o">===&lt;/span> &lt;span class="nx">module&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// do something
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="c1">// or nothing
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Add Diagrams to your Jekyll/Hugo/Gatsby blog with Mermaid JS</title><link>https://codewithhugo.com/mermaid-js-hugo-shortcode/</link><pubDate>Mon, 12 Aug 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/mermaid-js-hugo-shortcode/</guid><description>&lt;blockquote>
&lt;p>Add diagrams to your JAMStack (Jekyll/Hugo/Gatsby) blog with Mermaid JS&lt;/p>
&lt;/blockquote>
&lt;p>Update 17th January 2024: Hugo now has native hooks for diagrams, so that code-fences using &lt;code>```mermaid [content]```&lt;/code> can have a custom template, see the &lt;a href="https://gohugo.io/content-management/diagrams/#mermaid-diagrams">Content Management &amp;gt; Diagrams &amp;gt; Mermaid diagrams docs&lt;/a>.&lt;/p>
&lt;p>The following post will go through how to integrate Mermaid JS, a &amp;ldquo;simple markdown-like language for generating charts&amp;rdquo;.&lt;/p>
&lt;p>It will initially go through the benefits of MermaidJS, followed by a generic (cross-technology) integration, finally it will tackle the Hugo-way of dealing with this issue.&lt;/p></description></item><item><title>Yarn Workspaces: monorepo management without Lerna for applications and coding examples</title><link>https://codewithhugo.com/yarn-workspaces-application-monorepo/</link><pubDate>Mon, 05 Aug 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/yarn-workspaces-application-monorepo/</guid><description>&lt;blockquote>
&lt;p>How to use Yarn Workspaces to manage applications/coding examples.&lt;/p>
&lt;/blockquote>
&lt;p>Lerna isn&amp;rsquo;t required when you don&amp;rsquo;t need the git diffing and versioning facilites.&lt;/p>
&lt;p>What&amp;rsquo;s more Yarn Workspaces are a great lightweight tool to get up and running faster for simple Node.js monorepo actions.&lt;/p>
&lt;p>Example monorepo at: &lt;a href="https://github.com/HugoDF/yarn-workspaces-simple-monorepo">github.com/HugoDF/yarn-workspaces-simple-monorepo&lt;/a>&lt;/p></description></item><item><title>Add Search to a Hugo site with Lunr.js and Node.js</title><link>https://codewithhugo.com/hugo-lunrjs-search-index/</link><pubDate>Mon, 01 Jul 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/hugo-lunrjs-search-index/</guid><description>&lt;p>&lt;a href="https://gohugo.io">Hugo&lt;/a> &amp;ldquo;The world’s fastest framework for building websites&amp;rdquo; is a great option for &lt;a href="https://jamstack.org/">JAMStack&lt;/a> (JavaScript, APIs, prebuild Markup) sites.&lt;/p>
&lt;p>&lt;a href="https://lunrjs.com/">Lunr.js&lt;/a> is &amp;ldquo;A bit like Solr, but much smaller and not as bright&amp;rdquo;, it&amp;rsquo;s a pure JavaScript implementation of a Solr-like search engine.&lt;/p>
&lt;p>One of the only things it doesn&amp;rsquo;t provide out of the box is Search. It does give you a few options to integrate at &lt;a href="https://gohugo.io/tools/search/">&amp;ldquo;Search for your Hugo Website&amp;rdquo;&lt;/a>.&lt;/p>
&lt;p>None were plug and play so I wrote my own simple solution using Lunr.js, a small Node.js script and a few lines of client-side HTML/JavaScript code.&lt;/p>
&lt;p>This is a great example of a benefit of Node.js: it&amp;rsquo;s a breeze to integrate a pure JavaScript library and pre-compute the search index.&lt;/p>
&lt;p>You can see Search in action at &lt;a href="https://codewithhugo.com/search/?q=lunrjs">codewithhugo.com/search/?q=lunrjs&lt;/a>.&lt;/p>
&lt;p>Find the full gist at &lt;a href="https://gist.github.com/HugoDF/aac2e529f79cf90d2050d7183571684b">gist.github.com/HugoDF&lt;/a>&lt;/p></description></item><item><title>JavaScript Data Type Check Cheatsheet</title><link>https://codewithhugo.com/javascript-data-type-check-cheatsheet/</link><pubDate>Mon, 24 Jun 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/javascript-data-type-check-cheatsheet/</guid><description>&lt;blockquote>
&lt;p>JavaScript (JS) is a lightweight, interpreted, or just-in-time compiled programming language with first-class functions.&lt;/p>
&lt;p>&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript">MDN Web Docs - javascript&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>Detailed in this post are common JavaScript data type checks, pitfalls and idiomatic workarounds.&lt;/p></description></item><item><title>Tailwind CSS 1.x impressions, development and production setup for static sites with Tailwind CLI and PurgeCSS CLI</title><link>https://codewithhugo.com/tailwind-css-1.x-impressions-development-and-production-setup-for-static-sites-with-tailwind-cli-and-purgecss-cli/</link><pubDate>Mon, 17 Jun 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/tailwind-css-1.x-impressions-development-and-production-setup-for-static-sites-with-tailwind-cli-and-purgecss-cli/</guid><description>&lt;blockquote>
&lt;p>How to set Tailwind CSS up with a static site hosted on Netlify&lt;/p>
&lt;/blockquote>
&lt;p>See the full starter repository at &lt;a href="https://github.com/HugoDF/netlify-lambda-tailwind-static-starter">github.com/HugoDF/netlify-lambda-tailwind-static-starter&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://tailwindcss.com/">Tailwind CSS&lt;/a> is “a utility-first CSS framework for rapidly building custom designs”. I&amp;rsquo;ll be walking through my first impressions of it as well as how to leverage its CLI (and the PurgeCSS CLI) to add it to any project without a build process (eg. Webpack, Gulp).&lt;/p></description></item><item><title>Jest ignore or exclude file/function/statement from test coverage</title><link>https://codewithhugo.com/jest-exclude-coverage/</link><pubDate>Mon, 10 Jun 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-exclude-coverage/</guid><description>&lt;blockquote>
&lt;p>In computer science, test coverage is a measure used to describe the degree to which the source code of a program is executed when a particular test suite runs.&lt;/p>
&lt;p>&lt;a href="https://en.wikipedia.org/wiki/Code_coverage">Code coverage - Wikipedia&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>Code coverage is usually used as a quality metric for software eg. “Our code has to have 80%+ test coverage”. Gathering test coverage with Jest is as simple as using the &lt;code>--coverage&lt;/code> flag on invocation.&lt;/p>
&lt;p>This post goes through how to ignore files, functions, lines and statements from coverage in Jest using configuration or istanbul pragmas. As well as reasons and limits to why/how you would do such a thing.&lt;/p>
&lt;p>You can find a working examples repository at &lt;a href="https://github.com/HugoDF/jest-ignore-coverage">github.com/HugoDF/jest-ignore-coverage&lt;/a>.&lt;/p></description></item><item><title>Super-powered newsletter content with Pocket and Netlify Lambda</title><link>https://codewithhugo.com/pocket-newsletter-netlify-lambda/</link><pubDate>Mon, 03 Jun 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/pocket-newsletter-netlify-lambda/</guid><description>&lt;blockquote>
&lt;p>An example Netlify Lambda to fetch all “newsletter” posts from &lt;a href="https://getpocket.com">Pocket&lt;/a>.&lt;/p>
&lt;/blockquote>
&lt;p>&lt;a href="https://en.wikipedia.org/wiki/Pocket_(service)">Pocket is an application and web service for managing a reading list of articles from the Internet&lt;/a>. It&amp;rsquo;s quite widely used and tightly integrated into the Firefox browser. I find that I use it extensively to save articles (usually about development).&lt;/p>
&lt;p>For the &lt;a href="https://buttondown.email/hugo">Enterprise Node.js and JavaScript newsletter&lt;/a> I have a “From the Web” section, which is populated from links from the web. Since most links that end up there are at one point or another stored on Pocket, I built a lambda that fetches posts tagged with “newsletter” from Pocket. I then consume that lambda from a &lt;a href="https://gohugo.io">Hugo&lt;/a> newsletter file generator.&lt;/p>
&lt;p>See the lambda code at &lt;a href="https://github.com/HugoDF/pocket-newsletter-lambda/blob/master/src/lambda/newsletter.js">src/lambda/newsletter.js&lt;/a> in the repository &lt;a href="https://github.com/HugoDF/pocket-newsletter-lambda">github.com/HugoDF/pocket-newsletter-lambda&lt;/a>.&lt;/p>
&lt;p>&lt;a href="https://app.netlify.com/start/deploy?repository=https://github.com/HugoDF/pocket-newsletter-lambda">Deploy your own on Netlify&lt;/a>.&lt;/p>
&lt;p>The application is styled using TailwindCSS, you can see a starter project for that at &lt;a href="https://github.com/HugoDF/netlify-lambda-tailwind-static-starter">github.com/HugoDF/netlify-lambda-tailwind-static-starter&lt;/a>.&lt;/p></description></item><item><title>Avoiding recursion pitfalls: MongoDB nested query clause addition and removal</title><link>https://codewithhugo.com/mongo-or-query-add-remove/</link><pubDate>Mon, 20 May 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/mongo-or-query-add-remove/</guid><description>&lt;p>A case-study on where recursion can be useful for enterprise Node.js applications and how to avoid its common pitfalls like &lt;code>RangeError: Maximum call stack size exceeded&lt;/code>.&lt;/p>
&lt;p>The full repository for this post is on GitHub: &lt;a href="https://github.com/HugoDF/mongo-query-clause-modification">github.com/HugoDF/mongo-query-clause-modification&lt;/a>&lt;/p>
&lt;p>We&amp;rsquo;ll be implementing a solution to 2 real-world problems:&lt;/p></description></item><item><title>Read and parse POST/PATCH/PUT request JSON or form body with Express and no dependencies</title><link>https://codewithhugo.com/parse-express-json-form-body/</link><pubDate>Sat, 11 May 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/parse-express-json-form-body/</guid><description>&lt;p>When asked to handle data in a request body, developers who have used Express (the “Fast, unopinionated, minimalist web framework for &lt;a href="https://nodejs.org/en/">Node.js&lt;/a>”) before, reach for the &lt;a href="https://github.com/expressjs/body-parser">body-parser&lt;/a> library.&lt;/p>
&lt;p>What they might not know is that body-parser is a dependency of Express and its main JSON parsing and url encoded body parsing functionality is exposed as &lt;code>express.json()&lt;/code> and &lt;code>express.urlencoded()&lt;/code>ie. middleware for parsing the most common request bodies is built into Express.&lt;/p>
&lt;blockquote>
&lt;p>Note: &lt;code>express.json&lt;/code> and &lt;code>express.urlencoded&lt;/code> exist in Express 4.16+&lt;/p>
&lt;/blockquote>
&lt;p>The global &lt;code>express.json()&lt;/code> middleware is not to be confused with the &lt;code>res.json()&lt;/code> function which sends back a body as JSON (and sets the content format headers to JSON).&lt;/p>
&lt;p>The code for json bodies is at &lt;a href="https://codesandbox.io/s/olrn6x3n19?fontsize=14">https://codesandbox.io/s/olrn6x3n19?fontsize=14&lt;/a>, you can view the app at &lt;a href="https://olrn6x3n19.sse.codesandbox.io/">https://olrn6x3n19.sse.codesandbox.io/&lt;/a>.&lt;/p>
&lt;p>The code for URL encoded form bodies is at &lt;a href="https://codesandbox.io/s/6njqzmyw4k?fontsize=14">https://codesandbox.io/s/6njqzmyw4k?fontsize=14&lt;/a>, you can test it out at &lt;a href="https://6njqzmyw4k.sse.codesandbox.io/">https://6njqzmyw4k.sse.codesandbox.io/&lt;/a>.&lt;/p>
&lt;p>Real(ish) world usage in projects, you can also &lt;a href="https://codewithhugo.com/tags/express">find more Express posts on Code with Hugo&lt;/a>&lt;/p>
&lt;ul>
&lt;li>&lt;code>express.urlencoded&lt;/code>: &lt;a href="https://github.com/HugoDF/express-nunjucks-tailwind/blob/master/server.js">https://github.com/HugoDF/express-nunjucks-tailwind/blob/master/server.js&lt;/a>&lt;/li>
&lt;li>&lt;code>express.json&lt;/code>: &lt;a href="https://github.com/HugoDF/express-postgres-starter/blob/master/server.js">https://github.com/HugoDF/express-postgres-starter/blob/master/server.js&lt;/a>&lt;/li>
&lt;/ul></description></item><item><title>Using ES6 classes for Sequelize 4 or 5 models</title><link>https://codewithhugo.com/using-es6-classes-for-sequelize-4-models/</link><pubDate>Sat, 04 May 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/using-es6-classes-for-sequelize-4-models/</guid><description>&lt;p>The ES2015 or ES6 specification introduced &lt;code>class&lt;/code> to JavaScript.
Libraries like React went from &lt;code>React.createClass&lt;/code> to &lt;code>class MyComponent extends React.Component&lt;/code>,
ie went from rolling their own constructor to leveraging a language built-in to convey the programmer’s intent.&lt;/p>
&lt;p>For a Node.js web application’s persistence layer,
a few databases come to mind like &lt;a href="https://www.mongodb.com/">MongoDB&lt;/a>
(possibly paired with &lt;a href="http://mongoosejs.com/">mongoose&lt;/a>),
or a key-value store like &lt;a href="https://redis.io/">Redis&lt;/a>.&lt;/p>
&lt;p>To run a relational database with a Node application, &lt;a href="http://docs.sequelizejs.com/">Sequelize&lt;/a>,
“An easy-to-use multi SQL dialect ORM for Node.js” is a good option.
It allows the application to run backed by a MySQL or PostgreSQL instance and
provides an easy way to map from entities&amp;rsquo; representation in the database to JavaScript and vice versa.&lt;/p>
&lt;p>Sequelize’s v4 and lower API for model definitions looks like the following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">MyModel&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="nx">sequelize&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">define&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s2">&amp;#34;MyModel&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// fields and methods
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>To add class and instance methods you would write the following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Class Method
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="nx">MyModel&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">associate&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="kd">function&lt;/span> &lt;span class="p">(&lt;/span>&lt;span class="nx">models&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{};&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">// Instance Method
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="nx">MyModel&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">prototype&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">someMethod&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="kd">function&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">{..}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This is necessary pre-ES6 since there was no concept of classical inheritance. Since we have class now, why not leverage them? For developers who are used to having classes, the following would likely look familiar:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">class&lt;/span> &lt;span class="nx">MyModel&lt;/span> &lt;span class="kr">extends&lt;/span> &lt;span class="nx">Sequelize&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">Model&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">static&lt;/span> &lt;span class="nx">associate&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">models&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="p">{}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">someMethod&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="p">{}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Sequelize actually supports this, but the documentation is a bit lacking. One of the only place to find a reference to how to do this is in a GitHub issue: &lt;a href="https://github.com/sequelize/sequelize/issues/6524">https://github.com/sequelize/sequelize/issues/6524&lt;/a>.&lt;/p>
&lt;p>Here’s a cheat sheet for things you would want to do and how to achieve it using ES6 classe + inheriting from &lt;code>Sequelize.Model&lt;/code>:&lt;/p></description></item><item><title>Jest assert over single or specific argument/parameters with .toHaveBeenCalledWith and expect.anything()</title><link>https://codewithhugo.com/jest-specific-argument-parameter-assert/</link><pubDate>Mon, 29 Apr 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/jest-specific-argument-parameter-assert/</guid><description>&lt;blockquote>
&lt;p>With Jest it&amp;rsquo;s possible to assert of single or specific arguments/parameters of a mock function call with &lt;code>.toHaveBeenCalled&lt;/code>/&lt;code>.toBeCalled&lt;/code> and &lt;code>expect.anything()&lt;/code>.&lt;/p>
&lt;/blockquote>
&lt;p>The full example repository is at &lt;a href="https://github.com/HugoDF/jest-specific-argument-assert">github.com/HugoDF/jest-specific-argument-assert&lt;/a>, more specifically &lt;a href="https://github.com/HugoDF/jest-specific-argument-assert/blob/master/src/pinger.test.js#L17-L66">lines 17-66 in the src/pinger.test.js file&lt;/a>.&lt;/p>
&lt;p>You can use &lt;code>expect.anything()&lt;/code> to ignore certain parameters that a mock Jest function is called with, see the following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="nx">test&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;calls getPingConfigs with right accountId, searchRegex&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="kr">async&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">await&lt;/span> &lt;span class="nx">pinger&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="mi">1&lt;/span>&lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">mockPingConfig&lt;/span>&lt;span class="p">).&lt;/span>&lt;span class="nx">toHaveBeenCalledWith&lt;/span>&lt;span class="p">(&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="mi">1&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">anything&lt;/span>&lt;span class="p">(),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">expect&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">anything&lt;/span>&lt;span class="p">(),&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">new&lt;/span> &lt;span class="nb">RegExp&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="s1">&amp;#39;.*&amp;#39;&lt;/span>&lt;span class="p">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">);&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">});&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Read on for more details of the code under test and why one would use such an approach.&lt;/p>
&lt;p>The code under test follows module boundaries similar to what is described in &lt;a href="https://codewithhugo.com/node-postgres-express-docker-compose/">An enterprise-style Node.js REST API setup with Docker Compose, Express and Postgres&lt;/a>. Specifically a &lt;a href="https://codewithhugo.com/node-postgres-express-docker-compose/#architecture-example-user-session-management">3-tier (Presentation, Domain, Data) layering&lt;/a>, where we&amp;rsquo;ve only implemented the domain and (fake) data layers.&lt;/p></description></item><item><title>An enterprise-style Node.js REST API setup with Docker Compose, Express and Postgres</title><link>https://codewithhugo.com/node-postgres-express-docker-compose/</link><pubDate>Wed, 24 Apr 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-postgres-express-docker-compose/</guid><description>&lt;p>The why and how of enterprise-style Node.js application. A setup that’s easy to test and extend using battle-hardened technologies like Express.js, Postgres and Docker Compose to run locally.&lt;/p>
&lt;p>Dive into the code on GitHub directly: &lt;a href="https://github.com/HugoDF/express-postgres-starter">github.com/HugoDF/express-postgres-starter&lt;/a>.&lt;/p></description></item><item><title>Enable CDN (Cloudflare) caching for your Netlify site</title><link>https://codewithhugo.com/enable-cdn-netlify/</link><pubDate>Mon, 01 Apr 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/enable-cdn-netlify/</guid><description>&lt;p>This post goes through how to reduce the amount of static assets served through Netlify by leveraging a CDN (Content Delivery Network) like Cloudflare. This will reduce your Netlify bandwidth usage for cacheable assets.&lt;/p>
&lt;p>It will also mean that you’re following industry-accepted best practices around setting a time for which your assets should be cached by receiving browsers.&lt;/p>
&lt;p>In principle, it won’t significantly improve the speed at which your site loads since Netlify’s built-in CDN does a good job of that anyways. It will mean that you’re leveraging your CDN’s capacity to deliver assets quickly as opposed to Netlify’s.&lt;/p></description></item><item><title>Docker Compose Local HTTPS with nginx or Caddy and mkcert</title><link>https://codewithhugo.com/docker-compose-local-https/</link><pubDate>Mon, 25 Mar 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/docker-compose-local-https/</guid><description>&lt;p>A solution to serve your Dockerized application on Docker Compose with TLS/SSL locally by leveraging a HTTPS reverse-proxy and &lt;a href="https://github.com/FiloSottile/mkcert">mkcert&lt;/a>.&lt;/p>
&lt;p>Examples with Caddy and nginx are at: &lt;a href="https://github.com/HugoDF/docker-compose-local-https">github.com/HugoDF/docker-compose-local-https&lt;/a>&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Deployment options: Netlify + Dokku on DigitalOcean or Hetzner Cloud vs now.sh, GitHub Pages, Heroku and AWS</title><link>https://codewithhugo.com/deployment-options-netlify-dokku-on-digitalocean-vs-now.sh-github-pages-heroku-and-aws/</link><pubDate>Wed, 20 Mar 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/deployment-options-netlify-dokku-on-digitalocean-vs-now.sh-github-pages-heroku-and-aws/</guid><description>&lt;blockquote>
&lt;p>Settling on a &lt;a href="https://www.netlify.com/">Netlify&lt;/a> + &lt;a href="https://github.com/dokku/dokku">Dokku&lt;/a> on &lt;a href="https://m.do.co/c/4ee8b2d1adb4">DigitalOcean&lt;/a> (for US) or &lt;a href="https://www.hetzner.com/cloud">Hetzner Cloud&lt;/a> (for EU) deployment stack for JavaScript and Node.&lt;/p>
&lt;/blockquote>
&lt;p>Over the last few years I’ve never quite settled on a goto deployment strategy for my apps and projects. Technology-wise it has settled towards Vue or static site generators for the frontend with Node on the backend, whether that’s for API servers or handling rendering through handlebars.&lt;/p>
&lt;p>What I’ve finally settled on is &lt;a href="https://www.netlify.com/">Netlify&lt;/a> + &lt;a href="https://github.com/dokku/dokku">Dokku&lt;/a> on &lt;a href="https://m.do.co/c/4ee8b2d1adb4">DigitalOcean&lt;/a>. Netlify for anything static (and more with forms, CMS and lambda). Dokku, “The smallest PaaS implementation you&amp;rsquo;ve ever seen.“ for any backend apps and databases. This is what I’ve used to deploy my latest projects &lt;a href="https://accountableblogging.com/post-frequency">Post Frequency&lt;/a> and &lt;a href="https://accountableblogging.com/post-frequency">Accountable Blogging&lt;/a>, tools to quantify and improve your blogging output.&lt;/p>
&lt;p>This is a long list of pros/cons for a bunch of services:&lt;/p>
&lt;ul>
&lt;li>👍 → pro&lt;/li>
&lt;li>👎 → con&lt;/li>
&lt;li>🤔 → undecided&lt;/li>
&lt;li>🚨 → deal-breaker for me&lt;/li>
&lt;li>🤓 → nice-to-have&lt;/li>
&lt;/ul>
&lt;p>Table of contents:&lt;/p></description></item><item><title>JavaScript Object.defineProperty for a function: create mock object instances in Jest or AVA</title><link>https://codewithhugo.com/js-object-defineproperty-for-function/</link><pubDate>Mon, 18 Mar 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/js-object-defineproperty-for-function/</guid><description>&lt;p>This post goes through how to use &lt;code>Object.defineProperty&lt;/code> to mock how constructors create methods, ie. non-enumerable properties that are functions.&lt;/p>
&lt;p>The gist of &lt;code>Object.defineProperty&lt;/code> use with a function value boils down to:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">obj&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{}&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nb">Object&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">defineProperty&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">obj&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="s1">&amp;#39;yes&amp;#39;&lt;/span>&lt;span class="p">,&lt;/span> &lt;span class="p">{&lt;/span> &lt;span class="nx">value&lt;/span>&lt;span class="o">:&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="nb">Math&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">random&lt;/span>&lt;span class="p">()&lt;/span> &lt;span class="o">&amp;gt;&lt;/span> &lt;span class="p">.&lt;/span>&lt;span class="mi">5&lt;/span> &lt;span class="p">})&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">obj&lt;/span>&lt;span class="p">)&lt;/span> &lt;span class="c1">// {}
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span>&lt;span class="nx">console&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">log&lt;/span>&lt;span class="p">(&lt;/span>&lt;span class="nx">obj&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">yes&lt;/span>&lt;span class="p">())&lt;/span> &lt;span class="c1">// false or true depending on the call :D
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>As you can see, the &lt;code>yes&lt;/code> property is not enumerated, but it does exist. That&amp;rsquo;s great for setting functions as method mocks.&lt;/p>
&lt;p>It’s useful to testing code that uses things like Mongo’s &lt;code>ObjectId&lt;/code>. We don’t want actual ObjectIds strewn around our code. Although I did create an app that allows you generate ObjectId compatible values (&lt;a href="https://observablehq.com/@hugodf/mongodb-objectid-generator">see it here Mongo ObjectId Generator&lt;/a>).&lt;/p>
&lt;p>All the test and a quick explanation of what we’re doing and why we’re doing it, culminating in our glorious use of &lt;code>Object.defineProperty&lt;/code>, is on GitHub &lt;a href="https://github.com/HugoDF/mock-mongo-object-id">github.com/HugoDF/mock-mongo-object-id&lt;/a>. Leave it a star if you’re a fan 🙂 .&lt;/p></description></item><item><title>Fix git “tip of your current branch is behind its remote counterpart” - 4 real-world solutions</title><link>https://codewithhugo.com/fix-git-failed-to-push-updates-were-rejected/</link><pubDate>Mon, 11 Mar 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/fix-git-failed-to-push-updates-were-rejected/</guid><description>&lt;p>When working with &lt;code>git&lt;/code> a selection of GitLab, GitHub, BitBucket and rebase-trigger-happy colleagues/collaborators, it’s a rite of passage to see a message like the following:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-sh" data-lang="sh">&lt;span class="line">&lt;span class="cl">Pushing to git@github.com:some-project/some-repo.git
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">To git@github.com:some-project/some-repo.git
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> ! &lt;span class="o">[&lt;/span>rejected&lt;span class="o">]&lt;/span> master -&amp;gt; master &lt;span class="o">(&lt;/span>non-fast-forward&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">error: failed to push some refs to &lt;span class="s1">&amp;#39;git@github.com:some-project/some-repo.git&amp;#39;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">hint: Updates were rejected because the tip of your current branch is behind
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">hint: its remote counterpart. Merge the remote changes &lt;span class="o">(&lt;/span>e.g. &lt;span class="s1">&amp;#39;git pull&amp;#39;&lt;/span>&lt;span class="o">)&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">hint: before pushing again.
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">hint: See the &lt;span class="s1">&amp;#39;Note about fast-forwards&amp;#39;&lt;/span> in &lt;span class="s1">&amp;#39;git push --help&amp;#39;&lt;/span> &lt;span class="k">for&lt;/span> details.
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Pass cookies with axios or fetch requests</title><link>https://codewithhugo.com/pass-cookies-axios-fetch-requests/</link><pubDate>Mon, 04 Mar 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/pass-cookies-axios-fetch-requests/</guid><description>&lt;p>When sending requests from client-side JavaScript, by default cookies are not passed.&lt;/p>
&lt;blockquote>
&lt;p>By default, fetch won&amp;rsquo;t send or receive any cookies from the server, resulting in unauthenticated requests
&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch">https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>Two JavaScript HTTP clients I use are &lt;a href="https://github.com/axios/axios">axios&lt;/a>, a “Promise based HTTP client for the browser and Node.js” and the fetch API (see &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API">Fetch API on MDN&lt;/a>).&lt;/p></description></item><item><title>A testing guide for Express with request and response mocking/stubbing using Jest or sinon</title><link>https://codewithhugo.com/express-request-response-mocking/</link><pubDate>Mon, 18 Feb 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/express-request-response-mocking/</guid><description>&lt;p>To test an Express handler, it’s useful to know how to successfully mock/stub the &lt;code>request&lt;/code> and &lt;code>response&lt;/code> objects. The following examples will be written both using Jest and sinon (running in AVA).&lt;/p>
&lt;p>The rationale for this is the following. Jest is a very popular “all-in-one” testing framework. Sinon is one of the most popular “Standalone test spies, stubs and mocks for JavaScript” which “works with any unit testing framework”.&lt;/p>
&lt;p>The approach detailed in this post will be about how to test handlers independently of the Express app instance by calling them directly with mocked request (&lt;code>req&lt;/code>) and response (&lt;code>res&lt;/code>) objects. This is only 1 approach to testing Express handlers and middleware. The alternative is to fire up the Express server (ideally in-memory using SuperTest). I go into more detail on how to achieve that in &lt;a href="https://codewithhugo.com/testing-an-express-app-with-supertest-moxios-and-jest/">“Testing an Express app with SuperTest, moxios and Jest”&lt;/a>.&lt;/p>
&lt;p>One of the big conceptual leaps to testing Express applications with mocked request/response is understanding how to mock a chained API eg. &lt;code>res.status(200).json({ foo: 'bar' })&lt;/code>.&lt;/p>
&lt;p>This is achieved by returning the &lt;code>res&lt;/code> instance from each of its methods:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-js" data-lang="js">&lt;span class="line">&lt;span class="cl">&lt;span class="kr">const&lt;/span> &lt;span class="nx">mockResponse&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="kr">const&lt;/span> &lt;span class="nx">res&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">{};&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="c1">// replace the following () =&amp;gt; res
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="c1">// with your function stub/mock of choice
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="c1">// making sure they still return `res`
&lt;/span>&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="c1">&lt;/span> &lt;span class="nx">res&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">status&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="nx">res&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nx">res&lt;/span>&lt;span class="p">.&lt;/span>&lt;span class="nx">json&lt;/span> &lt;span class="o">=&lt;/span> &lt;span class="p">()&lt;/span> &lt;span class="p">=&amp;gt;&lt;/span> &lt;span class="nx">res&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="k">return&lt;/span> &lt;span class="nx">res&lt;/span>&lt;span class="p">;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">};&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>See the repository with examples and the working application at &lt;a href="https://github.com/HugoDF/mock-express-request-response">github.com/HugoDF/mock-express-request-response&lt;/a>.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Node.js configuration management without config or dotenv</title><link>https://codewithhugo.com/node-configuration-management/</link><pubDate>Wed, 13 Feb 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/node-configuration-management/</guid><description>&lt;p>The &lt;code>config&lt;/code> npm package is great (&lt;a href="https://www.npmjs.com/package/config">npmjs.com/package/config&lt;/a>), but it encourages confusing and non-&lt;a href="https://12factor.net/">12-factor-app&lt;/a>-compliant patterns.&lt;/p>
&lt;p>We’ll look at some of the patterns it encourages and why they’ll bring you struggles down the road as well a simple, single-file, no-dependency way to define your configuration.&lt;/p></description></item><item><title>Bash Cheatsheet: check if environment variables are set or file/symlinks exists + more</title><link>https://codewithhugo.com/just-enough-bash-to-be-dangerous/</link><pubDate>Tue, 05 Feb 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/just-enough-bash-to-be-dangerous/</guid><description>&lt;blockquote>
&lt;p>A bash scripting cheat sheet for developers who just want to get by.&lt;/p>
&lt;/blockquote>
&lt;p>Writing setup, CI and deployment flows means a bit of the old bash scripting.&lt;/p>
&lt;p>Despite my deep interest in the intricacies of Bash (&lt;em>/sarcasm&lt;/em>), I&amp;rsquo;ve kept hitting up Google and StackOverflow for solutions to the same couple of situations.&lt;/p>
&lt;p>To avoid having to do this again myself and for your reading pleasure, here they are.&lt;/p>
&lt;p>To be dangerous in terms of setup, CI and depoyment flows we will encounter the following:&lt;/p></description></item><item><title>Async JavaScript: history, patterns and gotchas</title><link>https://codewithhugo.com/async-js/</link><pubDate>Mon, 28 Jan 2019 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/async-js/</guid><description>&lt;p>A look at the history, patterns and gotchas of asynchronous operations in JavaScript.&lt;/p>
&lt;p>We&amp;rsquo;ll go through the pros and cons of callbacks, Promises and async/await. Present some pitfalls to bear in mind as well as introducing how you would deal with certain situations.&lt;/p>
&lt;p>Live-coding/workshop section touching on both Node and client-side JS situations at &lt;a href="https://github.com/HugoDF/async-js-presentation/tree/master/workshop">github.com/HugoDF/async-js-presentation/tree/master/workshop&lt;/a>.&lt;/p>
&lt;p>This given as a talk at Codebar London January Monthly 2019, see the slides:&lt;/p>
&lt;div style="width: 100%; padding: 50px;">
&lt;!-- SpeakderDeck embed -->
&lt;script async class="speakerdeck-embed" data-id="bdf4eba915de465096eecc8322c7f712" data-ratio="1.41436464088398" src="https://speakerdeck.com/assets/embed.js">&lt;/script>
&lt;/div>
&lt;p>&lt;strong>Table of Contents 🐳&lt;/strong>:&lt;/p></description></item><item><title>Connect to MongoDB on Dokku with your local command line or Robo3T</title><link>https://codewithhugo.com/connect-to-mongodb-on-dokku-with-your-local-command-line-or-robo3t/</link><pubDate>Mon, 31 Dec 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/connect-to-mongodb-on-dokku-with-your-local-command-line-or-robo3t/</guid><description>&lt;p>The Dokku mongo plugin provides a &lt;code>mongo:connect&lt;/code> command, which opens a tunnelled connection to you MongoDB instance on Dokku. This is how you can connect to your database using the tool of your choice, the examples will be using Robo3T.&lt;/p>
&lt;p>Table of Contents:&lt;/p></description></item><item><title>Sequelize Data Types: a practical guide</title><link>https://codewithhugo.com/sequelize-data-types-a-practical-guide/</link><pubDate>Wed, 19 Dec 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/sequelize-data-types-a-practical-guide/</guid><description>&lt;p>DataTypes are core to the Sequelize 4 library. This is a practical and complete guide to make up for the sparse Sequelize official documentation on the subject.&lt;/p>
&lt;p>First of all, DataTypes contains both the types and generators for default values: eg. &lt;code>NOW&lt;/code>, &lt;code>UUIDV1&lt;/code> and &lt;code>UUIDV4&lt;/code> are special default values.&lt;/p>
&lt;p>The places where you&amp;rsquo;re likely to encounter DataTypes are in the model field definition and in the migration definitions. Note that for a given model and field, the data type should be the &lt;em>same&lt;/em> in the model and in the migration.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Avoid null/undefined in JavaScript arrays</title><link>https://codewithhugo.com/avoiding-falsy-values-in-javascript-arrays/</link><pubDate>Thu, 13 Dec 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/avoiding-falsy-values-in-javascript-arrays/</guid><description>&lt;blockquote>
&lt;p>How to avoid &lt;code>TypeError: Cannot read property ___ of undefined&lt;/code> using &lt;code>Array.filter(Boolean)&lt;/code>&lt;/p>
&lt;/blockquote>
&lt;p>&lt;strong>tl;dr&lt;/strong>: &lt;code>Array.filter(Boolean)&lt;/code> filters out falsy values (&lt;code>null&lt;/code>, &lt;code>undefined&lt;/code>, &lt;code>0&lt;/code>, &lt;code>false&lt;/code>, &lt;code>''&lt;/code>) and avoids &lt;code>Cannot read property of undefined&lt;/code> errors.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>JavaScript array type check - “is array” vs object in-depth</title><link>https://codewithhugo.com/detecting-object-vs-array-in-javascript-by-example/</link><pubDate>Mon, 10 Dec 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/detecting-object-vs-array-in-javascript-by-example/</guid><description>&lt;blockquote>
&lt;p>Detecting Array vs Object in JavaScript with examples&lt;/p>
&lt;/blockquote>
&lt;p>&lt;strong>tl;dr&lt;/strong> To detect if something is an Array in JavaScript, use &lt;code>Array.isArray(somethingObjectToCheck)&lt;/code>.&lt;/p>
&lt;p>This post is a quarter useful, quarter history lesson, half quirky JavaScript exploration.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Technical content workflow for “Code with Hugo”: static site, Paper, Medium, DEV and Buttondown</title><link>https://codewithhugo.com/technical-content-workflow-for-code-with-hugo-static-site-paper-medium-dev-and-buttondown/</link><pubDate>Fri, 07 Dec 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/technical-content-workflow-for-code-with-hugo-static-site-paper-medium-dev-and-buttondown/</guid><description>&lt;p>&lt;strong>tl;dr&lt;/strong>: here are the tools used to power, build and publish &lt;a href="https://codewithhugo.com">codewithhugo.com&lt;/a>:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://www.dropbox.com/paper">Dropbox Paper&lt;/a> because it syncs desktop/mobile and outputs to markdown&lt;/li>
&lt;li>&lt;a href="http://unsplash.com">Unsplash&lt;/a> for cover images&lt;/li>
&lt;li>Edited Markdown + images → &lt;a href="https://gohugo.io">Hugo&lt;/a> static site generator&lt;/li>
&lt;li>Casper 2 (&lt;a href="https://ghost.org">https://ghost.org&lt;/a> default theme) for &lt;a href="http://gohugo.io">Hugo&lt;/a>&lt;/li>
&lt;li>Schedule:
&lt;ul>
&lt;li>Newsletter: Mondays 5pm UK every 2 weeks&lt;/li>
&lt;li>1+ post a week&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>Cross-post to Medium (Stories → Import a Story)&lt;/li>
&lt;li>Cross-post to &lt;a href="https://">dev.to&lt;/a> using RSS feed&lt;/li>
&lt;li>Social media/promotion &amp;lt; Organic traffic&lt;/li>
&lt;li>Newsletter powered by &lt;a href="https://buttondown.email">https://buttondown.email&lt;/a>&lt;/li>
&lt;li>Analytics: Google Analytics + Simple Analytics&lt;/li>
&lt;/ul>
&lt;p>This post has about 7 month’s worth of learning in it, so see the following table of contents:&lt;/p></description></item><item><title>Post Frequency: from prototype to production with Vue + Node</title><link>https://codewithhugo.com/post-frequency-from-prototype-to-production-with-vue-node/</link><pubDate>Mon, 26 Nov 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/post-frequency-from-prototype-to-production-with-vue-node/</guid><description>&lt;blockquote>
&lt;p>If you want to ship, use the tools you know. — A lot of people&lt;/p>
&lt;/blockquote>
&lt;p>Let&amp;rsquo;s apply that principle: I&amp;rsquo;m building &lt;a href="https://accountableblogging.com">Post Frequency/Accountable Blogging&lt;/a>,
I&amp;rsquo;m familiar with Vue and Node-based backends. Netlify makes frontend deployments trivial and
I’ve settled on Dokku for databases/backend applications hosting.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Add a Vanilla JavaScript paywall to a Hugo site using checkoutpage.co</title><link>https://codewithhugo.com/vanilla-js-hugo-paywall/</link><pubDate>Thu, 22 Nov 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/vanilla-js-hugo-paywall/</guid><description>&lt;p>How I created the buying mechanism for the &lt;a href="https://codewithhugo.com/guides/sequelize-es6-cheatsheet/">Sequelize ES6 Cheatsheet&lt;/a> using checkoutpage.co and sprinkles of vanilla JavaScript.&lt;/p>
&lt;p>This strictly &lt;em>isn’t&lt;/em> a paywall, it&amp;rsquo;s all client-side and doesn&amp;rsquo;t check much.&lt;/p>
&lt;p>In principle you can display the whole things using a bit of CSS magic. You could also read the source and reverse-engineer what query params work for this page.&lt;/p>
&lt;p>You could also spoof as Google Bot (or another crawler) and skip the pay overlay. If someone shares the link containing the &amp;ldquo;paid&amp;rdquo; query param whoever they’re sharing with will see it without paying.&lt;/p>
&lt;p>I don&amp;rsquo;t think it&amp;rsquo;s likely that people will go through the effort of doing the above reverse-engineering, so I&amp;rsquo;m going to walk anyone who will listen through how I&amp;rsquo;ve implemented it.&lt;/p>
&lt;p>If you like getting the cheatsheet for free but also want to support my work you can &lt;a href="https://checkoutpage.co/checkout/hugo/consulting-fee">Buy me a Coffee&lt;/a> or just &lt;a href="https://buttondown.email/hugo">subscribe to my mailing list&lt;/a>.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>A simple JavaScript/Node coding setup on Mac OS X</title><link>https://codewithhugo.com/a-simple-javascript/node-coding-setup-on-mac-os-x/</link><pubDate>Mon, 19 Nov 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/a-simple-javascript/node-coding-setup-on-mac-os-x/</guid><description>&lt;p>Here&amp;rsquo;s how I get productive for JavaScript/Node on Mac OS.&lt;/p>
&lt;p>It includes iTerm2, zsh, Node, Visual Studio Code and some git commands.&lt;/p></description></item><item><title>Progress and roadblocks: a journey into open-source</title><link>https://codewithhugo.com/progress-and-roadblocks-a-journey-into-open-source/</link><pubDate>Wed, 07 Nov 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/progress-and-roadblocks-a-journey-into-open-source/</guid><description>&lt;blockquote>
&lt;p>Enhancing generators for ES6 in the Sequelize CLI&lt;/p>
&lt;/blockquote>
&lt;p>This is the story of my discovery of ES6-class style model definitions with Sequelize (see &lt;a href="https://codewithhugo.com/using-es6-classes-for-sequelize-4-models/">Using ES6 classes for Sequelize 4 models&lt;/a>).
The realisation that the current Sequelize CLI model generator didn&amp;rsquo;t support it (as is expected since it&amp;rsquo;s not the most common way of doing it).
Finally, the impetus to just implement it myself.&lt;/p>
&lt;p>In other words, I started using a style of Sequelize models that the Sequelize CLI didn&amp;rsquo;t support. I didn&amp;rsquo;t get an answer on a &amp;ldquo;feature request&amp;rdquo; issue, so I just forked it, implemented it and published it.&lt;/p>
&lt;p>The Free Open Source Software movement has its ups and downs, its hype and abandoned projects.
One of the great aspects of it is that you can always (depending on the license) just fork something and publish it 🙂.&lt;/p>
&lt;p>Some links:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://github.com/HugoDF/sequelize-cli">sequelize-cli fork repo&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://www.npmjs.com/package/@hugodf/sequelize-cli">fork published on npm&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Live-tweet storms:&lt;/p>
&lt;ul>
&lt;li>&lt;a href="https://twitter.com/hugo__df/status/1057294226755592192">Day 1 (struggles)&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://twitter.com/hugo__df/status/1057671284920848385">Day 2 (fixes)&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://twitter.com/hugo__df/status/1057986197165879298">Day 3 (first publish)&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://twitter.com/hugo__df/status/1058284191673188354">Day 4 (finishing up)&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Table of contents:&lt;/p></description></item><item><title>A tiny case study about migrating to Netlify when disaster strikes at GitHub, featuring Cloudflare</title><link>https://codewithhugo.com/a-tiny-case-study-about-migrating-to-netlify-when-disaster-strikes-at-github-featuring-cloudflare/</link><pubDate>Fri, 02 Nov 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/a-tiny-case-study-about-migrating-to-netlify-when-disaster-strikes-at-github-featuring-cloudflare/</guid><description>&lt;blockquote>
&lt;p>Last Monday (22 October 2018) GitHub was going 💥, and scheduled content on Code with Hugo wasn’t going live.&lt;/p>
&lt;/blockquote>
&lt;p>For context, the blog’s setup is documented in &lt;a href="https://codewithhugo.com/switching-the-lights-on-hugo-vs-hugo-config-files/">“Switching the lights on: Hugo vs Hugo config files”&lt;/a>.&lt;/p>
&lt;p>&lt;strong>tl;dr&lt;/strong> &lt;em>(as is relevant to this post)&lt;/em>&lt;/p>
&lt;ul>
&lt;li>Cloudflare DNS + CDN + proxying + HTTPS&lt;/li>
&lt;li>GitHub Pages&lt;/li>
&lt;li>&lt;a href="https://gohugo.io">Hugo&lt;/a> as a static site generator&lt;/li>
&lt;li>CircleCI to deploy the site daily + on push (🤷‍♀️wasn’t in the post but that’s how I do it)&lt;/li>
&lt;/ul>
&lt;p>Contents of this post:&lt;/p></description></item><item><title>Simple, but not too simple: how using Zeit’s `micro` improves your Node applications</title><link>https://codewithhugo.com/simple-zeit-micro-improve-node/</link><pubDate>Mon, 22 Oct 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/simple-zeit-micro-improve-node/</guid><description>&lt;blockquote>
&lt;p>Leave the Express comfort zone to expand how you think about Node application architecture.&lt;/p>
&lt;/blockquote>
&lt;p>&lt;strong>tl;dr&lt;/strong>&lt;/p>
&lt;ul>
&lt;li>using a function composition model for building HTTP servers is awesome&lt;/li>
&lt;li>Functions as a Service are great but have some drawbacks&lt;/li>
&lt;li>&lt;code>micro&lt;/code> has a similar, simple API to FaaS but doesn&amp;rsquo;t have a compilation step&lt;/li>
&lt;li>&lt;code>micro&lt;/code>&amp;rsquo;s minimalism affects how you solve problems&lt;/li>
&lt;/ul>
&lt;p>An example comparing &lt;code>micro&lt;/code> and &lt;code>express&lt;/code> can be found at: &lt;a href="https://github.com/HugoDF/micro-vs-express-example">github.com/HugoDF/micro-vs-express-example&lt;/a>.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>6 months of Code with Hugo, in numbers</title><link>https://codewithhugo.com/6-months-of-code-with-hugo-in-numbers/</link><pubDate>Sun, 21 Oct 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/6-months-of-code-with-hugo-in-numbers/</guid><description>&lt;blockquote>
&lt;p>Going from 0 to small. A developer&amp;rsquo;s journey starting a technical blog from scratch.&lt;/p>
&lt;/blockquote>
&lt;p>Stats in this post as of 21st of October, &lt;a href="https://twitter.com/hugo__df">follow me on Twitter&lt;/a> for monthly updates.&lt;/p>
&lt;p>Contents:&lt;/p></description></item><item><title>Use ES modules/ES6 import in Node without Babel/Webpack using `esm`</title><link>https://codewithhugo.com/use-es-modules-in-node-without-babel/webpack-using-esm/</link><pubDate>Thu, 11 Oct 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/use-es-modules-in-node-without-babel/webpack-using-esm/</guid><description>&lt;p>Node has been implementing more and more ES6+ (ESNext) features natively. One of the features that is taking the longest to implement is modules. The reason for this is that Node and npm run on what is called CommonJS, with which you use &lt;code>require('module-name')&lt;/code> to import from other modules and use the &lt;code>module.exports&lt;/code> object to expose entities from a module.&lt;/p>
&lt;p>Node’s CommonJS was actually one of the first widely adopted module systems in JavaScript. The ease with which one can bundle CommonJS coupled with its widespread use in Node applications and tools means CommonJS quickly displaced RequireJS and SystemJS for frontend application dependency and module management&lt;/p>
&lt;blockquote>
&lt;p>I’m working on a “history of JavaScript module, bundling + dependency management” article, &lt;a href="https://buttondown.email/hugo">subscribe&lt;/a> to get it first).&lt;/p>
&lt;/blockquote>
&lt;p>CommonJS has some drawbacks, like being hard to statically analyse, which leads to for example bloated bundles. It’s also just not part of the ECMAScript specification, which ES modules are.&lt;/p>
&lt;p>For anyone who is still wondering, ECMAScript (or ES) modules use a syntax with &lt;code>import thing from 'my-module';&lt;/code> or &lt;code>import { something } from 'my-module'&lt;/code> to import things and &lt;code>export default&lt;/code> or &lt;code>export something&lt;/code> to expose entities from the module.&lt;/p>
&lt;p>Bundlers like Webpack, Rollup and Parcel have support for ES modules. For a Node server I’ve still tended to write in CommonJS style because Node has great support for most ESNext features out of the box (eg. rest/spread, async/await, destructuring, class, shorthand object syntax) and I don’t like messing with bundlers and transpilers.&lt;/p>
&lt;p>I’ve discovered the &lt;a href="https://github.com/standard-things/esm">esm&lt;/a> module, “Tomorrow&amp;rsquo;s ECMAScript modules today!” by &lt;a href="https://github.com/jdalton">John-David Dalton&lt;/a> (of lodash 😄). It allows you to use ES modules in Node with no compilation step. It’s small, has a small footprint and comes with some extra goodies&lt;/p>
&lt;p>What follows is some ways to use it that aren’t strictly documented. This covers use-cases like incremental adoption of ES modules (ie. convert some modules to ESM but not the whole app). Using this will help you share CommonJS and ESM modules.&lt;/p>
&lt;p>Table of Contents:&lt;/p></description></item><item><title>ObservableHQ notebooks for JavaScript demos and prototypes</title><link>https://codewithhugo.com/observablehq-notebooks-for-javascript-demos-and-prototypes/</link><pubDate>Mon, 08 Oct 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/observablehq-notebooks-for-javascript-demos-and-prototypes/</guid><description>&lt;p>&lt;a href="https://beta.observablehq.com/">ObservableHQ&lt;/a> is a platform being built by &lt;a href="https://twitter.com/mbostock">Mike Bostock&lt;/a> (creator of the D3 visualisation library), &lt;a href="https://twitter.com/jashkenas">Jeremy Ashkenas&lt;/a> (“Made CoffeeScript, Backbone.js, Underscore and other ragbag” from his &lt;a href="https://twitter.com/jashkenas">Twitter bio&lt;/a>) and &lt;a href="https://twitter.com/tmcw">Tom MacWright&lt;/a> (creator of the big presentation framework, simple-statistics and documentation.js as well as D3 contributor amongst other things).&lt;/p>
&lt;p>Observable is not just another JavaScript sandbox. It introduces the notebook paradigm to JavaScript projects, those of you familiar with Jupyter notebooks, this is the equivalent with JavaScript instead of Python. It turns out JavaScript is very well-suited to this type of paradigm.&lt;/p>
&lt;p>What’s more, JavaScript developers already have some sort of familiarity with reactivity since most frontend frameworks and view libraries build on it. It’s a different kind of reactivity, in that the library only re-renders or re-computes the section of the application that needs it (cf. Vue, Angular, React).&lt;/p>
&lt;p>You can read original post from Bostock detailing what is now ObservableHQ (then called &lt;code>d3.express&lt;/code>), &lt;a href="https://medium.com/@mbostock/a-better-way-to-code-2b1d2876a3a0">“A better way to code” on Medium&lt;/a>. In that post, the state of the art of why and how of data visualisation is explained. A “better way” is described, the “&lt;em>integrated discovery environment”&lt;/em>, or more simply “notebook”-s.&lt;/p>
&lt;p>That inception post is a 20 minute read, but here’s the &lt;strong>tl;dr:&lt;/strong>&lt;/p>
&lt;ol>
&lt;li>visualisations are usually one-offs - “&lt;em>The purpose of visualization is insight, not pictures.”&lt;/em>&lt;/li>
&lt;li>reactivity is great for building visualisations and discovering insights from data&lt;/li>
&lt;li>visual outputs help massively especially in the exploration phase&lt;/li>
&lt;li>a well-designed notebook system has the benefit of making chunks of code reusable without much effort (or re-packaging as a library)&lt;/li>
&lt;li>web-based notebooks are accessible from anywhere by anyone (if that’s wanted behaviour)&lt;/li>
&lt;/ol>
&lt;p>I especially like points 4 and 5, since they’re not a selling point for notebooks in general but for ObservableHQ’s take on the problem. What you end up with is a well-designed web-based reactive notebook environment that’s suited to prototyping.&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Impress interviewers with recursion in JavaScript with ES6 features</title><link>https://codewithhugo.com/impress-interviewers-with-recursion-in-javascript-with-es6-features/</link><pubDate>Mon, 01 Oct 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/impress-interviewers-with-recursion-in-javascript-with-es6-features/</guid><description>&lt;blockquote>
&lt;p>There’s nothing as flashy and useful for JavaScript interviews than recursion.&lt;/p>
&lt;/blockquote>
&lt;p>If you just want to be impressive with recursion in JavaScript, here are some semi real-world (technical test type) examples.&lt;/p>
&lt;p>The short definition of a recursive solution to a problem (in computer science) is: don’t use iteration. This usually means a function has to call itself with a smaller instance of the same problem until it hits a trivial case (usually defined in the problem).&lt;/p>
&lt;p>Hence, recursion is composed of a couple of steps:&lt;/p>
&lt;ol>
&lt;li>the base or terminating case, it’s the situation we want to end up in “A recursive function definition has one or more &lt;em>base cases&lt;/em>, meaning input(s) for which the function produces a result &lt;a href="https://en.wikipedia.org/wiki/Trivial_(mathematics)">trivially&lt;/a> (without recurring)” - &lt;a href="https://en.wikipedia.org/wiki/Recursion_(computer_science)#Recursive_functions_and_algorithms">Wikipedia, Recursion (computer science)&lt;/a>&lt;/li>
&lt;li>recursive case, where the program recurs (again [Wikipedia, Recursion (computer science)](http://one or more recursive cases, meaning input(s) for which the program recurs (calls itself)))&lt;/li>
&lt;/ol>
&lt;p>In this post:&lt;/p></description></item><item><title>Writing multiple Vue components in a single file</title><link>https://codewithhugo.com/writing-multiple-vue-components-in-a-single-file/</link><pubDate>Thu, 27 Sep 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/writing-multiple-vue-components-in-a-single-file/</guid><description>&lt;p>Writing multiple components in one file is a pattern from React where some files contain multiple components.&lt;/p>
&lt;p>Some of those components are &amp;ldquo;private&amp;rdquo; to the file/exported component since no other component needs to consume them.&lt;/p></description></item><item><title>Mocking/stubbing ES6 classes in tests with examples of Sequelize model unit tests</title><link>https://codewithhugo.com/unit-testing-sequelize-4-es6-class-models/</link><pubDate>Mon, 24 Sep 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/unit-testing-sequelize-4-es6-class-models/</guid><description>&lt;p>This post goes through some patterns that can be used to unit test ES6 classes.&lt;/p>
&lt;p>The examples will use Jest module auto-mocking but should be portable to other module mocking libraries (eg. Proxyquire) with some modifications.&lt;/p>
&lt;p>In &lt;a href="https://codewithhugo.com/using-es6-classes-for-sequelize-4-models/">&amp;ldquo;Using ES6 classes for Sequelize 4 models&amp;rdquo;&lt;/a> we explored how to define Sequelize models using ES6 classes.
One of the benefits of such an approach is that it allows easy unit testing, it allows you to instantiate the model without the baggage of all of Sequelize (and/or a database).&lt;/p></description></item><item><title>Why you should wrap your (JavaScript) dependencies</title><link>https://codewithhugo.com/why-you-should-wrap-your-javascript-dependencies/</link><pubDate>Mon, 17 Sep 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/why-you-should-wrap-your-javascript-dependencies/</guid><description>&lt;blockquote>
&lt;p>An email sending example.&lt;/p>
&lt;/blockquote></description></item><item><title>A gentle introduction to GraphQL API integrations</title><link>https://codewithhugo.com/a-gentle-introduction-to-graphql-api-integrations/</link><pubDate>Mon, 10 Sep 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/a-gentle-introduction-to-graphql-api-integrations/</guid><description>&lt;p>GraphQL is a great alternative to REST (or other HTTP API designs). This is an quick introduction to the core concepts around &lt;em>consuming&lt;/em> a GraphQL API.&lt;/p>
&lt;p>To see some examples consuming a GraphQL API:&lt;/p>
&lt;ul>
&lt;li>In Python, see &lt;a href="https://codewithhugo.com/python-graphql-client-requests-example-using-gql/">Python GraphQL client requests example using gql&lt;/a>&lt;/li>
&lt;li>In JavaScript (browser and Node), &lt;a href="https://buttondown.email/hugo/archive/9011fe42-c8f7-4201-8b38-d777c1f6910a">see last week&amp;rsquo;s Code with Hugo newsletter&lt;/a>&lt;/li>
&lt;/ul>
&lt;p>Table of contents:&lt;/p></description></item><item><title>JavaScript GraphQL client requests in Node and the browser using `graphql.js`</title><link>https://codewithhugo.com/javascript-graphql-client-requests-in-node-and-the-browser-using-graphql.js/</link><pubDate>Wed, 05 Sep 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/javascript-graphql-client-requests-in-node-and-the-browser-using-graphql.js/</guid><description>&lt;blockquote>
&lt;p>An example consuming a GraphQL API in JavaScript from Node and the browser using &lt;code>graphql.js&lt;/code>&lt;/p>
&lt;/blockquote>
&lt;p>See the example live: &lt;a href="https://example.codewithhugo.com/js-graphql-client-example/">https://example.codewithhugo.com/js-graphql-client-example/&lt;/a>.&lt;/p>
&lt;p>Full repo: &lt;a href="https://github.com/HugoDF/js-graphql-client-example">https://github.com/HugoDF/js-graphql-client-example&lt;/a>.&lt;/p></description></item><item><title>Build and deploy a Vue/React/Vanilla JS app with Parcel and GitHub pages</title><link>https://codewithhugo.com/build-and-deploy-a-vue/react/vanilla-js-app-with-parcel-and-github-pages/</link><pubDate>Tue, 04 Sep 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/build-and-deploy-a-vue/react/vanilla-js-app-with-parcel-and-github-pages/</guid><description>&lt;p>Parcel allows us to bundle frontend applications with no configuration and GitHub pages affords us a way to deploy them for free.&lt;/p>
&lt;p>Here&amp;rsquo;s how to use these two tools together to deploy a JavaScript application.&lt;/p>
&lt;p>Full example of a repo deployed like this: &lt;a href="https://github.com/HugoDF/js-graphql-client-example">https://github.com/HugoDF/js-graphql-client-example&lt;/a>, and see &lt;a href="https://example.codewithhugo.com/js-graphql-client-example/">https://example.codewithhugo.com/js-graphql-client-example/&lt;/a>.&lt;/p>
&lt;p>Or the repo with the demos: &lt;a href="https://github.com/HugoDF/parcel-gh-pages-deploy">https://github.com/HugoDF/parcel-gh-pages-deploy&lt;/a> and see &lt;a href="https://example.codewithhugo.com/parcel-gh-pages-deploy/">https://example.codewithhugo.com/parcel-gh-pages-deploy/&lt;/a>.&lt;/p></description></item><item><title>Change the date of a git commit</title><link>https://codewithhugo.com/change-the-date-of-a-git-commit/</link><pubDate>Wed, 29 Aug 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/change-the-date-of-a-git-commit/</guid><description>&lt;p>One of the greatest and worst things with git is that you can rewrite the history. Here’s a sneaky way of abusing that, I can’t think of a legitimate reason to do this.&lt;/p>
&lt;p>As with anything, thanks StackOverflow for all the options I can pick from 👍.&lt;/p></description></item><item><title>The step-by-step guide to create a domain redirect with Cloudflare</title><link>https://codewithhugo.com/the-step-by-step-guide-to-create-a-domain-redirect-with-cloudflare/</link><pubDate>Tue, 28 Aug 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/the-step-by-step-guide-to-create-a-domain-redirect-with-cloudflare/</guid><description>&lt;p>Redirecting a whole domain using Cloudflare can be useful but isn&amp;rsquo;t documented anywhere, here&amp;rsquo;s how to do it.&lt;/p></description></item><item><title>Python GraphQL client requests example using gql</title><link>https://codewithhugo.com/python-graphql-client-requests-example-using-gql/</link><pubDate>Mon, 27 Aug 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/python-graphql-client-requests-example-using-gql/</guid><description>&lt;blockquote>
&lt;p>An example consuming a GraphQL API from Python using gql.&lt;/p>
&lt;/blockquote>
&lt;p>Full code example at &lt;a href="https://github.com/HugoDF/python-graphql-client-example">HugoDF/python-graphql-client-example&lt;/a>.&lt;/p></description></item><item><title>Switching the lights on: Hugo vs Hugo config files</title><link>https://codewithhugo.com/switching-the-lights-on-hugo-vs-hugo-config-files/</link><pubDate>Fri, 17 Aug 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/switching-the-lights-on-hugo-vs-hugo-config-files/</guid><description>&lt;blockquote>
&lt;p>A simple Hugo blog setup&lt;/p>
&lt;/blockquote>
&lt;p>The story of getting &lt;a href="https://codewithhugo.com">codewithhugo.com&lt;/a> up and running.&lt;/p>
&lt;p>The &lt;strong>tl;dr&lt;/strong> is the following:&lt;/p>
&lt;ul>
&lt;li>I always rave about my blog setup, it’s simple, fast and just works&lt;/li>
&lt;li>I used &lt;a href="http://gohugo.io">Hugo&lt;/a>, “The world’s fastest framework for building websites”, a static site generator&lt;/li>
&lt;li>I used a theme, &lt;a href="https://github.com/eueung/hugo-casper-two">casper-two&lt;/a>, which is a Hugo port of the &lt;a href="https://ghost.org/">https://ghost.org/&lt;/a> default theme&lt;/li>
&lt;li>I deployed to GitHub pages behind Cloudflare *.&lt;/li>
&lt;/ul>
&lt;p>There&amp;rsquo;s been an update in the deployment saga, read it here: &lt;a href="https://codewithhugo.com/a-tiny-case-study-about-migrating-to-netlify-when-disaster-strikes-at-github-featuring-cloudflare/">&amp;ldquo;A tiny case study about migrating to Netlify when disaster strikes at GitHub, featuring Cloudflare&amp;rdquo;&lt;/a>&lt;/p></description></item><item><title>When to use Jest snapshot tests: comprehensive use-cases and examples 📸</title><link>https://codewithhugo.com/abusing-jest-snapshot-tests-some-nice-use-cases/</link><pubDate>Fri, 10 Aug 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/abusing-jest-snapshot-tests-some-nice-use-cases/</guid><description>&lt;p>There&amp;rsquo;s some nice use-cases for snapshot tests outside of the well-travelled React/Vue UI component ones.&lt;/p>
&lt;p>In other words, although React and Vue testing with snapshots is pretty well documented, that&amp;rsquo;s not the only place they&amp;rsquo;re useful.&lt;/p>
&lt;p>As a rule of thumb, you could replace a lot of unit tests that assert on with specific data with snapshot tests.&lt;/p>
&lt;p>We have the following pros for snapshot tests:&lt;/p>
&lt;ul>
&lt;li>the match data is stored in a separate file so it&amp;rsquo;s harder to lose track of things, eg. being skimmed over during review&lt;/li>
&lt;li>it&amp;rsquo;s a lot less effort to change than inline data matching, just run &lt;code>npx jest -u&lt;/code> and all snapshots get updated.&lt;/li>
&lt;/ul>
&lt;p>The following cons also come to mind:&lt;/p>
&lt;ul>
&lt;li>it&amp;rsquo;s a lost less effort to change than inline data matching, which means people need to pay attention to changes in snapshot files&lt;/li>
&lt;li>despite community efforts, the only major test library that supports out of the box is Jest (which locks you into that ecosystem)&lt;/li>
&lt;/ul>
&lt;p>That makes it particularly well-suited for a couple of areas:&lt;/p></description></item><item><title>Deploy to multiple environments with git and CircleCI</title><link>https://codewithhugo.com/deploy-to-multiple-environments-with-git-and-circleci/</link><pubDate>Wed, 01 Aug 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/deploy-to-multiple-environments-with-git-and-circleci/</guid><description>&lt;p>Easily deploying to multiple environments in a simple manner using GitHub, CircleCI and Heroku.&lt;/p>
&lt;p>Continuous Integration is awesome, but sometimes you need a buffer between auto-deploying things on merge and the production release. To do that with CircleCI requires some git branch-wrangling and a few lines of bash scripting.
We’ll imagine a scenario where a deploy is trivial (ie. we’ll pretend we’re using Heroku). For more complicated build steps we should still be able to follow similar principles.
This is not a CircleCI 2.0 workflows tutorial, it’s more of a git-flow/CircleCI hybrid to have 2 (or more) environments being released to and automatically deployed by CircleCI.&lt;/p>
&lt;p>I would like to thanks Chris Fidao, and &lt;a href="https://twitter.com/fideloper/status/1020330385333530624">this tweet&lt;/a>:&lt;/p>
&lt;p>&lt;img src="https://codewithhugo.com/img/2018-07-30-chris-fidao-tweet-screenshot.jpg" alt="🔥 Git is great, but not for what Linus probably intended. Despite its intentions, git(hub|lab|bucket) is actually used for: 1. backup (and distribution) 2. automation 3. release management 4. a hundred other things &amp;hellip;.. xxx. version control https://t.co/GLUYkppSLs &amp;amp;mdash; Chris Fidao (@fideloper) https://twitter.com/fideloper/status/1020330385333530624 20 July 2018">&lt;/p>
&lt;p>We&amp;rsquo;ll go through how to use GitHub + CircleCI for deployment automation and release management.&lt;/p></description></item><item><title>Install just redis-cli on Ubuntu, Debian, Jessie</title><link>https://codewithhugo.com/install-just-redis-cli-on-ubuntu-debian-jessie/</link><pubDate>Tue, 24 Jul 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/install-just-redis-cli-on-ubuntu-debian-jessie/</guid><description>&lt;p>As part of adding integration tests to an app on CircleCI I ran into the following issues:&lt;/p>
&lt;ul>
&lt;li>&lt;code>redis-cli&lt;/code>&amp;rsquo;s API has changed from Redis CLI versions 2 to 3 to 4
&lt;ul>
&lt;li>ie. this works in v4 &lt;code>redis-cli -u ${REDIS_URL}&lt;/code> but doesn’t in v2&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>the &amp;ldquo;only way&amp;rdquo; to install &lt;code>redis-cli&lt;/code> is through a &lt;code>redis-tools&lt;/code> or &lt;code>redis-server&lt;/code> install and I &lt;em>only&lt;/em> need the Redis CLI not the server or any other tools.&lt;/li>
&lt;/ul>
&lt;p>What follows is how &lt;em>not&lt;/em> to install &lt;code>redis-cli&lt;/code> and then how to install &lt;code>redis-cli&lt;/code> latest, properly.&lt;/p></description></item><item><title>Bring Redux to your queue logic: an Express setup with ES6 and bull queue</title><link>https://codewithhugo.com/bring-redux-to-your-queue-logic-an-express-setup-with-es6-and-bull-queue/</link><pubDate>Sat, 21 Jul 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/bring-redux-to-your-queue-logic-an-express-setup-with-es6-and-bull-queue/</guid><description>&lt;p>There always comes a point in a web application&amp;rsquo;s life where an operation is best served in the background, this is where queues come in.&lt;/p>
&lt;p>There are a few queuing solutions in Node. None of them are ridiculously dominant, eg. &lt;a href="https://github.com/Automattic/kue">Kue&lt;/a>, &lt;a href="https://github.com/smrchy/rsmq">RSMQ&lt;/a>, &lt;a href="https://github.com/bee-queue/bee-queue">Bee Queue&lt;/a>, &lt;a href="https://github.com/OptimalBits/bull">bull&lt;/a>.
The issue with Kue, RSMQ and Bee Queue was its use of a &lt;code>done&lt;/code> callback as the recommended API.&lt;/p>
&lt;p>Bull &lt;a href="https://github.com/OptimalBits/bull">https://github.com/OptimalBits/bull&lt;/a> is a premium Queue package for handling jobs and messages in NodeJS. It’s backed by Redis and is pretty feature-rich. Most of all, it leverages a Promise-based processing API which means &lt;code>async/await&lt;/code>.&lt;/p>
&lt;p>We’ll walk through an application that sends webhooks with a given payload to a set of URLs.&lt;/p>
&lt;p>You can find the full code content at &lt;a href="https://github.com/HugoDF/express-bull-es6">https://github.com/HugoDF/express-bull-es6&lt;/a>.&lt;/p></description></item><item><title>ES6 by example: a module/CLI to wait for Postgres in docker-compose</title><link>https://codewithhugo.com/es6-by-example-a-module/cli-to-wait-for-postgres-in-docker-compose/</link><pubDate>Wed, 11 Jul 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/es6-by-example-a-module/cli-to-wait-for-postgres-in-docker-compose/</guid><description>&lt;p>When using docker-compose, it’s good practice to make anything that relies on Postgres wait for it to be up before launching. This avoids connection issues inside the app.&lt;/p>
&lt;p>This post walks through how to deliver this functionality both as a CLI and a module that works both as a CommonJS module (&lt;code>require&lt;/code>) and ES modules, &lt;strong>without transpilation&lt;/strong>.&lt;/p>
&lt;p>“A fast, production ready, zero-dependency ES module loader for Node 6+!” is &lt;a href="https://github.com/standard-things/esm">esm&lt;/a>’s promise. From this sample project, it’s worked.&lt;/p></description></item><item><title>Better HTTP Polling in JavaScript with RxJS 5</title><link>https://codewithhugo.com/better-http-polling-with-rxjs-5/</link><pubDate>Mon, 09 Jul 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/better-http-polling-with-rxjs-5/</guid><description>&lt;p>Here is the implementation for a function that takes a Promise-based fetch function (eg. using axios) and a function that decides if a response has been successful (from a polling perspective) and polls the fetch function until the &lt;code>isSuccess&lt;/code> function returns &lt;code>true&lt;/code>:&lt;/p></description></item><item><title>Responsive, semantic images with CSS</title><link>https://codewithhugo.com/responsive-semantic-images-with-css/</link><pubDate>Thu, 05 Jul 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/responsive-semantic-images-with-css/</guid><description>&lt;blockquote>
&lt;p>CSS tip: &lt;code>object-fit&lt;/code> your images.&lt;/p>
&lt;/blockquote>
&lt;p>To have an image that doesn&amp;rsquo;t try to stretch to its width/height the classic CSS is as follows:&lt;/p></description></item><item><title>Testing an Express app with SuperTest, moxios and Jest</title><link>https://codewithhugo.com/testing-an-express-app-with-supertest-moxios-and-jest/</link><pubDate>Wed, 27 Jun 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/testing-an-express-app-with-supertest-moxios-and-jest/</guid><description>&lt;p>Testing is a crucial part of the software development process.
It helps to catch bugs, avoid regressions and to document the behaviour of a piece of software.&lt;/p>
&lt;p>Express is one of the most widespread libraries for building backend applications in JavaScript.
What follows is a summary of how to set up an efficient unit testing strategy for such an application as well as
a couple of situations you may be faced with when attempting to test.&lt;/p>
&lt;p>Full code example can be found at &lt;a href="https://github.com/HugoDF/express-supertest-moxios">https://github.com/HugoDF/express-supertest-moxios&lt;/a>.&lt;/p></description></item><item><title>How to make beautiful, simple CLI apps with Node</title><link>https://codewithhugo.com/how-to-make-beautiful-simple-cli-apps-with-node/</link><pubDate>Wed, 20 Jun 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/how-to-make-beautiful-simple-cli-apps-with-node/</guid><description>&lt;p>Channel your inner Sindre Sohrus and ship a beautifully simple CLI app using Node.&lt;/p>
&lt;p>Command line apps are a neat way to package repetitive tasks. This will walk you through some tools
that are useful to build CLI apps.&lt;/p></description></item><item><title>A Docker/docker-compose setup with Redis and Node/Express</title><link>https://codewithhugo.com/setting-up-express-and-redis-with-docker-compose/</link><pubDate>Wed, 06 Jun 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/setting-up-express-and-redis-with-docker-compose/</guid><description>&lt;p>Redis and Express are tools that provide a simple and clean approach to their problem domains. What follows is a Docker/Docker Compose v2 setup.&lt;/p>
&lt;p>The repo is available at: &lt;a href="https://github.com/HugoDF/express-redis-docker">https://github.com/HugoDF/express-redis-docker&lt;/a>.&lt;/p>
&lt;p>The &lt;code>docker-compose.yml&lt;/code> is available at: &lt;a href="https://github.com/HugoDF/express-redis-docker/blob/master/docker-compose.yml">github.com/HugoDF/express-redis-docker/docker-compose.yml&lt;/a>&lt;/p>
&lt;p>&lt;a href="https://redis.io/">Redis&lt;/a> is “an open source, in-memory data structure store, used as a database, cache and message broker”. It’s as simple and unopinionated as a database as it gets, it’s known for its performance and simplicity as a key-value store. It has great flexibility and can also be used as a message queue, circular buffer (and pretty much anything else a developer can come up with short of a relational database).&lt;/p>
&lt;p>&lt;a href="https://expressjs.com/">Express&lt;/a> is a “fast, unopinionated, minimalist web framework for Node.js”. In other words, it’s a wrapper around Node’s server, that provides a way to write what’s called “middleware” to share and compose functionality across HTTP endpoints as well as define said endpoints.&lt;/p>
&lt;p>Getting Redis and Express to work together is trivial. Getting Redis and Express to work together in a way that’s fool and future-proof, and reproducible across local and deployed environments, is slightly harder. That’s where Docker and Docker Compose come in.&lt;/p>
&lt;p>Docker is a containerisation system, Docker Compose is a way to define how multiple Docker containers interact. In the context of Node web application development, Docker tends to be used to define a container that has the required system-level dependencies (eg. Node version, any extra database drivers). Docker Compose would be used to define dependencies outside of the Node application, for example databases.&lt;/p></description></item><item><title>From AngularJS to Vue.js, CommonJS and Jest</title><link>https://codewithhugo.com/from-angularjs-to-vue.js-commonjs-and-jest/</link><pubDate>Wed, 30 May 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/from-angularjs-to-vue.js-commonjs-and-jest/</guid><description>&lt;blockquote>
&lt;p>The trials and tribulations of kicking off an AngularJS -&amp;gt; Vue.js migration&lt;/p>
&lt;/blockquote>
&lt;p>AngularJS was pretty groundbreaking. It’s still impressive to this day, packed with a router, an HTTP client, a dependency injection system and a bunch of other things I haven’t necessarily had the pleasure of dealing with. It also wraps most browser APIs as injectable services, that’s pretty cool. The downside is that it’s complicated: services, filters, injectors, directives, controllers, apps, components.&lt;/p>
&lt;p>With the tools we have today, AngularJS isn’t as strong a way to do things &lt;em>any more&lt;/em>. The fact that it’s in maintenance mode says it all. For a new application or new features, ie. for a project that &lt;em>not&lt;/em> in maintenance mode, it’s doesn’t have the same niceties as other frameworks and libraries like Angular 2+, React or Vue. There should be a way &lt;em>not&lt;/em> to rewrite the whole AngularJS application at once, here&amp;rsquo;s how I went about doing it:&lt;/p></description></item><item><title>Split an existing git commit</title><link>https://codewithhugo.com/split-an-existing-git-commit/</link><pubDate>Wed, 23 May 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/split-an-existing-git-commit/</guid><description>&lt;p>One of &lt;code>git&lt;/code>&amp;rsquo;s main differences when compared to other version control systems is that it lets the user rewrite the history. The main way to do this is to use &lt;code>git rebase&lt;/code>, usually followed by a &lt;code>git push&lt;/code> &lt;code>--``force&lt;/code> to overwrite history the remote with the local history.&lt;/p>
&lt;p>Here’s a look at how to split existing commits using &lt;code>rebase&lt;/code>, &lt;code>reset&lt;/code> and &lt;code>commit&lt;/code>.&lt;/p></description></item><item><title>A simple, real-world Vue.js directive example</title><link>https://codewithhugo.com/a-simple-real-world-vue.js-directive/</link><pubDate>Tue, 15 May 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/a-simple-real-world-vue.js-directive/</guid><description>&lt;p>&lt;a href="https://vuejs.org/">Vue.js&lt;/a> is &amp;ldquo;The Progressive JavaScript Framework&amp;rdquo;. It takes inspiration from all prior art in the view library and frontend framework world, including AngularJS, React, Angular, Ember, Knockout and Polymer.
In Vue (and Angular/AngularJS), a directive is a way to wrap functionality that usually applies to DOM elements. The example in Vue&amp;rsquo;s documentation is a &lt;code>focus&lt;/code> directive.&lt;/p>
&lt;p>When running Vue.js inside of AngularJS, an issue occured whereby the AngularJS router would try to resolve
normal anchors&amp;rsquo; &lt;code>href&lt;/code> on click.
The &lt;code>href&lt;/code>s weren&amp;rsquo;t AngularJS URLs so it would fall back to the default page.
One solution could have leveraged components to update &lt;code>window.location&lt;/code> directly, but here&amp;rsquo;s a nifty directive to do the same:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">a&lt;/span> &lt;span class="na">v-href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;&amp;#39;/my-url&amp;#39;&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>Go&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">a&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>That&amp;rsquo;s a pretty cool API, and it&amp;rsquo;s probably more idiomatic Vue than:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-html" data-lang="html">&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;&lt;/span>&lt;span class="nt">MyAnchor&lt;/span> &lt;span class="na">href&lt;/span>&lt;span class="o">=&lt;/span>&lt;span class="s">&amp;#34;/my-url&amp;#34;&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> Go
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">&amp;lt;/&lt;/span>&lt;span class="nt">MyAnchor&lt;/span>&lt;span class="p">&amp;gt;&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>There were a couple of gotchas:&lt;/p></description></item><item><title>Ditch syntax highlighting for better technical writing</title><link>https://codewithhugo.com/ditch-syntax-highlighting-for-better-technical-writing/</link><pubDate>Wed, 25 Apr 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/ditch-syntax-highlighting-for-better-technical-writing/</guid><description>&lt;p>Ditching syntax highlighting leads to more concise snippets,
more thought to be put to find a minimal example
and better overall flow in your writing.&lt;/p></description></item><item><title>React vs Angular vs Vue in 2019: How components won the “framework wars”</title><link>https://codewithhugo.com/how-components-won-the-framework-wars/</link><pubDate>Sun, 22 Apr 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/how-components-won-the-framework-wars/</guid><description>&lt;p>React, Vue or Angular: Why it doesn’t matter.&lt;/p>
&lt;blockquote>
&lt;p>2018 marked the end of JavaScript fatigue and the React vs Vue vs Angular (“framework wars”) debate&lt;/p>
&lt;/blockquote>
&lt;p>A typical frontend/JavaScript developer career usually involves some jQuery and associated plugins before moving on to React, Angular or Vue.&lt;/p>
&lt;p>Having experienced React, Vue and Angular, it seems they solve similar problems in a similar way. Where they differ is the setup experience and best-practices. In more ways than one, the mental model of these frameworks/libraries have converged to the component model.&lt;/p>
&lt;p>This is a win for the developers since the mental model is the same for all the most widespread frameworks. Which means going from one to the other does not pose as big a challenge as it used to.&lt;/p>
&lt;p>If you came here to read a more thorough review and have more background to the Vue, React and Angular ecosystems, I recommend the following from 2017:&lt;/p>
&lt;p>&lt;a href="https://medium.com/unicorn-supplies/angular-vs-react-vs-vue-a-2017-comparison-c5c52d620176">&lt;strong>Angular vs. React vs. Vue: A 2017 comparison&lt;/strong>&lt;/a>&lt;/p>
&lt;p>Table of contents:&lt;/p></description></item><item><title>Animations: CSS vs JavaScript</title><link>https://codewithhugo.com/animations-css-vs-javascript/</link><pubDate>Sun, 21 Jan 2018 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/animations-css-vs-javascript/</guid><description>&lt;blockquote>
&lt;p>When should you use JavaScript when you can use CSS?&lt;/p>
&lt;/blockquote>
&lt;p>This post was sparked by a question I answered on Quora:
&lt;a href="https://www.quora.com/Which-is-better-to-use-CSS-or-JS">&lt;strong>Which is better to use, CSS or JS?&lt;/strong>&lt;/a>&lt;/p>
&lt;p>The web frontend is built with the triumvirate of HTML, CSS and JavaScript (see &lt;a href="https://hackernoon.com/in-simple-terms-backend-code-frontend-code-and-how-they-interact-2485c5a1bbd2">“Backend code, frontend code and how they interact”&lt;/a> for more details): HTML says what things are, CSS how they look and JavaScript does all the things the other two can’t.&lt;/p>
&lt;p>Seeing it from this light the answer to the question: “When should you use JavaScript when you can use CSS?” is &lt;strong>never&lt;/strong>.&lt;/p>
&lt;p>HTML, CSS and JavaScript do overlap especially since HTML5 and CSS3. Animations before CSS3 used to be the premise of the JavaScript and complex animation orchestration still is with libraries like GSAP, but nowadays we can also animate using the transition CSS rule.&lt;/p>
&lt;p>This leads us to formulate some rules around using JavaScript in the browser:&lt;/p>
&lt;ul>
&lt;li>if you can do something with CSS over JavaScript, do it&lt;/li>
&lt;li>if you can do something with HTML over JavaScript, do it&lt;/li>
&lt;/ul></description></item><item><title>Learn git even if you don’t code</title><link>https://codewithhugo.com/learn-git-even-if-you-dont-code/</link><pubDate>Mon, 04 Dec 2017 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/learn-git-even-if-you-dont-code/</guid><description>&lt;p>The tools we wield define us, git is the one tool all developers use&lt;/p></description></item><item><title>In simple terms: code on the backend, frontend and how they interact</title><link>https://codewithhugo.com/in-simple-terms-code-on-the-backend-frontend-and-how-they-interact/</link><pubDate>Fri, 31 Mar 2017 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/in-simple-terms-code-on-the-backend-frontend-and-how-they-interact/</guid><description>&lt;p>A look at the shifting boundaries of the web development stack&lt;/p>
&lt;p>This was originally posted as an answer on Quora:
&lt;a href="https://www.quora.com/How-does-a-frontend-code-and-a-backend-code-interact-with-each-other/">&lt;strong>How do frontend code and backend code interact with each other?&lt;/strong>&lt;/a>&lt;/p>
&lt;p>Let’s start with quick definitions followed by state of the art best practices.&lt;/p></description></item><item><title>AVA, low-config testing for JavaScript</title><link>https://codewithhugo.com/ava-low-config-testing-for-javascript/</link><pubDate>Mon, 13 Mar 2017 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/ava-low-config-testing-for-javascript/</guid><description>&lt;p>A JavaScript testing library for 2017&lt;/p>
&lt;p>&lt;a href="https://github.com/avajs/ava">AVA&lt;/a> is a Futuristic JavaScript test runner.&lt;/p>
&lt;p>Some of its best features are:&lt;/p>
&lt;ul>
&lt;li>
&lt;p>it works out of the box, no need to specify a blob for test files or add Babel hooks&lt;/p>
&lt;/li>
&lt;li>
&lt;p>it runs tests in parallel, this stops you from using global state and runs faster&lt;/p>
&lt;/li>
&lt;li>
&lt;p>the tests are async by default&lt;/p>
&lt;/li>
&lt;/ul>
&lt;p>I quite like AVA since I can just drop it in and reap benefits without too much hassle. I’ve never had to fight against AVA to get tests running, that means more focus on the code and tests and less on the setup.&lt;/p></description></item><item><title>First-class and Higher Order Functions: Effective Functional JavaScript</title><link>https://codewithhugo.com/first-class-and-higher-order-functions-effective-functional-javascript/</link><pubDate>Mon, 23 Jan 2017 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/first-class-and-higher-order-functions-effective-functional-javascript/</guid><description>&lt;p>Functions: the killer JavaScript feature we never talk about.&lt;/p>
&lt;p>JavaScript is a very in-demand language today. It runs in a lot of places from the browser to embedded systems and it brings a non-blocking I/O model that is faster than others for some types of applications. What really sets JavaScript apart from the rest of scripting languages is its highly functional nature.&lt;/p>
&lt;blockquote>
&lt;p>JavaScript has more in common with functional languages like &lt;a href="http://javascript.crockford.com/little.html">Lisp or Scheme&lt;/a> than with C or Java. — Douglas Crockford in &lt;a href="http://javascript.crockford.com/javascript.html">JavaScript: the World’s Most Misunderstood Language&lt;/a>&lt;/p>
&lt;/blockquote>
&lt;p>It’s a widely publicised fact that Brendan Eich, when creating the first version of JavaScript, was actually told he would be able to build a &lt;a href="https://en.wikipedia.org/wiki/Lisp_(programming_language)">Lisp, a list processing language like Scheme or common Lisp&lt;/a>. It turns out that Sun Microsystems had other ideas and this programming language needed to wear a coat of procedural syntax inherited from a C or Java-like language. Despite all the semicolons, at its heart JavaScript remains a functional language with features such as first-class functions and closures.&lt;/p>
&lt;p>This post will focus on first-class functions and higher order functions. To read more about closures and how to leverage them to wield JavaScript’s functional powers, I’ve written the&lt;a href="https://hackernoon.com/ingredients-of-effective-functional-javascript-closures-partial-application-and-currying-66afe055102a"> following post&lt;/a>:
&lt;a href="https://hackernoon.com/ingredients-of-effective-functional-javascript-closures-partial-application-and-currying-66afe055102a">&lt;strong>Ingredients of Effective Functional JavaScript: Closures, Partial Application and Currying&lt;/strong>
*To use JavaScript to its full potential you have to embrace its strong functional programming base.*hackernoon.com&lt;/a>&lt;/p></description></item><item><title>Closures, Partial Application and Currying: Effective Functional JavaScript</title><link>https://codewithhugo.com/closures-partial-application-and-currying-effective-functional-javascript/</link><pubDate>Thu, 12 Jan 2017 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/closures-partial-application-and-currying-effective-functional-javascript/</guid><description>&lt;p>Spices and other ingredients of a great curry&lt;/p>
&lt;p>To use JavaScript to its full potential you have to embrace its strong functional programming base. We’re going to explore some crucial and powerful functional constructs: closures, partial application and currying that make JavaScript terse yet understandable.&lt;/p></description></item><item><title>10 minute JavaScript: Library development in ES6 with Babel, Mocha and npm scripts</title><link>https://codewithhugo.com/10-minute-javascript-library-development-in-es6-with-babel-mocha-and-npm-scripts/</link><pubDate>Mon, 24 Oct 2016 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/10-minute-javascript-library-development-in-es6-with-babel-mocha-and-npm-scripts/</guid><description>&lt;blockquote>
&lt;p>Warning: the ecosystem has moved on since this post was originally published, it’s now easier than ever to set up a library for development:
see &lt;a href="https://codewithhugo.com/writing-an-npm-module-with-typescript-and-microbundle/">Writing an npm module with microbundle&lt;/a> for an up to date view on how to develop a library&lt;/p>
&lt;/blockquote>
&lt;p>JavaScript has a thriving ecosystem of libraries delivered as packages on npm. Node has a high degree of compatibility with ES6 but it doesn’t have some features yet, namely import. This means that if we want to distribute useable packages over npm and we want to be writing ES6, we have to transpile back to ES5.&lt;/p>
&lt;p>We’ll be setting up a project that allows us to write ES6, and distribute it in ES5 (with Babel) and to test it with Mocha, all through npm scripts. We’ll also discuss some workflows that we can use to speed up our development.&lt;/p>
&lt;p>&lt;strong>TL;DR&lt;/strong> Use this package.json, create a src folder, add an index.js and some .js and .test.js files inside it and off you go. You can also check out &lt;a href="https://github.com/HugoDF/recursive-js">https://github.com/HugoDF/recursive-js&lt;/a>, it’s a decent example project. :)&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-json" data-lang="json">&lt;span class="line">&lt;span class="cl">&lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;name&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;library-js&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;version&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;1.0.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;description&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Starter for library development with ES6, Babel, Mocha and npm scripts&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;main&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;./dist/index.js&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;scripts&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;start&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm run dev&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;dev&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm test -- -w&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;init&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;mkdir dist&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;clean&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;rm -rf dist&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;prebuild&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm run clean &amp;amp;&amp;amp; npm run init&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;build&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;babel ./src -d ./dist --ignore test.js&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;pretest&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;npm run build&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;test&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;mocha --compilers js:babel-core/register ./src/**/*.test.js&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;test:single&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;mocha --compilers js:babel-core/register&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;author&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;Hugo Di Francesco&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;devDependencies&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">{&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;babel-cli&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;^6.16.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;babel-core&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;^6.17.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;babel-preset-es2015&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;^6.16.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;chai&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;^3.5.0&amp;#34;&lt;/span>&lt;span class="p">,&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;mocha&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="s2">&amp;#34;^3.1.2&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">},&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="nt">&amp;#34;files&amp;#34;&lt;/span>&lt;span class="p">:&lt;/span> &lt;span class="p">[&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="s2">&amp;#34;dist&amp;#34;&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> &lt;span class="p">]&lt;/span>
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl">&lt;span class="p">}&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div></description></item><item><title>Recursion in JavaScript with ES6, destructuring and rest/spread</title><link>https://codewithhugo.com/recursion-in-javascript-with-es6-destructuring-and-rest/spread/</link><pubDate>Mon, 10 Oct 2016 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/recursion-in-javascript-with-es6-destructuring-and-rest/spread/</guid><description>&lt;p>The latest ECMA standard for JavaScript (ECMAScript 6) makes JavaScript more readable by encouraging a more declarative style with functional constructs and new operators.&lt;/p></description></item><item><title>Why CSS is hard</title><link>https://codewithhugo.com/why-css-is-hard/</link><pubDate>Sat, 18 Jun 2016 00:00:00 +0000</pubDate><guid>https://codewithhugo.com/why-css-is-hard/</guid><description>&lt;p>Things we’re not used to tend to be harder&lt;/p>
&lt;p>I’m a developer, and I CSS. I probably have written more CSS than I have written Java, Ruby, PHP, Python and JavaScript combined.&lt;/p>
&lt;p>Being a front-end developer is a dying art. By front-end I don’t mean ES6, Babelifyied React SPAs. I mean meat and potatoes: “here’s a design, turn it into a website please”.&lt;/p>
&lt;p>CSS doesn’t get the love it deserves in the developer crowd. I think I know why that is: it’s hard. Especially coming from the fairy land that is Java/JavaScript/Ruby/PHP/&lt;em>insert language&lt;/em>.&lt;/p>
&lt;p>Now I’m not saying all these languages are &lt;em>easy&lt;/em> by any stretch but CSS always throws up issues that stump me… for a good while.&lt;/p></description></item></channel></rss>