Skip to content

Instantly share code, notes, and snippets.

Avatar

Saqib Ameen saqibameen

View GitHub Profile
@saqibameen
saqibameen / notes.js
Created Aug 10, 2020
if conditions in javascript
View notes.js
// 0 and '' are evaluated to false in JS.
let x = 0;
if(x) {
...
} else {
// Will come here
}
let str = '';
@saqibameen
saqibameen / creating-objects.js
Created Aug 7, 2020
Use computed keys in objects while creating them.
View creating-objects.js
// In ES5.
function createObjWithKey(key) {
let newObj = {};
newObj.[key] = 1;
return newObj;
}
// In ES6
function createObjWithKey(key) {
@saqibameen
saqibameen / redirection-next.js
Created Aug 5, 2020
Server side and client side redirection in next.js
View redirection-next.js
import Router from 'next/router';
const redirectLocation = '/error';
// If server.
if (ctx.res) {
ctx.res.writeHead(303, { Location: redirectLocation });
ctx.res.end();
} else {
// If browser.
Router.push(redirectLocation);
@saqibameen
saqibameen / redirection-next.js
Created Aug 5, 2020
Server side and client side redirection in next.js
View redirection-next.js
import Router from 'next/router';
const redirectLocation = '/error';
// If server.
if (ctx.res) {
ctx.res.writeHead(303, { Location: redirectLocation });
ctx.res.end();
} else {
// If browser.
Router.push(redirectLocation);
@saqibameen
saqibameen / redirection-next.js
Created Aug 5, 2020
Server side and client side redirection in next.js
View redirection-next.js
import Router from 'next/router';
const redirectLocation = '/error';
// If server.
if (ctx.res) {
ctx.res.writeHead(303, { Location: redirectLocation });
ctx.res.end();
} else {
// If browser.
Router.push(redirectLocation);
@saqibameen
saqibameen / string-to-expression.js
Created Jul 27, 2020
Convert a string into a an actual JavaScript expression/statement
View string-to-expression.js
// Source: https://stackoverflow.com/questions/55074927/eval-vs-function-constructor
(() => {
let secret = 42;
eval("console.log(secret)"); // 42
let fn = new Function("console.log(secret)");
fn(); // secret is not defined
})();
@saqibameen
saqibameen / limit-words.css
Created Jul 27, 2020
Limit no of characters inside your element
View limit-words.css
/*
* Limits the no of character in your element.
*/
.trim-text {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 75ch;
}
@saqibameen
saqibameen / async-await-error-handling.js
Created Jul 26, 2020
Code snippet to gracefully handle errors with async await
View async-await-error-handling.js
// HOC to handle error.
function handleError(fn){
return function(...params) {
return fn(...params).catch(function (err) {
console.error(`Oops! ${err}`);
});
}
}
// Our async function
View hosted-domains-api-response.js
// Response from Hosted Domains API by IPInfo.io
{
"domain": "comcast.net",
"ip": "69.252.80.75",
"asn": "AS7922",
"count": 939,
"range": "69.240.0.0/12",
"domains": [
[
"comcast.net",
View range-api-response.js
// Response from Range API agains comcast.net.
{
"domain": "comcast.net",
"num_ranges": "37330",
"ranges": [
"23.24.240.0/29",
"23.24.240.64/29",
"23.24.240.128/28",
"23.24.240.152/29",
"23.24.240.168/29",
You can’t perform that action at this time.