Skip to content

Instantly share code, notes, and snippets.

View ansarkmemon's full-sized avatar
🏠
Working from home

Ansar Memon (Amoury) ansarkmemon

🏠
Working from home
View GitHub Profile
@ansarkmemon
ansarkmemon / objectClone.js
Created July 9, 2018 06:52
Different Ways to Deep Clone an Object in Javascript
/**
* ES6 - Using .assign method
* Returns an independent copy/clone of an object
* @param {Object} obj - Object to be cloned
* @returns {Object}
*/
const deepClone = ( obj => Object.assign( {}, obj ));
@ansarkmemon
ansarkmemon / arrayClone.js
Created July 9, 2018 10:59
Different ways to Deep Clone an Array / Make an independent Copy of an array
/**
* Using Slice Method
* Returns an independent copy/clone of an array
* @param [Array] arr - Array to be cloned
* @returns [Array]
*/
const arrClone = ( arr => arr.slice());
/**
* Using Spread Operator
@ansarkmemon
ansarkmemon / factorial.js
Created August 5, 2018 07:33
Using Recursion to calculate the factorial - ES6
const fact = n => n === 1 ? 1 : n * fact(n - 1);
fact(5) // 120
@ansarkmemon
ansarkmemon / reverseString.js
Last active September 19, 2018 17:04
Function to reverse the string
/*
Given a string, return a new string with the reversed
order of characters
*/
function reverse(str) {
return str.split('').reverse().join('');
}
@ansarkmemon
ansarkmemon / helper_querySelector.js
Created October 2, 2018 03:09
This is the helper function to select HTML elements without using querySelector and querySelectorAll
/**
* This function checks if the string for the scope is provided and then calls appropriate function to returns the HTML Elements.
* @param {String} selector
* @param {String} scope
*/
window.$$ = (selector, scope) => {
let scoping;
if(scope) {
scoping = getElements(scope);
@ansarkmemon
ansarkmemon / _bind.js
Created October 2, 2018 03:13
If `bind` did not exist in JS but methods such `call` or `apply` can be used to create bind type functionality
const person1 = {
name: "Ansar",
sayHi() {
console.log("Hi " + this.name);
}
};
const person2 = {
name: "John"
};
@ansarkmemon
ansarkmemon / digital_root.js
Created April 15, 2019 08:14
Sum of Digits / Digital Root
// Calculates value of the sum of the digits recursively
function digital_root(n) {
return (n - 1) % 9 + 1;
}
digital_root(456); // returns 6
@ansarkmemon
ansarkmemon / toCamelCase.js
Created July 19, 2021 08:03
Change from snake case to camel case using regex
// Loop through the items in the array and replace the object keys
/**
* Input: [{ id: 1, created_at: "2021-07-19T07"}]
* Output: [{ id: 1, createdAt: "2021-07-19T07"}]
*/
const parsedRows = result.rows.map(row => {
const replaced = {};
for(let key in row) {