Skip to content

Instantly share code, notes, and snippets.

View elsangedy's full-sized avatar
🚀

Munir Ahmed Elsangedy elsangedy

🚀
View GitHub Profile
@elsangedy
elsangedy / app.js
Last active August 29, 2015 14:26
SPA
var app = (function() {
var App = function() {
var _modules = {
controllers: {},
directives: {},
constants: {},
services: {},
routes: {}
};
@elsangedy
elsangedy / recursive.js
Last active April 19, 2018 13:01
Recursive function to execute function until attempts finish or promise resolve
const waitFor = (delay = 0) => new Promise((resolve) => setTimeout(resolve, delay))
const recursiveToZero = (func, attempt, delay, error) => {
if (attempt === 0) {
throw error || new Error('Fail')
}
return func().catch((err) => waitFor(delay).then(() => recursiveToZero(func, attempt - 1, delay, err)))
}
import * as R from 'ramda'
const valid = (message, isValid) => message ? (isValid ? null : message) : isValid
const emailRegex = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/
export const isEmail = (value, message) => valid(message, emailRegex.test(value))
export const isNumber = (value, message) => valid(message, /^\d+$/.test(value))
export const required = (value, message) => valid(message, !!value && value.trim() !== '')
export const min = (value, minimum, message) => valid(message, value.length >= minimum)
export const max = (value, maximum, message) => valid(message, value.length <= maximum)
@elsangedy
elsangedy / component.js
Last active November 18, 2020 18:52
i18next
import React from 'react'
import { useT } from './hook.js'
const Comp = () => {
const { t, tDate, tTime, tDateTime, tCurrency } = useT()
const myDate = '1993-05-07T17:30:00z'
const myCurrency = 100
import { useReducer as useReducerBase } from 'react'
export const useReducer = (reducer, initialState) => {
const [state, dispatch] = useReducerBase(reducer, initialState)
const customDispatch = (type, payload) => dispatch({ type, payload })
return [state, customDispatch, dispatch]
}
@elsangedy
elsangedy / FormikFieldSpy.js
Last active August 9, 2019 00:18
FormikFieldSpy
import { useMemo, useEffect } from 'react'
import { getIn, connect } from 'formik'
import { useThrottle } from './useThrottle'
export const FormikFieldSpy = connect(
({ field, onChange, timeout, formik }) => {
const value = useMemo(() => getIn(formik.values, field), [
field,
@elsangedy
elsangedy / useCombineReducer.js
Created September 5, 2019 16:54
useCombineReducer
import { useMemo, useCallback, useReducer as useReducerBase } from "react";
export function useCombineReducer(initialState, ...reducers) {
const internalReducer = useCallback(
(state, action) => ({
...state,
...reducers.reduce(
(prev, current, idx) =>
idx === 0
? current(state, action, initialState)
@elsangedy
elsangedy / sample.js
Last active September 6, 2019 00:08
useOnScrollBottom
const Demo = () => {
const ref = React.useRef(null);
useOnScrollBottom(() => console.log("ON SCROLL BOTTOM"), 20); // based on window
useOnScrollBottom(() => console.log("ON SCROLL BOTTOM INSIDE"), 20, ref); // based on ref
...
}
@elsangedy
elsangedy / useHover.js
Created September 6, 2019 17:23
useHover
import { useMemo, useState } from 'react'
export const useHover = () => {
const [isHovered, setIsHovered] = useState(false)
const binds = useMemo(
() => ({
onMouseEnter: () => setIsHovered(true),
onMouseLeave: () => setIsHovered(false)
}),
@elsangedy
elsangedy / machine.js
Created September 12, 2019 20:11
Generated by XState Viz: https://xstate.js.org/viz
const fetchMachine = Machine({
id: 'infinite scroll',
initial: 'idle',
context: {
page: 0,
size: 25
},
states: {
idle: {
on: {