Skip to content

Instantly share code, notes, and snippets.

View bhaireshm's full-sized avatar
🐕

Bhairesh M bhaireshm

🐕
View GitHub Profile
@bhaireshm
bhaireshm / currencyFormatter.js
Created August 26, 2022 14:04
Converts number into formatted currency value.
/**
* Converts number into formatted currency value.
*
* @param {Number} val - Integer value
* @param {Intl.NumberFormatOptions} options - An object with some or all of the properties of Intl.NumberFormatOptions
* @returns formatted value.
*
* @example
* console.log(currencyFormatter(1234567890.1997)); // ₹1,23,45,67,890.20
* console.log(currencyFormatter(1234567890, {locales: "en-US", currency: "USD", maximumFractionDigits: 0})); // $1,234,567,890
@bhaireshm
bhaireshm / uniqueArrayOfObjects.js
Created August 8, 2022 15:11
Compares all the objects(both key and value) in the given array and returns the unique array of objects.
/**
* Compares all the objects(both key and value) in the given array and returns the unique array.
*
* @param {Object[]} arr - An array of objects.
* @returns unique array of object(s).
*
* @example uniqueArrayOfObjects([{a: 2}, {a: 2}]); // [{"a": 2}]
* @example uniqueArrayOfObjects([{a: {b: 2}}, {a: {b: 2}}]); // [{"a": {"b": 2}}]
* @example uniqueArrayOfObjects([{a: 2}, {a: 2, b: 3}]); // [{a: 2}, {a: 2, b: 3}]
*/
@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
@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 / 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 / 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 / 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 / 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 / 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 / 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