Skip to content

Instantly share code, notes, and snippets.

View joelbarbosa's full-sized avatar
🏠
Working from home

Joel Barbosa joelbarbosa

🏠
Working from home
View GitHub Profile
@joelbarbosa
joelbarbosa / eventtarget.ts
Last active April 22, 2021 19:02
type EventTarget with typescript
type EventTarget<T> = EventTarget & T;
interface FormEvent<T> extends Event {
target: EventTarget<T>;
}
function handleSubmit(event: FormEvent<{price: number}>) {
event.target.price;
}
const button = document.querySelector('button');
button.addEventListener("click", () => resizeWithTroattle(10));
const resizeWithTroattle = throttle(executeResize, 3000);
window.addEventListener("resize", () => resizeWithTroattle('test'));
function executeResize(number) {
console.log("resize: " + number);
}
function throttle(fn, time) {
const makeInterable = (array) => {
let nextIndex = 0;
return {
next: () => {
if (nextIndex < array.length) {
return {value: array[nextIndex ++], done: false}
} else {
return {done: true};
}
// Return the total number of matching pairs of socks that John can sell.
// 9
// 10 20 20 10 10 30 50 10 20
// output
// 3
function sockMerchant(n, ar) {
let totalPair=0;
@joelbarbosa
joelbarbosa / react-native.js
Created October 1, 2019 13:48 — forked from lelandrichardson/react-native.js
React Native flow types
declare var __DEV__: boolean;
declare module 'react-native' {
declare type Color = string | number;
declare type Transform =
{ perspective: number } |
{ scale: number } |
{ scaleX: number } |
@joelbarbosa
joelbarbosa / chunk.js
Created February 28, 2019 03:52
Creates an array of elements split into groups the length of size.
// chunk
// Creates an array of elements split into groups the length of size.
const chunk = (input, size) => {
return input.reduce((arr, item, idx) => {
if (idx % size === 0) {
return [...arr, [item]];
} else {
return [...arr.slice(0, -1), [...arr.slice(-1)[0], item]];
}
@joelbarbosa
joelbarbosa / bubblesort.js
Created February 28, 2019 03:10
Bubble Sort
function bublesort(arr1) {
const arr = [...arr1];
let swap
do {
swap = false;
for (i=0; i < arr.length-1; i++) {
if (arr[i] > arr[i+1]) {
let tmp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = tmp;
const debounce = (fn, wait, immediate) => {
let timeout;
return () => {
const later = () => {
timeout = null;
if (!immediate) fn.apply(this)
}
const callNow = immediate && !timeout;
clearTimeout(timeout)
timeout = setTimeout(later, wait)