Skip to content

Instantly share code, notes, and snippets.

View nelsonfncosta's full-sized avatar
:shipit:
burnit

Nélson Costa nelsonfncosta

:shipit:
burnit
View GitHub Profile
const loadImage = src =>
new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => resolve(img);
img.onerror = reject;
img.src = src;
});
@nelsonfncosta
nelsonfncosta / test.js
Created September 3, 2021 08:20
async promises test
import React from 'react'
import Dropzone from 'react-dropzone'
import { act, fireEvent, render, waitFor } from '@testing-library/react'
test('invoke onDragEnter when dragenter event occurs', async () => {
const file = new File([
JSON.stringify({ping: true})
], 'ping.json', { type: 'application/json' })
const data = mockData([file])
const onDragEnter = jest.fn()
@nelsonfncosta
nelsonfncosta / hast-tag-pyramid.js
Last active May 27, 2021 00:16
hast-tag-pyramid.js
let size = prompt("size?");
for(let i=1; i<=size; i++) console.log("#".repeat(i));
let size = prompt("size?")
for(let i=1; i<= size; i++){
let buffer = ' '.repeat(size-i)
let side = '#'.repeat(i-1)
console.log(`${buffer}${side} ${side}${buffer}`);
}
@nelsonfncosta
nelsonfncosta / act-warning.svg
Created May 18, 2021 15:13
Visual way how to explain the act warning from react testing library
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@nelsonfncosta
nelsonfncosta / drag-util.js
Created May 18, 2021 09:44
Make an element Draggable !
export function dragElement(elmnt, dragElement = elmnt) {
let pos1 = 0;
let pos2 = 0;
let pos3 = 0;
let pos4 = 0;
dragElement.onmousedown = dragMouseDown;
function dragMouseDown(e) {
e = e || window.event;
@nelsonfncosta
nelsonfncosta / array-buffer-to-string-ex.js
Last active April 25, 2021 15:24
ArrayBuffer to String example
function arrayBufferToString (buffer, encoding = 'UTF-8', callback = console.log) {
const blob = new Blob([buffer], { type: 'text/plain' })
const reader = new FileReader()
reader.onload = evt => callback(evt.target.result)
reader.readAsText(blob, encoding)
}
@nelsonfncosta
nelsonfncosta / stream-to-string-ex.js
Created April 22, 2021 22:11
Example of converting a stream to a string
function streamToString (stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
stream.on('error', (err) => reject(err))
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
}
@nelsonfncosta
nelsonfncosta / short-switch-ex.js
Created April 21, 2021 21:54
Shorthand example for switch statement
// Longhand
switch (data) {
case 'a':
test1();
break;
case 'b':
test2();
break;
@nelsonfncosta
nelsonfncosta / closures-ex.js
Created April 20, 2021 16:20
Example of how closures can help with memory management
function findCustomerCity(name) {
const texasCustomers = ['John', 'Ludwig', 'Kate'];
const californiaCustomers = ['Wade', 'Lucie','Kylie'];
return texasCustomers.includes(name) ? 'Texas' :
californiaCustomers.includes(name) ? 'California' : 'Unknown';
};
// For every call, memory is unnecessarily re-allocated to the variables texasCustometrs and californiaCustomers .
@nelsonfncosta
nelsonfncosta / read-file.js
Created March 23, 2021 19:06
READ FILE snippet
function readFile(file: File | null) {
const reader = new FileReader();
reader.addEventListener("load", (event) => {
const result = event.target.result;
console.log(result);
});
reader.addEventListener("progress", (event) => {