Skip to content

Instantly share code, notes, and snippets.

View bhaireshm's full-sized avatar
🐕

Bhairesh M bhaireshm

🐕
View GitHub Profile
@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 / 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 / stringToNumber.js
Last active February 24, 2025 09:24
Check and convert array of strings to array of number or object's string value to integer.
/**
* @param {String} s
* @returns number
* @example console.log(toNumber("-23.32"))
*/
const toNumber = (s, returnStrings = false) =>
isNaN(Number(s)) ? (returnStrings ? s : console.error(`${s} NaN`)) : Number(s);
/**
* @param {Array or Object} data - required
@bhaireshm
bhaireshm / removeEmptyProperty.js
Created February 15, 2022 07:35
Removes all the key's for which the value is empty.
/**
* Removes all the key's for which the value is empty.
* @param {Object} obj
*/
function removeEmptyProperty(obj = {}) {
const data = { ...obj };
if (!data) return data;
Object.keys(data).forEach((key) => {
if (typeof data[key] === "object") data[key] = removeEmptyProperty(data[key]);
if (!data[key]) delete data[key];
@bhaireshm
bhaireshm / createTable.js
Created March 28, 2022 07:34
Creates table with custom data using JavaScript.
/**
* Creates table with custom data.
*
* @param {Object} tableData
* @param {Object[]} tableData.data - Array of objects
* @param {String[]} tableData.fields - Fields to be shown
* @param {String[]} tableData.fieldTitles - Field Names
* @param {Object} tableData.tableProps - Field Names
* @param {String} tableData.tableProps.id - Table id
* @param {String[]} tableData.tableProps.classList - Table custom class list
@bhaireshm
bhaireshm / mergeObjects.js
Last active February 24, 2025 09:24
Merge two objects using JavaScript.
/**
* Merges Obj1 data into Obj2.
*
* @param {Object} obj1
* @param {Object} obj2
* @returns merged object
*
* @example
* const a = {v : [1,2]};
* const b = {v: [3]}
@bhaireshm
bhaireshm / stopNodeServers.bat
Created May 10, 2022 04:30
Batch script to stop all running node servers in windows.
@Echo Off
TASKLIST /FI "IMAGENAME eq node.exe"
Echo ================================================================================
Echo:
Echo This will stop all running node servers.
Echo:
Set "#="
Set /P "=Press Y to continue (press any other key to stop) :"<Nul
@bhaireshm
bhaireshm / sortObjectByMultipleKeys.js
Created May 10, 2022 04:53
Sort array of objects by key(s).
/**
* Sort array of objects by key(s)
*
* @param {Object[]} arr - Array of Objects
* @param {String[]} keys - Key's to be sorted. [pass hyphen(-) in front of string to order in descending]
* @example sortObjectByMultipleKeys(object, ["name", "-date"])
* @example sortObjectByMultipleKeys(object, ["-name", "date"])
*
* @returns Sorted array of objects.
*/
@bhaireshm
bhaireshm / arrayIntoChunks.js
Created August 1, 2022 16:21
Converts given array into given specified chunk(s).
/**
* Convert an array into given chunk(s).
*
* @param {any[]} arr - Any type of array
* @param {number} n - Number of chunks
* @example arrayIntoChunks([1,2,3,4,5,6], 3);
* @output [[1,2],[3,4],[5,6]]
*
* @returns Array of array[s].
*/
@bhaireshm
bhaireshm / compareObjects.js
Created August 8, 2022 14:39
Compare two objects, this method compares both key and value of given objects. This even works for nested objects.
/**
* This method compares both key and value of given objects. This even works for nested objects.
*
* @param {Object} obj1
* @param {Object} obj2
* @returns boolean
*
* @example compareObject({a: 2}, {a: 2}); // true
* @example compareObject({a: 2}, {a: 23}); // false
* @example compareObject({a: {b: 2}}, {a: {b: 2}}); // true