Skip to content

Instantly share code, notes, and snippets.

View DiegoVictor's full-sized avatar
:octocat:
Building Bugs

Diego Victor DiegoVictor

:octocat:
Building Bugs
View GitHub Profile
@DiegoVictor
DiegoVictor / events.js
Last active May 15, 2021 23:13
How to use Node.js EventEmmiter
// https://nodejs.org/api/events.html
const EventEmmiter = require("events");
const eventEmmiter = new EventEmmiter();
const cooking = async ({ name }) => ({ name });
const driveTo = async () => true;
const leave = async () => ({ delivered: true });
async function cook({ pizzaOrder, user }) {
console.log(`Cooking a ${pizzaOrder.name}`);
@DiegoVictor
DiegoVictor / uncaughtExceptions.js
Last active May 26, 2021 00:15
How to catch uncaught exceptions into Node.js
// https://nodejs.org/api/process.html
process.on("uncaughtException", (err) => {
console.log("Of course we can!");
console.log(`We catch a ${err.name}`);
});
class CallError extends Error {
constructor(message) {
super(message);
console.log(message);
@DiegoVictor
DiegoVictor / index.html
Last active June 15, 2021 01:50
Radial Image Transparency
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Radial Image Transparency</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
@DiegoVictor
DiegoVictor / index.js
Last active July 13, 2021 15:41
Palindrome Challenge
// node . [total-words] (e.g. node . 1000)
const firstAlphabetLetter = "a".charCodeAt();
const alphabetRange = "z".charCodeAt() - firstAlphabetLetter;
const [,,totalWordsArg = 10000] = process.argv;
const generateRandomWord = (min, max) => {
const randomMaxChars = Math.floor(Math.random() * max);
let word = "";
for (let i = 0; i <= min || i <= randomMaxChars; i++) {
@DiegoVictor
DiegoVictor / pool.js
Last active September 13, 2021 18:08
Implementing a pool of requests
const fs = require("fs");
const path = require("path");
const axios = require("axios");
const { pipeline } = require("stream/promises");
const api = axios.create({
baseURL: "https://pokeapi.co/api/v2/pokemon",
});
const requestsInParallel = 3;
const limit = 10;
@DiegoVictor
DiegoVictor / save-binary.js
Last active September 15, 2021 15:04
Save binary field with typeorm
require("reflect-metadata");
const {
EntitySchema,
createConnection,
Table,
getConnection,
} = require("typeorm");
const { randomUUID } = require("crypto");
const Tag = new EntitySchema({
@DiegoVictor
DiegoVictor / converter.js
Created December 1, 2021 23:57
Convert image to base64 using Node.js' native libs
// Usage: node converter.js <file path>
const {
promises: { readFile, writeFile },
} = require("fs");
const [, , filePath] = process.argv;
async function converter(path) {
return readFile(path)
@DiegoVictor
DiegoVictor / main.js
Created January 26, 2022 22:35
Generate promises from an array, run the promises in sequence, starting the next one once the previous finish
async function call(value) {
const label = `Promise ${value}`;
console.time(label);
return new Promise((resolve) => {
setTimeout(() => {
console.timeEnd(label);
resolve();
}, Math.floor(9 * Math.random() + 1) * 1000);
});
@DiegoVictor
DiegoVictor / funcs.js
Created January 31, 2022 22:46
Example code to run Jest from a Node.js script.
const sum = (n, m) => n + m;
module.exports = { sum };
@DiegoVictor
DiegoVictor / template.json
Last active January 19, 2023 21:02
AWS SNS notification for Android devices (FCM)
{
"default": "Hello There!",
"GCM": "{\"notification\":{\"body\":\"Hello There!\",\"title\":\"Hello\"}}"
}