Skip to content

Instantly share code, notes, and snippets.

View ahamed's full-sized avatar
🎯
Focusing on open source.

Sajeeb Ahamed ahamed

🎯
Focusing on open source.
View GitHub Profile
@ahamed
ahamed / create-enum.js
Last active August 9, 2022 17:15
Create a JavaScript representation of "Enum".
/**
* ✅ Function for creating an `enum` representation in JavaScript.
*
* This function will receive a plain object
* [key: string]: number | string | boolean.
* You've created the enum with a valid object and it will return you a
* Proxy. You are not able to extend the proxy anymore.
*
* @author 📩 Sajeeb Ahamed <[email protected]>
*
@ahamed
ahamed / array-from-array-like-object.js
Created August 18, 2022 06:55
Create an array using Array.from() static method from an array-like object.
/**
* Create an array from an array-like object.
* An array-like object is an object which has a length property
* and indexed elements. An indexed element means the properties
* of the object are like the index of an array.
*
* @author Sajeeb Ahamed <[email protected]>
*/
const arrayLikeObject = {
@ahamed
ahamed / groupBy.ts
Last active August 25, 2022 15:57
Implement the groupBy method using typescript.
/**
* JavaScript introduces a new array method name `group`, but this is only added to the firefox nightly build.
* Here we can create the similar implementation of the `Array.prototype.group` method.
*
* The `Array.prototype.group` method basically accespts a callback function as a parameter and group the array elements
* according to the callback function's return key and return an object.
* From the callback function you have to return a string or a number.
*
* @author Sajeeb Ahamed <[email protected]>
* @see https://www.typescriptlang.org/play?#code/PQKhCgAIUgpBDAbvAygYwE4EsAOAXSLAOzwwHsATAVzQFMBnSeSI2gdyYw3gE9IBbWngAWlFvEGQABgHNyVHFIA0kAEZUCIrI22QyRADZ94FCrQqQ8ZS8NqQAZlgy17ZAB4ssM4XiNqqWAYUAHRQMAAStM6QbHZo8ESQmLTweHYidvRY-IHwGIT8OAa0giSpWPp69jZ2UgCCXLzBOORWeDw4tMFyZApSAkKiIWFhkAAqttIN3DzNrWTtnd3yigMiYqrwWfEGfvBodPT4jMw7BptoANYOVERoeBWJW0yQOHkSQlFMRBY9CjWcGaQWjFUp4eijfZoMgYCjEGSWawZJLwXYXa72W73R4AckYzjwVAwiUutGMP0gBKJT0SZFUACtaPdQtBIAAxcj8AFndE3O4PSo8XqQYRIdLWKnEl70UjwvT5ZhEKj8VRRFkQVkAAXg
@ahamed
ahamed / useDebounce.ts
Created September 10, 2022 06:12
Implementation of the debounce hook for ReactJs.
import { useEffect, useState } from 'react';
/**
* useDebounce hook for implementing debounce in react.
* The idea is simple, keep an internal state and update
* the state after a specific delay time. If the given value
* is changed before the delay time then cancel the timeout and
* wait for the delay time once again. And finally return the debounced
* state value.
#1:
## Prototype:
An object is a data structure used in JavaScript that stores values in a key value pair. That means you can store a value into an object in a key. For example-
```js
const person = {
name: 'John Doe',
age: 20
};
```
Hoisting in JavaScript is a behavior where variable and function declarations are moved to the top of their containing scope during the compile phase. This means that regardless of where declarations occur within a scope, they are hoisted to the top. However, it's important to note that only declarations are hoisted, not initializations or assignments. Let me show you an example to illustrate this:
```js
console.log(myVar); // Outputs: undefined
var myVar = 10;
console.log(myVar); // Outputs: 10
```
In this example, even though `myVar` is logged before it's declared and initialized, it doesn't throw an error. This is because the declaration `var myVar;` is hoisted to the top of its scope during the compile phase, resulting in `myVar` being `undefined` when logged before its assignment.
For that reason we could call a function before of its declaration. For example-
```js