Skip to content

Instantly share code, notes, and snippets.

View MarwanShehata's full-sized avatar
🎯
"Luck is what happens when preparation meets opportunity.", Seneca

Marwan Shehata MarwanShehata

🎯
"Luck is what happens when preparation meets opportunity.", Seneca
View GitHub Profile
function binarySearch(list, item) {
let min = 0;
let max = list.length - 1;
let guess;
while (min <= max) {
guess = Math.floor((min + max) / 2);
if (list[guess] === item) {
return guess;
} else {
const obj = {
[Symbol("my_key")]: 1,
[Symbol("second_key")]: 2,
enum: 3,
nonEnum: 4,
};
Object.defineProperty(obj, "nonEnum", {
enumerable: false,
});
@MarwanShehata
MarwanShehata / serializationSymbols
Created March 3, 2024 11:44
Metaprogramming (Advanced): Symbols can be used for advanced metaprogramming techniques like property name creation and dynamic property access. However, these use cases are less common and typically require a deep understanding of JavaScript intern
const serializableSymbol = Symbol.for("serializable");
class User {
constructor(name) {
this.name = name;
this[serializableSymbol] = ["name", "age"]; // Mark 'name' and 'age' for serialization
}
getSerializableProperties() {
const properties = [];
@MarwanShehata
MarwanShehata / coercionProtection
Created March 3, 2024 12:08
You can implement custom behaviors for operators and methods using Symbols. For example, you can use the Symbol.toPrimitive well-known symbol to define how an object should be converted to a primitive value
const obj = {
[Symbol.toPrimitive]: function (hint) {
if (hint === "number") {
return 42;
} else if (hint === "string") {
return "Hello";
} else {
return true;
}
},
/*
Josh Comeau Reset https://www.joshwcomeau.com/css/custom-css-reset/ with my tweaks
*/
*,
*::before,
*::after {
box-sizing: border-box;
}
* {
margin: 0;
@MarwanShehata
MarwanShehata / countryCode
Last active April 19, 2024 05:15
The `ignore` variable in your code serves as a flag to prevent state updates after an asynchronous operation (in this case, fetching country data) has completed, especially when the component might have been unmounted or the state might have been upd
import {
createContext,
memo,
useCallback,
useContext,
useEffect,
useId,
useLayoutEffect,
useMemo,
useReducer,
@MarwanShehata
MarwanShehata / modal
Last active April 20, 2024 02:17
In this challenge you'll be adding a modal experience to the app. The user needs to be able to open the modal and then close it either by clicking the close icon in the modal itself, or by clicking anywhere outside of the modal. You'll need both c
export default function App() {
const [isOpen, setIsOpen] = React.useState(false);
const ref = React.useRef(null);
React.useEffect(() => {
if (isOpen === true) {
const handleEvent = (e) => {
const element = ref.current;
if (element && !element.contains(e.target)) {
function slow(id, cb) {
setTimeout(function () {
cb(null, id)
}, Math.random() * 3000)
}
let promises = []
for (let i = 0; i < 3; i++) {
const prom = new Promise((resolve, reject) => {
slow(i, function (err, res) {
@MarwanShehata
MarwanShehata / validator.ts
Last active October 26, 2024 19:44
This is not a library, this is just a list of functions I decided to put in a central location so I can copy them to future projects as needed. These functions are in typescript an do both compile-time (with generics) AND runtime validation. Feel fr
// **** Types **** //
export type TFunc = (...args: any[]) => any;
export type TEmail = `${string}@${string}`;
export type TColor = `#${string}`;
// **** Variables **** //
const EMAIL_RGX = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9-]*\.)+[A-Z]{2,}$/i,
@MarwanShehata
MarwanShehata / try-catch.ts
Created March 20, 2025 17:43 — forked from t3dotgg/try-catch.ts
Theo's preferred way of handling try/catch in TypeScript
// Types for the result object with discriminated union
type Success<T> = {
data: T;
error: null;
};
type Failure<E> = {
data: null;
error: E;
};