Skip to content

Instantly share code, notes, and snippets.

View akash-pal's full-sized avatar
🎯
Focusing

Akash Pal akash-pal

🎯
Focusing
View GitHub Profile
@akash-pal
akash-pal / App.jsx
Created May 12, 2024 12:19
Base House Listing Application
import * as React from 'react';
import Listings from './components/presentational/Listings';
export default function App() {
return <Listings />;
}
@akash-pal
akash-pal / index.jsx
Last active May 12, 2024 09:16
Higher-Order Components
import * as React from 'react';
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import './style.css';
const rootElement = document.getElementById('root');
const root = createRoot(rootElement);
const LoadingSpinner = () => (
<div className="spinner-wrapper">
@akash-pal
akash-pal / App.jsx
Last active May 12, 2024 13:22
Container/Presentational Pattern
import * as React from 'react';
import Listings from './components/container/Listings';
export default function App() {
return <Listings />;
}
@akash-pal
akash-pal / index.js
Created May 11, 2024 19:10
Module Pattern
import { sum, subtract, divide, multiply } from './math.js';
console.log('Sum', sum(1, 2));
console.log('Subtract', subtract(1, 2));
console.log('Divide', divide(1, 2));
console.log('Multiply', multiply(1, 2));
@akash-pal
akash-pal / index.js
Created May 11, 2024 19:01
Singleton Pattern Object
let counter = 0;
// 1. Create an object containing the `getCount`, `increment`, and `decrement` method.
const counterObject = {
getCount: () => counter,
increment: () => ++counter,
decrement: () => --counter,
};
// 2. Freeze the object using the `Object.freeze` method, to ensure the object is not modifiable.
@akash-pal
akash-pal / index.js
Created May 11, 2024 19:00
Singleton Pattern Class
let instance;
// 1. Creating the `Counter` class, which contains a `constructor`, `getInstance`, `getCount`, `increment` and `decrement` method.
// Within the constructor, we check to make sure the class hasn't already been instantiated.
class Counter {
constructor() {
if (instance) {
throw new Error("You can only create one instance!");
}
this.counter = counter;
@akash-pal
akash-pal / index.js
Created May 11, 2024 18:48
Prototype Pattern
class Dog {
constructor(name, age) {
this.name = name;
this.age = age;
}
bark() {
console.log(`${this.name} is barking!`);
}
@akash-pal
akash-pal / index.js
Created May 11, 2024 18:45
Factory Pattern
const createUser = (firstName, lastName) => ({
id: crypto.randomUUID(),
createdAt: Date.now(),
firstName,
lastName,
fullName: `${firstName} ${lastName}`,
});
createUser("John", "Doe");
createUser("Sarah", "Doe");
@akash-pal
akash-pal / analytics.js
Last active May 11, 2024 14:00
Observer Pattern
import Observer from './observer.js';
export function sendToGoogleAnalytics(data) {
console.log('Sent to Google analytics: ', data);
}
export function sendToCustomAnalytics(data) {
console.log('Sent to custom analytics: ', data);
}
@akash-pal
akash-pal / proxyPatternReflect.js
Created May 11, 2024 12:56
Proxy pattern using Reflect
const person = {
name: "John Doe",
age: 42,
email: "john@doe.com",
country: "Canada",
};
const personProxy = new Proxy(person, {
get: (target, prop) => {
console.log(`The value of ${prop} is ${target[prop]}`);