Get User Country and Region on Browser With JavaScript Only

Without Using IP Address

Detecting the country and region of the visiting user on browser is certainly possible using JavaScript alone, without using any third party geolocation service such as ipstack. The only consideration: It may not be accurate, and depend on the timezone selected by the user on their system, which can be changed. But since hardly anybody go about changing their system-auto-detected timezone nowadays (except for testing, which I have shown below), I believe it is a good enough and cheap detection method for non-critical use cases.

[Read More]

JSON Dictionary for Mapping Timezone Cities to Countries

In my blog post get user country and region on browser with JavaScript only, I explained how to generate and use a dictionary to map timezone cities to their countries. Below are the complete JSON dictionaries, 1. cities (region part removed) to countries and 2. [Read More]
json 

JavaScript Generate Your Own Random Number Without Math Random

A Custom Code for Finding Pseudo-generated Number

We know that there is no true random number. The best we get is pseudo-random number, which comes from a seed value. I was wondering if I could get a random number without using Math.random() function of JavaScript. So I developed an algorithm of my own, which is a few lines of code that works by keeping, updating, and shifting a seed array in the state. [Read More]

Compare Two JavaScript Objects and Get the Updated Keys

Where a Value of the Key Has Changed

We have two similar JavaScript objects, and we are interested in finding which of the key(s), if any, have changed at the first level. A key in the object could hold any data type in its value, including array and object, and it could also be deeply nested. The change could mean anything, such as added, added to, removed, removed from, modified, or shuffled etc.

This kind of key identification might be required in cases where you need to compare the old and new state, such as previous and new filters, and figure out what exactly changed.

[Read More]

Storybook Story or Control?

Storybook is UI development framework that lets you develop UI components in isolation. To showcase the multiple states of the component, it provides two options: add a story add a control. Both represents the state of the component. A story is the state you can choose from the left panel under the listed component. [Read More]

JavaScript Token Bucket Algorithm

And its Possible Uses

Token Bucket is a rate-limiting algorithm in which there is a bucket that gets refilled at a particular rate with tokens. To perform an action, one or more tokens can be taken out or redeemed from this bucket. When there are no more tokens left, the action cannot be performed until the bucket is refilled and further tokens are available. [Read More]

JavaScript: Change The Behavior of A Class Method At Runtime

Useful Strategy To Perform Different Operations Without Repeated If Else Or Switch Statements

Recently I came across a problem where I needed to perform two different sets of operations throughout the application for two types of users. The trouble was:

  1. I needed to turn on or off an operation based on certain condition, which would only be known once the user was logged in, not at the beginning.
[Read More]

How to Publish and Use a Private Javascript Library Without Npm Registry?

With the Help of Github Repository and Version Tags

Node Package Manager, or NPM, is a defacto registry of JavaScript libraries, where anyone can create and publish a library (called package), both public and private. For publishing private packages, subscription fee is required.

In this post, we will discuss how we can host our public or private JavaScript library at Github, maintain various versions of the library using tags, and, finally, include a specific version of the library in some project and use its code.

[Read More]