Find user's country without Geolocation and IP Lookup

I have this nagging problem of determining an end user’s country/region in my web apps. The reasons for doing that are many - Automatic conversion of an event date/time Display regional news Show prices in user’s currency .. and so on We could do two things to determine a user’s region - Use geo location Use external services to look up the IP Option (1) is ideal. It is quite easy - thanks to HTML5. ...

Multiple Ways to Define Functions in Javascript

We have looked at functions quite a bit on this blog over the last two years. We have seen many avatars of a function including a function object, a function’s method, function of functions, and so on. But, what we have not done is to document all the ways in which we can define functions.. so here it is. :) Functions in Javascript Function is a reusable body of code. Let’s say we have to add two numbers. You can write some simple code like so.. ...

Using Nullish Coalescing Operator

ES2020 introduced nullish coalescing operator. We will see just how and where can you find use for it? What it is? Nullish coalescing operator (??) is introduced in the newest Javascript specification. It’s primary design goal is quite simple - provide a way to check for nulls to return true or false values from an expression. The operator follows in the footsteps of shorcircuitinng operators && and || to provide developers with a reliable way of checking for - ...

Javascript - A Beginner Guide

Here’s a good guide to get you started on Javascript in a practical way. Why another guide? Practical - don’t learn for the sake of learning, learn for the sake of doing! Tactical - learn what matters, fast (yes, that’s what everyone says) Strategic - see the big picture This guide will get you started from the beginning, graduate you to using industry standard tools, and point you towards the right direction for learning even more than what can be offered in one guide or course. ...

Fastode Server Framework

Fastode is an experimental boilerplate / framework based on Fastify. Why another boilerplate? Well, I just wanted to experiment with technologies to create MVP products. Although I have used NestJS, AdonisJS and friends before, those frameworks, while being great, bring their structure and overhead to any project. I wanted to check how difficult or easy it is to just start with a bunch of basic features and leave everything else to the project. ...

Use Packages Locally Without Publishing Them

How can you use packages being developed locally in other projects - without publishing them to npm repository? NPM Packages Make Life Exciting NPM makes it really easy to create packages and reuse them across projects. I am not even talking about big, useful libraries like Vue, Loadash etc. - just the routine problems across our projects. For example - I use a module to validate a bunch of things and start user subscription for an app I use similar starter theme for sites across projects It is easier to create NPM packages and reuse them instead of copying the code everywhere and starting the great struggle for its maintenance. ...

Refer Project Folder from NPM Package

You can refer to other files/folders using a simple require or using basic node libraries. But, how can you do that with npm packages? The Problem When creating a bunch of Javascript files, here’s what you can do to refer other files - const awe = require("../plugins/awesome.js"); This fetches awesome.js from the plugins folder in the parent. You can do a more robust solution with - const awe = require(path.join(__dirname, "../", "plugins")); __dirname fetches the current directory, and you just join that with the path to reach to the file or folder. ...

Fastify and its plugins are great

I had been meaning to use more of Fastify for work, but only made that a reality recently. So far I have had a great experience on the server framework. What is Fastify? Fastify is a low-overhead server framework built on top of NodeJS. Fastify was started in late 2016 and jumped to v1.0 in Q1,2018. So - it’s a comparatively younger framework. But that has not stopped it from having great ambitions. ...

npm run does nothing at Svelte startup

I was getting back to Svelte for a small project and ran into a strange issue. npm run dev did not run the svelte app as expected - no output or errors. First I create a new project. npx degit sveltejs/template awesome-project-1 cd awesome-project-1 npm i npm run dev And, I get greeted with nothing. I did not have any errors or output to debug. tldr; Set config ignore-scripts to false and retry. ...

Asserts are not just for testing

Assert is a general-purpose validator and should not be used in the context of testing alone. An assert in Javascript simply checks for truthy values and produces an error otherwise. We use it quite a bit in automated testing when checking for correctness of output for pre-defined inputs. We use asserts like so - var assert = require("assert"); function getSum(x, y) { return x + y; } assert(getSum(1, 2) === 3); assert(getSum(3, 6) == 9); The above code block will go through fine since there are no errors, but I am well capable of producing a few errors. ...