Skip to content

Instantly share code, notes, and snippets.

View bhaireshm's full-sized avatar
🐕

Bhairesh M bhaireshm

🐕
View GitHub Profile
@bhaireshm
bhaireshm / getDateDifference.js
Last active February 24, 2025 09:25
Calculates the difference between two dates in days, hours and in minutes.
/**
* @param {Date} from
* @param {Date} to
*/
function dateDiff(from, to) {
from = new Date(from);
to = new Date(to);
var diffMs = to - from; // milliseconds between from & to
var diffDays = Math.floor(diffMs / 86400000); // days
var diffHrs = Math.floor((diffMs % 86400000) / 3600000); // hours
@bhaireshm
bhaireshm / getDateDifferenceInHours.js
Created June 22, 2021 13:02
Calculate the difference of two date in hours.
/**
* @param {Date} from
* @param {Date} to
*/
getDiffInHrs = (from, to) => Math.floor(Math.abs((new Date(from) - new Date(to)) / 36e5));
@bhaireshm
bhaireshm / eachWordCamelCase.js
Last active February 24, 2025 09:25
Converts sentenct into camelcase.
/**
* @param {any string} str
*/
const camelCase = (str) => str.replace(/(^|\s)\S/g, (t) => t.toUpperCase());
/**
* @param {string} url
*/
function isURLValid(url) {
return new RegExp(
"^(https?:\\/\\/)?" + // protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|" + // domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
/**
* @param {string} str
* @param {number} len - default is 50
*/
const shortenString = (str = "", len = 50) => { if(str.length > len) return str.substr(0, len); return str; }
@bhaireshm
bhaireshm / sortObjectByKey.js
Last active February 24, 2025 09:25
Sort's the array of objects by specified key name and can order by ascending or descending.
/**
* @param {Array of objects} obj
* @param {String} key
* @param {Number} ord : 1 = Ascending(Default), -1 = Descending
*/
const sortObjectByKey = (obj = [], key = "", ord = 1) =>
obj.sort((a, b) => (a[key] > b[key] ? 1 * 0 + ord : b[key] > a[key] ? 1 * 0 - ord : 0));
/**
* @param {number} length - default is 13
*/
function generateRandomString(length = 13) {
var res = "";
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (var i = 0; i < length; i++) res += chars.charAt(Math.floor(Math.random() * chars.length));
return res;
}
/**
* @param {Array} arr
*/
const getUniqueArray = (arr = []) => {
const uArr = [];
arr.forEach((a) => {
if (uArr.indexOf(a) === -1) uArr.push(a);
});
return uArr;
};
@bhaireshm
bhaireshm / objectToQueryParams.js
Created July 12, 2021 11:12
Convert an object into query string parameter.
/**
* @param {Object} o
*/
const objectToQueryParams = (o = {}) =>
Object.entries(o)
.map((p) => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`)
.join("&");
@bhaireshm
bhaireshm / hasOwnProperty.js
Last active February 24, 2025 09:24
Checks for the key in the given object. Can check multiple keys at once.
/**
* @param {Object} obj
* @param {String} keys : keys seperated by comma
* Example : console.log( hasOwnProperty({'a':1, 'b':2, 'c':3}, "a,d") );
*/
const hasOwnProperty = (obj, keys) => {
if(Object.entries(obj).length == 0 || keys.length == 0) return false;
else return keys.split(",").map((k) => {
if(k != "" && !Object.hasOwnProperty.call(obj, k)) return `${k} not found`;
}).filter(a=> typeof a == 'string')[0] || "All key(s) found";