This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {String} str | |
*/ | |
const isStrHasSpecialChar = (str) => | |
"<>@!#$%^&*()_+[]{}?:;|'\"\\,./~`-=".split("").map(s=> str.indexOf(s) > -1).includes(true); | |
//Example: console.log(isStrHasSpecialChar("hello h@rry")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {Object} o | |
*/ | |
const objectToQueryParams = (o = {}) => | |
Object.entries(o) | |
.map((p) => `${encodeURIComponent(p[0])}=${encodeURIComponent(p[1])}`) | |
.join("&"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {Array} arr | |
*/ | |
const getUniqueArray = (arr = []) => { | |
const uArr = []; | |
arr.forEach((a) => { | |
if (uArr.indexOf(a) === -1) uArr.push(a); | |
}); | |
return uArr; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {any string} str | |
*/ | |
const camelCase = (str) => str.replace(/(^|\s)\S/g, (t) => t.toUpperCase()); |