Skip to content

Instantly share code, notes, and snippets.

View marco-souza's full-sized avatar
🦕

Marco Antônio marco-souza

🦕
View GitHub Profile
@marco-souza
marco-souza / fileToBase64.ts
Created July 14, 2020 17:15
Convert File to Base64 in JavaScript/TypeScript
export const toBase64 = (file: File) =>
new Promise((resolve, reject) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = (error) => reject(error);
});
@marco-souza
marco-souza / cloudSettings
Last active June 23, 2021 15:30
MacOS - Visual Studio Code - Settings Sync Gist
{"lastUpload":"2021-06-23T15:30:45.087Z","extensionVersion":"v3.4.3"}
@marco-souza
marco-souza / mod.ts
Created April 1, 2021 19:11
hello-world-deno-deploy
addEventListener("fetch", (event) => {
const response = new Response("Hello World!", {
headers: { "content-type": "text/plain" },
});
event.respondWith(response);
});
@marco-souza
marco-souza / copyToClipboard.js
Created September 10, 2019 21:49
Function to copy text to clipboard in JS Vanilla
const copyToClipboard = (text, successCallback, errorCallback) => {
const textArea = document.createElement('textarea')
textArea.value = text
document.body.appendChild(textArea)
textArea.select()
try {
// Now that we've selected the anchor text, execute the copy command
document.execCommand('copy')
? successCallback()
@marco-souza
marco-souza / main.di.ts
Created July 11, 2020 15:57
Typescript/Node Dependency Injector (DI) - Koa server sample
import * as Koa from "koa";
let koaInstance: Koa;
class AppInjector {
static injectApp() {
if (!koaInstance) {
koaInstance = new Koa();
koaInstance.use(async (ctx) => {
ctx.body = "Hello World";
@marco-souza
marco-souza / sleep.ts
Created August 3, 2020 18:14
Async Sleep
const sleep = (ms: number): Promise<void> =>
new Promise(resolve => setTimeout(resolve, ms));
export default sleep;
@marco-souza
marco-souza / sha256.ts
Created August 3, 2020 18:19
JS/TS Async SHA-256
async function sha256(message: string): Promise<string> {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);
// hash the message
const hashBuffer = await crypto.subtle.digest('SHA-256', msgBuffer);
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray.map(b => ('00' + b.toString(16)).slice(-2)).join('');
return hashHex;
@marco-souza
marco-souza / useFetch.ts
Created August 8, 2020 01:32
JS/TS useFetch react hook
import { useState, useEffect } from "react";
export enum FetchStatus {
IDLE,
LOADING,
SUCCESS,
FAILURE,
CANCELED,
}
@marco-souza
marco-souza / FAQ.md
Last active July 17, 2020 07:14
React Testing Cookbook
  • Q: Makes sense to move useEffects to hooks which you have to load data? > A: I believe so. Every time I access a duck which loads external data, I'm interested in its data, not in calling a funciton to load it.
@marco-souza
marco-souza / estrela.json
Created March 27, 2020 20:24
test lottie animation
{"v":"5.6.6","fr":24,"ip":0,"op":48,"w":1080,"h":1080,"nm":"Teste","ddd":0,"assets":[],"layers":[{"ddd":0,"ind":1,"ty":4,"nm":"Shape Layer 2","sr":1,"ks":{"o":{"a":1,"k":[{"i":{"x":[0.833],"y":[0.833]},"o":{"x":[0.167],"y":[0.167]},"t":22,"s":[0]},{"t":27,"s":[100]}],"ix":11},"r":{"a":1,"k":[{"i":{"x":[0.295],"y":[1.212]},"o":{"x":[0.721],"y":[-0.232]},"t":12,"s":[0]},{"t":38,"s":[720]}],"ix":10},"p":{"a":0,"k":[540.004,548.22,0],"ix":2},"a":{"a":0,"k":[-3.033,-50.04,0],"ix":1},"s":{"a":0,"k":[65.947,65.947,100],"ix":6}},"ao":0,"shapes":[{"ty":"gr","it":[{"ty":"sr","sy":1,"d":1,"pt":{"a":0,"k":5,"ix":3},"p":{"a":0,"k":[0,0],"ix":4},"r":{"a":0,"k":0,"ix":5},"ir":{"a":0,"k":100,"ix":6},"is":{"a":0,"k":0,"ix":8},"or":{"a":0,"k":262,"ix":7},"os":{"a":0,"k":0,"ix":9},"ix":1,"nm":"Polystar Path 1","mn":"ADBE Vector Shape - Star","hd":false},{"ty":"fl","c":{"a":0,"k":[1,0,0,1],"ix":4},"o":{"a":0,"k":100,"ix":5},"r":1,"bm":0,"nm":"Fill 1","mn":"ADBE Vector Graphic - Fill","hd":false},{"ty":"tr","p":{"a":0,"k":[-3.03