Skip to content

Instantly share code, notes, and snippets.

View bhaireshm's full-sized avatar
🐕

Bhairesh M bhaireshm

🐕
View GitHub Profile
@bhaireshm
bhaireshm / shuffleString.js
Created August 26, 2021 17:20
Shuffles the given string and returns it.
/**
* @param {String} - str
*/
const shuffleString = (str) => {
str = str.trim().replace(/ /g, "");
let res = "";
const getRandomChar = (c) => c.charAt(Math.floor(Math.random() * c.length));
for (var i = 0; i < str.length; i++) res += getRandomChar(str);
return res;
};
@bhaireshm
bhaireshm / isStrHasSpecialChar.js
Created July 24, 2021 14:57
Check for any special character in string.
/**
* @param {String} str
*/
const isStrHasSpecialChar = (str) =>
"<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=".split("").map(s=> str.indexOf(s) > -1).includes(true);
//Example: console.log(isStrHasSpecialChar("hello h@rry"))
@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";
@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("&");
/**
* @param {Array} arr
*/
const getUniqueArray = (arr = []) => {
const uArr = [];
arr.forEach((a) => {
if (uArr.indexOf(a) === -1) uArr.push(a);
});
return uArr;
};
/**
* @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;
}
@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 {string} str
* @param {number} len - default is 50
*/
const shortenString = (str = "", len = 50) => { if(str.length > len) return str.substr(0, len); return str; }
/**
* @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
@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());