Skip to content

Instantly share code, notes, and snippets.

View helabenkhalfallah's full-sized avatar
🎯
Focusing

Héla Ben Khalfallah helabenkhalfallah

🎯
Focusing
View GitHub Profile
@helabenkhalfallah
helabenkhalfallah / RefactoringExample3.js
Created February 2, 2025 13:37
Refactoring Example 3
import React, { useState, useEffect } from "react";
export default function OrderDashboardBadWay({ userId }) {
const [orders, setOrders] = useState([]);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
const [selectedOrder, setSelectedOrder] = useState(null);
const [formattedOrders, setFormattedOrders] = useState([]);
useEffect(() => {
@helabenkhalfallah
helabenkhalfallah / RefactoringExample1.js
Created February 2, 2025 13:30
Refactoring Example
function formatUsersBadWay(users) {
// Keep only valid users: phone & email not null
for (let i = 0; i < users.length; i++) {
const user = users[i];
if (user.email === null || user.phone === null) {
users.splice(i, 1);
}
}
// Sort by name alphabetically
@helabenkhalfallah
helabenkhalfallah / RefactoringExample2.js
Created February 2, 2025 13:27
Refactoring Example (2)
const formatVirementsBadWay = (virements) => {
let dataSource = [];
for (let i = 0; i < virements.length; i++) {
const virement = virements[i];
const uiVirement = {};
// Reference interne
uiVirement.referenceInterne = virement.referenceInterne;
// Format emetteur
@helabenkhalfallah
helabenkhalfallah / BTree.js
Created November 6, 2024 10:18
B-Tree (JS)
class BTreeNode {
constructor(isLeaf = true) {
this.isLeaf = isLeaf;
this.keys = []; // Array of keys
this.children = []; // Array of child pointers (BTreeNode instances)
}
}
class BTree {
constructor(order = 3) {
@helabenkhalfallah
helabenkhalfallah / RedBlackTree.js
Created November 6, 2024 10:11
Red Black Tree (JS)
class Node {
constructor(value, color = 'RED') {
this.value = value;
this.color = color; // Nodes start as RED
this.left = null;
this.right = null;
this.parent = null;
}
isRed() {
@helabenkhalfallah
helabenkhalfallah / AVLTree.js
Created November 5, 2024 22:40
AVL Tree (JS)
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
this.height = 1; // Height of the node for balance factor calculations
}
}
class AVLTree {
@helabenkhalfallah
helabenkhalfallah / BST.js
Created November 5, 2024 22:09
Binary Search Tree (JS)
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BinarySearchTree {
constructor() {
@helabenkhalfallah
helabenkhalfallah / ProxySecureAPIGateway.js
Created May 20, 2024 20:35
Proxy secure API Gateway
import http from 'http';
import url from 'url';
// Validation function to perform security checks
const validateRequest = (req) => {
const apiKey = req.headers['x-api-key'];
if (!apiKey || apiKey !== 'your-secure-api-key') {
console.log('Invalid API Key');
return false;
}
@helabenkhalfallah
helabenkhalfallah / ProxyValidator.js
Created May 20, 2024 19:58
Proxy validation and sanitization
const validator = {
set(obj, prop, value, receiver) {
if (prop === "age") {
if (!Number.isInteger(value)) {
throw new TypeError("The age is not an integer");
}
if (value > 200) {
throw new RangeError("The age seems invalid");
}
}
// Function to create a reactive store
function createStore(initialState) {
// Store the initial state in a private variable
let state = initialState;
// Array to hold subscribers (callbacks)
const subscribers = [];
// Create a proxy for the state object
const stateProxy = new Proxy(state, {