Skip to content

Instantly share code, notes, and snippets.

View david-mateogit's full-sized avatar

David Mateo david-mateogit

  • Dominican Republic
View GitHub Profile
@david-mateogit
david-mateogit / useMercadoPago
Created November 5, 2022 21:36
Custom hook to load a MercadoPago Script and attach it to an element.
import { useEffect, useState } from "react";
export const useMercadoPago = (preferenceId, divEl) => {
const [status, setStatus] = useState(preferenceId ? "loading" : "idle");
useEffect(() => {
const divContainer = document.getElementById(divEl);
let script = divContainer.querySelector("script");
if (!preferenceId) {
setStatus("idle");
@david-mateogit
david-mateogit / debounce.js
Created March 20, 2020 02:25
Basic debounce function
function debounce(func, duration) {
let timeout;
return function(...args) => {
const effect = () => {
timeout = null;
return func.apply(this, args);
}
clearTimeout(timeout);
@david-mateogit
david-mateogit / throttle.js
Created March 18, 2020 16:17
Basic Throttle Function
function throttle(func, delay) {
let shouldWait = false;
return function(...args) {
if (!shouldWait) {
func.apply(this, args);
shouldWait = true;
setTimeout(function() {
shouldWait = false;
@david-mateogit
david-mateogit / binary-search.js
Created February 22, 2020 21:29
Binary search algorithm
// Binary search
// Take sorted array and search from the middle;
const values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const binary = (val, arr) => {
let lower = arr[0];
let upper = arr.length - 1;
// counter
@david-mateogit
david-mateogit / check.js
Last active January 30, 2020 20:18
Luhn’s Algorithm - Credit Card # Check.
// test cards
// https://developer.paypal.com/docs/classic/payflow/payflow-pro/payflow-pro-testing/#credit-card-numbers-for-testing
const card = 378282246310005;
let mod = 1;
let flag = true;
let sum = 0;
const digits = 16;